]>
Commit | Line | Data |
---|---|---|
02d1d628 AMH |
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 */ | |
3e27d469 | 22 | #define BBSIZ 16384 |
02d1d628 AMH |
23 | #define IO_FAKE_SEEK 1<<0L |
24 | #define IO_TEMP_SEEK 1<<1L | |
25 | ||
26 | ||
02d1d628 AMH |
27 | typedef enum { FDSEEK, FDNOSEEK, BUFFER, CBSEEK, CBNOSEEK, BUFCHAIN } io_type; |
28 | ||
29 | #ifdef _MSC_VER | |
30 | typedef int ssize_t; | |
31 | #endif | |
32 | ||
299da279 TC |
33 | typedef struct i_io_glue_t i_io_glue_t; |
34 | ||
35 | /* compatibility for now */ | |
36 | typedef i_io_glue_t io_glue; | |
02d1d628 AMH |
37 | |
38 | /* Callbacks we give out */ | |
39 | ||
299da279 TC |
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); | |
02d1d628 | 45 | |
4dfa5522 | 46 | typedef void (*closebufp)(void *p); |
02d1d628 AMH |
47 | |
48 | ||
49 | /* Callbacks we get */ | |
50 | ||
10461f9a TC |
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); | |
02d1d628 AMH |
57 | |
58 | extern char *io_type_names[]; | |
59 | ||
60 | ||
02d1d628 AMH |
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 */ | |
4dfa5522 | 72 | char *data; |
02d1d628 | 73 | size_t len; |
4dfa5522 AMH |
74 | closebufp closecb; /* free memory mapped segment or decrement refcount */ |
75 | void *closedata; | |
02d1d628 AMH |
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; | |
10461f9a TC |
85 | closel closecb; |
86 | destroyl destroycb; | |
02d1d628 AMH |
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 | ||
299da279 | 96 | struct i_io_glue_t { |
02d1d628 AMH |
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; | |
299da279 | 105 | }; |
02d1d628 | 106 | |
02d1d628 AMH |
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); | |
faa9b3e7 | 113 | io_glue *io_new_bufchain(void); |
4dfa5522 | 114 | io_glue *io_new_buffer(char *data, size_t len, closebufp closecb, void *closedata); |
10461f9a | 115 | io_glue *io_new_cb(void *p, readl readcb, writel writecb, seekl seekcb, closel closecb, destroyl destroycb); |
02d1d628 | 116 | size_t io_slurp(io_glue *ig, unsigned char **c); |
527c0c3e | 117 | void io_glue_DESTROY(io_glue *ig); |
02d1d628 | 118 | |
299da279 TC |
119 | #define i_io_type(ig) ((ig)->source.ig_type) |
120 | ||
02d1d628 | 121 | #endif /* _IOLAYER_H_ */ |