[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [26494] trunk/blender/source: New argument parsing library supporting multiple passes, case sensitive and insensitive arguments, default handlers and other features that were hacked in the previous ugly switch system .

Martin Poirier theeth at yahoo.com
Mon Feb 1 02:43:31 CET 2010


Revision: 26494
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=26494
Author:   theeth
Date:     2010-02-01 02:43:31 +0100 (Mon, 01 Feb 2010)

Log Message:
-----------
New argument parsing library supporting multiple passes, case sensitive and insensitive arguments, default handlers and other features that were hacked in the previous ugly switch system. Very simpler system for adding new arguments, easier to see conflicts and no more replication between BG and non BG mode arguments.

I've tested pretty much everything except GE options (-g options), but some small bugs could have sneaked in.

Modified Paths:
--------------
    trunk/blender/source/blender/blenkernel/BKE_global.h
    trunk/blender/source/creator/creator.c

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

Modified: trunk/blender/source/blender/blenkernel/BKE_global.h
===================================================================
--- trunk/blender/source/blender/blenkernel/BKE_global.h	2010-02-01 01:33:40 UTC (rev 26493)
+++ trunk/blender/source/blender/blenkernel/BKE_global.h	2010-02-01 01:43:31 UTC (rev 26494)
@@ -60,7 +60,7 @@
 	/* strings of recent opend files */
 	struct ListBase recent_files;
         
-	short afbreek, moving;
+	short afbreek, moving, file_loaded;
 	short background;
 	short winpos, displaymode;	/* used to be in Render */
 	short rendering;			/* to indicate render is busy, prevent renderwindow events etc */

Added: trunk/blender/source/blender/blenlib/BLI_args.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_args.h	                        (rev 0)
+++ trunk/blender/source/blender/blenlib/BLI_args.h	2010-02-01 01:43:31 UTC (rev 26494)
@@ -0,0 +1,53 @@
+/**
+ * A general argument parsing module
+ *
+ * $Id:
+ *
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+ * All rights reserved.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): none yet.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+#ifndef BLI_ARGS_H
+#define BLI_ARGS_H
+
+struct bArgs;
+typedef struct bArgs bArgs;
+
+/* returns the number of extra arguments consumed by the function. 0 is normal value, -1 stops parsing arguments, other negative indicates skip */
+typedef int	(*BA_ArgCallback)(int argc, char **argv, void *data);
+
+struct bArgs *BLI_argsInit(int argc, char **argv);
+void BLI_argsFree(struct bArgs *ba);
+
+/* pass starts at 1, -1 means valid all the time */
+void BLI_argsAdd(struct bArgs *ba, char *arg, int pass, BA_ArgCallback cb, void *data);
+void BLI_argsAddCase(struct bArgs *ba, char *arg, int pass, BA_ArgCallback cb, void *data); /* not case specific */
+
+void BLI_argsParse(struct bArgs *ba, int pass, BA_ArgCallback default_cb, void *data);
+
+void BLI_argsPrint(struct bArgs *ba);
+char **BLI_argsArgv(struct bArgs *ba);
+
+#endif


Property changes on: trunk/blender/source/blender/blenlib/BLI_args.h
___________________________________________________________________
Name: svn:keywords
   + Date Id Revision
Name: svn:eol-style
   + native

Added: trunk/blender/source/blender/blenlib/intern/BLI_args.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/BLI_args.c	                        (rev 0)
+++ trunk/blender/source/blender/blenlib/intern/BLI_args.c	2010-02-01 01:43:31 UTC (rev 26494)
@@ -0,0 +1,201 @@
+/**
+ * A general argument parsing module
+ *
+ * $Id:
+ *
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+ * All rights reserved.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): none yet.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+#include <ctype.h> /* for tolower */
+
+#include "MEM_guardedalloc.h"
+
+#include "BLI_string.h"
+#include "BLI_args.h"
+#include "BLI_ghash.h"
+
+typedef struct bAKey {
+	char *arg;
+	uintptr_t pass; /* cast easier */
+	int case_str; /* case specific or not */
+} bAKey;
+
+typedef struct bArgument {
+	BA_ArgCallback func;
+	void *data;
+} bArgument;
+
+struct bArgs {
+	GHash  *items;
+	int 	argc;
+	char  **argv;
+	int	  *passes;
+};
+
+unsigned int case_strhash(void *ptr) {
+	char *s= ptr;
+	unsigned int i= 0;
+	unsigned char c;
+
+	while ( (c= tolower(*s++)) )
+		i= i*37 + c;
+
+	return i;
+}
+
+static unsigned int	keyhash(void *ptr)
+{
+	bAKey *k = ptr;
+	return case_strhash(k->arg) ^ BLI_ghashutil_inthash((void*)k->pass);
+}
+
+static int keycmp(void *a, void *b)
+{
+	bAKey *ka = a;
+	bAKey *kb = b;
+	if (ka->pass == kb->pass || ka->pass == -1 || kb->pass == -1) { /* -1 is wildcard for pass */
+		if (ka->case_str == 1 || kb->case_str == 1)
+			return BLI_strcasecmp(ka->arg, kb->arg);
+		else
+			return strcmp(ka->arg, kb->arg);
+	} else {
+		return BLI_ghashutil_intcmp((void*)ka->pass, (void*)kb->pass);
+	}
+}
+
+bArgs *BLI_argsInit(int argc, char **argv)
+{
+	bArgs *ba = MEM_callocN(sizeof(bArgs), "bArgs");
+	ba->passes = MEM_callocN(sizeof(int) * argc, "bArgs passes");
+	ba->items = BLI_ghash_new(keyhash, keycmp);
+	ba->argc = argc;
+	ba->argv = argv;
+
+	return ba;
+}
+
+static void freeItem(void *val)
+{
+	MEM_freeN(val);
+}
+
+void BLI_argsFree(struct bArgs *ba)
+{
+	BLI_ghash_free(ba->items, freeItem, freeItem);
+	MEM_freeN(ba->passes);
+	MEM_freeN(ba);
+}
+
+void BLI_argsPrint(struct bArgs *ba)
+{
+	int i;
+	for (i = 0; i < ba->argc; i++) {
+		printf("argv[%d] = %s\n", i, ba->argv[i]);
+	}
+}
+
+char **BLI_argsArgv(struct bArgs *ba)
+{
+	return ba->argv;
+}
+
+void BLI_argsAdd(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);
+}
+
+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 = 0;
+
+	a->func = cb;
+	a->data = data;
+
+	BLI_ghash_insert(ba->items, key, a);
+}
+
+
+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);
+			BA_ArgCallback func = NULL;
+			void *data = NULL;
+
+			if (a) {
+				func = a->func;
+				data = a->data;
+			} else {
+				func = default_cb;
+				data = default_data;
+
+				if (func) {
+					printf("calling default on %s\n", ba->argv[i]);
+				}
+			}
+
+			if (func) {
+				int retval = func(ba->argc - i, ba->argv + i, data);
+
+				if (retval >= 0) {
+					int j;
+
+					/* use extra arguments */
+					for (j = 0; j <= retval; j++) {
+						ba->passes[i + j] = pass;
+					}
+					i += retval;
+				} else if (retval == -1){
+					ba->passes[i] = pass;
+					break;
+				}
+			}
+		}
+	}
+}


