995074c642a47001b033637779a39b7923a3d248
[scdataviz] / matdb.c
1 /*
2  * matdb: generic materials database information.
3
4
5
6     Copyright (C) 2008 Joseph Pingenot
7
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.
12
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.
17
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/>.
20
21 */
22
23 #include <matdb.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 static void print_property(gpointer key, gpointer value, gpointer user_data) {
28   fprintf(stderr, "\t\t%s=%g:\n", (char*)key, *(double*)value);
29 }
30 static void print_material(gpointer key, gpointer value, gpointer user_data) {
31   struct matdb_material *mat = (struct matdb_material*)value;
32   fprintf(stderr, "\tmaterial %s(%s):\n", mat->name->str, (char*)key);
33   g_hash_table_foreach(mat->properties, print_property, NULL);
34 }
35 static void print_bowing(gpointer key, gpointer value, gpointer user_data) {
36   struct matdb_bowing *bowing = (struct matdb_bowing*)value;
37   fprintf(stderr, "\tbowing %s:%s(%s):\n", bowing->from->str, bowing->to->str, (char*)key);
38   g_hash_table_foreach(bowing->properties, print_property, NULL);
39 }
40
41 void print_matdb(const struct matdb *mdb) {
42   fprintf(stderr, "matdb:\n");
43   g_hash_table_foreach(mdb->materials, &print_material, NULL);
44   g_hash_table_foreach(mdb->bowings, &print_bowing, NULL);
45 }
46
47 void destroy_material_gpointer(gpointer data) {
48   destroy_material((struct matdb_material *)data);
49 }
50 void destroy_bowing_gpointer(gpointer data) {
51   destroy_bowing((struct matdb_bowing *)data);
52 }
53 void destroy_material(struct matdb_material *mat) {
54   #ifdef DEBUG
55   fprintf(stderr, "destroy_material\n");
56   #endif
57   g_string_free(mat->name, TRUE);
58   g_hash_table_unref(mat->properties);
59   free(mat);
60 }
61 void destroy_bowing(struct matdb_bowing *bow) {
62   #ifdef DEBUG
63   fprintf(stderr, "destroy_bowing\n");
64   #endif
65   g_string_free(bow->from, TRUE);
66   g_string_free(bow->to, TRUE);
67   g_hash_table_unref(bow->properties);
68   free(bow);
69 }