diff options
Diffstat (limited to '')
-rw-r--r-- | src/others/mimesh/libg3d-0.0.8/plugins/import/imp_dae/imp_dae_xml.c | 257 |
1 files changed, 257 insertions, 0 deletions
diff --git a/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_dae/imp_dae_xml.c b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_dae/imp_dae_xml.c new file mode 100644 index 0000000..a67be0d --- /dev/null +++ b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_dae/imp_dae_xml.c | |||
@@ -0,0 +1,257 @@ | |||
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 | #include <string.h> | ||
24 | #include <ctype.h> | ||
25 | |||
26 | #include <g3d/context.h> | ||
27 | #include <g3d/debug.h> | ||
28 | |||
29 | #include <glib.h> | ||
30 | #include <libxml/tree.h> | ||
31 | |||
32 | #include "imp_dae_library.h" | ||
33 | #include "imp_dae_xml.h" | ||
34 | #include "imp_dae_cb.h" | ||
35 | |||
36 | gchar *dae_xml_get_attr(xmlNodePtr node, const gchar *attrname) | ||
37 | { | ||
38 | xmlAttrPtr attr; | ||
39 | gchar *val = NULL; | ||
40 | |||
41 | g_return_val_if_fail(node != NULL, NULL); | ||
42 | g_return_val_if_fail(attrname != NULL, NULL); | ||
43 | |||
44 | attr = node->properties; | ||
45 | while(attr != NULL) { | ||
46 | if(xmlStrcmp(attr->name, (xmlChar *)attrname) == 0) { | ||
47 | val = g_strdup((gchar *)attr->children->content); | ||
48 | break; | ||
49 | } | ||
50 | attr = attr->next; | ||
51 | } | ||
52 | |||
53 | return val; | ||
54 | } | ||
55 | |||
56 | xmlNodePtr dae_xml_get_child_by_tagname(xmlNodePtr parent, const gchar *tag) | ||
57 | { | ||
58 | xmlNodePtr node; | ||
59 | |||
60 | g_return_val_if_fail(parent != NULL, NULL); | ||
61 | |||
62 | node = parent->children; | ||
63 | while(node != NULL) { | ||
64 | if((node->type == XML_ELEMENT_NODE) && | ||
65 | (xmlStrcmp(node->name, (const xmlChar *)tag) == 0)) { | ||
66 | return node; | ||
67 | } | ||
68 | node = node->next; | ||
69 | } | ||
70 | return NULL; | ||
71 | } | ||
72 | |||
73 | xmlNodePtr dae_xml_next_child_by_tagname(xmlNodePtr parent, xmlNodePtr *node, | ||
74 | gchar *nodename) | ||
75 | { | ||
76 | xmlNodePtr tmpnode; | ||
77 | gchar *name; | ||
78 | |||
79 | do { | ||
80 | tmpnode = dae_xml_next_child(NULL, parent, node, NULL, &name); | ||
81 | if(tmpnode == NULL) | ||
82 | return NULL; | ||
83 | if(strcmp(name, nodename) == 0) { | ||
84 | g_free(name); | ||
85 | return tmpnode; | ||
86 | } | ||
87 | g_free(name); | ||
88 | } while(1); | ||
89 | } | ||
90 | |||
91 | xmlNodePtr dae_xml_next_child(DaeLibrary *lib, xmlNodePtr parent, | ||
92 | xmlNodePtr *node, xmlNodePtr *instance, gchar **nodename) | ||
93 | { | ||
94 | gchar *url, *name; | ||
95 | |||
96 | g_return_val_if_fail(node != NULL, NULL); | ||
97 | g_return_val_if_fail(nodename != NULL, NULL); | ||
98 | *nodename = NULL; | ||
99 | |||
100 | if(*node == NULL) | ||
101 | *node = parent->children; | ||
102 | else if(instance && *instance) | ||
103 | *node = (*instance)->next; | ||
104 | else | ||
105 | *node = (*node)->next; | ||
106 | |||
107 | if(instance) | ||
108 | *instance = NULL; | ||
109 | |||
110 | /* skip TEXT nodes */ | ||
111 | while(*node && ((*node)->type != XML_ELEMENT_NODE)) | ||
112 | *node = (*node)->next; | ||
113 | |||
114 | if(*node == NULL) | ||
115 | return NULL; | ||
116 | |||
117 | if(lib && instance && | ||
118 | (xmlStrncmp((*node)->name, (xmlChar *)"instance_", 9) == 0)) { | ||
119 | url = dae_xml_get_attr(*node, "url"); | ||
120 | if(url) { | ||
121 | /* skip 'instance_' part of node name */ | ||
122 | name = g_strdup((gchar *)((*node)->name + 9)); | ||
123 | #if DEBUG > 1 | ||
124 | g_debug("DAE: looking up '%s' in '%s'", url + 1, name); | ||
125 | #endif | ||
126 | *instance = *node; | ||
127 | *node = dae_library_lookup(lib, name, url + 1 /* skip '#' */); | ||
128 | g_free(url); | ||
129 | g_free(name); | ||
130 | } | ||
131 | } | ||
132 | if(*node == NULL) | ||
133 | return NULL; | ||
134 | |||
135 | *nodename = g_strdup((gchar *)(*node)->name); | ||
136 | |||
137 | return *node; | ||
138 | } | ||
139 | |||
140 | gboolean dae_xml_parse(DaeGlobalData *global, xmlNodePtr parent, | ||
141 | DaeChunkDesc *chunks, guint32 level, gpointer user_data) | ||
142 | { | ||
143 | DaeLocalData *local; | ||
144 | xmlNodePtr node = NULL, instance = NULL; | ||
145 | gchar *name; | ||
146 | gint i; | ||
147 | |||
148 | g_return_val_if_fail(parent != NULL, FALSE); | ||
149 | |||
150 | while(dae_xml_next_child(global->lib, parent, &node, &instance, &name)) { | ||
151 | #if DEBUG > 0 | ||
152 | g_debug("\\%s<%s>", debug_pad(level), name); | ||
153 | #endif | ||
154 | if(chunks) { | ||
155 | local = g_new0(DaeLocalData, 1); | ||
156 | local->parent = parent; | ||
157 | local->node = node; | ||
158 | local->instance = instance; | ||
159 | local->level = level + 1; | ||
160 | local->user_data = user_data; | ||
161 | |||
162 | /* find callback */ | ||
163 | for(i = 0; chunks[i].name != NULL; i ++) { | ||
164 | if(strcmp(chunks[i].name, name) == 0) { | ||
165 | /* found chunk */ | ||
166 | #if DEBUG > 3 | ||
167 | g_debug("DAE: found chunk description for '%s'", name); | ||
168 | #endif | ||
169 | if(chunks[i].callback) | ||
170 | chunks[i].callback(global, local); | ||
171 | break; | ||
172 | } | ||
173 | } | ||
174 | |||
175 | g_free(local); | ||
176 | |||
177 | if(chunks[i].name == NULL) | ||
178 | g_debug("DAE: unhandled chunk '%s' in '%s'", name, | ||
179 | (gchar *)parent->name); | ||
180 | } | ||
181 | |||
182 | g_free(name); | ||
183 | g3d_context_update_interface(global->context); | ||
184 | } | ||
185 | |||
186 | return TRUE; | ||
187 | } | ||
188 | |||
189 | gboolean dae_xml_next_int(xmlNodePtr node, gchar **nextp, gint *i) | ||
190 | { | ||
191 | gchar *s, *err = NULL; | ||
192 | |||
193 | s = *nextp; | ||
194 | |||
195 | if(s == NULL) | ||
196 | s = (gchar *)node->children->content; | ||
197 | |||
198 | /* skip leading whitespace */ | ||
199 | while(isspace(*s)) | ||
200 | s ++; | ||
201 | |||
202 | *i = strtol(s, &err, 0); | ||
203 | if(s == err) { | ||
204 | g_debug("DAE: imp_xml_next_int: error at '%.*s...'", 5, s); | ||
205 | return FALSE; | ||
206 | } | ||
207 | |||
208 | *nextp = err; | ||
209 | return TRUE; | ||
210 | } | ||
211 | |||
212 | gboolean dae_xml_next_double(xmlNodePtr node, gchar **nextp, GLdouble *d) | ||
213 | { | ||
214 | gchar *s, *err = NULL; | ||
215 | |||
216 | s = *nextp; | ||
217 | |||
218 | if(s == NULL) | ||
219 | s = (gchar *)node->children->content; | ||
220 | |||
221 | /* skip leading whitespace */ | ||
222 | while(isspace(*s)) | ||
223 | s ++; | ||
224 | |||
225 | *d = strtod(s, &err); | ||
226 | if(s == err) { | ||
227 | g_debug("DAE: imp_xml_next_float: error at '%.*s...'", 5, s); | ||
228 | return FALSE; | ||
229 | } | ||
230 | |||
231 | *nextp = err; | ||
232 | return TRUE; | ||
233 | } | ||
234 | |||
235 | gboolean dae_xml_next_float(xmlNodePtr node, gchar **nextp, GLfloat *f) | ||
236 | { | ||
237 | gchar *s, *err = NULL; | ||
238 | |||
239 | s = *nextp; | ||
240 | |||
241 | if(s == NULL) | ||
242 | s = (gchar *)node->children->content; | ||
243 | |||
244 | /* skip leading whitespace */ | ||
245 | while(isspace(*s)) | ||
246 | s ++; | ||
247 | |||
248 | *f = strtof(s, &err); | ||
249 | if(s == err) { | ||
250 | g_debug("DAE: imp_xml_next_float: error at '%.*s...'", 5, s); | ||
251 | return FALSE; | ||
252 | } | ||
253 | |||
254 | *nextp = err; | ||
255 | return TRUE; | ||
256 | } | ||
257 | |||