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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
|
=encoding UTF-8
=head1 NAME
BH_IO - I/O subsystem
=head1 SYNOPSIS
#include <BH/io.h>
BH_IO *io = BH_FileNew("input.txt", BH_FILE_WRITE | BH_FILE_TRUNCATE, NULL);
BH_IOWrite(io, "Hello, world!", 13, NULL);
BH_IOFree(io);
cc prog.c -o prog -lbh
=head1 DESCRIPTION
BH_IO provides a subsystem that allows you to work with various objects (files,
sockets, memory) via a single I/O interface. This allows you to write code that
is not tied to a specific input/output system.
It is guaranteed that any BH_IO object supports the following operations:
L</BH_IORead>, L</BH_IOWrite>, L</BH_IOCtl> и L</BH_IOCap>.
Depending on the implementation of a particular BH_IO object, additional
operations may be available: L</BH_IOFlags>, L</BH_IOClear>, L</BH_IOFlush>,
L</BH_IOSize>, L</BH_IOTell>, L</BH_IOSeek>, L</BH_IOPeek> and others.
By default, the I/O subsystem allows you to work with files (L</BH_FileNew>) or
RAM (L</BH_BytesNew>), as well as buffer I/O (L</BH_BufferNew>).
=head1 EXTENSIONS
BH_IO provides the developer with the ability to create custom implementations
of I/O devices. Example:
typedef struct BH_MyIO
{
BH_IO parent;
int flags;
};
static int ioDestroy(BH_MyIO *io)
{
/* Ommitted for the example */
}
static int ioRead(BH_MyIO *io,
BH_IOReadInfo *info)
{
/* Ommitted for the example */
}
static int ioWrite(BH_MyIO *io,
BH_IOWriteInfo *info)
{
/* Ommited for the example */
}
static int ioFlags(BH_MyIO *io,
int *flags)
{
*flags = io->flags;
return BH_OK;
}
static int ioCap(BH_MyIO *io,
int *op)
{
BH_UNUSED(io);
switch (*op)
{
case BH_IO_CTL_FLAGS:
return BH_OK;
default:
return BH_NOIMPL;
}
}
static int ioCtl(BH_MyIO *io,
BH_IOCtlInfo *info)
{
switch (info->op)
{
case BH_IO_CTL_FLAGS:
return ioFlags(io, (int *)info->arg);
default:
return BH_NOIMPL;
}
}
static int ioCallback(BH_MyIO *io,
int type,
void *arg)
{
switch (type)
{
case BH_IO_OP_DESTROY: return ioDestroy(io);
case BH_IO_OP_READ: return ioRead(io, (BH_IOReadInfo *)arg);
case BH_IO_OP_WRITE: return ioWrite(io, (BH_IOWriteInfo *)arg);
case BH_IO_OP_CTL: return ioCtl(io, (BH_IOCtlInfo *)arg);
case BH_IO_OP_CAP: return ioCap(io, (int*)arg);
default: return BH_NOIMPL;
}
}
BH_IO *BH_MyIONew(int *result)
{
BH_MyIO *io;
int code;
code = BH_OOM;
if ((io = malloc(sizeof(*io))))
{
io->parent.callback = (BH_IOCallback)ioCallback;
io->flags = 0;
}
if (result)
*result = code;
return (BH_IO*)io;
}
=head1 API CALLS
=head2 BH_FileNew
BH_IO *BH_FileNew(const char *path,
int mode,
int *result);
Creates an I/O device for working with the file using the I<path>.
The I<mode> parameter can take a combination of the following values:
=over
=item B<BH_FILE_READ>
Opens the file for reading
=item B<BH_FILE_WRTIE>
Opens the file for writing
=item B<BH_FILE_APPEND>
Opens the file in append mode
=item B<BH_FILE_TRUNCATE>
Truncates the file
=item B<BH_FILE_CREATE>
The file must be created
=item B<BH_FILE_EXIST>
The file must exist
=back
The optional parameter I<result> returns 0 or an error code.
This function returns a pointer to a new BH_IO object or NULL.
=head2 BH_IOIsFile
int BH_IOIsFile(BH_IO *device);
Checks if I/O device is a file.
=head2 BH_BufferNew
BH_IO *BH_BufferNew(BH_IO *device,
size_t size,
int *result);
Creates an I/O device to buffer data to another I<device>.
The I<size> parameter is responsible for the size of the read and write buffers.
The optional parameter I<result> returns 0 or an error code.
If successful, this function returns a pointer to the new BH_IO object or
NULL in case of an error.
=head2 BH_IOIsBuffer
int BH_IOIsBuffer(BH_IO *device);
Checks if I/O device is a buffer.
=head2 BH_BytesNew
BH_IO *BH_BytesNew(char *data,
size_t size,
int *result);
Creates an I/O device for the memory region I<data> with the size I<size>.
The optional parameter I<result> returns 0 or an error code.
If successful, this function returns a pointer to the new BH_IO object or
NULL in case of an error.
=head2 BH_IOIsBytes
int BH_IOIsBytes(BH_IO *device);
Checks if I/O device is a memory region/bytes.
=head2 BH_IOFree
void BH_IOFree(BH_IO *device);
Destroys the I/O device.
=head2 BH_IORead
int BH_IORead(BH_IO *device,
char *buffer,
size_t size,
size_t *actual);
Reads up to I<size> bytes from the I/O device and writes data to
I<buffer>.
The optional parameter I<actual> returns the number of bytes that was read.
If successful, this function returns 0 or an error code.
=head2 BH_IOWrite
int BH_IOWrite(BH_IO *io,
const char *buffer,
size_t size,
size_t *actual);
Writes up to I<size> bytes to the I/O device from I<buffer>.
The optional parameter I<actual> returns the number of bytes that was written.
If successful, this function returns 0 or an error code.
=head2 BH_IOCtl
int BH_IOCtl(BH_IO *device,
int op,
void *arg);
Manipulates the parameters of the I/O device using the I<op> command and
the I<arg> argument.
Possible values of I<op>:
=over
=item B<BH_IO_CTL_FLAGS>
Argument: int *
Return the I/O device flags.
=item B<BH_IO_CTL_CLEAR>
Reset the I/O device errors.
=item B<BH_IO_CTL_PEEK>
Argument: L<BH_IOReadInfo *|/BH_IOReadInfo>
Reads data from an I/O device without extracting it.
=item B<BH_IO_CTL_FLUSH>
Write buffered data to the I/O device.
=item B<BH_IO_CTL_SIZE>
Argument: int64_t *
Get the size of the I/O device.
=item B<BH_IO_CTL_TELL>
Argument: int64_t *
Reads the current offset of the I/O device reader pointer.
=item B<BH_IO_CTL_SEEK>
Argument: L<BH_IOSeekInfo *|/BH_IOSeekInfo>
Changes the current position of the I/O reader pointer.
=item B<BH_IO_CTL_GET_IO>
Argument: L<BH_IO **|/BH_IO>|void *
Gets the device I/O object used in the implementation.
=item B<BH_IO_CTL_SET_IO>
Argument: L<BH_IO *|/BH_IO>|void *
Sets the device I/O object to be used in the implementation.
=back
If successful, this function returns 0 or an error code.
=head2 BH_IOCap
int BH_IOCap(BH_IO *device,
int op);
Checks whether the I<op> command can be executed on the I/O device.
If successful, this function returns 0 or an error code.
=head2 BH_IOFlags
int BH_IOFlags(BH_IO *device,
int *flags);
Returns the current I<flags> flags of the I/O device.
Possible flags (and their combinations):
=over
=item B<BH_IO_FLAG_ERROR>
An error occurred during the execution.
=item B<BH_IO_FLAG_EOF>
The device has reached the end of the file.
=back
If successful, this function returns 0 or an error code.
=head2 BH_IOClear
int BH_IOClear(BH_IO *device);
Cleans the I/O device from errors.
If successful, this function returns 0 or an error code.
=head2 BH_IOPeek
int BH_IOPeek(BH_IO *device,
char *buffer,
size_t size,
size_t *actual);
Reads up to I<size> bytes from the I/O device without extraction and writes
the data to I<buffer>.
The optional parameter I<actual> returns the number of bytes that was actually
read.
If successful, this function returns 0 or an error code.
=head2 BH_IOFlush
int BH_IOFlush(BH_IO *device);
Writes buffered values to the I/O device.
If successful, this function returns 0 or an error code.
=head2 BH_IOSize
int BH_IOSize(BH_IO *device,
int64_t *size);
Reads the current size of the I/O device in bytes and writes the value to
I<size>.
For different types of I/O devices, this value can mean different things (for
example: the current file size, the size of the memory allocated for I/O, etc.).
If successful, this function returns 0 or an error code.
=head2 BH_IOTell
int BH_IOTell(BH_IO *device,
int64_t *offset);
Reads the current offset of the I/O reader pointer relative
to the start and writes the value to I<offset>.
If successful, this function returns 0 or an error code.
=head2 BH_IOSeek
int BH_IOSeek(BH_IO *device,
int64_t offset,
int whence);
Changes the current position of the I/O reader pointer, taking into account
the offset I<offset> and the initial position I<whence>.
Possible values of the initial position I<whence>:
=over
=item B<BH_IO_SEEK_SET>
Offset relative to the beginning of the device.
=item B<BH_IO_SEEK_CUR>
Offset relative to the current position of the device.
=item B<BH_IO_SEEK_END>
Offset relative to the end of the device.
=back
If successful, this function returns 0 or an error code.
=head2 BH_IOError
int BH_IOError(BH_IO *device);
Checks whether the I/O device is in an error state.
This function is equivalent to the following code:
(BH_IOFlags(device) & BH_IO_FLAG_ERROR)
=head2 BH_IOEndOfFile
int BH_IOEndOfFile(BH_IO *device);
Checks whether the I/O device has reached the end.
This function is equivalent to the following code:
(BH_IOFlags(device) & BH_IO_FLAG_EOF)
=head2 BH_IOReadLine
char *BH_IOReadLine(BH_IO *device,
char *str,
size_t size);
Reads a line from I<device> into I<str>, up to I<size-1> bytes.
Stops at I<\n> or EOF. The result is null-terminated. Partial lines may remain
in the stream if longer than buffer.
Returns I<str> on success, NULL on error.
=head2 BH_IOReadLineFull
char *BH_IOReadLineFull(BH_IO *device,
char *str,
size_t size);
Reads a line from I<device> into I<str>, up to I<size-1> bytes.
Stops at I<\n> or EOF. The result is null-terminated. Consumes the entire line
from the stream, discarding excess data if the line is too long. Ensures no
partial line remains.
Returns I<str> on success, NULL on error.
=head1 STRUCTURES
=head2 BH_IO
typedef struct BH_IO
{
BH_IOCallback callback;
} BH_IO;
=head2 BH_IOReadInfo
typedef struct BH_IOReadInfo
{
char *data;
size_t size;
size_t *actual;
} BH_IOReadInfo;
=head2 BH_IOWriteInfo
typedef struct BH_IOWriteInfo
{
const char *data;
size_t size;
size_t *actual;
} BH_IOWriteInfo;
=head2 BH_IOCtlInfo
typedef struct BH_IOCtlInfo
{
int op;
void *arg;
} BH_IOCtlInfo;
=head2 BH_IOSeekInfo
typedef struct BH_IOSeekInfo
{
int64_t offset;
int whence;
} BH_IOSeekInfo;
=head1 SEE ALSO
L<BH>
|