From: Joseph Pingenot Date: Thu, 24 Jul 2008 03:23:43 +0000 (-0500) Subject: * dotcod matdb parser seems to work great. X-Git-Url: http://vcs.maemo.org/git/?a=commitdiff_plain;h=1e4d66b750fbb1c39ffb97e4d7758a243201cb08;hp=1851b161c89d81370acea767bfd840187c119119;p=scdataviz * dotcod matdb parser seems to work great. --- diff --git a/matdb-dotcode.c b/matdb-dotcode.c index c72c74f..df195fb 100644 --- a/matdb-dotcode.c +++ b/matdb-dotcode.c @@ -26,7 +26,25 @@ #include #include -void insert_into_matdb(struct matdb *mdb, struct matdb_material **mat, struct matdb_bowing **bow) { +#ifdef DEBUG +#undef DEBUG +#endif +#ifdef DEBUG_2 +#undef DEBUG_2 +#endif + +/*Functions to be used by the hash.*/ +static void destroy_string(gpointer data) { + free((char*)data); +} +static void destroy_double(gpointer data){ + free((double*)data); +} +static void destroy_inner_bowhash(gpointer data) { + g_hash_table_unref(data); +} + +int insert_into_matdb(struct matdb *mdb, struct matdb_material **mat, struct matdb_bowing **bow) { if((*mat) != NULL) { #ifdef DEBUG fprintf(stderr, "inserting material %s\n", (*mat)->name->str); @@ -38,17 +56,22 @@ void insert_into_matdb(struct matdb *mdb, struct matdb_material **mat, struct ma #ifdef DEBUG fprintf(stderr, "inserting bowing %s:%s\n", (*bow)->from->str, (*bow)->to->str); #endif - g_hash_table_insert(mdb->bowings, (gpointer)g_strdup((*bow)->from->str), (gpointer)(*bow)); + /*Inner table*/ + GHashTable *it; + if((it=g_hash_table_lookup(mdb->bowings, (*bow)->from->str)) == NULL) { + if((it = g_hash_table_new_full(&g_str_hash, &g_str_equal, &destroy_string, &destroy_inner_bowhash)) == NULL) { + (*bow) = NULL; + return 1; + } + g_hash_table_insert(mdb->bowings, (*bow)->from->str, it); + #ifdef DEBUG + fprintf(stderr, "Got new hash table (from=%s)\n", (*bow)->from->str); + #endif + } + g_hash_table_insert(it, (gpointer)g_strdup((*bow)->to->str), (gpointer)(*bow)); (*bow) = NULL; } -} - -/*Functions to be used by the hash.*/ -static void destroy_string(gpointer data) { - free((char*)data); -} -static void destroy_double(gpointer data){ - free((double*)data); + return 0; } /*Removes leading and trailing whitespace, comments, and reduces @@ -261,30 +284,55 @@ struct matdb* read_matdb_dotcode(const GString *name, int* err) { #endif section=2; }else{ + #ifdef DEBUG + fprintf(stderr, "\t%d/%s\t", section, line); + #endif /*Process a property.*/ switch(section) { case 1: + #ifdef DEBUG + fprintf(stderr, "(1)\t"); + #endif ht = mat->properties; break; case 2: + #ifdef DEBUG + fprintf(stderr, "(2)\t"); + #endif ht = bow->properties; break; default: + #ifdef DEBUG + fprintf(stderr, "(default)\t"); + #endif *err &= 16; section = 0; + } + if(section == 0) { + #ifdef DEBUG + fprintf(stderr, "section was 0\n"); + #endif continue; } if((value = (double*)malloc(sizeof(double))) == NULL) { *err &= 2048; + #ifdef DEBUG + fprintf(stderr, "malloc of value failed\n"); + #endif continue; } - if(sscanf(" %g", i, value) != 1) { + int num; + if((num=sscanf(i, " %lg", value)) != 1) { *err &= 2048; + free(value); + #ifdef DEBUG + fprintf(stderr, "bad sscanf to get value (scanned \"%s\", returned %d)\n", i, num); + #endif continue; } - g_hash_table_insert(ht, (gpointer)g_strdup(i), (gpointer)value); -#ifdef DEBUG_2 - fprintf(stderr, "\t%d/%s\t%g(%s)\n", section, line, *value, i); + g_hash_table_insert(ht, g_strdup(line), value); +#ifdef DEBUG + fprintf(stderr, "%g(%s)\n", *value, line); #endif } free(line); diff --git a/matdb.c b/matdb.c index 995074c..644eb40 100644 --- a/matdb.c +++ b/matdb.c @@ -27,15 +27,19 @@ static void print_property(gpointer key, gpointer value, gpointer user_data) { fprintf(stderr, "\t\t%s=%g:\n", (char*)key, *(double*)value); } +static void print_inner_bowing(gpointer key, gpointer value, gpointer user_data) { + struct matdb_bowing *bowing = (struct matdb_bowing*)value; + fprintf(stderr, "\tbowing %s:%s[%s:%s]:\n", bowing->from->str, bowing->to->str, (char*)user_data, (char*)key); + g_hash_table_foreach(bowing->properties, &print_property, NULL); +} static void print_material(gpointer key, gpointer value, gpointer user_data) { struct matdb_material *mat = (struct matdb_material*)value; fprintf(stderr, "\tmaterial %s(%s):\n", mat->name->str, (char*)key); - g_hash_table_foreach(mat->properties, print_property, NULL); + g_hash_table_foreach(mat->properties, &print_property, NULL); } static void print_bowing(gpointer key, gpointer value, gpointer user_data) { - struct matdb_bowing *bowing = (struct matdb_bowing*)value; - fprintf(stderr, "\tbowing %s:%s(%s):\n", bowing->from->str, bowing->to->str, (char*)key); - g_hash_table_foreach(bowing->properties, print_property, NULL); + GHashTable *ht = value; + g_hash_table_foreach(ht, &print_inner_bowing, key); } void print_matdb(const struct matdb *mdb) {