git » fp-git.git » commit 93c72d0

improved resource matching features (now order is supported).

author ecalot
2005-12-18 19:53:47 UTC
committer ecalot
2005-12-18 19:53:47 UTC
parent 7a93eccbedfa51394c2d3f9df43331b71979b8d3

improved resource matching features (now order is supported).

PR/src/Makefile +1 -1
PR/src/include/resourcematch.h +14 -0
PR/src/lib/layers/idlist.c +20 -18
PR/src/lib/layers/resourcematch.c +57 -27

diff --git a/PR/src/Makefile b/PR/src/Makefile
index 5298b5c..b497e43 100644
--- a/PR/src/Makefile
+++ b/PR/src/Makefile
@@ -205,7 +205,7 @@ $(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
+.parseresource.o: lib/layers/parseresource.c include/search.h include/memory.h include/resourcematch.h
 	$(INFO) Compiling resource list parsing feature...
 	$(CC) $(OPTIONS) -c lib/layers/parseresource.c -o $@
 
diff --git a/PR/src/include/resourcematch.h b/PR/src/include/resourcematch.h
new file mode 100644
index 0000000..9792059
--- /dev/null
+++ b/PR/src/include/resourcematch.h
@@ -0,0 +1,14 @@
+
+#include "reslist.h" /* tResourceId*/
+
+typedef struct resourceMatch {
+	int flag;
+	int value;
+	int order;
+	char* index;
+	char* path;
+} tResourceMatch;
+
+void freeRM(tResourceMatch *r);
+int initRM(const char* text, tResourceMatch *r);
+int runRM(const tResourceMatch *r, const char* path, const tResourceId *id);
diff --git a/PR/src/lib/layers/idlist.c b/PR/src/lib/layers/idlist.c
index 88b9ced..967cedf 100644
--- a/PR/src/lib/layers/idlist.c
+++ b/PR/src/lib/layers/idlist.c
@@ -40,22 +40,24 @@ idlist.c: Princed Resources : Partial Id list
 #include <string.h> /* strcat strlen */
 #include "disk.h"
 #include "memory.h"
+#include "resourcematch.h"
 #include "idlist.h"
 
 /* Id list for partial manipulation. Private type */
 typedef enum {eString,eId,eIdValue,eIdIndex}tResLocationType;
-
+/*
 typedef struct {
   tResLocationType type;
   union {
     char*        text;
     tResourceId  id;
   } field;
-}tResIdListItem;
-
+}tResourceMatch;
+*/
 typedef struct {
 	int             count;
-	tResIdListItem* list;
+	/*tResourceMatch* list;*/
+	tResourceMatch* list;
 }tResIdList;
 
 static tResIdList partialList;
@@ -132,14 +134,16 @@ void parseGivenPath(char* path) {
 		}
 		i++;
 	}
