git » fp-git.git » commit 197648a

added flag parsing feature, but not binded

author ecalot
2006-06-26 04:34:53 UTC
committer ecalot
2006-06-26 04:34:53 UTC
parent 2f1dbbbc0676d929d850841892b5ceb20f621227

added flag parsing feature, but not binded

PR/src/Makefile +5 -1
PR/src/include/stringflag.h +41 -0
PR/src/include/stringformat.h +6 -0
PR/src/lib/layers/stringflag.c +26 -6
PR/src/lib/layers/stringformat.c +36 -32

diff --git a/PR/src/Makefile b/PR/src/Makefile
index ea04727..31b7ec0 100644
--- a/PR/src/Makefile
+++ b/PR/src/Makefile
@@ -55,7 +55,7 @@ endif
 ACTIONS  = .import.o .export.o .classify.o
 XML      = .parse.o .search.o .unknown.o .translate.o .tree.o
 COMPRESS = .lzg_compress.o .lzg_decompress.o .rle_compress.o .rle_decompress.o .rlev_decompress.o 
-LAYERS   = .dat.o .memory.o .list.o .reslist.o .pallist.o .disk.o .idlist.o .autodetect.o .stringformat.o .resourcematch.o .auxiliary.o
+LAYERS   = .dat.o .memory.o .list.o .reslist.o .pallist.o .disk.o .idlist.o .autodetect.o .stringflag.o .stringformat.o .resourcematch.o .auxiliary.o
 FORMAT   = .bmp.o .mid.o .pal.o .wav.o .plv.o
 OBJECT   = .object.o .pop2_256c.o .pop1_16c.o .image2.o .image16.o .image256.o .binary.o .sound_common.o .level_common.o .image_common.o
 CONSOLE  = .main.o .filedir.o
@@ -243,6 +243,10 @@ $(XMLFILE): xml/resources.xml
 	$(INFO) Compiling unknown string format parsing feature...
 	$(CC) $(OPTIONS) -c lib/layers/stringformat.c -o $@
 
+.stringflag.o: lib/layers/stringflag.c include/translate.h include/stringflag.h
+	$(INFO) Compiling unknown string flag parsing feature...
+	$(CC) $(OPTIONS) -c lib/layers/stringflag.c -o $@
+
 .pop2_256c.o: lib/object/palette/pop2_256c.c include/palette.h include/memory.h include/dat.h include/disk.h include/auxiliary.h include/pal.h
 	$(INFO) Compiling the POP2 256 colors palette class support...
 	$(CC) $(OPTIONS) -c  lib/object/palette/pop2_256c.c -o $@
diff --git a/PR/src/include/stringflag.h b/PR/src/include/stringflag.h
new file mode 100644
index 0000000..af16038
--- /dev/null
+++ b/PR/src/include/stringflag.h
@@ -0,0 +1,41 @@
+/*  Princed V3 - Prince of Persia Level Editor for PC Version
+    Copyright (C) 2003 Princed Development Team
+
+    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
+
+    The authors of this program may be contacted at http://forum.princed.com.ar
+*/
+
+/*
+stringflag.h: Princed Resources : Flag string parsing header
+¯¯¯¯¯¯¯¯¯¯¯¯
+ Copyright 2005 Princed Development Team
+  Created: 26 Jun 2006
+
+  Author: Enrique Calot <ecalot.cod@princed.com.ar>
+  Version: 1.00 (2006-Jun-26)
+
+ Note:
+  DO NOT remove this copyright notice
+*/
+
+#ifndef _PR_STRINGFLAG_H_
+#define _PR_STRINGFLAG_H_
+
+unsigned long parseflag(const char* stringflag);
+const char* generateflag(unsigned long integerflag);
+
+#endif
+
diff --git a/PR/src/include/stringformat.h b/PR/src/include/stringformat.h
index 5e30bad..416c310 100644
--- a/PR/src/include/stringformat.h
+++ b/PR/src/include/stringformat.h
@@ -36,6 +36,12 @@ stringformat.h: Princed Resources : Format string parsing header
 
 #define isNumber(k) ('0'<=(k) && (k)<='9')
 
