aboutsummaryrefslogtreecommitdiff
path: root/backends/PalmOS/Src/missing/_stdio.cpp
blob: 672ba4ba83e760fba9c8a723ac5343ca36d05b91 (plain)
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
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2001  Ludvig Strigeus
 * Copyright (C) 2001-2003 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header$
 *
 */

#include "stdio.h"
#include "extend.h"
///////////////////////////////////////////////////////////////////////////////
//FileRef gLogFile;

// WARNING :	printf functions must only be used if you compile your code with
// 				4byte int option, use standard functions if 2byte int mode
// TODO : enable use of 2byte or 4byte (ifdef PRINTF_4BYTE ...)


static void DrawStatus(Boolean show) {
	UInt8 x,y;
	UInt8 *screen = (UInt8 *)(BmpGetBits(WinGetBitmap(WinGetDisplayWindow())));
	UInt8 color = (show? gVars->indicator.on : gVars->indicator.off);

	if (gVars->screenLocked)
		if (screen == gVars->flipping.pageAddr1)
			screen = gVars->flipping.pageAddr2;
		else
			screen = gVars->flipping.pageAddr1;

	screen += 320 + 305;
	for(y=0;y<3;y++)
	{
		for(x=0;x<14;x++)
			screen[x] = color;
		screen += 319;
	}
}
///////////////////////////////////////////////////////////////////////////////
UInt16 fclose(FileRef *stream) {
	Err error = VFSFileClose(*stream);
	
	if (error == errNone)
		MemPtrFree(stream);

#ifdef DEBUG
	FrmCustomAlert(FrmWarnAlert,"error fclose",0,0);
#endif
	return error;
}
///////////////////////////////////////////////////////////////////////////////
UInt16 feof(FileRef *stream) {
	Err error = VFSFileEOF(*stream);

#ifdef DEBUG
		switch (error)
		{
			case vfsErrFileEOF:
				FrmCustomAlert(FrmWarnAlert,"vfsErrFileEOF",0,0);
				break;
			case expErrNotOpen:
				FrmCustomAlert(FrmWarnAlert,"expErrNotOpen",0,0);
				break;
			case vfsErrFileBadRef:
				FrmCustomAlert(FrmWarnAlert,"vfsErrFileBadRef",0,0);
				break;
			case vfsErrIsADirectory:
				FrmCustomAlert(FrmWarnAlert,"vfsErrIsADirectory",0,0);
				break;
			case vfsErrNoFileSystem:
				FrmCustomAlert(FrmWarnAlert,"vfsErrNoFileSystem",0,0);
				break;
		}
#endif

	return error;
}
///////////////////////////////////////////////////////////////////////////////
Char *fgets(Char *s, UInt32 n, FileRef *stream) {
	UInt32 numBytesRead;
	DrawStatus(true);
	Err error = VFSFileRead(*stream, n, s, &numBytesRead);
	DrawStatus(false);
	if (error == errNone || error == vfsErrFileEOF) {
		UInt32 reset = 0;
		Char *endLine = StrChr(s, '\n');
		
		if (endLine >= s) {
			reset = (endLine - s);
			s[reset] = 0;
			reset = numBytesRead - (reset + 1);
			VFSFileSeek(*stream, vfsOriginCurrent, -reset);
		}
	
		return s;
	}
#ifdef DEBUG
		switch (error)
		{
			case expErrNotOpen:
				FrmCustomAlert(FrmWarnAlert,"expErrNotOpen",0,0);
				break;
			case vfsErrFileBadRef:
				FrmCustomAlert(FrmWarnAlert,"vfsErrFileBadRef",0,0);
				break;
			case vfsErrFileEOF:
				FrmCustomAlert(FrmWarnAlert,"vfsErrFileEOF",0,0);
				break;
			case vfsErrFilePermissionDenied:
				FrmCustomAlert(FrmWarnAlert,"vfsErrFilePermissionDenied",0,0);
				break;
			case vfsErrIsADirectory:
				FrmCustomAlert(FrmWarnAlert,"vfsErrIsADirectory",0,0);
				break;
			case vfsErrNoFileSystem:
				FrmCustomAlert(FrmWarnAlert,"vfsErrNoFileSystem",0,0);
				break;
		}
#endif

	return NULL;
}
///////////////////////////////////////////////////////////////////////////////
FileRef *fopen(const Char *filename, const Char *type) {
	Err err;
	UInt16 openMode;
	FileRef *fileRefP = (FileRef *)MemPtrNew(sizeof(FileRef *));
	
	if (StrCompare(type,"r")==0)
		openMode = vfsModeRead;
	else if (StrCompare(type,"rb")==0)
		openMode = vfsModeRead;
	else if (StrCompare(type,"w")==0)
		openMode = vfsModeCreate|vfsModeWrite;
	else if (StrCompare(type,"wb")==0)
		openMode = vfsModeCreate|vfsModeWrite;
	else
		openMode = vfsModeReadWrite;

	if (openMode & vfsModeRead) {
		// if read file :
		// first try to load from the specfied card
		err = VFSFileOpen (gVars->volRefNum, filename, openMode, fileRefP);
		//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, fileRefP);
					if (!err)
						return fileRefP;
				}
			}
		} else {
			return fileRefP;
		}
	} else {
		// if write file :
		// use only the specified card
		// FIXME : vfsModeCreate|vfsModeWrite will failed on OS3.5 Cli�
		err = VFSFileDelete(gVars->volRefNum, filename);	// delete it if exists
		err = VFSFileCreate(gVars->volRefNum, filename);
		openMode = vfsModeWrite;
		if (!err) {
			err = VFSFileOpen (gVars->volRefNum, filename, openMode, fileRefP);
			if (!err)
				return fileRefP;
		}
	}

