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
|
#include <BH/IO.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
typedef struct BH_File
{
BH_IO parent;
int flags;
int mode;
HANDLE handle;
} BH_File;
static int fileInit(BH_File *file,
const char *path,
int mode)
{
DWORD access = 0, how = 0;
/* Check if path is valid */
if (!path)
return BH_ERROR;
/* Determine read/write access flags */
if (mode & BH_FILE_READ)
access |= GENERIC_READ;
if (mode & BH_FILE_WRITE)
access |= GENERIC_WRITE;
if (!access)
return BH_ERROR;
/* Determine open mode flags */
if (mode & BH_FILE_TRUNCATE)
{
switch (mode & (BH_FILE_CREATE | BH_FILE_EXIST))
{
case 0: how = CREATE_ALWAYS; break;
case BH_FILE_CREATE: how = CREATE_NEW; break;
case BH_FILE_EXIST: how = TRUNCATE_EXISTING; break;
default: return BH_ERROR;
}
}
else
{
switch (mode & (BH_FILE_CREATE | BH_FILE_EXIST))
{
case 0: how = OPEN_ALWAYS; break;
case BH_FILE_CREATE: how = CREATE_NEW; break;
case BH_FILE_EXIST: how = OPEN_EXISTING; break;
default: return BH_ERROR;
}
}
/* Save mode that we are in and open file */
file->flags = 0;
file->mode = mode;
file->handle = CreateFileA(path, access, FILE_SHARE_READ, NULL, how, FILE_ATTRIBUTE_NORMAL, NULL);
if (file->handle == INVALID_HANDLE_VALUE)
return BH_ERROR;
/* Truncate file if needed */
if (mode & BH_FILE_TRUNCATE)
SetEndOfFile(file->handle);
return BH_OK;
}
static int fileDestroy(BH_File *file)
{
/* Close the file handle on destruction */
CloseHandle(file->handle);
free(file);
return BH_OK;
}
static int fileRead(BH_File *file,
BH_IOReadInfo *info)
{
DWORD readed;
/* Read data from the file */
if (!ReadFile(file->handle, info->data, (DWORD)info->size, &readed, NULL))
goto error;
/* Check if we reached end of file */
if (!readed)
file->flags |= BH_IO_FLAG_EOF;
else
file->flags &= ~BH_IO_FLAG_EOF;
if (info->actual)
*info->actual = readed;
return BH_OK;
error:
file->flags |= BH_IO_FLAG_ERROR;
return BH_ERROR;
}
static int fileWrite(BH_File *file,
BH_IOWriteInfo *info)
{
DWORD written;
LARGE_INTEGER position;
/* Adjust current position in the file to the end */
if (file->mode & BH_FILE_APPEND)
{
position.QuadPart = 0;
if (!SetFilePointerEx(file->handle, position, NULL, FILE_END))
goto error;
}
/* Write data to the file */
if (!WriteFile(file->handle, info->data, (DWORD)info->size, &written, NULL))
goto error;
/* Check for end of file */
if (!written)
file->flags |= BH_IO_FLAG_EOF;
else
file->flags &= ~BH_IO_FLAG_EOF;
if (info->actual)
*info->actual = written;
return BH_OK;
error:
file->flags |= BH_IO_FLAG_ERROR;
return BH_ERROR;
}
static int fileFlush(BH_File *file)
{
/* Flush OS buffers */
FlushFileBuffers(file->handle);
return BH_OK;
}
static int fileSeek(BH_File *file,
BH_IOSeekInfo *info)
{
LARGE_INTEGER position;
/* Set read/write position in the file */
position.QuadPart = info->offset;
if (!SetFilePointerEx(file->handle, position, NULL, info->whence))
goto error;
return BH_OK;
error:
file->flags |= BH_IO_FLAG_ERROR;
return BH_ERROR;
}
static int fileTell(BH_File *file,
int64_t *pos)
{
LARGE_INTEGER dummy, position;
/* Readback current position in the file */
dummy.QuadPart = 0;
if (!SetFilePointerEx(file->handle, dummy, &position, BH_IO_SEEK_CUR))
goto error;
*pos = position.QuadPart;
return BH_OK;
error:
file->flags |= BH_IO_FLAG_ERROR;
return BH_ERROR;
}
static int fileSize(BH_File *file,
int64_t *size)
{
LARGE_INTEGER dummy;
/* Get current file size */
if (!GetFileSizeEx(file->handle, &dummy))
goto error;
*size = dummy.QuadPart;
return BH_OK;
error:
file->flags |= BH_IO_FLAG_ERROR;
return BH_ERROR;
}
static int fileFlags(BH_File *file,
int *flags)
{
*flags = file->flags;
return BH_OK;
}
static int fileClear(BH_File *file)
{
/* Clear BH_IO_FLAG_ERROR flag */
file->flags &= ~BH_IO_FLAG_ERROR;
return BH_OK;
}
static int fileCap(BH_File *file,
int *op)
{
BH_UNUSED(file);
/* Return operations supported by the file input/output device */
switch (*op)
{
case BH_IO_CTL_FLAGS:
case BH_IO_CTL_CLEAR:
case BH_IO_CTL_FLUSH:
case BH_IO_CTL_SIZE:
case BH_IO_CTL_TELL:
case BH_IO_CTL_SEEK:
return BH_OK;
default:
return BH_NOIMPL;
}
}
static int fileCtl(BH_File *file,
BH_IOCtlInfo *info)
{
/* Handle supported operations */
switch (info->op)
{
case BH_IO_CTL_FLAGS:
return fileFlags(file, (int *)(info->arg));
case BH_IO_CTL_CLEAR:
return fileClear(file);
case BH_IO_CTL_FLUSH:
return fileFlush(file);
case BH_IO_CTL_SIZE:
return fileSize(file, (int64_t *)(info->arg));
case BH_IO_CTL_TELL:
return fileTell(file, (int64_t *)(info->arg));
case BH_IO_CTL_SEEK:
return fileSeek(file, (BH_IOSeekInfo *)(info->arg));
default:
return BH_NOIMPL;
}
}
static int fileCallback(BH_File *file,
int type,
void *arg)
{
/* Handle basic input/output operations */
switch (type)
{
case BH_IO_OP_DESTROY: return fileDestroy(file);
case BH_IO_OP_READ: return fileRead(file, (BH_IOReadInfo *)arg);
case BH_IO_OP_WRITE: return fileWrite(file, (BH_IOWriteInfo *)arg);
case BH_IO_OP_CTL: return fileCtl(file, (BH_IOCtlInfo *)arg);
case BH_IO_OP_CAP: return fileCap(file, (int*)arg);
default: return BH_NOIMPL;
}
}
BH_IO *BH_FileNew(const char *path,
int mode,
int *result)
{
BH_File *file;
int code;
code = BH_OOM;
/* Allocate new file object and initialize it */
if ((file = malloc(sizeof(*file))))
{
file->parent.callback = (BH_IOCallback)fileCallback;
if ((code = fileInit(file, path, mode)))
{
free(file);
file = NULL;
}
}
/* Report error code */
if (result)
*result = code;
return (BH_IO*)file;
}
|