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/>.
28 #include "matdb-dotcode.h"
37 /*Functions to be used by the hash.*/
38 static void destroy_string(gpointer data) {
41 static void destroy_double(gpointer data){
44 static void destroy_inner_bowhash(gpointer data) {
45 g_hash_table_unref(data);
48 int insert_into_matdb(struct matdb *mdb, struct matdb_material **mat, struct matdb_bowing **bow) {
51 fprintf(stderr, "inserting material %s\n", (*mat)->name->str);
53 g_hash_table_insert(mdb->materials, (gpointer)g_strdup((*mat)->name->str), (gpointer)(*mat));
58 fprintf(stderr, "inserting bowing %s:%s\n", (*bow)->from->str, (*bow)->to->str);
62 if((it=g_hash_table_lookup(mdb->bowings, (*bow)->from->str)) == NULL) {
63 if((it = g_hash_table_new_full(&g_str_hash, &g_str_equal, &destroy_string, &destroy_inner_bowhash)) == NULL) {
67 g_hash_table_insert(mdb->bowings, (*bow)->from->str, it);
69 fprintf(stderr, "Got new hash table (from=%s)\n", (*bow)->from->str);
72 g_hash_table_insert(it, (gpointer)g_strdup((*bow)->to->str), (gpointer)(*bow));
78 /*Removes leading and trailing whitespace, comments, and reduces
79 redundant whitespace down to one tab, makes an empty line an empty string.*/
80 static void prettify_line(char *line) {
83 register int in_whitespace;
84 register int leading_whitespace;
85 for(look=write=line, in_whitespace=0, leading_whitespace=1; *look != '\0'; look++) {
87 fprintf(stderr, " look=%d(%c) write=%d(%c) in_whitespace=%d leading_whitespace=%d: ", (look-line), *look, (write-line), *write, in_whitespace, leading_whitespace);
91 fprintf(stderr, "comment\n");
101 fprintf(stderr, "whitespace\n");
103 if((!leading_whitespace) && (!in_whitespace)) *write++ = '\t';
108 fprintf(stderr, "default\n");
111 in_whitespace=leading_whitespace=0;
115 for(look=write=line; *look != '\0'; look++) {
120 if(*(look+1) == '\0') {
129 * 1: failure malloc'ing / initializing mdb struct
130 * 2: failure opening file.
131 * 4: failure reading line.
132 * 8: warning reading line: no tab separator (line parsing error)
133 * 16: warning reading line: bad section declaration
134 * 32: warning reading line: unable to allocate material struct.
135 * 64: warning reading line: ignored line (unallocated material struct)
136 * 128: warning reading line: unable to allocate bowing struct
137 * 256: warning reading line: ignored line (unallocated bowing struct)
138 * 512: warning reading file: improper section detected (programming
140 *1024: warning reading file: no : separator in bowing materials
142 *2048: warning reading file: unable to read numeric value for property.
144 struct matdb* read_matdb_dotcode(const GString *name, int* err) {
146 fprintf(stderr, "in read_matdb_dotcode(%s, %d)\n", name->str, *err);
149 struct matdb *mdb = (struct matdb*)malloc(sizeof(struct matdb));
150 if(mdb == NULL) {*err = 1; return NULL;}
153 if((mdb->materials = g_hash_table_new_full(&g_str_hash, &g_str_equal, &destroy_string, &destroy_material_gpointer)) == NULL) {
158 if((mdb->bowings = g_hash_table_new_full(&g_str_hash, &g_str_equal, &destroy_string, &destroy_bowing_gpointer)) == NULL) {
160 g_hash_table_unref(mdb->materials);
164 struct matdb_material *mat = NULL;
165 struct matdb_bowing *bow = NULL;
167 * 0 (no/global section)
174 FILE *infile = fopen(name->str, "r");
176 g_hash_table_unref(mdb->materials);
177 g_hash_table_unref(mdb->bowings);
183 fprintf(stderr, "infile=%x\n", (unsigned int)infile);
187 while(!feof(infile)) {
189 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:"");
192 if((val = getline(&line, &n, infile)) == -1) {
194 fprintf(stderr, "getline returned %d\n", val);
196 if(!feof(infile)) *err = 4;
201 fprintf(stderr, "line=(%s)\n", line);
205 fprintf(stderr, "%d: prettified line=(%s)\n", section, line);
211 char *i = index(line, '\t');
216 /*At this point, we have line which stores the first word on the
217 line, and i which stores the second word on the line.
222 fprintf(stderr, "part_a=(%s) part_b=(%s)\n", line, i);
224 /*If we have a material or bowing underway, save it off*/
225 if(strcasecmp(line, "material") == 0) {
226 insert_into_matdb(mdb, &mat, &bow);
227 if((mat = (struct matdb_material*)malloc(sizeof(struct matdb_material))) == NULL) {
230 if((mat->name = g_string_new(i)) == NULL) {
236 if((mat->properties = g_hash_table_new_full(&g_str_hash, &g_str_equal, &destroy_string, &destroy_double)) == NULL) {
238 g_string_free(mat->name, TRUE);
244 fprintf(stderr, "new material (%s):\n", i);
247 }else if(strcasecmp(line, "bow") == 0) {
248 insert_into_matdb(mdb, &mat, &bow);
249 if((bow = (struct matdb_bowing*)malloc(sizeof(struct matdb_bowing))) == NULL) {
252 if((to = index(i, ':')) == NULL) {
259 /*Same trick as before, but i now stores the from material,
260 and to the to material
262 if((bow->from = g_string_new(i)) == NULL) {
268 if((bow->to = g_string_new(to)) == NULL) {
270 g_string_free(bow->from, TRUE);
275 if((bow->properties = g_hash_table_new_full(&g_str_hash, &g_str_equal, &destroy_string, &destroy_double)) == NULL) {
277 g_string_free(bow->to, TRUE);
278 g_string_free(bow->from, TRUE);
284 fprintf(stderr, "new bowing (%s:%s):\n", i, to);
289 fprintf(stderr, "\t%d/%s\t", section, line);
291 /*Process a property.*/
295 fprintf(stderr, "(1)\t");
297 ht = mat->properties;
301 fprintf(stderr, "(2)\t");
303 ht = bow->properties;
307 fprintf(stderr, "(default)\t");
314 fprintf(stderr, "section was 0\n");
318 if((value = (double*)malloc(sizeof(double))) == NULL) {
321 fprintf(stderr, "malloc of value failed\n");
326 if((num=sscanf(i, " %lg", value)) != 1) {
330 fprintf(stderr, "bad sscanf to get value (scanned \"%s\", returned %d)\n", i, num);
334 g_hash_table_insert(ht, g_strdup(line), value);
336 fprintf(stderr, "%g(%s)\n", *value, line);
344 insert_into_matdb(mdb, &mat, &bow);