Property changes on: trunk/blender/source/blender/blenlib/intern/BLI_args.c
___________________________________________________________________
Name: svn:keywords
   + Date Id Revision
Name: svn:eol-style
   + native

Modified: trunk/blender/source/creator/creator.c
===================================================================
--- trunk/blender/source/creator/creator.c	2010-02-01 01:33:40 UTC (rev 26493)
+++ trunk/blender/source/creator/creator.c	2010-02-01 01:43:31 UTC (rev 26494)
@@ -44,6 +44,8 @@
 #include "BLI_winstuff.h"
 #endif
 
+#include "BLI_args.h"
+
 #include "GEN_messaging.h"
 
 #include "DNA_ID.h"
@@ -108,8 +110,8 @@
 #endif
 
 /*	Local Function prototypes */
-static void print_help(void);
-static void print_version(void);
+static int print_help(int argc, char **argv, void *data);
+static int print_version(int argc, char **argv, void *data);
 
 /* for the callbacks: */
 
@@ -153,7 +155,7 @@
 	}
 }
 
-static void print_version(void)
+static int print_version(int argc, char **argv, void *data)
 {
 #ifdef BUILD_DATE
 	printf ("Blender %d.%02d (sub %d) Build\n", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION);
@@ -165,9 +167,13 @@
 #else
 	printf ("Blender %d.%02d (sub %d) Build\n", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION);
 #endif
+
+	exit(0);
+
+	return 0;
 }
 
-static void print_help(void)
+static int print_help(int argc, char **argv, void *data)
 {
 	printf ("Blender %d.%02d (sub %d) Build\n", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION);
 	printf ("Usage: blender [args ...] [file] [args ...]\n");
@@ -261,6 +267,10 @@
 	printf ("    \"blender -b -o /tmp test.blend -f 1\"\n");
 	printf ("  ...may not render to /tmp because loading the blend file overwrites the output path that was set\n");
 	printf ("    \"blender -b test.blend -o /tmp -f 1\" works as expected.\n\n");
+
+	exit(0);
+
+	return 0;
 }
 
 
@@ -278,11 +288,597 @@
 	}
 }*/
 
+static int end_arguments(int argc, char **argv, void *data)
+{
+	return -1;
+}
+
+static int disable_python(int argc, char **argv, void *data)
+{
+	G.f &= ~G_DOSCRIPTLINKS;
+	return 0;
+}
+
+
+static int forked_tongue(int argc, char **argv, void *data)
+{
+	printf ("-y was used to disable script links because,\n");
+	printf ("\t-p being taken, Ton was of the opinion that Y\n");
+	printf ("\tlooked like a split (disabled) snake, and also\n");
+	printf ("\twas similar to a python's tongue (unproven).\n\n");
+
+	printf ("\tZr agreed because it gave him a reason to add a\n");
+	printf ("\tcompletely useless text into Blender.\n\n");
+
+	printf ("\tADDENDUM! Ton, in defense, found this picture of\n");

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list