+/* private functions. only for stringf* files */
+int  sf_emit(char c, int* i, char* buffer);
+void sf_emitString(const char* txt, int size, int zeroflag, int* i, char* buffer);
+void sf_emitNumber(int n, int size, int zeroflag, int* i, char* buffer);
+
+/* public function */
 const char* parseformat(const char* format,long value,const char* index, const char* type, const char* extension, long numberOfThisType, int order, const char* desc,const char* name);
 
 	/* This function parses format in this way:
diff --git a/PR/src/lib/layers/stringflag.c b/PR/src/lib/layers/stringflag.c
index 6d53580..ecd0b69 100644
--- a/PR/src/lib/layers/stringflag.c
+++ b/PR/src/lib/layers/stringflag.c
@@ -31,10 +31,9 @@ stringflag.c: Princed Resources : Flag string parsing feature routines
   DO NOT remove this copyright notice
 */
 
-/*#include "stringflag.h"*/
-
-unsigned long parseflag(const char* stringflag);
-const char* generateflag(unsigned long integerflag);
+#include "stringflag.h"
+#include "stringformat.h" /* sf_* functions */
+#include <stdlib.h> /* NULL */
 
 unsigned long parseflag(const char* stringflag) {
 
@@ -58,12 +57,33 @@ unsigned long parseflag(const char* stringflag) {
 	return result;
 }
 
+const char* generateflag(unsigned long integerflag) {
+	int i=0;
+	static char buffer[87];
+	int number=1;
+	int emitComma=0;
+
+	while (integerflag) {
+		if (integerflag&1) {
+			if (emitComma) sf_emit(',',&i,buffer);
+			sf_emitNumber(number,1,0,&i,buffer);
+			emitComma=1;
+		}
+						
+		number++;
+		integerflag>>=1;				
+	}
+
+	return sf_emit(0,&i,buffer)?buffer:NULL; /* close the string and return it (return NULL in case of error)*/
+}
+
 /*#define DEBUG_STRINGS*/
 
 #ifdef DEBUG_STRINGS
+#include <stdio.h>
 int main(int a,char** b) {
-	printf("%x\n", parseflag(
-		b[1]
+	printf("%s\n", generateflag(
+		0xffffffff
 	));
 	return 0;
 }
diff --git a/PR/src/lib/layers/stringformat.c b/PR/src/lib/layers/stringformat.c
index c9d8942..151257d 100644
--- a/PR/src/lib/layers/stringformat.c
+++ b/PR/src/lib/layers/stringformat.c
@@ -36,35 +36,32 @@ stringformat.c: Princed Resources : Format string parsing feature routines
 #include "stringformat.h"
 #include <string.h> /* strlen */
 
-static int i;
-static char buffer[200];
-
-int sf_emit(char c) {
-	if (i==200) return 0; /* false */
-	buffer[i++]=c;
+int sf_emit(char c, int* i, char* buffer) {
+	if (*i==200) return 0; /* false */
+	buffer[(*i)++]=c;
 	return 1; /* true */
 }
 
-void sf_emitString(const char* txt, int size, int zeroflag) {
+void sf_emitString(const char* txt, int size, int zeroflag, int* i, char* buffer) {
 	int complete;
 	int length;
 
 	length=strlen(txt);
 	complete=(length>size)?0:size-length;
 
-	while (complete--) sf_emit(zeroflag?'0':' ');
+	while (complete--) sf_emit(zeroflag?'0':' ',i,buffer);
 
-	for (complete=0;complete<length;complete++) sf_emit(txt[complete]);
+	for (complete=0;complete<length;complete++) sf_emit(txt[complete],i,buffer);
 }
 
-void sf_recursiveEmit(int k) {
+void sf_recursiveEmit(int k, int* i, char* buffer) {
 	if (k) {
-		sf_recursiveEmit(k/10);
-		sf_emit('0'+(k%10));
+		sf_recursiveEmit(k/10,i,buffer);
+		sf_emit('0'+(k%10),i,buffer);
 	}
 }
 
-void sf_emitNumber(int n, int size, int zeroflag) {
+void sf_emitNumber(int n, int size, int zeroflag, int* i, char* buffer) {
 	int complete;
 	int digits=0;
 	int k=n;
@@ -76,14 +73,21 @@ void sf_emitNumber(int n, int size, int zeroflag) {
 
 	complete=(digits>size)?0:size-digits;
 
-	while (complete--) sf_emit(zeroflag?'0':' ');
+	while (complete--) sf_emit(zeroflag?'0':' ',i,buffer);
 
-	sf_recursiveEmit(n);
+	sf_recursiveEmit(n,i,buffer);
 }
 
-#define sf_read_k(k) (k=*(format++))
+
+#define emit(a) sf_emit(a,&i,buffer)
+#define emitString(a,b,c) sf_emitString(a,b,c,&i,buffer)
+#define emitNumber(a,b,c) sf_emitNumber(a,b,c,&i,buffer)
+#define read_k(k) (k=*(format++))
 
 const char* parseformat(const char* format,long value,const char* index, const char* type, const char* extension, long numberOfThisType, int order, const char* desc,const char* name) {
+	int i;
+	static char buffer[200];
+
 	/* This function parses format in this way:
 	 * %% - a % sign
 	 * %I - the human-like index name
@@ -116,64 +120,64 @@ const char* parseformat(const char* format,long value,const char* index, const c
 
 	i=0; /* initialize buffer */
 
-	while (sf_read_k(k)) { /* for each byte in format as k */
+	while (read_k(k)) { /* for each byte in format as k */
 		if (k=='%') {
 			zeroflag=0;
 			size=0;
 
-			sf_read_k(k); /* read next char */
+			read_k(k); /* read next char */
 			if (!k) return NULL; /* just in case the string is finished in the wrong place */
 
 			if (k=='0') { /* it's %0... */
 				zeroflag=1;
-				sf_read_k(k); /* read next char */
+				read_k(k); /* read next char */
 				if (!k) return NULL; /* just in case the string is finished in the wrong place */
 			}
 			while (isNumber(k)) { /* it's %0[0-9]... */
 				size=size*10+k-'0';
-				sf_read_k(k); /* read next char */
+				read_k(k); /* read next char */
 				if (!k) return NULL; /* just in case the string is finished in the wrong place */
 			}
 			switch (k) {
 			case 'v': /* value */
-				sf_emitNumber(value,size,zeroflag);
+				emitNumber(value,size,zeroflag);
 				break;
 			case 'i': /* index name */
-				sf_emitString(index,size,zeroflag);
+				emitString(index,size,zeroflag);
 				break;
 			case 'I': /* index human name */
-				sf_emitString(translateInt2Ext(index),size,zeroflag);
+				emitString(translateInt2Ext(index),size,zeroflag);
 				break;
 			case 't': /* item type (image, wave, etc) */
-				sf_emitString(type,size,zeroflag);
+				emitString(type,size,zeroflag);
 				break;
 			case 'e': /* extension (bmp, wav, etc) */
-				sf_emitString(extension,size,zeroflag);
+				emitString(extension,size,zeroflag);
 				break;
 			case 'n': /* number of the item typed in %t */
-				sf_emitNumber(numberOfThisType,size,zeroflag);
+				emitNumber(numberOfThisType,size,zeroflag);
 				break;
 			case 'o': /* order number (in case the index and values are the same) */
-				sf_emitNumber(order,size,zeroflag);
+				emitNumber(order,size,zeroflag);
 				break;
 			case 'd': /* description */
-				sf_emitString(desc,size,zeroflag);
+				emitString(desc,size,zeroflag);
 				break;
 			case 'm': /* name */
-				sf_emitString(name,size,zeroflag);
+				emitString(name,size,zeroflag);
 				break;
 			case '%': /* the % symbol */
-				sf_emit(k); /* sf_emit it */
+				emit(k); /* sf_emit it */
 				break;
 			default:
 				return NULL;
 			}
 		} else { /* normal characters are sf_emitted */
-			sf_emit(k);
+			emit(k);
 		}
 	}
 
-	return sf_emit(0)?buffer:NULL; /* close the string and return it (return NULL in case of error)*/
+	return emit(0)?buffer:NULL; /* close the string and return it (return NULL in case of error)*/
 }
 
 #ifdef DEBUG_STRINGS