aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_ar/imp_ar_carini.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/others/mimesh/libg3d-0.0.8/plugins/import/imp_ar/imp_ar_carini.c')
-rw-r--r--src/others/mimesh/libg3d-0.0.8/plugins/import/imp_ar/imp_ar_carini.c247
1 files changed, 247 insertions, 0 deletions
diff --git a/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_ar/imp_ar_carini.c b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_ar/imp_ar_carini.c
new file mode 100644
index 0000000..8ff4b18
--- /dev/null
+++ b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_ar/imp_ar_carini.c
@@ -0,0 +1,247 @@
1/* $Id$ */
2
3/*
4 libg3d - 3D object loading library
5
6 Copyright (C) 2005-2009 Markus Dahms <mad@automagically.de>
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library 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 GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21*/
22
23#define _ISOC99_SOURCE 1 /* strtof() */
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <glib.h>
29#include <g3d/types.h>
30
31#define CARINI_IN_VAR 1
32#define CARINI_IN_VALUE 2
33#define CARINI_IN_COMMENT 3
34
35static gchar *ar_carini_getpath(GQueue *queue)
36{
37 gchar *tmp, *path = NULL;
38 gint32 i;
39
40 for(i = 0; i < g_queue_get_length(queue); i ++)
41 {
42 if(path == NULL)
43 path = g_strdup((gchar *)g_queue_peek_nth(queue, i));
44 else
45 {
46 tmp = g_strdup_printf("%s.%s",
47 (gchar *)g_queue_peek_nth(queue, i), path);
48 g_free(path);
49 path = tmp;
50 }
51 }
52
53 return path;
54}
55
56GHashTable *ar_carini_load(void)
57{
58 GHashTable *ht;
59 GQueue *q;
60 FILE *f;
61 gchar var[256], *varp; /* section or variable */
62 gchar val[256], *valp; /* value */
63 gchar *tmps, *varname;
64 guint32 in = CARINI_IN_VAR;
65 gint32 c;
66
67 f = fopen("car.ini", "r");
68 if(f == NULL)
69 f = fopen("Car.ini", "r");
70
71 if(f == NULL)
72 {
73 g_error("failed to read 'car.ini'\n");
74 return NULL;
75 }
76
77 ht = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
78 if(ht == NULL)
79 {
80 fclose(f);
81 g_error("could not create hash table");
82 return NULL;
83 }
84
85 q = g_queue_new();
86
87 valp = val;
88 memset(val, 0, 256);
89 varp = var;
90 memset(var, 0, 256);
91
92 while(!feof(f))
93 {
94 c = fgetc(f);
95 switch(c)
96 {
97 case '\0':
98 case EOF:
99 break;
100
101 case '{':
102 /* push section */
103 g_queue_push_head(q, g_strdup(var));
104
105 in = CARINI_IN_VAR;
106 varp = var;
107 memset(var, 0, 256);
108 break;
109
110 case '}':
111 /* pop section */
112 tmps = (gchar *)g_queue_pop_head(q);
113 g_free(tmps);
114
115 in = CARINI_IN_VAR;
116 varp = var;
117 memset(var, 0, 256);
118 break;
119
120 case ';':
121 in = CARINI_IN_COMMENT;
122 break;
123
124 case '=':
125 if(in == CARINI_IN_VAR)
126 {
127 in = CARINI_IN_VALUE;
128 valp = val;
129 memset(val, 0, 256);
130 }
131 break;
132
133 case '\n':
134 case '\r':
135 if(in == CARINI_IN_VALUE)
136 {
137 tmps = ar_carini_getpath(q);
138 if(tmps)
139 {
140 varname = g_strdup_printf("%s.%s", tmps, var);
141 g_free(tmps);
142 }
143 else
144 varname = g_strdup(var);
145
146 /* update hash table */
147 g_hash_table_insert(ht, varname, g_strdup(val));
148#if DEBUG > 2
149 printf("D: %s = %s\n", varname, val);
150#endif
151
152 in = CARINI_IN_VAR;
153 varp = var;
154 memset(var, 0, 256);
155 }
156 else if(in == CARINI_IN_COMMENT)
157 {
158 in = CARINI_IN_VAR;
159 }
160 break;
161
162 case ' ':
163 case '\t':
164 if(in == CARINI_IN_VALUE)
165 {
166 *valp = c;
167 valp ++;
168 }
169 break;
170
171 default:
172 if(in == CARINI_IN_VALUE)
173 {
174 *valp = c;
175 valp ++;
176 }
177 else if(in == CARINI_IN_VAR)
178 {
179 *varp = c;
180 varp ++;
181 }
182 break;
183 } /* switch(c) */
184 } /* !feof(f) */
185
186 /* clean up */
187 g_queue_free(q);
188
189 return ht;
190}
191
192void ar_carini_free(GHashTable *ht)
193{
194 g_hash_table_destroy(ht);
195}
196
197G3DFloat ar_carini_get_float(GHashTable *ht, gchar *path)
198{
199 gchar *value;
200 G3DFloat valf;
201
202 value = (gchar *)g_hash_table_lookup(ht, path);
203 if(value == NULL)
204 return 0.0;
205
206 valf = strtof(value, NULL);
207 return valf;
208}
209
210gboolean ar_carini_get_position(GHashTable *ht, gchar *prefix,
211 G3DFloat *x, G3DFloat *y, G3DFloat *z)
212{
213 gchar *path, *value;
214
215 /* x */
216 path = g_strdup_printf("%s.x", prefix);
217 value = (gchar *)g_hash_table_lookup(ht, path);
218 if(value)
219 *x = strtof(value, NULL);
220 else
221 *x = 0.0;
222 g_free(path);
223
224 /* y */
225 path = g_strdup_printf("%s.y", prefix);
226 value = (gchar *)g_hash_table_lookup(ht, path);
227 if(value)
228 *y = strtof(value, NULL);
229 else
230 *y = 0.0;
231 g_free(path);
232
233 /* z */
234 path = g_strdup_printf("%s.z", prefix);
235 value = (gchar *)g_hash_table_lookup(ht, path);
236 if(value)
237 *z = strtof(value, NULL);
238 else
239 *z = 0.0;
240 g_free(path);
241
242#if DEBUG > 0
243 printf("D: position for '%s': %.2f, %.2f, %.2f\n", prefix, *x, *y, *z);
244#endif
245
246 return TRUE;
247}