git » fp-git.git » commit 7a93ecc

Improved parsing features

author ecalot
2005-12-18 03:19:24 UTC
committer ecalot
2005-12-18 03:19:24 UTC
parent cc7a3b509a9eda4694fffe7b8198194a713b87e6

Improved parsing features

PR/src/Makefile +5 -1
PR/src/include/search.h +2 -0
PR/src/lib/layers/idlist.c +14 -11
PR/src/lib/layers/resourcematch.c +91 -0

diff --git a/PR/src/Makefile b/PR/src/Makefile
index d0bfc1f..5298b5c 100644
--- a/PR/src/Makefile
+++ b/PR/src/Makefile
@@ -54,7 +54,7 @@ endif
 ACTIONS  = .import.o .export.o .classify.o
 XML      = .parse.o .search.o .resources.o .translate.o
 COMPRESS = .compress.o .lzg_compress.o .lzg_uncompress.o .rle_compress.o .rle_uncompress.o 
-LAYERS   = .dat.o .memory.o .list.o .reslist.o .disk.o .idlist.o .autodetect.o .stringformat.o
+LAYERS   = .dat.o .memory.o .list.o .reslist.o .disk.o .idlist.o .autodetect.o .stringformat.o .parseresource.o
 FORMAT   = .bmp.o .mid.o .pal.o .wav.o .plv.o
 CONSOLE  = .main.o .filedir.o
 MAIN     = .pr.o
@@ -205,6 +205,10 @@ $(XMLFILE): xml/resources.xml
 	$(INFO) Compiling unknown string format parsing feature...
 	$(CC) $(OPTIONS) -c lib/layers/stringformat.c -o $@
 
+.parseresource.o: lib/layers/parseresource.c include/search.h include/memory.h
+	$(INFO) Compiling resource list parsing feature...
+	$(CC) $(OPTIONS) -c lib/layers/parseresource.c -o $@
+
 .bmp.o: lib/formats/bmp.c include/bmp.h include/common.h include/dat.h include/disk.h include/memory.h
 	$(INFO) Compiling bitmap files support \(bmp\)...
 	$(CC) $(OPTIONS) -c lib/formats/bmp.c -o $@
diff --git a/PR/src/include/search.h b/PR/src/include/search.h
index 4c64977..521b92b 100644
--- a/PR/src/include/search.h
+++ b/PR/src/include/search.h
@@ -67,5 +67,7 @@ typedef struct tPassCompare {
 	const tTag* tag;
 }tPassCompare;
 
+int getOrder(const char* order); 
+
 #endif
 
diff --git a/PR/src/lib/layers/idlist.c b/PR/src/lib/layers/idlist.c
index 4ee2975..88b9ced 100644
--- a/PR/src/lib/layers/idlist.c
+++ b/PR/src/lib/layers/idlist.c
@@ -73,17 +73,20 @@ void parseGivenPath(char* path) {
 	 * format: \([^@]*\)/(@\([0-9]\+\)?\(:[a-z ]*\)?\(#[a-z0-9]\+\)\)?
 	 * where: \1 is the dat filename, \2 is the partical list whole match if exists,
 	 *        \3 is the resource id, \4 is the index name and \5 is the order
-	 * examples:                         rID Ind  Ord
-	 *  datfile.dat@111:shape#first  --> 111 shap 0
-	 *  datfile.dat@111:shape        --> 111 shap *
-	 *  datfile.dat@111#first        --> 111 *    0
-	 *  datfile.dat@#first           --> *   *    0
-	 *  datfile.dat@:shape           --> *   shap *
-	 *  datfile.dat@111              --> 111 *    *
-	 *  datfile.dat@#last            --> *   *    last
-	 *  datfile.dat@#second          --> *   *    1
-	 *  datfile.dat@:shap            --> *   shap *
-	 *  datfile.dat@#785             --> *   *    785
+	 * examples:                         rID Ind  Ord  Restricted to
+	 *  datfile.dat@111:shape#first  --> 111 shap 0    *
+	 *  datfile.dat@111:shape        --> 111 shap *    *
+	 *  datfile.dat@111#first        --> 111 *    0    *
+	 *  datfile.dat@#first           --> *   *    0    *
+	 *  datfile.dat@:shape           --> *   shap *    *
+	 *  datfile.dat@111              --> 111 *    *    *
+	 *  datfile.dat@#last            --> *   *    last *
+	 *  datfile.dat@#second          --> *   *    1    *
+	 *  datfile.dat@:shap            --> *   shap *    *
+	 *  datfile.dat@:tga palette     --> *   palt *    *
+	 *  datfile.dat@#785             --> *   *    785  *
+	 *  datfile.dat@/a.bmp           --> *   *    *    a.bmp
+	 *  datfile.dat@/img*.bmp        --> *   *    *    img*.bmp
 
 		PRE:  partialList.list was not allocated
 		POST:
diff --git a/PR/src/lib/layers/resourcematch.c b/PR/src/lib/layers/resourcematch.c
new file mode 100644
index 0000000..94ebe96
--- /dev/null
+++ b/PR/src/lib/layers/resourcematch.c
@@ -0,0 +1,91 @@
+
+#include <stdio.h>
+#include "memory.h"
+#include "search.h" /* getOrder */
+
+#define dVal 1
+#define dInd 2
+#define dOrd 4
+#define dPat 8
+
+typedef enum {eVal=0,eInd,eOrd,ePat,eDone} tStatus;
+
+char aux[200];
+
+/* default values */
+int xvalue=0;
+int xorder=0;
+char* xindex=NULL;
+char* xpath=NULL;
+
+void xemit(tStatus s, char c) {
+	static tStatus old=eDone;
+	static char* a=aux;
+	if (s!=old) { /* status change */
+		*a=0;
+		switch (old) {
+		case eVal:xvalue=atoi(aux);break;
+		case eInd:xindex=strallocandcopy(aux);break;
+		case eOrd:xorder=getOrder(aux);break;
+		case ePat:xpath =strallocandcopy(aux);break;
+		default:break;
+		}
+		old=s;
+		a=aux;
+	}
+ 	*(a++)=c;
+/*	printf("emiting '%c' (%d)\n",x,a);*/
+}
+
+int parse(const char* text, int* flag, int* value, char** index, int* order,char** path) {
+	tStatus status=eVal;
+	const char* t;
+
+	*flag=0;
+
+	for (t=text;status!=eDone;t++) {
+		switch (*t) {
+		case '/': /* path */
+			status=ePat;
+			*flag|=dPat;
+			break;
+		case ':': /* index */
+			if (status!=ePat) { /* after path, no index is allowed */
+				status=eInd;
+				if (*flag&dInd) {
+					freeAllocation(xindex);
+					freeAllocation(xpath);
+					return -1; /* ind flag has been set, duplicated index? */
+				}
+				*flag|=dInd;
+			} else xemit(status,*t);
+			break;
+		case '#': /* order */
+			if (status!=ePat) { /* after path, no order is allowed */
+				status=eOrd;
+				if (*flag&dOrd) {
+					freeAllocation(xindex);
+					freeAllocation(xpath);
+					return -1; /* ord flag has been set, duplicated order? */
+				}
+				*flag|=dOrd;
+			} else xemit(status,*t);
+			break;
+		case 0:
+			status=eDone;
+			break;
+		default: /* by default "value" */
+			if (status==eVal) *flag|=dVal;
+			xemit(status,*t);
+			break;
+		}
+	}
+	xemit(eDone,0);
+
+	*value=xvalue;
+	*order=xorder;
+	*index=xindex;
+	*path=xpath;
+	
+	return 0; /* Ok */
+}