Commit | Line | Data |
---|---|---|
0778adbf TC |
1 | #ifndef IMAGER_IOLAYERT_H |
2 | #define IMAGER_IOLAYERT_H | |
3 | ||
4 | #ifndef _MSC_VER | |
5 | #include <unistd.h> | |
6 | #endif | |
7 | #include <sys/types.h> | |
f6d10024 | 8 | #include <stddef.h> |
8d14daab | 9 | #include <stdio.h> |
0778adbf TC |
10 | |
11 | typedef enum { FDSEEK, FDNOSEEK, BUFFER, CBSEEK, CBNOSEEK, BUFCHAIN } io_type; | |
12 | ||
13 | #ifdef _MSC_VER | |
14 | typedef int ssize_t; | |
15 | #endif | |
16 | ||
17 | typedef struct i_io_glue_t i_io_glue_t; | |
18 | ||
19 | /* compatibility for now */ | |
20 | typedef i_io_glue_t io_glue; | |
21 | ||
22 | /* Callbacks we give out */ | |
23 | ||
24 | typedef ssize_t(*i_io_readp_t) (io_glue *ig, void *buf, size_t count); | |
25 | typedef ssize_t(*i_io_writep_t)(io_glue *ig, const void *buf, size_t count); | |
26 | typedef off_t (*i_io_seekp_t) (io_glue *ig, off_t offset, int whence); | |
2b405c9e | 27 | typedef int (*i_io_closep_t)(io_glue *ig); |
0778adbf TC |
28 | typedef ssize_t(*i_io_sizep_t) (io_glue *ig); |
29 | ||
30 | typedef void (*i_io_closebufp_t)(void *p); | |
31 | typedef void (*i_io_destroyp_t)(i_io_glue_t *ig); | |
32 | ||
33 | ||
34 | /* Callbacks we get */ | |
35 | ||
36 | typedef ssize_t(*i_io_readl_t) (void *p, void *buf, size_t count); | |
37 | typedef ssize_t(*i_io_writel_t)(void *p, const void *buf, size_t count); | |
38 | typedef off_t (*i_io_seekl_t) (void *p, off_t offset, int whence); | |
2b405c9e | 39 | typedef int (*i_io_closel_t)(void *p); |
0778adbf TC |
40 | typedef void (*i_io_destroyl_t)(void *p); |
41 | typedef ssize_t(*i_io_sizel_t) (void *p); | |
42 | ||
43 | extern char *io_type_names[]; | |
44 | ||
45 | ||
46 | ||
47 | /* Structures to describe data sources */ | |
48 | ||
49 | typedef struct { | |
50 | io_type type; | |
51 | int fd; | |
52 | } io_fdseek; | |
53 | ||
54 | typedef struct { | |
55 | io_type type; /* Must be first parameter */ | |
56 | char *name; /* Data source name */ | |
57 | char *data; | |
58 | size_t len; | |
59 | i_io_closebufp_t closecb; /* free memory mapped segment or decrement refcount */ | |
60 | void *closedata; | |
61 | } io_buffer; | |
62 | ||
63 | typedef struct { | |
64 | io_type type; /* Must be first parameter */ | |
65 | char *name; /* Data source name */ | |
66 | void *p; /* Callback data */ | |
67 | i_io_readl_t readcb; | |
68 | i_io_writel_t writecb; | |
69 | i_io_seekl_t seekcb; | |
70 | i_io_closel_t closecb; | |
71 | i_io_destroyl_t destroycb; | |
72 | } io_cb; | |
73 | ||
74 | typedef union { | |
75 | io_type type; | |
76 | io_fdseek fdseek; | |
77 | io_buffer buffer; | |
78 | io_cb cb; | |
79 | } io_obj; | |
80 | ||
81 | struct i_io_glue_t { | |
82 | io_obj source; | |
83 | int flags; /* Flags */ | |
84 | void *exdata; /* Pair specific data */ | |
85 | i_io_readp_t readcb; | |
86 | i_io_writep_t writecb; | |
87 | i_io_seekp_t seekcb; | |
88 | i_io_closep_t closecb; | |
89 | i_io_sizep_t sizecb; | |
90 | i_io_destroyp_t destroycb; | |
91 | }; | |
92 | ||
93 | #define i_io_type(ig) ((ig)->source.ig_type) | |
94 | #define i_io_read(ig, buf, size) ((ig)->readcb((ig), (buf), (size))) | |
95 | #define i_io_write(ig, data, size) ((ig)->writecb((ig), (data), (size))) | |
96 | #define i_io_seek(ig, offset, whence) ((ig)->seekcb((ig), (offset), (whence))) | |
97 | #define i_io_close(ig) ((ig)->closecb(ig)) | |
98 | ||
99 | ||
100 | #endif |