*/
#include <matdb-dotcode.h>
+#define _GNU_C
#include <stdio.h>
#include <strings.h>
+#include <stdlib.h>
+
+/*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.*/
in_whitespace=leading_whitespace=0;
}
}
- for(look=write=line, *look != '\0'; look++) {
+ for(look=write=line; *look != '\0'; look++) {
switch(*look) {
case ' ':
case '\n':
*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;
}
* 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);
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) {
#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) {
}else{
/*Process a property.*/
switch(section) {
- case: 1
+ case 1:
ht = mat->properties;
break;
- case: 2
+ case 2:
ht = bow->properties;
break;
default:
*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);
-}
--- /dev/null
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+
+*/
+
+#include <matdb.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+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);
+}