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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
|
/*
* 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.c
* @brief GFILE code.
*
*/
#define GFILE_IMPLEMENTATION
#include "gfx.h"
#if GFX_USE_GFILE
struct GFILE {
const struct GFILEVMT * vmt;
uint16_t flags;
#define GFILEFLG_OPEN 0x0001 // File is open
#define GFILEFLG_READ 0x0002 // Read the file
#define GFILEFLG_WRITE 0x0004 // Write the file
#define GFILEFLG_APPEND 0x0008 // Append on each write
#define GFILEFLG_BINARY 0x0010 // Treat as a binary file
#define GFILEFLG_DELONCLOSE 0x0020 // Delete on close
#define GFILEFLG_CANSEEK 0x0040 // Seek operations are valid
#define GFILEFLG_FAILONBLOCK 0x0080 // Fail on a blocking call
#define GFILEFLG_MUSTEXIST 0x0100 // On open file must exist
#define GFILEFLG_MUSTNOTEXIST 0x0200 // On open file must not exist
#define GFILEFLG_TRUNC 0x0400 // On open truncate the file
void * obj;
long int pos;
};
typedef struct GFILEVMT {
const struct GFILEVMT * next;
uint8_t flags;
#define GFSFLG_WRITEABLE 0x0001
#define GFSFLG_CASESENSITIVE 0x0002
#define GFSFLG_SEEKABLE 0x0004
#define GFSFLG_FAST 0x0010
#define GFSFLG_SMALL 0x0020
#define GFSFLG_TEXTMODES 0x0040
char prefix;
bool_t (*del) (const char *fname);
bool_t (*exists) (const char *fname);
long int (*filesize) (const char *fname);
bool_t (*ren) (const char *oldname, const char *newname);
bool_t (*open) (GFILE *f, const char *fname);
void (*close) (GFILE *f);
int (*read) (GFILE *f, void *buf, int size);
int (*write) (GFILE *f, const void *buf, int size);
bool_t (*setpos) (GFILE *f, long int pos);
long int (*getsize) (GFILE *f);
bool_t (*eof) (GFILE *f);
} GFILEVMT;
// The chain of FileSystems
#define GFILE_CHAINHEAD 0
// The table of GFILE's
static GFILE gfileArr[GFILE_MAX_GFILES];
GFILE *gfileStdIn;
GFILE *gfileStdOut;
GFILE *gfileStdErr;
/**
* The order of the file-systems below determines the order
* that they are searched to find a file.
* The last defined is the first searched.
*/
/********************************************************
* The ChibiOS BaseFileStream VMT
********************************************************/
#if GFILE_NEED_CHIBIOSFS && GFX_USE_OS_CHIBIOS
#include "../src/gfile/inc_chibiosfs.c"
#endif
/********************************************************
* The Memory Pointer VMT
********************************************************/
#if GFILE_NEED_MEMFS
#include "../src/gfile/inc_memfs.c"
#endif
/********************************************************
* The RAM file-system VMT
********************************************************/
#if GFILE_NEED_RAMFS
#include "../src/gfile/inc_ramfs.c"
#endif
/********************************************************
* The FAT file-system VMT
********************************************************/
#ifndef GFILE_NEED_FATFS
#include "../src/gfile/inc_fatfs.c"
#endif
/********************************************************
* The native file-system
********************************************************/
#if GFILE_NEED_NATIVEFS
#include "../src/gfile/inc_nativefs.c"
#endif
/********************************************************
* The ROM file-system VMT
********************************************************/
#if GFILE_NEED_ROMFS
#include "../src/gfile/inc_romfs.c"
#endif
/********************************************************
* IO routines
********************************************************/
/**
* The chain of file systems.
*/
static const GFILEVMT const * FsChain = GFILE_CHAINHEAD;
/**
* The init routine
*/
void _gfileInit(void) {
#if GFILE_NEED_NATIVEFS
NativeStdIn.flags = GFILEFLG_OPEN|GFILEFLG_READ;
NativeStdIn.vmt = &FsNativeVMT;
NativeStdIn.obj = (void *)stdin;
NativeStdIn.pos = 0;
gfileStdIn = &NativeStdIn;
NativeStdOut.flags = GFILEFLG_OPEN|GFILEFLG_WRITE|GFILEFLG_APPEND;
NativeStdOut.vmt = &FsNativeVMT;
NativeStdOut.obj = (void *)stdout;
NativeStdOut.pos = 0;
gfileStdOut = &NativeStdOut;
NativeStdErr.flags = GFILEFLG_OPEN|GFILEFLG_WRITE|GFILEFLG_APPEND;
NativeStdErr.vmt = &FsNativeVMT;
NativeStdErr.obj = (void *)stderr;
NativeStdErr.pos = 0;
gfileStdErr = &NativeStdErr;
#endif
}
void _gfileDeinit(void)
{
/* ToDo */
}
bool_t gfileExists(const char *fname) {
const GFILEVMT *p;
#if GFILE_ALLOW_DEVICESPECIFIC
if (fname[0] && fname[1] == '|') {
for(p = FsChain; p; p = p->next) {
if (p->prefix == fname[0])
return p->exists && p->exists(fname+2);
}
return FALSE;
}
#endif
for(p = FsChain; p; p = p->next) {
if (p->exists && p->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 = FsChain; p; p = p->next) {
if (p->prefix == fname[0])
return p->del && p->del(fname+2);
}
return FALSE;
}
#endif
for(p = FsChain; p; p = p->next) {
if (p->del && p->del(fname))
return TRUE;
}
return FALSE;
}
long int gfileGetFilesize(const char *fname) {
const GFILEVMT *p;
long int res;
#if GFILE_ALLOW_DEVICESPECIFIC
if (fname[0] && fname[1] == '|') {
for(p = FsChain; p; p = p->next) {
if (p->prefix == fname[0])
return p->filesize ? p->filesize(fname+2) : -1;
}
return -1;
}
#endif
for(p = FsChain; p; p = p->next) {
if (p->filesize && (res = p->filesize(fname)) != -1)
return res;
}
return -1;
}
bool_t gfileRename(const char *oldname, const char *newname) {
const GFILEVMT *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 = FsChain; p; p = p->next) {
if (p->prefix == ch)
return p->ren && p->ren(oldname, newname);
}
return FALSE;
}
#endif
for(p = FsChain; p; p = p->next) {
if (p->ren && p->ren(oldname,newname))
return TRUE;
}
return FALSE;
}
static uint16_t mode2flags(const char *mode) {
uint16_t flags;
switch(mode[0]) {
case 'r':
flags = GFILEFLG_READ|GFILEFLG_MUSTEXIST;
while (*++mode) {
switch(mode[0]) {
case '+': flags |= GFILEFLG_WRITE; break;
case 'b': flags |= GFILEFLG_BINARY; break;
}
}
return flags;
case 'w':
flags = GFILEFLG_WRITE|GFILEFLG_TRUNC;
while (*++mode) {
switch(mode[0]) {
case '+': flags |= GFILEFLG_READ; break;
case 'b': flags |= GFILEFLG_BINARY; break;
case 'x': flags |= GFILEFLG_MUSTNOTEXIST; break;
}
}
return flags;
case 'a':
flags = GFILEFLG_WRITE|GFILEFLG_APPEND;
while (*++mode) {
switch(mode[0]) {
case '+': flags |= GFILEFLG_READ; break;
case 'b': flags |= GFILEFLG_BINARY; break;
case 'x': flags |= GFILEFLG_MUSTNOTEXIST; break;
}
}
return flags;
}
return 0;
}
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) {
uint16_t flags;
GFILE * f;
const GFILEVMT *p;
// Get the requested mode
if (!(flags = mode2flags(mode)))
return 0;
#if GFILE_ALLOW_DEVICESPECIFIC
if (fname[0] && fname[1] == '|') {
// First find an available GFILE slot.
for (f = gfileArr; f < &gfileArr[GFILE_MAX_GFILES]; f++) {
if (!(f->flags & GFILEFLG_OPEN)) {
// Try to open the file
f->flags = flags;
for(p = FsChain; p; p = p->next) {
if (p->prefix == fname[0])
return testopen(p, f, fname+2) ? f : 0;
}
// File not found
break;
}
}
// No available slot
return 0;
}
#endif
// First find an available GFILE slot.
for (f = gfileArr; f < &gfileArr[GFILE_MAX_GFILES]; f++) {
if (!(f->flags & GFILEFLG_OPEN)) {
// Try to open the file
f->flags = flags;
for(p = FsChain; p; p = p->next) {
if (testopen(p, f, fname))
return f;
}
// File not found
break;
}
}
// No available slot
return 0;
}
#if GFILE_NEED_CHIBIOSFS && GFX_USE_OS_CHIBIOS
GFILE * gfileOpenBaseFileStream(void *BaseFileStreamPtr, 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
if (!(f->flags = mode2flags(mode)))
return 0;
// If we want write but the fs doesn't allow it then return
if ((f->flags & GFILEFLG_WRITE) && !(FsCHIBIOSVMT.flags & GFSFLG_WRITEABLE))
return 0;
// File is open - fill in all the details
f->vmt = &FsCHIBIOSVMT;
f->obj = BaseFileStreamPtr;
f->pos = 0;
f->flags |= GFILEFLG_OPEN|GFILEFLG_CANSEEK;
return f;
}
}
// No available slot
return 0;
}
#endif
#if GFILE_NEED_MEMFS
GFILE * gfileOpenMemory(void *memptr, 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
if (!(f->flags = mode2flags(mode)))
return 0;
// If we want write but the fs doesn't allow it then return
if ((f->flags & GFILEFLG_WRITE) && !(FsMemVMT.flags & GFSFLG_WRITEABLE))
return 0;
// File is open - fill in all the details
f->vmt = &FsMemVMT;
f->obj = memptr;
f->pos = 0;
f->flags |= GFILEFLG_OPEN|GFILEFLG_CANSEEK;
return f;
}
}
// No available slot
return 0;
}
#endif
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);
}
/********************************************************
* String VMT routines
********************************************************/
#if GFILE_NEED_STRINGS && (GFILE_NEED_PRINTG || GFILE_NEED_SCANG)
#include <string.h>
// Special String VMT
static int StringRead(GFILE *f, void *buf, int size) {
// size must be 1 for a complete read
if (!((char *)f->obj)[f->pos])
return 0;
((char *)buf)[0] = ((char *)f->obj)[f->pos];
return 1;
}
static int StringWrite(GFILE *f, const void *buf, int size) {
// size must be 1 for a complete write
((char *)f->obj)[f->pos] = ((char *)buf)[0];
return 1;
}
static const GFILEVMT StringVMT = {
0, // next
0, // flags
'_', // prefix
0, 0, 0, 0,
0, 0, StringRead, StringWrite,
0, 0, 0,
};
#endif
/********************************************************
* printg routines
********************************************************/
#if GFILE_NEED_PRINTG
#include <stdarg.h>
#define MAX_FILLER 11
#define FLOAT_PRECISION 100000
int fnprintg(GFILE *f, int maxlen, const char *fmt, ...) {
int res;
va_list ap;
va_start(ap, fmt);
res = vfnprintg(f, maxlen, fmt, ap);
va_end(ap);
return res;
}
static char *ltoa_wd(char *p, long num, unsigned radix, long divisor) {
int i;
char * q;
if (!divisor) divisor = num;
q = p + MAX_FILLER;
do {
i = (int)(num % radix);
i += '0';
if (i > '9')
i += 'A' - '0' - 10;
*--q = i;
num /= radix;
} while ((divisor /= radix) != 0);
i = (int)(p + MAX_FILLER - q);
do {
*p++ = *q++;
} while (--i);
return p;
}
int vfnprintg(GFILE *f, int maxlen, const char *fmt, va_list arg) {
int ret;
char *p, *s, c, filler;
int i, precision, width;
bool_t is_long, left_align;
long l;
#if GFILE_ALLOW_FLOATS
float f;
char tmpbuf[2*MAX_FILLER + 1];
#else
char tmpbuf[MAX_FILLER + 1];
#endif
ret = 0;
if (maxlen < 0)
return 0;
if (!maxlen)
maxlen = -1;
while (*fmt) {
if (*fmt != '%') {
gfileWrite(f, fmt, 1);
ret++; if (--maxlen) return ret;
fmt++;
continue;
}
fmt++;
p = s = tmpbuf;
left_align = FALSE;
filler = ' ';
width = 0;
precision = 0;
if (*fmt == '-') {
fmt++;
left_align = TRUE;
}
if (*fmt == '.') {
fmt++;
filler = '0';
}
while (1) {
c = *fmt++;
if (c >= '0' && c <= '9')
c -= '0';
else if (c == '*')
c = va_arg(ap, int);
else
break;
width = width * 10 + c;
}
if (c == '.') {
while (1) {
c = *fmt++;
if (c >= '0' && c <= '9')
c -= '0';
else if (c == '*')
c = va_arg(ap, int);
else
break;
precision = precision * 10 + c;
}
}
/* Long modifier.*/
if (c == 'l' || c == 'L') {
is_long = TRUE;
if (*fmt)
c = *fmt++;
}
else
is_long = (c >= 'A') && (c <= 'Z');
/* Command decoding.*/
switch (c) {
case 0:
return ret;
case 'c':
filler = ' ';
*p++ = va_arg(ap, int);
break;
case 's':
filler = ' ';
if ((s = va_arg(ap, char *)) == 0)
s = "(null)";
if (precision == 0)
precision = 32767;
for (p = s; *p && (--precision >= 0); p++);
break;
case 'D':
case 'd':
if (is_long)
l = va_arg(ap, long);
else
l = va_arg(ap, int);
if (l < 0) {
*p++ = '-';
l = -l;
}
p = ltoa_wd(p, l, 10, 0);
break;
#if GFILE_ALLOW_FLOATS
case 'f':
f = (float) va_arg(ap, double);
if (f < 0) {
*p++ = '-';
f = -f;
}
l = f;
p = ltoa_wd(p, l, 10, 0);
*p++ = '.';
l = (f - l) * FLOAT_PRECISION;
p = ltoa_wd(p, l, 10, FLOAT_PRECISION / 10);
break;
#endif
case 'X':
case 'x':
c = 16;
goto unsigned_common;
case 'U':
case 'u':
c = 10;
goto unsigned_common;
case 'O':
case 'o':
c = 8;
unsigned_common:
if (is_long)
l = va_arg(ap, long);
else
l = va_arg(ap, int);
p = ltoa_wd(p, l, c, 0);
break;
default:
*p++ = c;
break;
}
i = (int)(p - s);
if ((width -= i) < 0)
width = 0;
if (left_align == FALSE)
width = -width;
if (width < 0) {
if (*s == '-' && filler == '0') {
gfileWrite(f, s++, 1);
ret++; if (--maxlen) return ret;
i--;
}
do {
gfileWrite(f, &filler, 1);
ret++; if (--maxlen) return ret;
} while (++width != 0);
}
while (--i >= 0) {
gfileWrite(f, s++, 1);
ret++; if (--maxlen) return ret;
}
while (width) {
gfileWrite(f, &filler, 1);
ret++; if (--maxlen) return ret;
width--;
}
}
return ret;
}
#if GFILE_NEED_STRINGS
int snprintg(char *buf, int maxlen, const char *fmt, ...) {
int res;
GFILE f;
va_list ap;
if (maxlen <= 1) {
if (maxlen == 1)
*buf = 0;
return 0;
}
f.flags = GFILEFLG_OPEN|GFILEFLG_WRITE;
f.vmt = &StringVMT;
f.pos = 0;
f.obj = buf;
va_start(ap, fmt);
res = vfnprintg(&f, maxlen-1, fmt, ap);
va_end(ap);
buf[res] = 0;
return res;
}
int vsnprintg(char *buf, int maxlen, const char *fmt, va_list arg) {
int res;
GFILE f;
if (maxlen <= 1) {
if (maxlen == 1)
*buf = 0;
return 0;
}
f.flags = GFILEFLG_OPEN|GFILEFLG_WRITE;
f.vmt = &StringVMT;
f.pos = 0;
f.obj = buf;
res = vfnprintg(&f, maxlen-1, fmt, arg);
buf[res] = 0;
return res;
}
#endif
#endif
/********************************************************
* scang routines
********************************************************/
#if GFILE_NEED_SCANG
int fscang(GFILE *f, const char *fmt, ...) {
int res;
va_list ap;
va_start(ap, fmt);
res = vfscang(f, fmt, ap);
va_end(ap);
return res;
}
int vfscang(GFILE *f, const char *fmt, va_list arg) {
int res, width, size, base;
char c;
bool_t assign;
void *p;
for(res = 0; *fmt; fmt++) {
switch(*fmt) {
case ' ': case '\t': case '\r': case '\n': case '\v': case '\f':
break;
case '%':
fmt++;
assign = TRUE;
width = 0;
size = 1;
if (*fmt == '*') {
fmt++;
assign = FALSE;
}
while(*fmt >= '0' && *fmt <= '9')
width = width * 10 + (*fmt++ - '0');
if (*fmt == 'h') {
fmt++;
size = 0;
} else if (*fmt == 'l') {
fmt++;
size = 2;
} else if (*fmt == 'L') {
fmt++;
size = 3;
}
switch(*fmt) {
case 0:
return res;
case '%':
goto matchchar;
case 'c':
if (!width) {
while(1) {
if (!gfileRead(f, &c, 1)) return res;
switch(c) {
case ' ': case '\t': case '\r':
case '\n': case '\v': case '\f': continue;
}
break;
}
width = 1;
} else {
if (!gfileRead(f, &c, 1)) return res;
}
if (assign) {
p = va_arg(ap, char *);
res++;
*((char *)p)++ = c;
}
while(--width) {
if (!gfileRead(f, &c, 1)) return res;
if (assign) *((char *)p)++ = c;
}
break;
case 's':
while(1) {
if (!gfileRead(f, &c, 1)) return res;
switch(c) {
case ' ': case '\t': case '\r':
case '\n': case '\v': case '\f': continue;
}
break;
}
if (assign) {
p = va_arg(ap, char *);
res++;
*((char *)p)++ = c;
}
if (width) {
while(--width) {
if (!gfileRead(f, &c, 1)) {
if (assign) *((char *)p) = 0;
return res;
}
if (assign) *((char *)p)++ = c;
}
} else {
while(1) {
if (!gfileRead(f, &c, 1)) {
if (assign) *((char *)p) = 0;
return res;
}
switch(c) {
case ' ': case '\t': case '\r':
case '\n': case '\v': case '\f': break;
default:
if (assign) *((char *)p)++ = c;
continue;
}
break;
}
//ungetch(c);
}
if (assign) *((char *)p) = 0;
break;
case 'd':
case 'i':
case 'o':
case 'u':
case 'x':
case 'b':
/*
while (isspace (*buf))
buf++;
if (*s == 'd' || *s == 'u')
base = 10;
else if (*s == 'x')
base = 16;
else if (*s == 'o')
base = 8;
else if (*s == 'b')
base = 2;
if (!width) {
if (isspace (*(s + 1)) || *(s + 1) == 0)
width = strcspn (buf, ISSPACE);
else
width = strchr (buf, *(s + 1)) - buf;
}
strncpy (tmp, buf, width);
tmp[width] = '\0';
buf += width;
if (!noassign)
atob (va_arg (ap, u_int32_t *), tmp, base);
}
if (!noassign)
count++;
*/
#if GFILE_ALLOW_FLOATS
case 'e': case 'f': case 'g':
#endif
default:
return res;
}
break;
default:
matchchar:
while(1) {
if (!gfileRead(f, &c, 1)) return res;
switch(c) {
case ' ': case '\t': case '\r':
case '\n': case '\v': case '\f': continue;
}
break;
}
if (c != *fmt) return res;
break;
}
}
return res;
}
#if GFILE_NEED_STRINGS
int sscang(const char *buf, const char *fmt, ...) {
int res;
GFILE f;
va_list ap;
f.flags = GFILEFLG_OPEN|GFILEFLG_READ;
f.vmt = &StringVMT;
f.pos = 0;
f.obj = buf;
va_start(ap, fmt);
res = vfscang(&f, fmt, ap);
va_end(ap);
return res;
}
int vsscang(const char *buf, const char *fmt, va_list arg) {
int res;
GFILE f;
f.flags = GFILEFLG_OPEN|GFILEFLG_READ;
f.vmt = &StringVMT;
f.pos = 0;
f.obj = buf;
res = vfscang(&f, fmt, arg);
return res;
}
#endif
#endif
/********************************************************
* stdio emulation routines
********************************************************/
#if GFILE_NEED_STDIO
size_t gstdioRead(void * ptr, size_t size, size_t count, FILE *f) {
return gfileRead(f, ptr, size*count)/size;
}
size_t gstdioWrite(const void * ptr, size_t size, size_t count, FILE *f) {
return gfileWrite(f, ptr, size*count)/size;
}
int gstdioSeek(FILE *f, size_t offset, int origin) {
switch(origin) {
case SEEK_SET:
break;
case SEEK_CUR:
offset += f->pos;
break;
case SEEK_END:
offset += gfileGetSize(f);
break;
default:
return -1;
}
return gfileSetPos(f, offset) ? 0 : -1;
}
int gstdioGetpos(FILE *f, long int *pos) {
if (!(f->flags & GFILEFLG_OPEN))
return -1;
*pos = f->pos;
return 0;
}
#endif
#endif /* GFX_USE_GFILE */
|