2 * matdb-dotcode: reads in Craig's Dotcode materials database.
6 Copyright (C) 2008 Joseph Pingenot
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Affero General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Affero General Public License for more details.
18 You should have received a copy of the GNU Affero General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <matdb-dotcode.h>
29 /*Functions to be used by the hash.*/
30 static void destroy_string(gpointer data) {
33 static void destroy_double(gpointer data){
37 /*Removes leading and trailing whitespace, comments, and reduces
38 redundant whitespace down to one tab, makes an empty line an empty string.*/
39 static void prettify_line(char *line) {
42 register int in_whitespace;
43 register int leading_whitespace;
44 for(look=write=line, in_whitespace=0, leading_whitespace=1; *look != '\0'; look++) {
49 if((!leading_whitespace) && (!in_whitespace)) *write++ = '\t';
57 in_whitespace=leading_whitespace=0;
60 for(look=write=line; *look != '\0'; look++) {
65 if(*(look+1) == '\0') break;
75 * 1: failure malloc'ing / initializing mdb struct
76 * 2: failure opening file.
77 * 4: failure reading line.
78 * 8: warning reading line: no tab separator (line parsing error)
79 * 16: warning reading line: bad section declaration
80 * 32: warning reading line: unable to allocate material struct.
81 * 64: warning reading line: ignored line (unallocated material struct)
82 * 128: warning reading line: unable to allocate bowing struct
83 * 256: warning reading line: ignored line (unallocated bowing struct)
84 * 512: warning reading file: improper section detected (programming
86 *1024: warning reading file: no : separator in bowing materials
88 *2048: warning reading file: unable to read numeric value for property.
90 struct matdb* read_matdb_dotcode(const GString *name, int* err) {
92 fprintf(stderr, "in read_matdb_dotcode(%s, %d)\n", name->str, *err);
95 struct matdb *mdb = (struct matdb*)malloc(sizeof(struct matdb));
96 if(mdb == NULL) {*err = 1; return NULL;}
99 if((mdb->materials = g_hash_table_new_full(g_str_hash, g_str_equal, &destroy_string, &destroy_material_gpointer)) == NULL) {
104 if((mdb->bowings = g_hash_table_new_full(g_str_hash, g_str_equal, &destroy_string, &destroy_bowing_gpointer)) == NULL) {
106 g_hash_table_unref(mdb->materials);
110 struct matdb_material *mat = NULL;
111 struct matdb_bowing *bow = NULL;
113 * 0 (no/global section)
119 FILE *infile = fopen(name->str, "r");
121 g_hash_table_unref(mdb->materials);
122 g_hash_table_unref(mdb->bowings);
128 fprintf(stderr, "infile=%x\n", (unsigned int)infile);
132 while(!feof(infile)) {
134 if((val = getline(&line, NULL, infile)) == -1) {
136 fprintf(stderr, "getline returned %d\n", val);
138 if(!feof(infile)) *err = 4;
143 fprintf(stderr, "getline got line (%s)\n", line);
147 fprintf(stderr, "line=(%s)\n", line);
151 fprintf(stderr, "prettified line=(%s)\n", line);
157 char *i = index(line, '\t');
162 /*At this point, we have line which stores the first word on the
163 line, and i which stores the second word on the line.
168 fprintf(stderr, "part_a=(%s) part_b=(%s)\n", line, i);
171 /*If we have a material or bowing underway, save it off*/
173 g_hash_table_insert(mdb->materials, (gpointer)g_strdup(mat->name->str), (gpointer)mat);
177 g_hash_table_insert(mdb->materials, (gpointer)g_strdup(bow->from->str), (gpointer)mat);
180 if(strcasecmp(line, "material") == 0) {
181 if((mat = (struct matdb_material*)malloc(sizeof(struct matdb_material))) == NULL) {
184 if((mat->name = g_string_new(i)) == NULL) {
189 if((mat->properties = g_hash_table_new_full(g_str_hash, g_str_equal, &destroy_string, &destroy_double)) == NULL) {
191 g_string_free(mat->name, TRUE);
196 fprintf(stderr, "new material (%s):\n", i);
198 }else if(strcasecmp(line, "bow") == 0) {
199 if((bow = (struct matdb_bowing*)malloc(sizeof(struct matdb_bowing))) == NULL) {
202 if((to = index(i, ':')) == NULL) {
208 /*Same trick as before, but i now stores the from material,
209 and to the to material
211 if((bow->from = g_string_new(i)) == NULL) {
216 if((bow->to = g_string_new(to)) == NULL) {
218 g_string_free(bow->from, TRUE);
222 if((bow->properties = g_hash_table_new_full(g_str_hash, g_str_equal, &destroy_string, &destroy_double)) == NULL) {
224 g_string_free(bow->to, TRUE);
225 g_string_free(bow->from, TRUE);
230 fprintf(stderr, "new bowing (%s:%s):\n", i, to);
237 /*Process a property.*/
240 ht = mat->properties;
243 ht = bow->properties;
250 if((value = (double*)malloc(sizeof(double))) == NULL) {
254 if(sscanf(" %g", i, value) != 1) {
258 g_hash_table_insert(ht, (gpointer)g_strdup(i), (gpointer)value);
260 fprintf(stderr, "\t%d/%s\t%g(%s)\n", section, line, *value, i);