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>
36 /*Functions to be used by the hash.*/
37 static void destroy_string(gpointer data) {
40 static void destroy_double(gpointer data){
43 static void destroy_inner_bowhash(gpointer data) {
44 g_hash_table_unref(data);
47 int insert_into_matdb(struct matdb *mdb, struct matdb_material **mat, struct matdb_bowing **bow) {
50 fprintf(stderr, "inserting material %s\n", (*mat)->name->str);
52 g_hash_table_insert(mdb->materials, (gpointer)g_strdup((*mat)->name->str), (gpointer)(*mat));
57 fprintf(stderr, "inserting bowing %s:%s\n", (*bow)->from->str, (*bow)->to->str);
61 if((it=g_hash_table_lookup(mdb->bowings, (*bow)->from->str)) == NULL) {
62 if((it = g_hash_table_new_full(&g_str_hash, &g_str_equal, &destroy_string, &destroy_inner_bowhash)) == NULL) {
66 g_hash_table_insert(mdb->bowings, (*bow)->from->str, it);
68 fprintf(stderr, "Got new hash table (from=%s)\n", (*bow)->from->str);
71 g_hash_table_insert(it, (gpointer)g_strdup((*bow)->to->str), (gpointer)(*bow));
77 /*Removes leading and trailing whitespace, comments, and reduces
78 redundant whitespace down to one tab, makes an empty line an empty string.*/
79 static void prettify_line(char *line) {
82 register int in_whitespace;
83 register int leading_whitespace;
84 for(look=write=line, in_whitespace=0, leading_whitespace=1; *look != '\0'; look++) {
86 fprintf(stderr, " look=%d(%c) write=%d(%c) in_whitespace=%d leading_whitespace=%d: ", (look-line), *look, (write-line), *write, in_whitespace, leading_whitespace);
90 fprintf(stderr, "comment\n");
100 fprintf(stderr, "whitespace\n");
102 if((!leading_whitespace) && (!in_whitespace)) *write++ = '\t';
107 fprintf(stderr, "default\n");
110 in_whitespace=leading_whitespace=0;
114 for(look=write=line; *look != '\0'; look++) {
119 if(*(look+1) == '\0') {
128 * 1: failure malloc'ing / initializing mdb struct
129 * 2: failure opening file.
130 * 4: failure reading line.
131 * 8: warning reading line: no tab separator (line parsing error)
132 * 16: warning reading line: bad section declaration
133 * 32: warning reading line: unable to allocate material struct.
134 * 64: warning reading line: ignored line (unallocated material struct)
135 * 128: warning reading line: unable to allocate bowing struct
136 * 256: warning reading line: ignored line (unallocated bowing struct)
137 * 512: warning reading file: improper section detected (programming
139 *1024: warning reading file: no : separator in bowing materials
141 *2048: warning reading file: unable to read numeric value for property.
143 struct matdb* read_matdb_dotcode(const GString *name, int* err) {
145 fprintf(stderr, "in read_matdb_dotcode(%s, %d)\n", name->str, *err);
148 struct matdb *mdb = (struct matdb*)malloc(sizeof(struct matdb));
149 if(mdb == NULL) {*err = 1; return NULL;}
152 if((mdb->materials = g_hash_table_new_full(&g_str_hash, &g_str_equal, &destroy_string, &destroy_material_gpointer)) == NULL) {
157 if((mdb->bowings = g_hash_table_new_full(&g_str_hash, &g_str_equal, &destroy_string, &destroy_bowing_gpointer)) == NULL) {
159 g_hash_table_unref(mdb->materials);
163 struct matdb_material *mat = NULL;
164 struct matdb_bowing *bow = NULL;
166 * 0 (no/global section)
173 FILE *infile = fopen(name->str, "r");
175 g_hash_table_unref(mdb->materials);
176 g_hash_table_unref(mdb->bowings);
182 fprintf(stderr, "infile=%x\n", (unsigned int)infile);
186 while(!feof(infile)) {
188 fprintf(stderr, "mat=%x(%s), bow=%x(%s:%s)\n", (unsigned int)mat, mat?mat->name->str:"", (unsigned int)bow, bow?bow->from->str:"", bow?bow->to->str:"");
191 if((val = getline(&line, &n, infile)) == -1) {
193 fprintf(stderr, "getline returned %d\n", val);
195 if(!feof(infile)) *err = 4;
200 fprintf(stderr, "line=(%s)\n", line);
204 fprintf(stderr, "%d: prettified line=(%s)\n", section, line);
210 char *i = index(line, '\t');
215 /*At this point, we have line which stores the first word on the
216 line, and i which stores the second word on the line.
221 fprintf(stderr, "part_a=(%s) part_b=(%s)\n", line, i);
223 /*If we have a material or bowing underway, save it off*/
224 if(strcasecmp(line, "material") == 0) {
225 insert_into_matdb(mdb, &mat, &bow);
226 if((mat = (struct matdb_material*)malloc(sizeof(struct matdb_material))) == NULL) {
229 if((mat->name = g_string_new(i)) == NULL) {
235 if((mat->properties = g_hash_table_new_full(&g_str_hash, &g_str_equal, &destroy_string, &destroy_double)) == NULL) {
237 g_string_free(mat->name, TRUE);
243 fprintf(stderr, "new material (%s):\n", i);
246 }else if(strcasecmp(line, "bow") == 0) {
247 insert_into_matdb(mdb, &mat, &bow);
248 if((bow = (struct matdb_bowing*)malloc(sizeof(struct matdb_bowing))) == NULL) {
251 if((to = index(i, ':')) == NULL) {
258 /*Same trick as before, but i now stores the from material,
259 and to the to material
261 if((bow->from = g_string_new(i)) == NULL) {
267 if((bow->to = g_string_new(to)) == NULL) {
269 g_string_free(bow->from, TRUE);
274 if((bow->properties = g_hash_table_new_full(&g_str_hash, &g_str_equal, &destroy_string, &destroy_double)) == NULL) {
276 g_string_free(bow->to, TRUE);
277 g_string_free(bow->from, TRUE);
283 fprintf(stderr, "new bowing (%s:%s):\n", i, to);
288 fprintf(stderr, "\t%d/%s\t", section, line);
290 /*Process a property.*/
294 fprintf(stderr, "(1)\t");
296 ht = mat->properties;
300 fprintf(stderr, "(2)\t");
302 ht = bow->properties;
306 fprintf(stderr, "(default)\t");
313 fprintf(stderr, "section was 0\n");
317 if((value = (double*)malloc(sizeof(double))) == NULL) {
320 fprintf(stderr, "malloc of value failed\n");
325 if((num=sscanf(i, " %lg", value)) != 1) {
329 fprintf(stderr, "bad sscanf to get value (scanned \"%s\", returned %d)\n", i, num);
333 g_hash_table_insert(ht, g_strdup(line), value);
335 fprintf(stderr, "%g(%s)\n", *value, line);
343 insert_into_matdb(mdb, &mat, &bow);