git » fp-git.git » commit 95d75ab

ugly attribute-name vs integer binding

author ecalot
2006-01-02 09:53:28 UTC
committer ecalot
2006-01-02 09:53:28 UTC
parent 5e1b5fa04fd2d4c57f93fedd5703a2a453db589c

ugly attribute-name vs integer binding

PR/src/lib/xml/tree.c +57 -28
PR/src/lib/xml/unknown.c +1 -0

diff --git a/PR/src/lib/xml/tree.c b/PR/src/lib/xml/tree.c
index 6ccef62..a2b778d 100644
--- a/PR/src/lib/xml/tree.c
+++ b/PR/src/lib/xml/tree.c
@@ -85,47 +85,75 @@ void increase(const char* attr,tList* l) {
 	}
 }
 
+#define attributeCount 8
+/* this is the most ugly thing I ever made... nevertheless it was the only way to make it abstract */
+#define bindAttr(name,i) attrInfo[i].offset=(long)(&(parent->name))-(long)(parent)
+#define getAttr(tag) (*( (char**)((long)(tag)+attrInfo[i].offset) ))
+
 void commonFactor(tTag* parent) {
 	tTag* child;
 	const tAttrCount* a;
-	int c=0;
-	int max=0;
-	const char* result=NULL;
-	tList l=list_create(sizeof(tAttrCount),attrcmp,NULL);
+	int i;
+	int max;
+	const char* result;
+	struct attributeInfo {
+		int c;
+		tList l;
+		long offset;
+	} attrInfo[attributeCount];
+	
+	bindAttr(palette,0);
+	bindAttr(paletteindex,1);
+	bindAttr(paletteorder,2);
+	bindAttr(type,3);
+	bindAttr(file,4);
+	bindAttr(index,5);
+	bindAttr(order,6);
+	bindAttr(flags,7);
+
+	/* initialize counting list */
+	for (i=0;i<attributeCount;i++) {
+		attrInfo[i].l=list_create(sizeof(tAttrCount),attrcmp,NULL);
+		attrInfo[i].c=0;
+	}
 	
 	for (child=parent->child;child;child=child->next) {
-		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++;
+		for (i=0;i<attributeCount;i++) {
+			if (getAttr(child))
+				increase(getAttr(child),&(attrInfo[i].l));
+			else
+				increase(getAttr(child),&(attrInfo[i].l));
+			attrInfo[i].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) {
+	for (i=0;i<attributeCount;i++) {
+		max=0;
+		result=NULL;
+		list_firstCursor(&(attrInfo[i].l));
+		while ((a=list_getCursor(&(attrInfo[i].l)))) {
+			printf("running through %s (%d)\n",a->attr,a->count);
+			if (a->count>attrInfo[i].c/2 && a->count>max) {
 				max=a->count;
 				result=a->attr;
 			}
+			list_nextCursor(&(attrInfo[i].l));
 		}
-		list_nextCursor(&l);
-	}
-
-	if (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); /* TODO: assign result but avoid releasing below */
-		}
-		for (child=parent->child;child;child=child->next) {
-			if (child->palette&&!strcmp(child->palette,parent->palette)) {
-				freeAllocation(child->palette); /* the first time this is ran, result is released! */
-				child->palette=NULL;
+	
+		if (result) {
+			if (getAttr(parent)!=result) { /* it is possible, and is the most probable case, that the parent was already the best choice. In that case, do nothing */
+				freeAllocation(getAttr(parent));
+				getAttr(parent)=strallocandcopy(result); /* TODO: assign result but avoid releasing below */
+			}
+			for (child=parent->child;child;child=child->next) {
+				if (getAttr(child)&&!strcmp(getAttr(child),getAttr(parent))) {
+					freeAllocation(getAttr(child)); /* the first time this is ran, result is released! */
+					getAttr(child)=NULL;
+				}
 			}
 		}
+		list_drop(&(attrInfo[i].l));
 	}
-	list_drop(&l);	
 }
 
 void rec_tree_commonfactor(tTag* tag) {
@@ -136,7 +164,7 @@ void rec_tree_commonfactor(tTag* tag) {
 	}
 }
 
-#ifdef DEBUG_CF
+#ifndef DEBUG_CF
 void test() {
 	tTag tr[100];
 	memset(tr,sizeof(tr),0);
@@ -369,5 +397,6 @@ void unknown_fixtreeinheritances(tTag* *tree) {
 		freeAllocation((*tree)->path);
 		(*tree)->path=NULL;		
 	}
+	test();
 }
 
diff --git a/PR/src/lib/xml/unknown.c b/PR/src/lib/xml/unknown.c
index 80a7be1..cf966df 100644
--- a/PR/src/lib/xml/unknown.c
+++ b/PR/src/lib/xml/unknown.c
@@ -108,6 +108,7 @@ int unknownLogStop () {
 	
 	/* now we'll add the new generated part of the tree at the end of the second level (resources id the first) */
 	if (unknownFile.tree) {
+		rec_tree_commonfactor(unknownFile.tree); /* here some common factors are moved up */
 		if (unknownFile.tree->child) {
 			for (t=unknownFile.tree->child;t->next;t=t->next);
 			t->next=unknownFile.status.folderFirst; /* the first folder of the new tree */