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
|
/* ScummVM - Scumm Interpreter
* Copyright (C) 2001 Ludvig Strigeus
* Copyright (C) 2001-2006 The ScummVM project
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* $URL$
* $Id$
*
*/
#include <stdio.h>
#include <stdlib.h>
#define CACHE_SIZE 1024
enum {
MODE_BUFREAD = 1,
MODE_BUFWRITE,
MODE_BUFNONE
};
FILE gStdioOutput = {0,0,0,0,0,0};
static void dummy(Boolean) {};
static LedProc gStdioLedProc = dummy;
static UInt16 gStdioVolRefNum = sysInvalidRefNum;
static UInt32 gCacheSize = CACHE_SIZE;
// TODO : implement "errno"
void StdioInit(UInt16 volRefNum, const Char *output) { // DONE
gStdioVolRefNum = volRefNum;
gStdioOutput.mode = MODE_BUFWRITE;
VFSFileDelete(gStdioVolRefNum, output);
VFSFileCreate(gStdioVolRefNum, output);
VFSFileOpen (gStdioVolRefNum, output,vfsModeWrite, &gStdioOutput.fileRef);
}
void StdioSetLedProc(LedProc ledProc) { // DONE
if (ledProc)
gStdioLedProc = ledProc;
else
gStdioLedProc = dummy;
}
void StdioSetCacheSize(UInt32 s) { // DONE
gCacheSize = s;
}
void StdioRelease() { // DONE
// there is no cache on stdout/stderr
VFSFileClose(gStdioOutput.fileRef);
}
UInt16 fclose(FILE *stream) { // DONE
UInt32 numBytesWritten;
Err e;
if (stream->cacheSize) {
if (stream->bufSize > 0 && stream->mode == MODE_BUFWRITE)
VFSFileWrite(stream->fileRef, stream->bufSize, stream->cache, &numBytesWritten);
MemPtrFree(stream->cache);
}
e = VFSFileClose(stream->fileRef);
e = MemPtrFree(stream);
return e;
}
UInt16 feof(FILE *stream) { // DONE
Err e;
if (stream->cacheSize) {
switch (stream->mode) {
case MODE_BUFWRITE:
return 0; // never set in this mode
case MODE_BUFREAD:
if (stream->bufSize > 0)
return 0;
break;
}
}
e = VFSFileEOF(stream->fileRef);
return e;
}
UInt16 ferror(FILE *stream) {
return (stream->err);
}
Int16 fgetc(FILE *stream) {
UInt32 numBytesRead;
Char c;
numBytesRead = fread(&c, 1, 1, stream);
return (int)(numBytesRead == 1 ? c : EOF);
}
Char *fgets(Char *s, UInt32 n, FILE *stream) {
UInt32 numBytesRead;
numBytesRead = fread(s, n, 1, stream);
if (numBytesRead) {
UInt32 reset = 0;
Char *endLine = StrChr(s, '\n');
if (endLine >= s) {
reset = (endLine - s);
s[reset] = 0;
reset = numBytesRead - (reset + 1);
fseek(stream, -reset, SEEK_CUR);
}
return s;
}
return NULL;
}
FILE *fopen(const Char *filename, const Char *type) { // DONE
Err err;
UInt16 openMode;
Boolean cache = true;
FILE *fileP = (FILE *)MemPtrNew(sizeof(FILE));
if (!fileP)
return NULL;
MemSet(fileP, sizeof(FILE), 0);
if (StrCompare(type,"r")==0 || StrCompare(type,"rb")==0) {
fileP->mode = MODE_BUFREAD;
openMode = vfsModeRead;
} else if (StrCompare(type,"w")==0 || StrCompare(type,"wb")==0) {
fileP->mode = MODE_BUFWRITE;
openMode = vfsModeCreate|vfsModeWrite;
} else {
cache = false;
fileP->mode = MODE_BUFNONE;
openMode = vfsModeReadWrite;
}
if (cache) {
fileP->cacheSize = gCacheSize;
if (gCacheSize) fileP->cache = (UInt8 *)MemGluePtrNew(gCacheSize);
if (!fileP->cache) fileP->cacheSize = 0;
}
if (openMode & vfsModeRead) {
// if read file :
// first try to load from the specfied card
err = VFSFileOpen (gStdioVolRefNum, filename, openMode, &fileP->fileRef);
//if err (not found ?) parse each avalaible card for the specified file
if (err) {
UInt16 volRefNum;
UInt32 volIterator = vfsIteratorStart;
while (volIterator != vfsIteratorStop) {
err = VFSVolumeEnumerate(&volRefNum, &volIterator);
if (!err) {
err = VFSFileOpen (volRefNum, filename, openMode, &fileP->fileRef);
if (!err)
return fileP;
}
}
} else {
return fileP;
}
} else {
// if write file :
// use only the specified card
err = VFSFileDelete(gStdioVolRefNum, filename); // delete it if exists
err = VFSFileCreate(gStdioVolRefNum, filename);
openMode = vfsModeWrite;
if (!err) {
err = VFSFileOpen (gStdioVolRefNum, filename, openMode, &fileP->fileRef);
if (!err)
return fileP;
}
}
if (fileP->cacheSize)
MemPtrFree(fileP->cache);
MemPtrFree(fileP); // prevent memory leak
return NULL;
}
UInt32 fread(void *ptr, UInt32 size, UInt32 nitems, FILE *stream) { // DONE
Err e = errNone;
UInt32 numBytesRead, rsize = (size * nitems);
// try to read on a write only stream ?
if (stream->mode == MODE_BUFWRITE || !rsize)
return 0;
// cached ?
if (stream->cacheSize) {
// empty buffer ? fill it if required
if (stream->bufSize == 0 && rsize < stream->cacheSize) {
gStdioLedProc(true);
e = VFSFileRead(stream->fileRef, stream->cacheSize, stream->cache, &numBytesRead);
gStdioLedProc(false);
stream->bufSize = numBytesRead;
stream->bufPos = 0;
}
// we have the data in the cache
if (stream->bufSize >= rsize) {
MemMove(ptr, (stream->cache + stream->bufPos), rsize);
stream->bufPos += rsize;
stream->bufSize -= rsize;
numBytesRead = rsize;
// not enough but something ?
} else if (stream->bufSize > 0) {
UInt8 *next = (UInt8 *)ptr;
MemMove(ptr, (stream->cache + stream->bufPos), stream->bufSize);
rsize -= stream->bufSize;
gStdioLedProc(true);
e = VFSFileRead(stream->fileRef, rsize, (next + stream->bufSize), &numBytesRead);
gStdioLedProc(false);
numBytesRead += stream->bufSize;
stream->bufSize = 0;
stream->bufPos = 0;
// nothing in the cache ?
} else {
gStdioLedProc(true);
e = VFSFileRead(stream->fileRef, rsize, ptr, &numBytesRead);
gStdioLedProc(false);
}
// no ? direct read
} else {
gStdioLedProc(true);
e = VFSFileRead(stream->fileRef, rsize, ptr, &numBytesRead);
gStdioLedProc(false);
}
if (e == errNone || e == vfsErrFileEOF)
return (UInt32)(numBytesRead / size);
return 0;
}
UInt32 fwrite(const void *ptr, UInt32 size, UInt32 nitems, FILE *stream) { // DONE
Err e = errNone;
UInt32 numBytesWritten = (size * nitems);
// try to write on a read only stream ?
if (stream->mode == MODE_BUFREAD || !numBytesWritten)
return 0;
// cached ?
if (stream->cacheSize) {
// can cache it ?
if ((stream->bufSize + numBytesWritten) <= stream->cacheSize) {
MemMove((stream->cache + stream->bufSize), ptr, numBytesWritten);
stream->bufSize += numBytesWritten;
// not enough room ? write cached data and new data
} else {
gStdioLedProc(true);
e = VFSFileWrite(stream->fileRef, stream->bufSize, stream->cache, &numBytesWritten);
e = VFSFileWrite(stream->fileRef, (size * nitems), ptr, &numBytesWritten);
gStdioLedProc(false);
stream->bufSize = 0;
}
// no ? direct write
} else {
gStdioLedProc(true);
e = VFSFileWrite(stream->fileRef, (size * nitems), ptr, &numBytesWritten);
gStdioLedProc(false);
}
if ((e == errNone || e == vfsErrFileEOF)) {
return (UInt32)(numBytesWritten / size);
}
return 0;
}
Int16 fseek(FILE *stream, Int32 offset, Int32 whence) { // DONE
UInt32 numBytesWritten;
Err e;
if (stream->cacheSize) {
switch (stream->mode) {
case MODE_BUFWRITE:
e = VFSFileWrite(stream->fileRef, stream->bufSize, stream->cache, &numBytesWritten);
stream->bufSize = 0;
break;
case MODE_BUFREAD:
// reposition file postion if needed
if (whence == SEEK_CUR)
e = VFSFileSeek(stream->fileRef, vfsOriginCurrent, -stream->bufSize);
stream->bufSize = 0;
stream->bufPos = 0;
break;
}
}
e = VFSFileSeek(stream->fileRef, whence, offset);
return (e ? -1 : 0);
}
Int32 ftell(FILE *stream) { // DONE
Err e;
UInt32 filePos;
e = VFSFileTell(stream->fileRef ,&filePos);
if (stream->cacheSize) {
switch (stream->mode) {
case MODE_BUFWRITE:
filePos += stream->bufSize;
break;
case MODE_BUFREAD:
filePos -= stream->bufSize;
break;
}
}
if (e) return -1; // errno = ?
return filePos;
}
Int32 fprintf(FILE *stream, const Char *formatStr, ...) { // DONE
if (!stream->fileRef)
return 0;
UInt32 numBytesWritten;
Char buf[256];
va_list va;
va_start(va, formatStr);
vsprintf(buf, formatStr, va);
va_end(va);
numBytesWritten = fwrite(buf, StrLen(buf), 1, stream);
return numBytesWritten;
}
Int32 printf(const Char *format, ...) { // DONE
if (!stdout->fileRef)
return 0;
UInt32 numBytesWritten;
Char buf[256];
va_list va;
va_start(va, format);
vsprintf(buf, format, va);
va_end(va);
numBytesWritten = fwrite(buf, StrLen(buf), 1, stdout);
return numBytesWritten;
}
Int32 sprintf(Char* s, const Char* formatStr, ...) {
Int16 count;
va_list va;
va_start(va, formatStr);
count = vsprintf(s, formatStr, va);
va_end(va);
return count;
}
Int32 snprintf(Char* s, UInt32 len, const Char* formatStr, ...) {
// len is ignored
Int16 count;
va_list va;
va_start(va, formatStr);
count = vsprintf(s, formatStr, va);
va_end(va);
return count;
}
/* WARNING : vsprintf
* -------
* This function can handle only %[+- ][.0][field length][sxXdoiucp] format strings
* compiler option : 4byte int mode only !
*
* TODO : check http://www.ijs.si/software/snprintf/ for a portable implementation of vsnprintf
* This one make use of sprintf so need to check if it works with PalmOS.
*/
static Char *StrIToBase(Char *s, Int32 i, UInt8 b) {
const Char *conv = "0123456789ABCDEF";
Char o;
Int16 c, n = 0;
Int32 div, mod;
do {
div = i / b;
mod = i % b;
s[n++] = *(conv + mod);
i = div;
} while (i >= b);
if (i > 0) {
s[n + 0] = *(conv + i);
s[n + 1] = 0;
} else {
s[n + 0] = 0;
n--;
}
for (c=0; c <= (n >> 1); c++) {
o = s[c];
s[c] = s[n - c];
s[n - c]= o;
}
return s;
}
static void StrProcC_(Char *ioStr, UInt16 maxLen) {
Char *found;
Int16 length;
while (found = StrStr(ioStr, "`c`")) {
if (found[3] == 0) { // if next char is NULL
length = maxLen - (found - ioStr + 2);
MemMove(found, found + 4, length);
maxLen -= 2;
}
}
}
static void StrProcXO(Char *ioStr, UInt16 maxLen, Char *tmp) {
Char *found, *last, mod, fill;
Int16 len, count, next;
Int32 val;
while (found = StrChr(ioStr, '`')) {
last = StrChr(found + 1, '`');
if (!last)
return;
*last = 0;
next = 0;
fill = *(found + 1);
mod = *(found + 2);
count = StrAToI(found + 3);
len = maxLen - (last - ioStr);
MemMove(found, (last + 1), len);
// x and X always 8char on palmos ... o set to 8char (not supported on palmos)
while ((found[next] == '0' || found[next] == ' ') && next < 8) // WARNING : reduce size only (TODO ?)
next++;
// convert to base 8
if (mod == 'o') {
StrNCopy(tmp, found + next, 8 - next);
tmp[8 - next] = 0;
val = StrAToI(tmp);
StrIToBase(tmp, val, 8); // now we have the same but in octal
next = 8 - StrLen(tmp);
MemMove(found + next, tmp, StrLen(tmp));
} else {
// if val is 0, keep last 0
if (next == 8)
next = 7;
}
if ((8 - next) > count)
count = 8 - next;
if (count == 0)
count = 1;
len = maxLen - (found - ioStr) - (8 - count);
MemSet(found, next, fill);
MemMove(found, found + (8 - count), len);
// ... and upper case
if (mod == 'x') {
while (count--) {
if (*found >='A' && *found <='F')
*found = (*found + 32);
found++;
}
}
}
}
Int32 vsprintf(Char* s, const Char* formatStr, _Palm_va_list argParam) {
Char format[256], result[256], tmp[32];
Char *found, *mod, *num;
UInt32 next;
Boolean zero;
Int16 count, len;
MemSet(format, sizeof(format), 'x');
MemSet(result, sizeof(result), 'y');
MemSet(tmp, sizeof(tmp), 'z');
StrCopy(format,formatStr); // copy actual formatStr to temp buffer
next = 0; // start of the string
while (found = StrChr(format + next, '%')) {
mod = found + 1;
if (*mod == '%') { // just a % ?
mod++;
} else {
if (*mod == '+' ||
*mod == '-' ||
*mod == ' ' ) // skip
mod++;
if (*mod == '0' ||
*mod == '.' ) {
*mod++ = '0';
zero = true;
} else {
zero = false;
}
num = mod;
while ( *mod >= '0' &&
*mod <= '9' ) // search format char
mod++;
// get the numeric value
if (num < mod) {
StrNCopy(tmp, num, mod - num);
tmp[mod - num] = 0;
count = StrAToI(tmp);
} else {
count = 0;
}
if (*mod == 'l') // already set to %...l(x) ?
mod++;
// prepare new format
#if !defined(COMPILE_ZODIAC) || defined(PALMOS_68K)
if (*mod == 'c') {
StrCopy(tmp, "`c`%c%c");
} else
#endif
if (*mod == 'p') {
StrCopy(tmp, "%08lX"); // %x = %08X in palmos
} else {
len = 0;
switch (*mod) {
case 'x':
case 'X':
case 'o':
tmp[0] = '`';
tmp[1] = (zero) ? '0' : ' ';
tmp[2] = *mod;
StrIToA(tmp + 3, count);
len += StrLen(tmp);
tmp[len++] = '`';
tmp[len] = 0;
if (*mod == 'o') { // set as base 10 num and convert later
*mod = 'd';
count = 8; // force 8char
}
break;
}
StrNCopy(tmp + len, found, (num - found));
len += (num - found);
if (count) {
StrIToA(tmp + len, count);
len += StrLen(tmp + len);
}
if (*mod == 'd' ||
*mod == 'i' ||
*mod == 'x' ||
*mod == 'X' ||
*mod == 'u'
) {
tmp[len++] = 'l';
}
tmp[len + 0] = *mod;
tmp[len + 1] = 0;
}
mod++;
MemMove(found + StrLen(tmp), mod, StrLen(mod) + 1);
StrNCopy(found, tmp, StrLen(tmp));
mod = found + StrLen(tmp);
}
next = (mod - format);
}
// Copy result in a temp buffer to process last formats
StrVPrintF(result, format, argParam);
#if !defined(COMPILE_ZODIAC) || defined(PALMOS_68K)
StrProcC_(result, 256);
#endif
StrProcXO(result, 256, tmp);
StrCopy(s, result);
return StrLen(s);
}
|