git » fp-git.git » commit e076545

testing xml common factor routines, it seems to work. Nothink hooked yet.

author ecalot
2006-01-01 14:22:18 UTC
committer ecalot
2006-01-01 14:22:18 UTC
parent 330960e2b9c64f2049dfbff23e92198931d3c7a1

testing xml common factor routines, it seems to work. Nothink hooked yet.

PR/src/include/list.h +1 -1
PR/src/lib/layers/list.c +1 -1
PR/src/lib/xml/tree.c +55 -11

diff --git a/PR/src/include/list.h b/PR/src/include/list.h
index 81a5f39..db0238f 100644
--- a/PR/src/include/list.h
+++ b/PR/src/include/list.h
@@ -57,6 +57,6 @@ void  list_drop(tList *list);
 void  list_nextCursor(tList* list);
 void  list_firstCursor(tList* list);
 int   list_moveCursor(tList* list,const void* data);
-const void* list_getCursor(tList* list);
+void* list_getCursor(tList* list);
 
 #endif
diff --git a/PR/src/lib/layers/list.c b/PR/src/lib/layers/list.c
index 10a44af..cdc08c9 100644
--- a/PR/src/lib/layers/list.c
+++ b/PR/src/lib/layers/list.c
@@ -120,7 +120,7 @@ int list_moveCursor(tList* list,const void* data) {
 	}
 } 
 
-const void* list_getCursor(tList* list) {
+void* list_getCursor(tList* list) {
 	if (!list->cursor) return NULL;
 	return list->cursor->data;
 } 
diff --git a/PR/src/lib/xml/tree.c b/PR/src/lib/xml/tree.c
index cee54ba..7b35802 100644
--- a/PR/src/lib/xml/tree.c
+++ b/PR/src/lib/xml/tree.c
@@ -21,7 +21,7 @@
 /*
 tree.c: Princed Resources : Tree handling routines
 \xaf\xaf\xaf\xaf\xaf\xaf
- Copyright 2003 Princed Development Team
+ Copyright 2005 Princed Development Team
   Created: 28 Dec 2005
 
   Author: Enrique Calot <ecalot.cod@princed.com.ar>
@@ -65,13 +65,15 @@ const void* list_getCursor(tList* list);
 */
 
 typedef struct {
-	char* attr;
+	/*const*/char* attr;
 	int count;
 }tAttrCount;
 
 void attrfree() {}
 
-int attrcmp(const tAttrCount* a,const tAttrCount* b) {
+int attrcmp(const void* x,const void* y) {
+	register const tAttrCount* a=x;
+	register const tAttrCount* b=y;
 	/* the index has the priority */
 	int c=strcmp(a->attr,b->attr);
 	if (c>0) return GT;
@@ -79,12 +81,16 @@ int attrcmp(const tAttrCount* a,const tAttrCount* b) {
 	return EQ;
 }
 
-void increase(char* attr,tList* l) {
+void increase(/*const */char* attr,tList* l) {
 	tAttrCount a;
+	tAttrCount* aux;
+
+	printf("increase: %s\n",attr);
 	a.attr=attr;
-	a.count=0;
+	a.count=1; /* if it appeared for the first time */
 	if (list_moveCursor(l,&a)) {
-		/* TODO: int count */
+		aux=list_getCursor(l);
+		aux->count++;
 	} else {
 		list_insert(l,&a);
 	}
@@ -99,12 +105,16 @@ void commonFactor(tTag* parent) {
 	tList l=list_create(sizeof(tAttrCount),attrcmp,attrfree);
 	
 	for (child=parent->child;child;child=child->next) {
-		increase(child->palette,&l); /* TODO: use an array of attributes */
+		if (child->palette)
+			increase(child->palette,&l); /* TODO: use an array of attributes */
+		else
+			increase(parent->palette,&l); /* TODO: use an array of attributes */
 		c++;
 	}
 	
 	list_firstCursor(&l);
 	while ((a=list_getCursor(&l))) {
+		printf("running through %s (%d)\n",a->attr,a->count);
 		if (a->count>c/2) {
 			if (a->count>max) {
 				max=a->count;
@@ -113,19 +123,51 @@ void commonFactor(tTag* parent) {
 		}
 		list_nextCursor(&l);
 	}
+	printf("result=%s\n",result);
 	if (result) {
-		freeAllocation(parent->palette);
-		parent->palette=strallocandcopy(result);
+		if (parent->palette!=result) { /* it is possible, and is the most probable case, that the parent was already the best choice. In that case, do nothing */
+			freeAllocation(parent->palette);
+			parent->palette=strallocandcopy(result);
+		}
 		for (child=parent->child;child;child=child->next) {
-			freeAllocation(child->palette);
-			child->palette=NULL;
+			if (child->palette&&!strcmp(child->palette,parent->palette)) {
+				freeAllocation(child->palette); /* the first time this is ran, result is released! */
+				child->palette=NULL;
+			}
 		}
 	}
 	list_drop(&l);
 	
+}
+
+void test() {
+	tTag tr[100];
+	memset(tr,sizeof(tr),0);
+
+	tr[0].child=&(tr[1]);
+	tr[1].next =&(tr[2]);
+	tr[2].next =&(tr[3]);
+	tr[3].next =&(tr[4]);
+	tr[4].next =&(tr[5]);
+	tr[5].next =NULL;
 	
+	tr[0].palette=strallocandcopy("jojo");
+/*	tr[1].palette=strallocandcopy("joj1");
+	tr[2].palette=strallocandcopy("joj1");*/
+	tr[3].palette=strallocandcopy("jojo");
+	tr[4].palette=strallocandcopy("joj4");
+	tr[5].palette=strallocandcopy("joj5");
+
+	generateXML(0,tr,stdout); 
+
+	commonFactor(tr);
+	
+	generateXML(0,tr,stdout); 
+
 }
 
+
+
 /***************************************************************\
 |                     Tag generation routines                   |
 \***************************************************************/
@@ -304,5 +346,7 @@ void unknown_fixtreeinheritances(tTag* *tree) {
 		freeAllocation((*tree)->path);
 		(*tree)->path=NULL;		
 	}
+
+	test();
 }