* configure.ac:
[scdataviz] / src / 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 <stdio.h>
24 #include <stdlib.h>
25 #include "matdb.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_inner_bowing(gpointer key, gpointer value, gpointer user_data) {
31   struct matdb_bowing *bowing = (struct matdb_bowing*)value;
32   fprintf(stderr, "\tbowing %s:%s[%s:%s]:\n", bowing->from->str, bowing->to->str, (char*)user_data, (char*)key);
33   g_hash_table_foreach(bowing->properties, &print_property, NULL);
34 }
35 static void print_material(gpointer key, gpointer value, gpointer user_data) {
36   struct matdb_material *mat = (struct matdb_material*)value;
37   fprintf(stderr, "\tmaterial %s(%s):\n", mat->name->str, (char*)key);
38   g_hash_table_foreach(mat->properties, &print_property, NULL);
39 }
40 static void print_bowing(gpointer key, gpointer value, gpointer user_data) {
41   GHashTable *ht = value;
42   g_hash_table_foreach(ht, &print_inner_bowing, key);
43 }
44
45 void print_matdb(const struct matdb *mdb) {
46   fprintf(stderr, "matdb:\n");
47   g_hash_table_foreach(mdb->materials, &print_material, NULL);
48   g_hash_table_foreach(mdb->bowings, &print_bowing, NULL);
49 }
50
51 void destroy_material_gpointer(gpointer data) {
52   destroy_material((struct matdb_material *)data);
53 }
54 void destroy_bowing_gpointer(gpointer data) {
55   destroy_bowing((struct matdb_bowing *)data);
56 }
57 void destroy_material(struct matdb_material *mat) {
58   #ifdef DEBUG
59   fprintf(stderr, "destroy_material\n");
60   #endif
61   g_string_free(mat->name, TRUE);
62   g_hash_table_unref(mat->properties);
63   free(mat);
64 }
65 void destroy_bowing(struct matdb_bowing *bow) {
66   #ifdef DEBUG
67   fprintf(stderr, "destroy_bowing\n");
68   #endif
69   g_string_free(bow->from, TRUE);
70   g_string_free(bow->to, TRUE);
71   g_hash_table_unref(bow->properties);
72   free(bow);
73 }