]> git.imager.perl.org - imager.git/blob - iolayer.h
iolayer modifications:
[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 enum { FDSEEK, FDNOSEEK, BUFFER, CBSEEK, CBNOSEEK, BUFCHAIN } io_type;
28
29 #ifdef _MSC_VER
30 typedef int ssize_t;
31 #endif
32
33 typedef struct i_io_glue_t i_io_glue_t;
34
35 /* compatibility for now */
36 typedef i_io_glue_t io_glue;
37
38 /* Callbacks we give out */
39
40 typedef ssize_t(*readp) (io_glue *ig, void *buf, size_t count);
41 typedef ssize_t(*writep)(io_glue *ig, const void *buf, size_t count);
42 typedef off_t  (*seekp) (io_glue *ig, off_t offset, int whence);
43 typedef void   (*closep)(io_glue *ig);
44 typedef ssize_t(*sizep) (io_glue *ig);
45
46 typedef void   (*closebufp)(void *p);
47
48
49 /* Callbacks we get */
50
51 typedef ssize_t(*readl) (void *p, void *buf, size_t count);
52 typedef ssize_t(*writel)(void *p, const void *buf, size_t count);
53 typedef off_t  (*seekl) (void *p, off_t offset, int whence);
54 typedef void   (*closel)(void *p);
55 typedef void   (*destroyl)(void *p);
56 typedef ssize_t(*sizel) (void *p);
57
58 extern char *io_type_names[];
59
60
61
62 /* Structures to describe data sources */
63
64 typedef struct {
65   io_type       type;
66   int           fd;
67 } io_fdseek;
68
69 typedef struct {
70   io_type       type;           /* Must be first parameter */
71   char          *name;          /* Data source name */
72   char          *data;
73   size_t        len;
74   closebufp     closecb;        /* free memory mapped segment or decrement refcount */
75   void          *closedata;
76 } io_buffer;
77
78 typedef struct {
79   io_type       type;           /* Must be first parameter */
80   char          *name;          /* Data source name */
81   void          *p;             /* Callback data */
82   readl         readcb;
83   writel        writecb;
84   seekl         seekcb;
85   closel        closecb;
86   destroyl      destroycb;
87 } io_cb;
88
89 typedef union {
90   io_type       type;
91   io_fdseek     fdseek;
92   io_buffer     buffer;
93   io_cb         cb;
94 } io_obj;
95
96 struct i_io_glue_t {
97   io_obj        source;
98   int           flags;          /* Flags */
99   void          *exdata;        /* Pair specific data */
100   readp         readcb;
101   writep        writecb;
102   seekp         seekcb;
103   closep        closecb;
104   sizep         sizecb;
105 };
106
107 void io_glue_commit_types(io_glue *ig);
108 void io_glue_gettypes    (io_glue *ig, int reqmeth);
109
110
111 /* XS functions */
112 io_glue *io_new_fd(int fd);
113 io_glue *io_new_bufchain(void);
114 io_glue *io_new_buffer(char *data, size_t len, closebufp closecb, void *closedata);
115 io_glue *io_new_cb(void *p, readl readcb, writel writecb, seekl seekcb, closel closecb, destroyl destroycb);
116 size_t   io_slurp(io_glue *ig, unsigned char **c);
117 void     io_glue_DESTROY(io_glue *ig);
118
119 #define i_io_type(ig) ((ig)->source.ig_type)
120
121 #endif /* _IOLAYER_H_ */