aboutsummaryrefslogtreecommitdiff
path: root/include/BH/IO.h
blob: ada9dc082c1a2bb8276f364587feb2d2806c2efa (plain)
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#ifndef BH_IO_H
#define BH_IO_H


#include "Common.h"


#define BH_IO_OP_DESTROY        0x0000
#define BH_IO_OP_READ           0x0001
#define BH_IO_OP_WRITE          0x0002
#define BH_IO_OP_CTL            0x0003
#define BH_IO_OP_CAP            0x0004


#define BH_IO_CTL_FLAGS         0x0000
#define BH_IO_CTL_CLEAR         0x0001
#define BH_IO_CTL_PEEK          0x0002
#define BH_IO_CTL_FLUSH         0x0003
#define BH_IO_CTL_SIZE          0x0004
#define BH_IO_CTL_TELL          0x0005
#define BH_IO_CTL_SEEK          0x0006
#define BH_IO_CTL_GET_IO        0x0007
#define BH_IO_CTL_SET_IO        0x0008


#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_FILE_READ            0x0001
#define BH_FILE_WRITE           0x0002
#define BH_FILE_READWRITE       0x0003
#define BH_FILE_APPEND          0x0010
#define BH_FILE_TRUNCATE        0x0020
#define BH_FILE_CREATE          0x0040
#define BH_FILE_EXIST           0x0080


typedef int (*BH_IOCallback)(void *, int, void *);


typedef struct BH_IO
{
    BH_IOCallback callback;
} BH_IO;


typedef struct BH_IOReadInfo
{
    char *data;
    size_t size;
    size_t *actual;
} BH_IOReadInfo;


typedef struct BH_IOWriteInfo
{
    const char *data;
    size_t size;
    size_t *actual;
} BH_IOWriteInfo;


typedef struct BH_IOCtlInfo
{
    int op;
    void *arg;
} BH_IOCtlInfo;


typedef struct BH_IOSeekInfo
{
    int64_t offset;
    int whence;
} BH_IOSeekInfo;


/**
 * Creates an input/output device representing a file at the given \a path.
 *
 * \param path    File path
 * \param mode    Open mode
 * \param result  Result code
 *
 * \return On success, returns IO device pointer.
 * \return On failure, returns NULL pointer.
 */
BH_IO *BH_FileNew(const char *path,
                  int mode,
                  int *result);


/**
 * Creates an input/output deivce that buffers access to other \a device.
 *
 * \param device  IO device pointer
 * \param size    Buffer size
 * \param result  Result code
 *
 * \return On success, returns IO device pointer.
 * \return On failure, returns NULL pointer.
 */
BH_IO *BH_BufferNew(BH_IO *device,
                    size_t size,
                    int *result);


/**
 * Creates an input/output devices that access memory buffer.
 *
 * \param data    Buffer pointer
 * \param size    Buffer size
 * \param result  Result code
 *
 * \return On success, returns IO device pointer.
 * \return On failure, returns NULL pointer.
 */
BH_IO *BH_BytesNew(char *data,
                   size_t size,
                   int *result);


/**
 * Destroys the input/output \a device.
 *
 * \param device  IO device pointer
 */
void BH_IOFree(BH_IO *device);


/**
 * Reads up to \a size bytes from the \a device into \a buffer.
 *
 * \param device  IO device pointer
 * \param buffer  Buffer pointer
 * \param size    Buffer size
 * \param actual  Bytes read (optional)
 *
 * \return On success, returns zero.
 * \return On failure, returns error code.
 */
int BH_IORead(BH_IO *device,
              char *buffer,
              size_t size,
              size_t *actual);


/**
 * Writes up to \a size bytes to the \a device from \a buffer.
 *
 * \param io      IO device pointer
 * \param buffer  Buffer pointer
 * \param size    Buffer size
 * \param actual  Bytes written (optional)
 *
 * \return On success, returns zero.
 * \return On failure, returns error code.
 */
int BH_IOWrite(BH_IO *io,
               const char *buffer,
               size_t size,
               size_t *actual);


/**
 * Manupulates an input/output \a device with specific \a op and and \a arg.
 *
 * \param device  IO device pointer
 * \param op      Operation
 * \param arg     Argument
 *
 * \return On success, returns zero.
 * \return On failure, returns error code.
 */
int BH_IOCtl(BH_IO *device,
             int op,
             void *arg);


/**
 * Checks if an input/output \a device supports specific \a op.
 *
 * \param device  IO device pointer
 * \param op      Operation
 *
 * \return On success, returns zero.
 * \return On failure, returns error code.
 */
int BH_IOCap(BH_IO *device,
             int op);


/**
 * Peeks up to \a size bytes from the \a device into \a buffer.
 *
 * \param device  IO device pointer
 * \param buffer  Buffer pointer
 * \param size    Buffer size
 * \param actial  Bytes peeked (optional)
 *
 * \return On success, returns zero.
 * \return On failure, returns error code.
 */
int BH_IOPeek(BH_IO *device,
              char *buffer,
              size_t size,
              size_t *actual);


/**
 * Tells current \a offset in the \a device.
 *
 * \param device  IO device pointer
 * \param offset  Position
 *
 * \return On success, returns zero.
 * \return On failure, returns error code.
 */
int BH_IOTell(BH_IO *device,
              int64_t *offset);


/**
 * Seeks to specified \a offset and \a whence in the \a device.
 *
 * \param device  IO device pointer
 * \param offset  Position
 * \param whence  Direction
 *
 * \return On success, returns zero.
 * \return On failure, returns error code.
 */
int BH_IOSeek(BH_IO *device,
              int64_t offset,
              int whence);


/**
 * Flushes the internal buffers of the \a device.
 *
 * \param device  IO device pointer
 *
 * \return On success, returns zero.
 * \return On failure, returns error code.
 */
int BH_IOFlush(BH_IO *device);


/**
 * Returns total or available size of the \a device.
 *
 * \param device  IO pointer
 * \param size    Available/total size
 *
 * \return On success, returns zero.
 * \return On failure, returns error code.
 */
int BH_IOSize(BH_IO *device,
              int64_t *size);


/**
 * Returns flags of the \a device.
 *
 * \param device  IO device pointer
 * \param flags   Flags
 *
 * \return On success, returns zero.
 * \return On failure, returns error code.
 */
int BH_IOFlags(BH_IO *device,
               int *flags);


/**
 * Returns error flag of the \a device.
 *
 * \param device  IO device pointer
 *
 * \return Returns error flag.
 */
int BH_IOError(BH_IO *device);


/**
 * Returns end-of-file flag of the \a device.
 *
 * \param device  IO device pointer
 *
 * \return Returns end-of-file flag.
 */
int BH_IOEndOfFile(BH_IO *device);


/**
 * Clears errors of the \a device.
 *
 * \param device  IO device pointer
 *
 * \return On success, returns zero.
 * \return On failure, returns error code.
 */
int BH_IOClear(BH_IO *device);


#endif /* BH_IO_H */