Added io_buffer for reading from scalars. Also added test cases. Added
[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
27typedef union { int i; void *p; } iorp;
28
29typedef enum { FDSEEK, FDNOSEEK, BUFFER, CBSEEK, CBNOSEEK, BUFCHAIN } io_type;
30
31#ifdef _MSC_VER
32typedef int ssize_t;
33#endif
34
35struct _io_glue;
36
37/* Callbacks we give out */
38
39typedef ssize_t(*readp) (struct _io_glue *ig, void *buf, size_t count);
40typedef ssize_t(*writep)(struct _io_glue *ig, const void *buf, size_t count);
41typedef off_t (*seekp) (struct _io_glue *ig, off_t offset, int whence);
42typedef void (*closep)(struct _io_glue *ig);
43typedef ssize_t(*sizep) (struct _io_glue *ig);
44
4dfa5522 45typedef void (*closebufp)(void *p);
02d1d628
AMH
46
47
48/* Callbacks we get */
49
50typedef ssize_t(*readl) (int fd, void *buf, size_t count);
51typedef ssize_t(*writel)(int fd, const void *buf, size_t count);
52typedef off_t (*seekl) (int fd, off_t offset, int whence);
53typedef ssize_t(*sizel) (int fd);
54
55extern char *io_type_names[];
56
57
58typedef 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
69typedef struct {
70 off_t offset;
71 off_t cpos;
72} io_ex_rseek;
73
74
75typedef 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
84typedef 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
4dfa5522
AMH
95typedef 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
02d1d628
AMH
101
102/* Structures to describe data sources */
103
104typedef struct {
105 io_type type;
106 int fd;
107} io_fdseek;
108
109typedef struct {
110 io_type type; /* Must be first parameter */
111 char *name; /* Data source name */
4dfa5522 112 char *data;
02d1d628 113 size_t len;
4dfa5522
AMH
114 closebufp closecb; /* free memory mapped segment or decrement refcount */
115 void *closedata;
02d1d628
AMH
116} io_buffer;
117
118typedef 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
127typedef union {
128 io_type type;
129 io_fdseek fdseek;
130 io_buffer buffer;
131 io_cb cb;
132} io_obj;
133
134typedef 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
4dfa5522 145void io_obj_setp_buffer(io_obj *io, char *p, size_t len, closebufp closecb, void *closedata);
02d1d628
AMH
146void io_obj_setp_cb (io_obj *io, void *p, readl readcb, writel writecb, seekl seekcb);
147void io_glue_commit_types(io_glue *ig);
148void io_glue_gettypes (io_glue *ig, int reqmeth);
149
150
151/* XS functions */
152io_glue *io_new_fd(int fd);
faa9b3e7 153io_glue *io_new_bufchain(void);
4dfa5522 154io_glue *io_new_buffer(char *data, size_t len, closebufp closecb, void *closedata);
02d1d628
AMH
155size_t io_slurp(io_glue *ig, unsigned char **c);
156void io_glue_DESTROY(io_glue *ig);
157
158#endif /* _IOLAYER_H_ */