-	partialList.list=(tResIdListItem*)malloc(sizeof(tResIdListItem)*partialList.count);
+	partialList.list=(tResourceMatch*)malloc(sizeof(tResourceMatch)*partialList.count);
 
 	/* Parse values and save them in the list */
 	for(i=separator;j!=partialList.count;i++) {
-		unsigned int value;
+		printf("hola %s j=%d\n",path+i,j);
+		initRM(path+i,partialList.list+j); /* parsing error*/
+/*		unsigned int value;
 		int converted;
 		char index[5];
-		converted=sscanf(path+i,"%u:%5s",&value,index); /* TODO: support order */
+		converted=sscanf(path+i,"%u:%5s",&value,index); * TODO: support order *
 		switch (converted) {
 		case 2:
 			partialList.list[j].type=eId;
@@ -153,10 +157,10 @@ void parseGivenPath(char* path) {
 			partialList.list[j].field.id.order=0;
 			break;
 		default:
-			/* TODO: test this */
+			* TODO: test this *
 			if (sscanf(path+i,":%5s",index)) {
 				partialList.list[j].type=eIdIndex;
-				strncpy(partialList.list[j].field.id.index,index,5); /* TODO: check str5lowercpy */
+				strncpy(partialList.list[j].field.id.index,index,5); * TODO: check str5lowercpy *
 				partialList.list[j].field.id.order=0;
 			} else {
 				char* aux;
@@ -169,6 +173,7 @@ void parseGivenPath(char* path) {
 			}
 			break;
 		}
+		*/
 		while (path[i]) i++;
 		j++;
 	}
@@ -188,7 +193,7 @@ int isInThePartialList(const char* vFile, tResourceId id) {
 	if (!partialList.count) return 1;
 
 	for (i=0;i<partialList.count;i++) {
-		switch (partialList.list[i].type) {
+/*		switch (partialList.list[i].type) {
 		case eIdValue:
 			if (id.value==partialList.list[i].field.id.value) return 1;
 			break;
@@ -201,18 +206,15 @@ int isInThePartialList(const char* vFile, tResourceId id) {
 		case eId:
 			if (!resIdCmp(id,partialList.list[i].field.id)) return 1;
 			break;
-		}
+		}*/
+		if (runRM(partialList.list+i,repairFolders(vFile),&id)) return 1;
 	}
 	return 0;
 }
 
 void freePartialList() {
-	int i;
-	for (i=0;i<partialList.count;i++) {
-		if (partialList.list[i].type==eString)
-			free(partialList.list[i].field.text);
-	}
-	free(partialList.list);
-	partialList.count=0;
+	void* aux=partialList.list;
+	while (partialList.count--) freeRM(partialList.list++);
+	free(aux);
 }
 
diff --git a/PR/src/lib/layers/resourcematch.c b/PR/src/lib/layers/resourcematch.c
index 94ebe96..eb73ebb 100644
--- a/PR/src/lib/layers/resourcematch.c
+++ b/PR/src/lib/layers/resourcematch.c
@@ -1,22 +1,27 @@
 
+/* TODO: rename this file to resourcematch.c */
+
 #include <stdio.h>
 #include "memory.h"
 #include "search.h" /* getOrder */
+#include "resourcematch.h"
 
 #define dVal 1
 #define dInd 2
 #define dOrd 4
 #define dPat 8
 
-typedef enum {eVal=0,eInd,eOrd,ePat,eDone} tStatus;
+typedef enum {eDone=0,eVal,eInd,eOrd,ePat} tStatus;
 
 char aux[200];
 
 /* default values */
-int xvalue=0;
-int xorder=0;
-char* xindex=NULL;
-char* xpath=NULL;
+static tResourceMatch result;
+
+void freeRM(tResourceMatch *r) {
+	freeAllocation(r->index);
+	freeAllocation(r->path);
+}
 
 void xemit(tStatus s, char c) {
 	static tStatus old=eDone;
@@ -24,68 +29,93 @@ void xemit(tStatus s, char c) {
 	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;
+		case eVal:result.value=atoi(aux);break;
+		case eInd:result.index=strallocandcopy(aux);break;
+		case eOrd:result.order=getOrder(aux);break;
+		case ePat:result.path =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) {
+int initRM(const char* text, tResourceMatch *r) {
 	tStatus status=eVal;
 	const char* t;
-
-	*flag=0;
+	
+	/* set up default values */
+	result.flag=0;
+	result.value=0;
+	result.order=0;
+	result.index=NULL;
+	result.path=NULL;
 
 	for (t=text;status!=eDone;t++) {
 		switch (*t) {
 		case '/': /* path */
 			status=ePat;
-			*flag|=dPat;
+			result.flag|=dPat;
+			 xemit(status,'/'); /* starts with / */
 			break;
 		case ':': /* index */
 			if (status!=ePat) { /* after path, no index is allowed */
 				status=eInd;
-				if (*flag&dInd) {
-					freeAllocation(xindex);
-					freeAllocation(xpath);
+				if (result.flag&dInd) {
+					freeRM(&result);
 					return -1; /* ind flag has been set, duplicated index? */
 				}
-				*flag|=dInd;
+				result.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);
+				if (result.flag&dOrd) {
+					freeRM(&result);
 					return -1; /* ord flag has been set, duplicated order? */
 				}
-				*flag|=dOrd;
+				result.flag|=dOrd;
 			} else xemit(status,*t);
 			break;
 		case 0:
 			status=eDone;
 			break;
 		default: /* by default "value" */
-			if (status==eVal) *flag|=dVal;
+			if (status==eVal) result.flag|=dVal;
 			xemit(status,*t);
 			break;
 		}
 	}
 	xemit(eDone,0);
 
-	*value=xvalue;
-	*order=xorder;
-	*index=xindex;
-	*path=xpath;
+	*r=result;
 	
 	return 0; /* Ok */
 }	
+
+/* matches becomes false only if the flag is true and the match is false */
+#define compare(x,a) m=m&&( (!(r->flag&x)) || (a) )
+
+int runRM(const tResourceMatch *r, const char* path, const tResourceId *id) {
+	int m=1; /* by default it matches */
+
+	printf("Matching: path='%s', id=(%d,%s,%d) <=> flag=%x path='%s' id=(%d,%s,%d)\n",
+		path,
+		id->value,id->index,id->order,
+		r->flag,
+		r->path,
+		r->value,r->index,r->order
+	);
+
+
+	/* compare each field */
+	compare(dOrd,r->order==id->order);
+	compare(dVal,r->value==id->value);
+/*	compare(dInd,matchesIn()||matchesIn(translate()));*/
+	compare(dPat,matchesIn(path,r->path));
+	
+	return m;
+}
+