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
|
/*
* This file is subject to the terms of the GFX License. If a copy of
* the license was not distributed with this file, you can obtain one at:
*
* http://ugfx.org/license.html
*/
/**
* @file src/gfile/gfile_gfile.c
* @brief GFILE code.
*
*/
#include "gfx.h"
#if GFX_USE_GFILE
#include "gfile_fs.h"
/**
* Define the VMT's for the file-systems we want to search for files.
* Virtual file-systems that have special open() calls do not need to
* be in this list.
*/
#if GFILE_NEED_ROMFS
extern const GFILEVMT FsROMVMT;
#endif
#if GFILE_NEED_NATIVEFS
extern const GFILEVMT FsNativeVMT;
#endif
#if GFILE_NEED_FATFS
extern const GFILEVMT FsFatFSVMT;
#endif
#if GFILE_NEED_RAMFS
extern const GFILEVMT FsRAMVMT;
#endif
/**
* The order of the file-systems below determines the order
* that they are searched to find a file.
*/
static const GFILEVMT const * FsArray[] = {
#if GFILE_NEED_ROMFS
&FsROMVMT,
#endif
#if GFILE_NEED_NATIVEFS
&FsNativeVMT,
#endif
#if GFILE_NEED_FATFS
&FsFatFSVMT,
#endif
#if GFILE_NEED_RAMFS
&FsRAMVMT,
#endif
};
/*
* The table of GFILE's
*/
static GFILE gfileArr[GFILE_MAX_GFILES];
GFILE *gfileStdIn;
GFILE *gfileStdOut;
GFILE *gfileStdErr;
/**
* The init routine
*/
void _gfileInit(void) {
#if GFILE_NEED_NATIVEFS
extern void _gfileNativeAssignStdio(void);
_gfileNativeAssignStdio();
#endif
}
void _gfileDeinit(void)
{
/* ToDo */
}
/**
* Internal routine to find an empty GFILE slot and interpret flags.
*/
GFILE *_gfileFindSlot(const char *mode) {
GFILE * f;
// First find an available GFILE slot.
for (f = gfileArr; f < &gfileArr[GFILE_MAX_GFILES]; f++) {
if (!(f->flags & GFILEFLG_OPEN)) {
// Get the flags
switch(mode[0]) {
case 'r':
f->flags = GFILEFLG_READ|GFILEFLG_MUSTEXIST;
while (*++mode) {
switch(mode[0]) {
case '+': f->flags |= GFILEFLG_WRITE; break;
case 'b': f->flags |= GFILEFLG_BINARY; break;
}
}
break;
case 'w':
f->flags = GFILEFLG_WRITE|GFILEFLG_TRUNC;
while (*++mode) {
switch(mode[0]) {
case '+': f->flags |= GFILEFLG_READ; break;
case 'b': f->flags |= GFILEFLG_BINARY; break;
case 'x': f->flags |= GFILEFLG_MUSTNOTEXIST; break;
}
}
break;
case 'a':
f->flags = GFILEFLG_WRITE|GFILEFLG_APPEND;
while (*++mode) {
switch(mode[0]) {
case '+': f->flags |= GFILEFLG_READ; break;
case 'b': f->flags |= GFILEFLG_BINARY; break;
case 'x': f->flags |= GFILEFLG_MUSTNOTEXIST; break;
}
}
break;
default:
return 0;
}
return f;
}
}
return 0;
}
/********************************************************
* IO routines
********************************************************/
bool_t gfileExists(const char *fname) {
const GFILEVMT * const *p;
#if GFILE_ALLOW_DEVICESPECIFIC
if (fname[0] && fname[1] == '|') {
for(p = FsArray; p < &FsArray[sizeof(FsArray)/sizeof(FsArray[0])]; p++) {
if (p[0]->prefix == fname[0])
return p[0]->exists && p[0]->exists(fname+2);
}
return FALSE;
}
#endif
for(p = FsArray; p < &FsArray[sizeof(FsArray)/sizeof(FsArray[0])]; p++) {
if (p[0]->exists && p[0]->exists(fname))
return TRUE;
}
return FALSE;
}
bool_t gfileDelete(const char *fname) {
const GFILEVMT **p;
#if GFILE_ALLOW_DEVICESPECIFIC
if (fname[0] && fname[1] == '|') {
for(p = FsArray; p < &FsArray[sizeof(FsArray)/sizeof(FsArray[0])]; p++) {
if (p[0]->prefix == fname[0])
return p[0]->del && p[0]->del(fname+2);
}
return FALSE;
}
#endif
for(p = FsArray; p < &FsArray[sizeof(FsArray)/sizeof(FsArray[0])]; p++) {
if (p[0]->del && p[0]->del(fname))
return TRUE;
}
return FALSE;
}
long int gfileGetFilesize(const char *fname) {
const GFILEVMT * const *p;
long int res;
#if GFILE_ALLOW_DEVICESPECIFIC
if (fname[0] && fname[1] == '|') {
for(p = FsArray; p < &FsArray[sizeof(FsArray)/sizeof(FsArray[0])]; p++) {
if (p[0]->prefix == fname[0])
return p[0]->filesize ? p[0]->filesize(fname+2) : -1;
}
return -1;
}
#endif
for(p = FsArray; p < &FsArray[sizeof(FsArray)/sizeof(FsArray[0])]; p++) {
if (p[0]->filesize && (res = p[0]->filesize(fname)) != -1)
return res;
}
return -1;
}
bool_t gfileRename(const char *oldname, const char *newname) {
const GFILEVMT * const *p;
#if GFILE_ALLOW_DEVICESPECIFIC
if ((oldname[0] && oldname[1] == '|') || (newname[0] && newname[1] == '|')) {
char ch;
if (oldname[0] && oldname[1] == '|') {
ch = oldname[0];
oldname += 2;
if (newname[0] && newname[1] == '|') {
if (newname[0] != ch)
// Both oldname and newname are fs specific but different ones.
return FALSE;
newname += 2;
}
} else {
ch = newname[0];
newname += 2;
}
for(p = FsArray; p < &FsArray[sizeof(FsArray)/sizeof(FsArray[0])]; p++) {
if (p[0]->prefix == ch)
return p[0]->ren && p[0]->ren(oldname, newname);
}
return FALSE;
}
#endif
for(p = FsArray; p < &FsArray[sizeof(FsArray)/sizeof(FsArray[0])]; p++) {
if (p[0]->ren && p[0]->ren(oldname,newname))
return TRUE;
}
return FALSE;
}
static bool_t testopen(const GFILEVMT *p, GFILE *f, const char *fname) {
// If we want write but the fs doesn't allow it then return
if ((f->flags & GFILEFLG_WRITE) && !(p->flags & GFSFLG_WRITEABLE))
return FALSE;
// Try to open
if (!p->open || !p->open(f, fname))
return FALSE;
// File is open - fill in all the details
f->vmt = p;
f->pos = 0;
f->flags |= GFILEFLG_OPEN;
if (p->flags & GFSFLG_SEEKABLE)
f->flags |= GFILEFLG_CANSEEK;
return TRUE;
}
GFILE *gfileOpen(const char *fname, const char *mode) {
GFILE * f;
const GFILEVMT * const *p;
// Get an empty file and set the flags
if (!(f = _gfileFindSlot(mode)))
return 0;
#if GFILE_ALLOW_DEVICESPECIFIC
if (fname[0] && fname[1] == '|') {
for(p = FsArray; p < &FsArray[sizeof(FsArray)/sizeof(FsArray[0])]; p++) {
if (p[0]->prefix == fname[0])
return testopen(p[0], f, fname+2) ? f : 0;
}
// File not found
return 0;
}
#endif
for(p = FsArray; p < &FsArray[sizeof(FsArray)/sizeof(FsArray[0])]; p++) {
if (testopen(p[0], f, fname))
return f;
}
// File not found
return 0;
}
void gfileClose(GFILE *f) {
if (!f || !(f->flags & GFILEFLG_OPEN))
return;
if (f->vmt->close)
f->vmt->close(f);
f->flags = 0;
}
size_t gfileRead(GFILE *f, void *buf, size_t len) {
size_t res;
if (!f || (f->flags & (GFILEFLG_OPEN|GFILEFLG_READ)) != (GFILEFLG_OPEN|GFILEFLG_READ))
return 0;
if (!f->vmt->read)
return 0;
if ((res = f->vmt->read(f, buf, len)) <= 0)
return 0;
f->pos += res;
return res;
}
size_t gfileWrite(GFILE *f, const void *buf, size_t len) {
size_t res;
if (!f || (f->flags & (GFILEFLG_OPEN|GFILEFLG_WRITE)) != (GFILEFLG_OPEN|GFILEFLG_WRITE))
return 0;
if (!f->vmt->write)
return 0;
if ((res = f->vmt->write(f, buf, len)) <= 0)
return 0;
f->pos += res;
return res;
}
long int gfileGetPos(GFILE *f) {
if (!f || !(f->flags & GFILEFLG_OPEN))
return 0;
return f->pos;
}
bool_t gfileSetPos(GFILE *f, long int pos) {
if (!f || !(f->flags & GFILEFLG_OPEN))
return FALSE;
if (!f->vmt->setpos || !f->vmt->setpos(f, pos))
return FALSE;
f->pos = pos;
return TRUE;
}
long int gfileGetSize(GFILE *f) {
if (!f || !(f->flags & GFILEFLG_OPEN))
return 0;
if (!f->vmt->getsize)
return 0;
return f->vmt->getsize(f);
}
bool_t gfileEOF(GFILE *f) {
if (!f || !(f->flags & GFILEFLG_OPEN))
return TRUE;
if (!f->vmt->eof)
return FALSE;
return f->vmt->eof(f);
}
bool_t gfileMount(char fs, const char* drive) {
const GFILEVMT * const *p;
// Find the correct VMT
for(p = FsArray; p < &FsArray[sizeof(FsArray)/sizeof(FsArray[0])]; p++) {
if (p[0]->prefix == fs) {
if (!p[0]->mount)
return FALSE;
return p[0]->mount(drive);
}
}
return FALSE;
}
bool_t gfileUnmount(char fs, const char* drive) {
const GFILEVMT * const *p;
// Find the correct VMT
for(p = FsArray; p < &FsArray[sizeof(FsArray)/sizeof(FsArray[0])]; p++) {
if (p[0]->prefix == fs) {
if (!p[0]->mount)
return FALSE;
return p[0]->unmount(drive);
}
}
return FALSE;
}
bool_t gfileSync(GFILE *f) {
if (!f->vmt->sync)
return FALSE;
return f->vmt->sync(f);
}
#if GFILE_NEED_FILELISTS
gfileList *gfileOpenFileList(char fs, const char *path, bool_t dirs) {
const GFILEVMT * const *p;
gfileList * pfl;
// Find the correct VMT
for(p = FsArray; p < &FsArray[sizeof(FsArray)/sizeof(FsArray[0])]; p++) {
if (p[0]->prefix == fs) {
if (!p[0]->flopen)
return 0;
pfl = p[0]->flopen(path, dirs);
if (pfl) {
pfl->vmt = p[0];
pfl->dirs = dirs;
}
return pfl;
}
}
return 0;
}
const char *gfileReadFileList(gfileList *pfl) {
return pfl->vmt->flread ? pfl->vmt->flread(pfl) : 0;
}
void gfileCloseFileList(gfileList *pfl) {
if (pfl->vmt->flclose)
pfl->vmt->flclose(pfl);
}
#endif
#endif /* GFX_USE_GFILE */
|