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
|
#include <BH/IO.h>
#include <stdlib.h>
#define BUFFER_SIZE (sizeof(char *))
void BH_IOFree(BH_IO *device)
{
/* Prevent working with NULL io */
if (!device)
return;
/* Call the IO device destruction handler and deallocate */
device->callback(device, BH_IO_OP_DESTROY, NULL);
}
int BH_IORead(BH_IO *device,
char *buffer,
size_t size,
size_t *actual)
{
BH_IOReadInfo info;
/* Prevent working with NULL io */
if (!device)
return BH_ERROR;
info.data = buffer;
info.size = size;
info.actual = actual;
return device->callback(device, BH_IO_OP_READ, &info);
}
int BH_IOWrite(BH_IO *device,
const char *buffer,
size_t size,
size_t *actual)
{
BH_IOWriteInfo info;
/* Prevent working with NULL io */
if (!device)
return BH_ERROR;
info.data = buffer;
info.size = size;
info.actual = actual;
return device->callback(device, BH_IO_OP_WRITE, &info);
}
int BH_IOCtl(BH_IO *device,
int op,
void *arg)
{
BH_IOCtlInfo info;
if (!device)
return BH_ERROR;
info.op = op;
info.arg = arg;
return device->callback(device, BH_IO_OP_CTL, &info);
}
int BH_IOCap(BH_IO *device,
int op)
{
if (!device)
return BH_ERROR;
return device->callback(device, BH_IO_OP_CAP, &op);
}
int BH_IOPeek(BH_IO *device,
char *buffer,
size_t size,
size_t *actual)
{
BH_IOReadInfo readInfo;
BH_IOSeekInfo posInfo;
size_t readed;
readInfo.data = buffer;
readInfo.size = size;
readInfo.actual = actual;
if (BH_IOCap(device, BH_IO_CTL_PEEK))
{
if (BH_IOCap(device, BH_IO_CTL_SEEK))
return BH_NOIMPL;
if (BH_IORead(device, buffer, size, &readed))
return BH_ERROR;
posInfo.whence = BH_IO_SEEK_CUR;
posInfo.offset = -(int64_t)readed;
if (BH_IOCtl(device, BH_IO_CTL_SEEK, &posInfo))
return BH_ERROR;
if (actual)
*actual = readed;
return BH_OK;
}
return BH_IOCtl(device, BH_IO_CTL_PEEK, &readInfo);
}
int BH_IOTell(BH_IO *device,
int64_t *offset)
{
return BH_IOCtl(device, BH_IO_CTL_TELL, offset);
}
int BH_IOSeek(BH_IO *device,
int64_t offset,
int whence)
{
BH_IOSeekInfo info;
info.offset = offset;
info.whence = whence;
return BH_IOCtl(device, BH_IO_CTL_SEEK, &info);
}
int BH_IOFlush(BH_IO *device)
{
return BH_IOCtl(device, BH_IO_CTL_FLUSH, NULL);
}
int BH_IOSize(BH_IO *device,
int64_t *size)
{
return BH_IOCtl(device, BH_IO_CTL_SIZE, size);
}
int BH_IOFlags(BH_IO *device, int *flags)
{
return BH_IOCtl(device, BH_IO_CTL_FLAGS, flags);
}
int BH_IOClear(BH_IO *device)
{
return BH_IOCtl(device, BH_IO_CTL_CLEAR, NULL);
}
int BH_IOError(BH_IO *device)
{
int flags;
if (BH_IOFlags(device, &flags))
return BH_ERROR;
return flags & BH_IO_FLAG_ERROR;
}
int BH_IOEndOfFile(BH_IO *device)
{
int flags;
if (BH_IOFlags(device, &flags))
return BH_ERROR;
return flags & BH_IO_FLAG_EOF;
}
|