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