author | harlekin <harlekin> | 2002-02-15 16:25:32 (UTC) |
---|---|---|
committer | harlekin <harlekin> | 2002-02-15 16:25:32 (UTC) |
commit | 9847b3a997f504e82ff391efe6304d3bb110b617 (patch) (unidiff) | |
tree | 7b28cb45598cba9f1fb595b75127a0b9b4787d2e /core/pim/today/minidom.c | |
parent | 453278dfcc3f9c96862cb69aed21df2597718d2d (diff) | |
download | opie-9847b3a997f504e82ff391efe6304d3bb110b617.zip opie-9847b3a997f504e82ff391efe6304d3bb110b617.tar.gz opie-9847b3a997f504e82ff391efe6304d3bb110b617.tar.bz2 |
initial import
-rw-r--r-- | core/pim/today/minidom.c | 446 |
1 files changed, 446 insertions, 0 deletions
diff --git a/core/pim/today/minidom.c b/core/pim/today/minidom.c new file mode 100644 index 0000000..4155f48 --- a/dev/null +++ b/core/pim/today/minidom.c | |||
@@ -0,0 +1,446 @@ | |||
1 | /* | ||
2 | * The GGZ Gaming Zone Metaserver Project | ||
3 | * Copyright (C) 2001 Josef Spillner, dr_maux@users.sourceforge.net | ||
4 | * Published under GNU GPL conditions. | ||
5 | */ | ||
6 | |||
7 | #include <stdio.h> | ||
8 | #include <string.h> | ||
9 | #include <stdlib.h> | ||
10 | #include "minidom.h" | ||
11 | |||
12 | /* Remove all line breaks, spaces, tabs, and check/remove XML header */ | ||
13 | char *minidom_cleanstream(const char *stream) | ||
14 | { | ||
15 | static char *cs = NULL; | ||
16 | unsigned int i, j; | ||
17 | int inside; | ||
18 | int spaceprotect; | ||
19 | int spacesonly; | ||
20 | |||
21 | if(!stream) return NULL; | ||
22 | if(cs) | ||
23 | { | ||
24 | free(cs); | ||
25 | cs = NULL; | ||
26 | } | ||
27 | cs = (char*)malloc(strlen(stream) + 1); | ||
28 | |||
29 | j = 0; | ||
30 | inside = 0; | ||
31 | spaceprotect = 0; | ||
32 | spacesonly = 0; | ||
33 | for(i = 0; i < strlen(stream); i++) | ||
34 | { | ||
35 | if(stream[i] == '\t') continue; | ||
36 | if(stream[i] == '\n') | ||
37 | { | ||
38 | if(inside) | ||
39 | { | ||
40 | cs[j] = ' '; | ||
41 | j++; | ||
42 | } | ||
43 | continue; | ||
44 | } | ||
45 | if((!inside) && (!spaceprotect) && (stream[i] == ' ')) continue; | ||
46 | if((stream[i] != ' ') && (stream[i] != '<') && (spacesonly)) spacesonly = 0; | ||
47 | if(stream[i] == '<') | ||
48 | { | ||
49 | if(spacesonly) j = spacesonly; | ||
50 | spacesonly = 0; | ||
51 | inside = 1; | ||
52 | if(stream[i + 1] == '?') inside = 2; | ||
53 | spaceprotect = 0; | ||
54 | if(stream[i + 1] == '/') spaceprotect = -1; | ||
55 | } | ||
56 | if(inside != 2) | ||
57 | { | ||
58 | cs[j] = stream[i]; | ||
59 | j++; | ||
60 | } | ||
61 | if(stream[i] == '>') | ||
62 | { | ||
63 | inside = 0; | ||
64 | spaceprotect++; /* 1 on opening tag, 0 on closing tag */ | ||
65 | if(stream[i - 1] == '/') spaceprotect = 0; | ||
66 | spacesonly = j; | ||
67 | } | ||
68 | } | ||
69 | cs[j] = 0; | ||
70 | |||
71 | /*printf("DEBUG: cleanstream: return %s\n", cs);*/ | ||
72 | return cs; | ||
73 | } | ||
74 | |||
75 | /* Return position of c or -1 if not found */ | ||
76 | int strpos(const char *s, char c) | ||
77 | { | ||
78 | unsigned int i; | ||
79 | |||
80 | if(!s) return -1; | ||
81 | for(i = 0; i < strlen(s); i++) | ||
82 | if(s[i] == c) return i; | ||
83 | return -1; | ||
84 | } | ||
85 | |||
86 | /* Add a complete tag to an element, return the new child */ | ||
87 | ELE *minidom_makechild(ELE *parent, char *tag) | ||
88 | { | ||
89 | char *token; | ||
90 | int i, j, k, l, len, size, count; | ||
91 | ELE *ele; | ||
92 | ATT *att; | ||
93 | int pos; | ||
94 | |||
95 | if(!tag) return parent; | ||
96 | |||
97 | ele = (ELE*)malloc(sizeof(ELE)); | ||
98 | ele->parent = parent; | ||
99 | ele->name = NULL; | ||
100 | ele->value = NULL; | ||
101 | ele->at = NULL; | ||
102 | ele->el = NULL; | ||
103 | ele->elnum = 0; | ||
104 | ele->atnum = 0; | ||
105 | |||
106 | if(parent) | ||
107 | { | ||
108 | parent->elnum++; | ||
109 | parent->el = (ELE**)realloc(parent->el, (parent->elnum + 1) * sizeof(ELE*)); | ||
110 | parent->el[parent->elnum - 1] = ele; | ||
111 | parent->el[parent->elnum] = NULL; | ||
112 | } | ||
113 | |||
114 | /*printf("TAG: %s\n", tag);*/ | ||
115 | i = 0; | ||
116 | k = 0; | ||
117 | l = 0; | ||
118 | count = 0; | ||
119 | token = strdup(tag); | ||
120 | len = strlen(tag); | ||
121 | for(j = 0; j < len; j++) | ||
122 | { | ||
123 | if(tag[j] == '\"') k++; | ||
124 | if(tag[j] == '\'') k++; | ||
125 | if(tag[j] == '=') l++; | ||
126 | if(((tag[j] == ' ') && ((k == 2) || (l == 0))) || (j == len - 1)) | ||
127 | { | ||
128 | size = j - i; | ||
129 | if(j == len - 1) size++; | ||
130 | strncpy(token, tag + i, size); | ||
131 | token[size] = 0; | ||
132 | /*printf("ATTRIBUTE: %s\n", token);*/ | ||
133 | if(count == 0) | ||
134 | { | ||
135 | /* name */ | ||
136 | /*printf(" ** %s\n", token);*/ | ||
137 | ele->name = (char*)malloc(strlen(token) + 1); | ||
138 | strcpy(ele->name, token); | ||
139 | } | ||
140 | else | ||
141 | { | ||
142 | att = (ATT*)malloc(sizeof(ATT)); | ||
143 | pos = strpos(token, '='); | ||
144 | if(pos == -1) | ||
145 | { | ||
146 | att->name = strdup(token); | ||
147 | att->value = NULL; | ||
148 | } | ||
149 | else | ||
150 | { | ||
151 | att->name = (char*)malloc(pos + 1); | ||
152 | att->value = (char*)malloc(strlen(token) - pos + 1); | ||
153 | memcpy(att->name, token, pos); | ||
154 | memcpy(att->value, token + pos + 1 + 1, strlen(token) - pos - 1 - 2); /* exclude "" marks */ | ||
155 | att->name[pos] = 0; | ||
156 | att->value[strlen(token) - pos - 1 - 2] = 0; | ||
157 | } | ||
158 | ele->atnum++; | ||
159 | ele->at = (ATT**)realloc(ele->at, (ele->atnum + 1) * sizeof(ATT*)); | ||
160 | ele->at[ele->atnum - 1] = att; | ||
161 | ele->at[ele->atnum] = NULL; | ||
162 | } | ||
163 | i = j + 1; | ||
164 | k = 0; | ||
165 | l = 0; | ||
166 | count++; | ||
167 | } | ||
168 | } | ||
169 | free(token); | ||
170 | |||
171 | return ele; | ||
172 | } | ||
173 | |||
174 | /* Parses a stream to add its contents to a DOM */ | ||
175 | DOM *minidom_parse(const char *stream) | ||
176 | { | ||
177 | DOM *dom; | ||
178 | char *cs; | ||
179 | unsigned int i; | ||
180 | int mark, lastmark; | ||
181 | char *token; | ||
182 | int error = 0; | ||
183 | ELE *ele, *cp; /* root node and current pointer */ | ||
184 | int endtag; | ||
185 | |||
186 | if(!stream) return NULL; | ||
187 | cs = minidom_cleanstream(stream); | ||
188 | |||
189 | dom = (DOM*)malloc(sizeof(DOM)); | ||
190 | dom->processed = 0; | ||
191 | dom->valid = 0; | ||
192 | dom->el = NULL; | ||
193 | |||
194 | /*ele = (ELE*)malloc(sizeof(ELE));*/ /* memory loss! */ | ||
195 | /*ele->parent = NULL; | ||
196 | ele->name = NULL; | ||
197 | ele->value = NULL; | ||
198 | ele->el = NULL; | ||
199 | ele->at = NULL; | ||
200 | ele->elnum = 0;*/ | ||
201 | ele = NULL; | ||
202 | cp = NULL; | ||
203 | token = NULL; | ||
204 | mark = -1; | ||
205 | error = 0; | ||
206 | lastmark = 0; | ||
207 | endtag = 0; | ||
208 | for(i = 0; i < strlen(cs); i++) | ||
209 | { | ||
210 | if(cs[i] == '<') | ||
211 | { | ||
212 | if(mark == -1) mark = i + 1; | ||
213 | else error = 1; | ||
214 | if((int)i != lastmark) | ||
215 | { | ||
216 | if(token) free(token); | ||
217 | token = (char*)malloc(i - lastmark + 1); | ||
218 | memcpy(token, cs + lastmark, i - lastmark); | ||
219 | token[i - lastmark] = 0; | ||
220 | /*printf(" --> content: %s\n", token);*/ | ||
221 | cp->value = (char*)malloc(strlen(token) + 1); | ||
222 | strcpy(cp->value, token); | ||
223 | } | ||
224 | } | ||
225 | if(cs[i] == '>') | ||
226 | { | ||
227 | if(mark != -1) | ||
228 | { | ||
229 | if(token) free(token); | ||
230 | token = (char*)malloc(i - mark + 1); | ||
231 | memcpy(token, cs + mark, i - mark); | ||
232 | token[i - mark] = 0; | ||
233 | /*printf("--> token: %s\n", token);*/ | ||
234 | if((token[0] == '/') && (cp)) cp = cp->parent; | ||
235 | else | ||
236 | { | ||
237 | if(token[i - mark - 1] == '/') | ||
238 | { | ||
239 | token[i - mark - 1] = 0; | ||
240 | /*cp = cp->parent;*/ | ||
241 | endtag = 1; | ||
242 | } | ||
243 | /*printf("INSERT AT: %i\n", cp);*/ | ||
244 | cp = minidom_makechild(cp, token); | ||
245 | if((cp) && (!ele)) ele = cp; | ||
246 | /*if(cp) cp = cp->parent;*/ /* QUICK HACK?! */ | ||
247 | if(endtag) | ||
248 | { | ||
249 | if(cp) cp = cp->parent; | ||
250 | endtag = 0; | ||
251 | } | ||
252 | } | ||
253 | mark = -1; | ||
254 | lastmark = i + 1; | ||
255 | } | ||
256 | else error = 1; | ||
257 | } | ||
258 | } | ||
259 | |||
260 | dom->valid = !error; | ||
261 | dom->processed = 1; | ||
262 | dom->el = ele; | ||
263 | |||
264 | return dom; | ||
265 | } | ||
266 | |||
267 | /* Loads an XML file and return the DOM */ | ||
268 | DOM *minidom_load(const char *file) | ||
269 | { | ||
270 | DOM *dom; | ||
271 | FILE *f; | ||
272 | char buf[1024]; | ||
273 | char *buffer; | ||
274 | |||
275 | f = fopen(file, "r"); | ||
276 | if(!f) return NULL; | ||
277 | |||
278 | buffer = (char*)malloc(1); | ||
279 | strcpy(buffer, ""); | ||
280 | while(fgets(buf, sizeof(buf), f)) | ||
281 | { | ||
282 | if(strlen(buf) > 0) | ||
283 | { | ||
284 | /*printf("Got: %s\n", buf);*/ | ||
285 | buffer = (char*)realloc(buffer, strlen(buffer) + strlen(buf) + 1); | ||
286 | /*buf[strlen(buf) - 1] = 0;*/ | ||
287 | /*printf("Add: %s", buf);*/ | ||
288 | strcat(buffer, buf); | ||
289 | } | ||
290 | } | ||
291 | fclose(f); | ||
292 | |||
293 | /*printf("DEBUG: ready!\n"); | ||
294 | printf("DEBUG: load: parse %s\n", buffer);*/ | ||
295 | dom = minidom_parse(buffer); | ||
296 | free(buffer); | ||
297 | |||
298 | return dom; | ||
299 | } | ||
300 | |||
301 | void minidom_internal_dump(ELE *ele); /* forward decl */ | ||
302 | |||
303 | /* Dump out the DOM in XML format */ | ||
304 | /* FIXME: return a char* and print this if dump is needed */ | ||
305 | void minidom_dump(DOM *dom) | ||
306 | { | ||
307 | if(!dom) return; | ||
308 | if(!dom->processed) | ||
309 | { | ||
310 | printf("ERROR: DOM is incomplete!\n"); | ||
311 | return; | ||
312 | } | ||
313 | if(!dom->valid) | ||
314 | { | ||
315 | printf("ERROR: DOM is invalid!\n"); | ||
316 | return; | ||
317 | } | ||
318 | if(!dom->el) | ||
319 | { | ||
320 | printf("ERROR: DOM is empty!\n");/* is this really an error? */ | ||
321 | return; | ||
322 | } | ||
323 | minidom_internal_dump(dom->el); | ||
324 | } | ||
325 | |||
326 | void minidom_internal_dump(ELE *ele) | ||
327 | { | ||
328 | int i; | ||
329 | static int indent = 0; | ||
330 | static int start = 0; | ||
331 | |||
332 | if(!ele) return; | ||
333 | if(!start) | ||
334 | { | ||
335 | start = 1; | ||
336 | printf("<?xml version=\"1.0\"?>\n"); | ||
337 | } | ||
338 | indent++; | ||
339 | for(i = 0; i < (indent - 1) * 2; i++) | ||
340 | printf(" "); | ||
341 | printf("<%s", ele->name); | ||
342 | |||
343 | i = 0; | ||
344 | while((ele->at) && (ele->at[i])) | ||
345 | { | ||
346 | if(ele->at[i]->value) | ||
347 | printf(" %s=\"%s\"", ele->at[i]->name, ele->at[i]->value); | ||
348 | else | ||
349 | printf(" %s", ele->at[i]->name); | ||
350 | i++; | ||
351 | } | ||
352 | |||
353 | if((!ele->value) && (!ele->el)) printf("/"); | ||
354 | printf(">\n"); | ||
355 | if(ele->value) | ||
356 | { | ||
357 | for(i = 0; i < (indent - 1) * 2; i++) | ||
358 | printf(" "); | ||
359 | printf(" %s\n", ele->value); | ||
360 | } | ||
361 | |||
362 | i = 0; | ||
363 | while((ele->el) && (ele->el[i])) | ||
364 | { | ||
365 | minidom_internal_dump(ele->el[i]); | ||
366 | i++; | ||
367 | } | ||
368 | |||
369 | if((ele->value) || (ele->el)) | ||
370 | { | ||
371 | for(i = 0; i < (indent -1) * 2; i++) | ||
372 | printf(" "); | ||
373 | printf("</%s>\n", ele->name); | ||
374 | } | ||
375 | indent--; | ||
376 | } | ||
377 | |||
378 | /* Clean up after all operations */ | ||
379 | void minidom_free(DOM *dom) | ||
380 | { | ||
381 | if(!dom) return; | ||
382 | free(dom); | ||
383 | dom = NULL; | ||
384 | } | ||
385 | |||
386 | /* Query a list of elements */ | ||
387 | ELE **MD_querylist(ELE *parent, const char *name) | ||
388 | { | ||
389 | static ELE **elelist = NULL; | ||
390 | int i, j; | ||
391 | |||
392 | if(!parent) return NULL; | ||
393 | /*if(elelist) | ||
394 | { | ||
395 | i = 0; | ||
396 | while(elelist[i]) | ||
397 | { | ||
398 | free(elelist[i]); | ||
399 | i++; | ||
400 | } | ||
401 | free(elelist); | ||
402 | elelist = NULL; | ||
403 | }*/ /* MEMORY HOLE !*/ | ||
404 | |||
405 | i = 0; | ||
406 | j = 1; | ||
407 | while((parent->el) && (parent->el[i])) | ||
408 | { | ||
409 | if(!strcmp(parent->el[i]->name, name)) | ||
410 | { | ||
411 | elelist = (ELE**)malloc((j + 1) * sizeof(ELE*)); | ||
412 | elelist[j - 1] = parent->el[i]; | ||
413 | elelist[j] = NULL; | ||
414 | j++; | ||
415 | } | ||
416 | i++; | ||
417 | } | ||
418 | |||
419 | return elelist; | ||
420 | } | ||
421 | |||
422 | /* Query a single element */ | ||
423 | ELE *MD_query(ELE *parent, const char *name) | ||
424 | { | ||
425 | ELE **elelist; | ||
426 | |||
427 | elelist = MD_querylist(parent, name); | ||
428 | if((elelist) && (elelist[0])) return elelist[0]; | ||
429 | return NULL; | ||
430 | } | ||
431 | |||
432 | /* Get an attribute's value */ | ||
433 | char *MD_att(ELE *element, const char *name) | ||
434 | { | ||
435 | int i; | ||
436 | |||
437 | if((!element) || (!element->at)) return NULL; | ||
438 | i = 0; | ||
439 | while((element->at[i])) | ||
440 | { | ||
441 | if(!strcmp(element->at[i]->name, name)) return element->at[i]->value; | ||
442 | i++; | ||
443 | } | ||
444 | return NULL; | ||
445 | } | ||
446 | |||