* Seems to be coming along. Not convinced that the bowing conversions are right.
[scdataviz] / matdb-dotcode.c
index c72c74f..df195fb 100644 (file)
 #include <strings.h>
 #include <stdlib.h>
 
 #include <strings.h>
 #include <stdlib.h>
 
-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);
   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
 #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;
   }
     (*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
 }
 
 /*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{
 #endif
       section=2;
     }else{
+      #ifdef DEBUG
+      fprintf(stderr, "\t%d/%s\t", section, line);
+      #endif
       /*Process a property.*/
       switch(section) {
       case 1:
       /*Process a property.*/
       switch(section) {
       case 1:
+       #ifdef DEBUG
+       fprintf(stderr, "(1)\t");
+       #endif
        ht = mat->properties;
        break;
       case 2:
        ht = mat->properties;
        break;
       case 2:
+       #ifdef DEBUG
+       fprintf(stderr, "(2)\t");
+       #endif
        ht = bow->properties;
        break;
       default:
        ht = bow->properties;
        break;
       default:
+       #ifdef DEBUG
+       fprintf(stderr, "(default)\t");
+       #endif
        *err &= 16;
        section = 0;
        *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;
        continue;
       }
       if((value = (double*)malloc(sizeof(double))) == NULL) {
        *err &= 2048;
+       #ifdef DEBUG
+       fprintf(stderr, "malloc of value failed\n");
+       #endif
        continue;
       }
        continue;
       }
-      if(sscanf(" %g", i, value) != 1) {
+      int num;
+      if((num=sscanf(i, " %lg", value)) != 1) {
        *err &= 2048;
        *err &= 2048;
+       free(value);
+       #ifdef DEBUG
+       fprintf(stderr, "bad sscanf to get value (scanned \"%s\", returned %d)\n", i, num);
+       #endif
        continue;
       }
        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);
 #endif
     }
     free(line);