[Scummvm-cvs-logs] CVS: scummvm/backends/PalmOS/Src/missing _stdio.cpp,1.8,1.8.2.1 stdio.h,1.4,1.4.2.1

Chris Apers chrilith at users.sourceforge.net
Fri Mar 19 04:56:04 CET 2004


Update of /cvsroot/scummvm/scummvm/backends/PalmOS/Src/missing
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18706/missing

Modified Files:
      Tag: branch-0-6-0
	_stdio.cpp stdio.h 
Log Message:
Revamped [...]printf functions, may fix a bug with insane engine since %o was not supported

Index: _stdio.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/backends/PalmOS/Src/missing/_stdio.cpp,v
retrieving revision 1.8
retrieving revision 1.8.2.1
diff -u -d -r1.8 -r1.8.2.1
--- _stdio.cpp	20 Jan 2004 14:32:01 -0000	1.8
+++ _stdio.cpp	19 Mar 2004 12:45:23 -0000	1.8.2.1
@@ -43,13 +43,6 @@
 	}
 }
 
-///////////////////////////////////////////////////////////////////////////////
-//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 ...)
-///////////////////////////////////////////////////////////////////////////////
 UInt16 fclose(FileRef *stream) {
 	Err error = VFSFileClose(*stream);
 	
@@ -61,7 +54,7 @@
 #endif
 	return error;
 }
-///////////////////////////////////////////////////////////////////////////////
+
 UInt16 feof(FileRef *stream) {
 	Err error = VFSFileEOF(*stream);
 
@@ -88,7 +81,7 @@
 
 	return error;
 }
-///////////////////////////////////////////////////////////////////////////////
+
 Char *fgets(Char *s, UInt32 n, FileRef *stream) {
 	UInt32 numBytesRead;
 	DrawStatus(true);
@@ -133,7 +126,7 @@
 
 	return NULL;
 }
-///////////////////////////////////////////////////////////////////////////////
+
 FileRef *fopen(const Char *filename, const Char *type) {
 	Err err;
 	UInt16 openMode;
@@ -216,7 +209,7 @@
 	MemPtrFree(fileRefP); // prevent memory leak
 	return NULL;
 }
-///////////////////////////////////////////////////////////////////////////////
+
 UInt32 fread(void *ptr, UInt32 size, UInt32 nitems, FileRef *stream) {
 	UInt32 numBytesRead;
 	DrawStatus(true);
@@ -250,7 +243,7 @@
 #endif
 	return 0;
 }
-///////////////////////////////////////////////////////////////////////////////
+
 UInt32 fwrite(const void *ptr, UInt32 size, UInt32 nitems, FileRef *stream) {
 	UInt32 numBytesWritten;
 	DrawStatus(true);
@@ -262,12 +255,12 @@
 
 	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;
@@ -278,8 +271,8 @@
 	
 	return filePos;
 }
-///////////////////////////////////////////////////////////////////////////////
-UInt16 fprintf(FileRef *stream, const Char *format, ...) {
+
+Int32 fprintf(FileRef *stream, const Char *formatStr, ...) {
 	if (!*stream)
 		return 0;
 
@@ -287,15 +280,15 @@
 	Char buf[256];
 	va_list va;
 
-	va_start(va, format);
-	vsprintf(buf, format, va);
+	va_start(va, formatStr);
+	vsprintf(buf, formatStr, va);
 	va_end(va);
 
 	VFSFileWrite (*stream, StrLen(buf), buf, &numBytesWritten);
 	return numBytesWritten;
 }
-///////////////////////////////////////////////////////////////////////////////
-Int16 printf(const Char *format, ...) {
+
+Int32 printf(const Char *format, ...) {
 	if (!*stdout)
 		return 0;
 
@@ -310,63 +303,220 @@
 	VFSFileWrite (*stdout, StrLen(buf), buf, &numBytesWritten);
 	return numBytesWritten;
 }
-///////////////////////////////////////////////////////////////////////////////
-Int16 sprintf(Char* s, const Char* formatStr, ...) {
-	Char buf[256];
+
+Int32 sprintf(Char* s, const Char* formatStr, ...) {
 	Int16 count;
 	va_list va;
 
 	va_start(va, formatStr);
-	count = vsprintf(buf, formatStr, va);
+	count = vsprintf(s, formatStr, va);
 	va_end(va);
 	
-	StrCopy(s,buf);
 	return count;
 }
-///////////////////////////////////////////////////////////////////////////////
-static void StrProcessCModifier(Char *ioStr, UInt16 maxLen) {
+
+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][sxXdoiuc] format strings
+ * compiler option : 4byte int mode only !
+ *
+ * TODO : check http://www.ijs.si/software/snprintf/ for a potable implementation of vsnprintf
+ * This one make use of sprintf so need to check if it works with PalmOS.
+ */
+
+static void StrProcC_(Char *ioStr, UInt16 maxLen) {
 	Char *found;
-	UInt16 length;
+	Int16 length;
 	
-	while (found = StrChr(ioStr, '`')) {
-		if (found[1] == 0) { // if next char is NULL
-			length = maxLen - (found - ioStr);
-			MemMove(found, found + 2, 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;
 		}
 	}
 }
 
-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, "%02lx","%02x");
+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;
 
-	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");
+		*last	= 0;
+		next 	= 0;
+		fill	= *(found + 1);
+		mod		= *(found + 2);
+		count	= StrAToI(found + 3);
 
-	retval = StrVPrintF(s, format, argParam);	// wrong value may be return due to %c%c
-	StrProcessCModifier(s, 256);
+		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] == ' ')	// 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));
+		}
+
+		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], tmp[32];
 	