#ifdef DEBUG
	else
	{
		switch (err)
		{
			case expErrCardReadOnly:
				FrmCustomAlert(FrmWarnAlert,"expErrCardReadOnly",0,0);
				break;
			case expErrNotOpen:
				FrmCustomAlert(FrmWarnAlert,"expErrNotOpen",0,0);
				break;
			case vfsErrBadName:
				FrmCustomAlert(FrmWarnAlert,"vfsErrBadName",0,0);
				break;
			case vfsErrFileNotFound:
				FrmCustomAlert(FrmWarnAlert,"vfsErrFileNotFound",0,0);
				break;
			case vfsErrFilePermissionDenied:
				FrmCustomAlert(FrmWarnAlert,"vfsErrFilePermissionDenied",0,0);
				break;
			case vfsErrVolumeBadRef:
				FrmCustomAlert(FrmWarnAlert,"vfsErrVolumeBadRef",0,0);
				break;
			default:
				FrmCustomAlert(FrmWarnAlert,"unknow",0,0);
				break;
		}
	}
#endif

	MemPtrFree(fileRefP); // prevent memory leak
	return NULL;
}
///////////////////////////////////////////////////////////////////////////////
UInt32 fread(void *ptr, UInt32 size, UInt32 nitems, FileRef *stream) {
	UInt32 numBytesRead;
	DrawStatus(true);
	Err error = VFSFileRead(*stream, size*nitems, ptr, &numBytesRead);
	DrawStatus(false);
	if (error == errNone || error == vfsErrFileEOF)
		return (UInt32)(numBytesRead/size);

#ifdef DEBUG
		switch (error)
		{
			case expErrNotOpen:
				FrmCustomAlert(FrmWarn,"expErrNotOpen",0,0);
				break;
			case vfsErrFileBadRef:
				FrmCustomAlert(FrmWarn,"vfsErrFileBadRef",0,0);
				break;
			case vfsErrFileEOF:
				FrmCustomAlert(FrmWarn,"vfsErrFileEOF",0,0);
				break;
			case vfsErrFilePermissionDenied:
				FrmCustomAlert(FrmWarn,"vfsErrFilePermissionDenied",0,0);
				break;
			case vfsErrIsADirectory:
				FrmCustomAlert(FrmWarn,"vfsErrIsADirectory",0,0);
				break;
			case vfsErrNoFileSystem:
				FrmCustomAlert(FrmWarn,"vfsErrNoFileSystem",0,0);
				break;
		}
#endif
	return 0;
}
///////////////////////////////////////////////////////////////////////////////
UInt32 fwrite(const void *ptr, UInt32 size, UInt32 nitems, FileRef *stream) {
	UInt32 numBytesWritten;
	DrawStatus(true);
	Err error = VFSFileWrite(*stream, size*nitems, ptr, &numBytesWritten);
	DrawStatus(false);

	if (error == errNone || error == vfsErrFileEOF)
		return (UInt32)(numBytesWritten/size);

	return NULL;
}
///////////////////////////////////////////////////////////////////////////////
Int32 fseek(FileRef *stream, Int32 offset, Int32 whence) {
	Err error = VFSFileSeek(*stream, whence, offset);
	return error;
}
///////////////////////////////////////////////////////////////////////////////
UInt32 ftell(FileRef *stream) {
	Err e;
	UInt32 filePos;

	e = VFSFileTell(*stream,&filePos);
	if (e != errNone)
		return e;
	
	return filePos;
}
///////////////////////////////////////////////////////////////////////////////
UInt16 fprintf(FileRef *stream, const Char *format, ...) {
	if (!*stream)
		return 0;

	UInt32 numBytesWritten;
	Char buf[256];
	va_list va;

	va_start(va, format);
	vsprintf(buf, format, va);
	va_end(va);

	VFSFileWrite (*stream, StrLen(buf), buf, &numBytesWritten);
	return numBytesWritten;
}
///////////////////////////////////////////////////////////////////////////////
Int16 printf(const Char *format, ...) {
	if (!*stdout)
		return 0;

	UInt32 numBytesWritten;
	Char buf[256];
	va_list va;

	va_start(va, format);
	vsprintf(buf, format, va);
	va_end(va);

	VFSFileWrite (*stdout, StrLen(buf), buf, &numBytesWritten);
	return numBytesWritten;
}
///////////////////////////////////////////////////////////////////////////////
Int16 sprintf(Char* s, const Char* formatStr, ...) {
	Char buf[256];
	Int16 count;
	va_list va;

	va_start(va, formatStr);
	count = vsprintf(buf, formatStr, va);
	va_end(va);
	
	StrCopy(s,buf);
	return count;
}
///////////////////////////////////////////////////////////////////////////////
static void StrProcessCModifier(Char *ioStr, UInt16 maxLen) {
	Char *found;
	UInt16 length;
	
	while (found = StrChr(ioStr, '`')) {
		if (found[1] == 0) { // if next char is NULL
			length = maxLen - (found - ioStr);
			MemMove(found, found + 2, length);
			maxLen -= 2;
		}
	}
}

