*/
#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);
-}