]> git.imager.perl.org - imager.git/blob - iolayer.h
might be valid this time
[imager.git] / iolayer.h
1 #ifndef _IOLAYER_H_
2 #define _IOLAYER_H_
3
4
5 /* How the IO layer works:
6  * 
7  * Start by getting an io_glue object.  Then define its
8  * datasource via io_obj_setp_buffer or io_obj_setp_cb.  Before
9  * using the io_glue object be sure to call io_glue_commit_types().
10  * After that data can be read via the io_glue->readcb() method.
11  *
12  */
13
14
15 #include <stdio.h>
16 #ifndef _MSC_VER
17 #include <unistd.h>
18 #endif
19 #include <sys/types.h>
20
21 /* #define BBSIZ 1096 */
22 #define BBSIZ 16384
23 #define IO_FAKE_SEEK 1<<0L
24 #define IO_TEMP_SEEK 1<<1L
25
26
27 typedef union { int i; void *p; } iorp;
28
29 typedef enum { FDSEEK, FDNOSEEK, BUFFER, CBSEEK, CBNOSEEK, BUFCHAIN } io_type;
30
31 #ifdef _MSC_VER
32 typedef int ssize_t;
33 #endif
34
35 struct _io_glue;
36
37 /* Callbacks we give out */
38
39 typedef ssize_t(*readp) (struct _io_glue *ig, void *buf, size_t count);
40 typedef ssize_t(*writep)(struct _io_glue *ig, const void *buf, size_t count);
41 typedef off_t  (*seekp) (struct _io_glue *ig, off_t offset, int whence);
42 typedef void   (*closep)(struct _io_glue *ig);
43 typedef ssize_t(*sizep) (struct _io_glue *ig);
44
45 typedef void   (*closebufp)(void *p);
46
47
48 /* Callbacks we get */
49
50 typedef ssize_t(*readl) (int fd, void *buf, size_t count);
51 typedef ssize_t(*writel)(int fd, const void *buf, size_t count);
52 typedef off_t  (*seekl) (int fd, off_t offset, int whence);
53 typedef ssize_t(*sizel) (int fd);
54
55 extern char *io_type_names[];
56
57
58 typedef struct _io_blink {
59   char buf[BBSIZ];
60   /* size_t cnt; */
61   size_t len;                   /* How large is this buffer = BBZIS for now */
62   struct _io_blink *next;
63   struct _io_blink *prev;
64 } io_blink;
65
66
67 /* Structures that describe callback interfaces */
68
69 typedef struct {
70   off_t offset;
71   off_t cpos;
72 } io_ex_rseek;
73
74
75 typedef struct {
76   off_t offset;
77   off_t cpos;
78   io_blink *head;
79   io_blink *tail;
80   io_blink *cp;
81 } io_ex_fseek;
82
83
84 typedef struct {
85   off_t offset;                 /* Offset of the source - not used */
86   off_t length;                 /* Total length of chain in bytes */
87   io_blink *head;               /* Start of chain */
88   io_blink *tail;               /* End of chain */
89   off_t tfill;                  /* End of stream in last link */
90   io_blink *cp;                 /* Current element of list */
91   off_t cpos;                   /* Offset within the current */
92   off_t gpos;                   /* Global position in stream */
93 } io_ex_bchain;
94
95 typedef struct {
96   off_t offset;                 /* Offset of the source - not used */
97   off_t cpos;                   /* Offset within the current */
98 } io_ex_buffer;
99
100
101
102 /* Structures to describe data sources */
103
104 typedef struct {
105   io_type       type;
106   int           fd;
107 } io_fdseek;
108
109 typedef struct {
110   io_type       type;           /* Must be first parameter */
111   char          *name;          /* Data source name */
112   char          *data;
113   size_t        len;
114   closebufp     closecb;        /* free memory mapped segment or decrement refcount */
115   void          *closedata;
116 } io_buffer;
117
118 typedef struct {
119   io_type       type;           /* Must be first parameter */
120   char          *name;          /* Data source name */
121   void          *p;             /* Callback data */
122   readl         readcb;
123   writel        writecb;
124   seekl         seekcb;
125 } io_cb;
126
127 typedef union {
128   io_type       type;
129   io_fdseek     fdseek;
130   io_buffer     buffer;
131   io_cb         cb;
132 } io_obj;
133
134 typedef struct _io_glue {
135   io_obj        source;
136   int           flags;          /* Flags */
137   void          *exdata;        /* Pair specific data */
138   readp         readcb;
139   writep        writecb;
140   seekp         seekcb;
141   closep        closecb;
142   sizep         sizecb;
143 } io_glue;
144
145 void io_obj_setp_buffer(io_obj *io, char *p, size_t len, closebufp closecb, void *closedata);
146 void io_obj_setp_cb      (io_obj *io, void *p, readl readcb, writel writecb, seekl seekcb);
147 void io_glue_commit_types(io_glue *ig);
148 void io_glue_gettypes    (io_glue *ig, int reqmeth);
149
150
151 /* XS functions */
152 io_glue *io_new_fd(int fd);
153 io_glue *io_new_bufchain(void);
154 io_glue *io_new_buffer(char *data, size_t len, closebufp closecb, void *closedata);
155 size_t   io_slurp(io_glue *ig, unsigned char **c);
156 void io_glue_DESTROY(io_glue *ig);
157
158 #endif /* _IOLAYER_H_ */