Int16 vsprintf(Char* s, const Char* formatStr, _Palm_va_list argParam) {
	Char format[256];
	Int16 retval;
	// TODO : need a better modifier
	StrCopy(format,formatStr);
	StrReplace(format, 256, "%ld",	"%d");
	StrReplace(format, 256, "%li",	"%i");
	StrReplace(format, 256, "%lx",	"%x");
	StrReplace(format, 256, "%lx",	"%X");
	StrReplace(format, 256, "%2ld",	"%2d");
	StrReplace(format, 256, "%03ld","%.3d");
	StrReplace(format, 256, "%02ld","%.2d");
	StrReplace(format, 256, "%01ld","%.1d");
	StrReplace(format, 256, "%02ld","%02d");

	StrReplace(format, 256, "%2ld","%2d");
	StrReplace(format, 256, "%3ld","%3d");
	StrReplace(format, 256, "%4ld","%4d");
	StrReplace(format, 256, "%5ld","%5d");
	StrReplace(format, 256, "%6ld","%6d");
	StrReplace(format, 256, "%02lx","%02x");
	StrReplace(format, 256, "`%c%c","%c");

	retval = StrVPrintF(s, format, argParam);	// wrong value may be return due to %c%c
	StrProcessCModifier(s, 256);
	
	return retval;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////