From b3d75c36341a21496fe61e25c61acfe10bb8ced5 Mon Sep 17 00:00:00 2001 From: "trelane@digitasaru.net" Date: Wed, 23 Jul 2008 17:03:28 -0500 Subject: [PATCH] * making progress. --- matdb-dotcode.c | 73 ++++++++++++++++++++++++++++++++++++------------------- matdb-dotcode.h | 2 +- matdb.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++ matdb.h | 10 ++++++-- scdataviz.c | 7 +++++- 5 files changed, 126 insertions(+), 29 deletions(-) create mode 100644 matdb.c diff --git a/matdb-dotcode.c b/matdb-dotcode.c index 85560e4..ee4a05f 100644 --- a/matdb-dotcode.c +++ b/matdb-dotcode.c @@ -21,8 +21,18 @@ */ #include +#define _GNU_C #include #include +#include + +/*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); +} /*Removes leading and trailing whitespace, comments, and reduces redundant whitespace down to one tab, makes an empty line an empty string.*/ @@ -47,7 +57,7 @@ static void prettify_line(char *line) { in_whitespace=leading_whitespace=0; } } - for(look=write=line, *look != '\0'; look++) { + for(look=write=line; *look != '\0'; look++) { switch(*look) { case ' ': case '\n': @@ -78,19 +88,22 @@ static void prettify_line(char *line) { *2048: warning reading file: unable to read numeric value for property. */ struct matdb* read_matdb_dotcode(const GString *name, int* err) { + #ifdef DEBUG + fprintf(stderr, "in read_matdb_dotcode(%s, %d)\n", name->str, *err); + #endif *err=0; struct matdb *mdb = (struct matdb*)malloc(sizeof(struct matdb)); if(mdb == NULL) {*err = 1; return NULL;} double *value; - if((mdb->materials = g_ptr_array_new()) == NULL) { + if((mdb->materials = g_hash_table_new_full(g_str_hash, g_str_equal, &destroy_string, &destroy_material_gpointer)) == NULL) { *err = 1; free(mdb); return NULL; } - if((mdb->bowings = g_ptr_array_new()) == NULL) { + if((mdb->bowings = g_hash_table_new_full(g_str_hash, g_str_equal, &destroy_string, &destroy_bowing_gpointer)) == NULL) { *err = 1; - g_ptr_array_free(mdb->materials, TRUE); + g_hash_table_unref(mdb->materials); free(mdb); return NULL; } @@ -101,19 +114,34 @@ struct matdb* read_matdb_dotcode(const GString *name, int* err) { * 1 (material) * 2 (bow) */ - section=0; + int section=0; char *line; FILE *infile = fopen(name->str, "r"); if(infile == NULL) { + g_hash_table_unref(mdb->materials); + g_hash_table_unref(mdb->bowings); free(mdb); *err=2; return NULL; + #ifdef DEBUG + }else{ + fprintf(stderr, "infile=%x\n", (unsigned int)infile); + #endif } + int val; while(!feof(infile)) { line=NULL; - if(getline(&line, NULL, infile) == -1) { + if((val = getline(&line, NULL, infile)) == -1) { + #ifdef DEBUG + fprintf(stderr, "getline returned %d\n", val); + #endif if(!feof(infile)) *err = 4; + //fclose(infile); break; + }else{ + #ifdef DEBUG + fprintf(stderr, "getline got line (%s)\n", line); + #endif } #ifdef DEBUG fprintf(stderr, "line=(%s)\n", line); @@ -142,15 +170,15 @@ struct matdb* read_matdb_dotcode(const GString *name, int* err) { if(section == 0) { /*If we have a material or bowing underway, save it off*/ if(mat != NULL) { - g_ptr_array_add(mdb->materials, (gpointer)mat); - mat = NULL: + g_hash_table_insert(mdb->materials, (gpointer)g_strdup(mat->name->str), (gpointer)mat); + mat = NULL; } if(bow != NULL) { - g_ptr_array_add(mdb->bowings, (gpointer)bow); - bow = NULL: + g_hash_table_insert(mdb->materials, (gpointer)g_strdup(bow->from->str), (gpointer)mat); + bow = NULL; } if(strcasecmp(line, "material") == 0) { - if((mat = (struct *matdb_material)malloc(sizeof(struct matdb_material))) == NULL) { + if((mat = (struct matdb_material*)malloc(sizeof(struct matdb_material))) == NULL) { *err &= 32; } if((mat->name = g_string_new(i)) == NULL) { @@ -167,8 +195,8 @@ struct matdb* read_matdb_dotcode(const GString *name, int* err) { #ifdef DEBUG fprintf(stderr, "new material (%s):\n", i); #endif - }else if(strncasecmp(line, "bow") == 0) { - if((bow = (struct *matdb_bowing)malloc(sizeof(struct matdb_bowing))) == NULL) { + }else if(strcasecmp(line, "bow") == 0) { + if((bow = (struct matdb_bowing*)malloc(sizeof(struct matdb_bowing))) == NULL) { *err &= 128; } if((to = index(i, ':')) == NULL) { @@ -208,10 +236,10 @@ struct matdb* read_matdb_dotcode(const GString *name, int* err) { }else{ /*Process a property.*/ switch(section) { - case: 1 + case 1: ht = mat->properties; break; - case: 2 + case 2: ht = bow->properties; break; default: @@ -227,21 +255,16 @@ struct matdb* read_matdb_dotcode(const GString *name, int* err) { *err &= 2048; continue; } - g_hash_table_insert(ht, (gpointer)i, value); + g_hash_table_insert(ht, (gpointer)g_strdup(i), (gpointer)value); #ifdef DEBUG - fprintf(stderr, "\t%d/%s\t%g(%s)\n", section, line, value, i); + fprintf(stderr, "\t%d/%s\t%g(%s)\n", section, line, *value, i); #endif } free(line); - line=value=i=to=NULL; + line=i=to=NULL; + value=NULL; } - fclose(file); + fclose(infile); return mdb; } -static void destroy_string(gpointer data) { - free((char*)data); -} -static void destroy_double(gpointer data){ - free((double*)data); -} diff --git a/matdb-dotcode.h b/matdb-dotcode.h index c3a7219..ebe4719 100644 --- a/matdb-dotcode.h +++ b/matdb-dotcode.h @@ -25,6 +25,6 @@ #include -struct matdb* read_matdb_dotcode(GString *name); +struct matdb* read_matdb_dotcode(const GString *name, int *err); #endif diff --git a/matdb.c b/matdb.c new file mode 100644 index 0000000..891bec5 --- /dev/null +++ b/matdb.c @@ -0,0 +1,63 @@ +/* + * matdb: generic materials database information. + + + + Copyright (C) 2008 Joseph Pingenot + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 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 Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + +*/ + +#include +#include +#include + +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_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); +} +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); +} + +void print_matdb(const struct matdb *mdb) { + fprintf(stderr, "matdb:\n"); + g_hash_table_foreach(mdb->materials, &print_material, NULL); + g_hash_table_foreach(mdb->bowings, &print_bowing, NULL); +} + +void destroy_material_gpointer(gpointer data) { + destroy_material((struct matdb_material *)data); +} +void destroy_bowing_gpointer(gpointer data) { + destroy_bowing((struct matdb_bowing *)data); +} +void destroy_material(struct matdb_material *mat) { + free(mat->name); + g_hash_table_unref(mat->properties); + free(mat); +} +void destroy_bowing(struct matdb_bowing *bow) { + free(bow->from); + free(bow->to); + g_hash_table_unref(bow->properties); + free(bow); +} diff --git a/matdb.h b/matdb.h index 6aff98e..19747f8 100644 --- a/matdb.h +++ b/matdb.h @@ -37,8 +37,14 @@ struct matdb_bowing { }; struct matdb { - GPtrArray *materials; - GPtrArray *bowings; + GHashTable *materials; + GHashTable *bowings; }; +void print_matdb(const struct matdb *mdb); +void destroy_material_gpointer(gpointer data); +void destroy_bowing_gpointer(gpointer data); +void destroy_material(struct matdb_material *mat); +void destroy_bowing(struct matdb_bowing *bow); + #endif diff --git a/scdataviz.c b/scdataviz.c index 25a2818..5145d48 100644 --- a/scdataviz.c +++ b/scdataviz.c @@ -37,8 +37,13 @@ int main(int argc, char *argv[]) GtkWidget *button; GtkWidget *graph; GString *file = g_string_new("/usr/local/install/nanoSim/share/docos/nanoSim/matdb.txt"); - int err; + int err=0; struct matdb *mdb = read_matdb_dotcode(file, &err); + //fprintf(stderr, "read_matdb_dotcode(%s, %d)=%x", file->str, err, + //(int)mdb); + fprintf(stderr, "err=%d\n", err); + print_matdb(mdb); + gtk_init (&argc, &argv); -- 1.7.9.5