1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
#ifndef BH_IO_H
#define BH_IO_H
#include "common.h"
#define BH_IO_INFO_CB 0x0000
#define BH_IO_INIT_CB 0x0001
#define BH_IO_DESTROY_CB 0x0002
#define BH_IO_OPEN_CB 0x0003
#define BH_IO_CLOSE_CB 0x0004
#define BH_IO_READ_CB 0x0005
#define BH_IO_WRITE_CB 0x0006
#define BH_IO_PEEK_CB 0x0007
#define BH_IO_TELL_CB 0x0008
#define BH_IO_SEEK_CB 0x0009
#define BH_IO_FLUSH_CB 0x000A
#define BH_IO_SIZE_CB 0x000B
#define BH_IO_FLAGS_CB 0x000C
#define BH_IO_CLEAR_CB 0x000D
#define BH_IO_READ 0x0001
#define BH_IO_WRITE 0x0002
#define BH_IO_READWRITE 0x0003
#define BH_IO_APPEND 0x0010
#define BH_IO_TRUNCATE 0x0020
#define BH_IO_CREATE 0x0040
#define BH_IO_EXIST 0x0080
#define BH_IO_SEEK_SET 0x0000
#define BH_IO_SEEK_CUR 0x0001
#define BH_IO_SEEK_END 0x0002
#define BH_IO_FLAG_OK 0x0000
#define BH_IO_FLAG_ERROR 0x0001
#define BH_IO_FLAG_EOF 0x0002
#define BH_IO_FLAG_OPEN 0x0004
#define BH_FILE_CLASSNAME "bh_file"
typedef struct bh_io_s bh_io_t;
typedef int (*bh_io_func_t)(void *, int, void *, void *);
/**
* Creates the IO handle that represents file.
*
* \param path Path to the file
*
* \return On success, returns IO handle.
* \return On failure, returns NULL pointer.
*/
bh_io_t *bh_file_new(const char *path);
/**
* Creates the IO handle that represents buffered IO.
*
* \param io IO handle
*
* \return On success, returns IO handle.
* \return On failure, returns NULL pointer.
*/
bh_io_t *bh_buffer_new(bh_io_t *io);
/**
* Creates the IO handle with specified handler and context.
*
* \param func IO actions handler
* \param data Initialization data
*
* \return On success, returns IO handle.
* \return On failure, returns NULL pointer.
*/
bh_io_t *bh_io_new(bh_io_func_t func,
void *data);
/**
* Destroys the IO.
*
* \param io IO handle
*/
void bh_io_free(bh_io_t *io);
/**
* Returns the IO instance classname.
*
* \param io IO handle
*
* \return On success, returns pointer to constant string.
* \return On failure, returns NULL pointer
*/
const char *bh_io_classname(bh_io_t *io);
/**
* Opens the IO in specified mode of operation.
*
* \param io IO handle
* \param mode Mode of operation
*
* \return On success, returns zero value.
* \return On failure, returns error code.
*/
int bh_io_open(bh_io_t *io,
int mode);
/**
* Closes the IO.
*
* \param io IO handle
*
* \return On success, returns zero value.
* \return On failure, returns error code.
*/
int bh_io_close(bh_io_t *io);
/**
* Reads up to specified amount of bytes from the IO into memory buffer.
*
* \param io IO handle
* \param buffer Pointer to the buffer
* \param size Bytes to read
* \param actual Bytes read
*
* \return On success, returns zero value.
* \return On failure, returns error code.
*/
int bh_io_read(bh_io_t *io,
char *buffer,
size_t size,
size_t *actual);
/**
* Writes up to specified amount of bytes into the IO from the memory buffer.
*
* \param io IO handle
* \param buffer Pointer to the buffer
* \param size Bytes to write
* \param actual Bytes written
*
* \return On success, returns zero value.
* \return On failure, returns error code.
*/
int bh_io_write(bh_io_t *io,
const char *buffer,
size_t size,
size_t *actual);
/**
* Peeks up to specified amount of bytes from the IO handle.
*
* \param io IO handle
* \param buffer Pointer to the buffer
* \param size Bytes to peek
* \param actial Bytes peeked
*
* \return On success, returns zero value.
* \return On failure, returns error code.
*/
int bh_io_peek(bh_io_t *io,
char *buffer,
size_t size,
size_t *actual);
/**
* Tells current offset in the IO.
*
* \param io IO handle
* \param position Offset
*
* \return On success, returns zero value.
* \return On failure, returns error code.
*/
int bh_io_tell(bh_io_t *io,
int64_t *position);
/**
* Seeks to specified offset in the IO.
*
* \param io IO handle
* \param position Offset
* \param direction Direction
*
* \return On success, returns zero value.
* \return On failure, returns error code.
*/
int bh_io_seek(bh_io_t *io,
int64_t position,
int direction);
/**
* Flushes the internal buffers of the IO.
*
* \param io IO handle
*
* \return On success, returns zero value.
* \return On failure, returns error code.
*/
int bh_io_flush(bh_io_t *io);
/**
* Returns the size of the IO (either total or available size).
*
* \param io IO handle
* \param size Available/total size
*
* \return On success, returns zero value.
* \return On failure, returns error code.
*/
int bh_io_size(bh_io_t *io,
int64_t *size);
/**
* Returns flags of the IO.
*
* \param io IO handle
*
* \return Flags of the IO
*/
int bh_io_flags(bh_io_t *io);
/**
* Clears errors of the IO.
*
* \param io IO handle
*
* \return On success, returns zero value.
* \return On failure, returns error code.
*/
int bh_io_clear(bh_io_t *io);
#endif /* BH_IO_H */
|