[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [26680] trunk/blender/source/blender/ blenlib/intern/BLI_args.c: Fun stuff: conflict detection in argument parsing library ( could have been useful to detect a previous bug, prevents further bugs).

Martin Poirier theeth at yahoo.com
Sun Feb 7 16:36:20 CET 2010


Revision: 26680
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=26680
Author:   theeth
Date:     2010-02-07 16:36:20 +0100 (Sun, 07 Feb 2010)

Log Message:
-----------
Fun stuff: conflict detection in argument parsing library (could have been useful to detect a previous bug, prevents further bugs).

Prints a warning in the console at runtime when filling in the arguments if conflicts exist.

Modified Paths:
--------------
    trunk/blender/source/blender/blenlib/intern/BLI_args.c

Modified: trunk/blender/source/blender/blenlib/intern/BLI_args.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/BLI_args.c	2010-02-07 15:24:10 UTC (rev 26679)
+++ trunk/blender/source/blender/blenlib/intern/BLI_args.c	2010-02-07 15:36:20 UTC (rev 26680)
@@ -44,6 +44,7 @@
 } bAKey;
 
 typedef struct bArgument {
+	bAKey *key;
 	BA_ArgCallback func;
 	void *data;
 } bArgument;
@@ -55,7 +56,7 @@
 	int	  *passes;
 };
 
-unsigned int case_strhash(void *ptr) {
+static unsigned int case_strhash(void *ptr) {
 	char *s= ptr;
 	unsigned int i= 0;
 	unsigned char c;
@@ -86,6 +87,17 @@
 	}
 }
 
+static bArgument *lookUp(struct bArgs *ba, char *arg, int pass, int case_str)
+{
+	bAKey key;
+
+	key.case_str = case_str;
+	key.pass = pass;
+	key.arg = arg;
+
+	return BLI_ghash_lookup(ba->items, &key);
+}
+
 bArgs *BLI_argsInit(int argc, char **argv)
 {
 	bArgs *ba = MEM_callocN(sizeof(bArgs), "bArgs");
@@ -122,49 +134,52 @@
 	return ba->argv;
 }
 
-void BLI_argsAdd(struct bArgs *ba, char *arg, int pass, BA_ArgCallback cb, void *data)
+static void internalAdd(struct bArgs *ba, char *arg, int pass, int case_str, BA_ArgCallback cb, void *data)
 {
-	bArgument *a = MEM_callocN(sizeof(bArgument), "bArgument");
-	bAKey *key = MEM_callocN(sizeof(bAKey), "bAKey");
+	bArgument *a;
+	bAKey *key;
 
+	a = lookUp(ba, arg, pass, case_str);
+
+	if (a) {
+		printf("WARNING: conflicting argument\n");
+		printf("\ttrying to add '%s' on pass %i, %scase sensitive\n", arg, pass, case_str == 1? "not ": "");
+		printf("\tconflict with '%s' on pass %i, %scase sensitive\n\n", a->key->arg, (int)a->key->pass, a->key->case_str == 1? "not ": "");
+	}
+
+	a = MEM_callocN(sizeof(bArgument), "bArgument");
+	key = MEM_callocN(sizeof(bAKey), "bAKey");
+
 	key->arg = arg;
 	key->pass = pass;
-	key->case_str = 0;
+	key->case_str = case_str;
 
+	a->key = key;
 	a->func = cb;
 	a->data = data;
 
 	BLI_ghash_insert(ba->items, key, a);
 }
 
+void BLI_argsAdd(struct bArgs *ba, char *arg, int pass, BA_ArgCallback cb, void *data)
+{
+	internalAdd(ba, arg, pass, 0, cb, data);
+}
+
 void BLI_argsAddCase(struct bArgs *ba, char *arg, int pass, BA_ArgCallback cb, void *data)
 {
-	bArgument *a = MEM_callocN(sizeof(bArgument), "bArgument");
-	bAKey *key = MEM_callocN(sizeof(bAKey), "bAKey");
-
-	key->arg = arg;
-	key->pass = pass;
-	key->case_str = 1;
-
-	a->func = cb;
-	a->data = data;
-
-	BLI_ghash_insert(ba->items, key, a);
+	internalAdd(ba, arg, pass, 1, cb, data);
 }
 
 
 void BLI_argsParse(struct bArgs *ba, int pass, BA_ArgCallback default_cb, void *default_data)
 {
-	bAKey key;
 	int i = 0;
 
-	key.case_str = -1; /* signal what side of the comparison it is */
-	key.pass = pass;
-
 	for( i = 1; i < ba->argc; i++) { /* skip argv[0] */
-		key.arg = ba->argv[i];
 		if (ba->passes[i] == 0) {
-			bArgument *a = BLI_ghash_lookup(ba->items, &key);
+			 /* -1 signal what side of the comparison it is */
+			bArgument *a = lookUp(ba, ba->argv[i], pass, -1);
 			BA_ArgCallback func = NULL;
 			void *data = NULL;
 
@@ -174,10 +189,6 @@
 			} else {
 				func = default_cb;
 				data = default_data;
-
-				if (func) {
-					printf("calling default on %s\n", ba->argv[i]);
-				}
 			}
 
 			if (func) {





More information about the Bf-blender-cvs mailing list