-	return retval;
+	Char *found, *mod, *num;
+	UInt32 next;
+	Boolean zero;
+	Int16 count, len;
+	
+	MemSet(format, sizeof(format), 'x');
+	MemSet(tmp, sizeof(tmp), 'y');
+
+	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 (*mod == 'c') {
+				StrCopy(tmp, "`c`%c%c");
+
+			} 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);
+	}
+	
+	StrVPrintF(s, format, argParam);
+
+	StrProcC_(s, 256);
+	StrProcXO(s, 256, tmp);
+	
+	return StrLen(s);
 }
-///////////////////////////////////////////////////////////////////////////////
-///////////////////////////////////////////////////////////////////////////////
-///////////////////////////////////////////////////////////////////////////////
-///////////////////////////////////////////////////////////////////////////////

Index: stdio.h
===================================================================
RCS file: /cvsroot/scummvm/scummvm/backends/PalmOS/Src/missing/stdio.h,v
retrieving revision 1.4
retrieving revision 1.4.2.1
diff -u -d -r1.4 -r1.4.2.1
--- stdio.h	6 Jan 2004 12:45:25 -0000	1.4
+++ stdio.h	19 Mar 2004 12:45:23 -0000	1.4.2.1
@@ -29,6 +29,7 @@
 //extern FileRef	gLogFile;
 
 typedef FileRef FILE;
+typedef UInt32 size_t;
 
 #define stdin		0
 #define stdout		(&gVars->logFile)
@@ -37,21 +38,22 @@
 #define clearerr(a)
 #define fflush(a)
 #define vsnprintf(a,b,c,d)	vsprintf(a,c,d)
-#define snprintf(a,b,c,d)	sprintf(a,c,d)
 
 #define	SEEK_SET			vfsOriginBeginning 
 #define	SEEK_CUR			vfsOriginCurrent  
 #define	SEEK_END			vfsOriginEnd
 
-UInt16 fclose(FileRef *stream);
-UInt16 feof(FileRef *stream);
-Char *fgets(Char *s, UInt32 n, FileRef *stream);
-FileRef *fopen(const Char *filename, const Char *type);
-UInt32 fread(void *ptr, UInt32 size, UInt32 nitems, FileRef *stream);
-UInt32 fwrite(const void *ptr, UInt32 size, UInt32 nitems, FileRef *stream);
-Int32 fseek(FileRef *stream, Int32 offset, Int32 whence);
-UInt32 ftell(FileRef *stream);
-UInt16 fprintf(FileRef *stream, const Char *format, ...);
-Int16 printf(const Char* formatStr, ...);
-Int16 sprintf(Char* s, const Char* formatStr, ...);
-Int16 vsprintf(Char* s, const Char* formatStr, _Palm_va_list argParam);
+UInt16		fclose	(FileRef *stream);
+UInt16		feof	(FileRef *stream);
+Char *		fgets	(Char *s, UInt32 n, FileRef *stream);
+FileRef *	fopen	(const Char *filename, const Char *type);
+UInt32		fread	(void *ptr, UInt32 size, UInt32 nitems, FileRef *stream);
+UInt32		fwrite	(const void *ptr, UInt32 size, UInt32 nitems, FileRef *stream);
+Int32		fseek	(FileRef *stream, Int32 offset, Int32 whence);
+UInt32		ftell	(FileRef *stream);
+
+Int32	fprintf	(FileRef *stream, const Char *formatStr, ...);
+Int32	printf	(const Char* formatStr, ...);
+Int32	sprintf	(Char* s, const Char* formatStr, ...);
+Int32	snprintf(Char* s, UInt32 len, const Char* formatStr, ...);
+Int32	vsprintf(Char* s, const Char* formatStr, _Palm_va_list argParam);





More information about the Scummvm-git-logs mailing list