/[zanavi_public1]/navit/navit/xmlconfig.c
ZANavi

Contents of /navit/navit/xmlconfig.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 30 - (show annotations) (download)
Wed Aug 22 17:01:27 2012 UTC (11 years, 8 months ago) by zoff99
File MIME type: text/plain
File size: 33517 byte(s)
ZANavi 2.0, lots of changes, everything in its own thread, more performance
1 /**
2 * ZANavi, Zoff Android Navigation system.
3 * Copyright (C) 2011-2012 Zoff <zoff@zoff.cc>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 /**
21 * Navit, a modular navigation system.
22 * Copyright (C) 2005-2009 Navit Team
23 *
24 * This program is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU General Public License
26 * version 2 as published by the Free Software Foundation.
27 *
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the
35 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
36 * Boston, MA 02110-1301, USA.
37 */
38
39 /* see http://library.gnome.org/devel/glib/stable/glib-Simple-XML-Subset-Parser.html
40 * for details on how the xml file parser works.
41 */
42
43 #include <stdlib.h>
44 #include <glib.h>
45 #include <glib/gprintf.h>
46 #include <string.h>
47 #include <ctype.h>
48 #include "debug.h"
49 #include "config.h"
50 #include "file.h"
51 #include "coord.h"
52 #include "layout.h"
53 #include "mapset.h"
54 #include "projection.h"
55 #include "map.h"
56 #include "navigation.h"
57 #include "navit.h"
58 #include "plugin.h"
59 #include "route.h"
60 #include "speech.h"
61 #include "track.h"
62 #include "vehicle.h"
63 #include "point.h"
64 #include "graphics.h"
65 #include "gui.h"
66 #include "osd.h"
67 #include "log.h"
68 #include "announcement.h"
69 #include "vehicleprofile.h"
70 #include "roadprofile.h"
71 #include "config_.h"
72 #include "xmlconfig.h"
73
74 #ifdef HAVE_GLIB
75 #define ATTR_DISTANCE 1
76 const int xml_attr_distance=1;
77 #else
78 #include "ezxml.h"
79 const int xml_attr_distance = 2;
80 #define ATTR_DISTANCE 2
81 #define G_MARKUP_ERROR 0
82 #define G_MARKUP_ERROR_INVALID_CONTENT 0
83 #define G_MARKUP_ERROR_PARSE 0
84 #define G_MARKUP_ERROR_UNKNOWN_ELEMENT 0
85 typedef void * GMarkupParseContext;
86 #endif
87
88 struct xistate
89 {
90 struct xistate *parent;
91 struct xistate *child;
92 const gchar *element;
93 const gchar **attribute_names;
94 const gchar **attribute_values;
95 };
96
97 struct xmldocument
98 {
99 const gchar *href;
100 const gchar *xpointer;
101 gpointer user_data;
102 struct xistate *first;
103 struct xistate *last;
104 int active;
105 int level;
106 };
107
108 struct xmlstate
109 {
110 const gchar **attribute_names;
111 const gchar **attribute_values;
112 struct xmlstate *parent;
113 struct attr element_attr;
114 const gchar *element;
115 xmlerror **error;
116 struct element_func *func;
117 struct object_func *object_func;
118 struct xmldocument *document;
119 };
120
121 struct attr_fixme
122 {
123 char *element;
124 char **attr_fixme;
125 };
126
127 static struct attr ** convert_to_attrs(struct xmlstate *state, struct attr_fixme *fixme)
128 {
129 const gchar **attribute_name = state->attribute_names;
130 const gchar **attribute_value = state->attribute_values;
131 const gchar *name;
132 int count = 0;
133 struct attr **ret;
134 static int fixme_count;
135
136 while (*attribute_name)
137 {
138 count++;
139 attribute_name++;
140 }ret=g_new(struct attr *, count+1);
141 attribute_name = state->attribute_names;
142 count = 0;
143 while (*attribute_name)
144 {
145 name = *attribute_name;
146 if (fixme)
147 {
148 char **attr_fixme = fixme->attr_fixme;
149 while (attr_fixme[0])
150 {
151 if (!strcmp(name, attr_fixme[0]))
152 {
153 name = attr_fixme[1];
154 if (fixme_count++ < 10)
155 {
156 //dbg(0,"Please change attribute '%s' to '%s' in <%s />\n",attr_fixme[0], attr_fixme[1], fixme->element);
157 }
158 break;
159 }
160 attr_fixme += 2;
161 }
162 }
163 ret[count] = attr_new_from_text(name, *attribute_value);
164 if (ret[count])
165 {
166 count++;
167 }
168 else if (strcmp(*attribute_name, "enabled") && strcmp(*attribute_name, "xmlns:xi"))
169 {
170 // dbg(0, "failed to create attribute '%s' with value '%s'\n", *attribute_name, *attribute_value);
171 }
172 attribute_name++;
173 attribute_value++;
174 }
175 ret[count] = NULL;
176 //dbg(1, "ret=%p\n", ret);
177 return ret;
178 }
179
180 static const char * find_attribute(struct xmlstate *state, const char *attribute, int required)
181 {
182 const gchar **attribute_name = state->attribute_names;
183 const gchar **attribute_value = state->attribute_values;
184 while (*attribute_name)
185 {
186 if (!g_ascii_strcasecmp(attribute, *attribute_name))
187 return *attribute_value;
188 attribute_name++;
189 attribute_value++;
190 }
191 if (required)
192 g_set_error(state->error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, "element '%s' is missing attribute '%s'", state->element, attribute);
193 return NULL;
194 }
195
196 static int find_boolean(struct xmlstate *state, const char *attribute, int deflt, int required)
197 {
198 const char *value;
199
200 value = find_attribute(state, attribute, required);
201 if (!value)
202 return deflt;
203 if (g_ascii_strcasecmp(value, "no") && g_ascii_strcasecmp(value, "0") && g_ascii_strcasecmp(value, "false"))
204 return 1;
205 return 0;
206 }
207
208 /**
209 * * Convert a string number to int
210 * *
211 * * @param val the string value to convert
212 * * @returns int value of converted string
213 * */
214 static int convert_number(const char *val)
215 {
216 if (val)
217 return g_ascii_strtoull(val, NULL, 0);
218 else
219 return 0;
220 }
221
222 static int xmlconfig_announce(struct xmlstate *state)
223 {
224 const char *type, *value;
225 char key[32];
226 int level[3];
227 int i;
228 enum item_type itype;
229 char *tok, *type_str, *str;
230
231 type = find_attribute(state, "type", 1);
232 if (!type)
233 return 0;
234 for (i = 0; i < 3; i++)
235 {
236 sprintf(key, "level%d", i);
237 value = find_attribute(state, key, 0);
238 if (value)
239 level[i] = convert_number(value);
240 else
241 level[i] = -1;
242 }
243 type_str = g_strdup(type);
244 str = type_str;
245 while ((tok = strtok(str, ",")))
246 {
247 itype = item_from_name(tok);
248 navigation_set_announce(state->parent->element_attr.u.data, itype, level);
249 str = NULL;
250 }
251 g_free(type_str);
252 return 1;
253 }
254 /**
255 * * Define the elements in our config
256 * *
257 * */
258
259 #define NEW(x) (void *(*)(struct attr *, struct attr **))(x)
260 #define GET(x) (int (*)(void *, enum attr_type type, struct attr *attr, struct attr_iter *iter))(x)
261 #define ITERN(x) (struct attr_iter * (*)(void *))(x)
262 #define ITERD(x) (void (*)(struct attr_iter *iter))(x)
263 #define SET(x) (int (*)(void *, struct attr *attr))(x)
264 #define ADD(x) (int (*)(void *, struct attr *attr))(x)
265 #define REMOVE(x) (int (*)(void *, struct attr *attr))(x)
266 #define INIT(x) (int (*)(void *))(x)
267 #define DESTROY(x) (void (*)(void *))(x)
268
269 static struct object_func object_funcs[] =
270 {
271 { attr_announcement, NEW(announcement_new), GET(announcement_get_attr), NULL, NULL, SET(announcement_set_attr), ADD(announcement_add_attr) },
272 { attr_arrows, NEW(arrows_new) },
273 { attr_circle, NEW(circle_new), NULL, NULL, NULL, NULL, ADD(element_add_attr) },
274 { attr_config, NEW(config_new), GET(config_get_attr), ITERN(config_attr_iter_new), ITERD(config_attr_iter_destroy), SET(config_set_attr), ADD(config_add_attr), REMOVE(config_remove_attr), NULL, DESTROY(config_destroy) },
275 { attr_coord, NEW(coord_new_from_attrs) },
276 { attr_cursor, NEW(cursor_new), NULL, NULL, NULL, NULL, ADD(cursor_add_attr) },
277 { attr_debug, NEW(debug_new) },
278 { attr_graphics, NEW(graphics_new) },
279 { attr_gui, NEW(gui_new), GET(gui_get_attr), NULL, NULL, SET(gui_set_attr), ADD(gui_add_attr) },
280 { attr_icon, NEW(icon_new), NULL, NULL, NULL, NULL, ADD(element_add_attr) },
281 { attr_image, NEW(image_new) },
282 { attr_itemgra, NEW(itemgra_new), NULL, NULL, NULL, NULL, ADD(itemgra_add_attr) },
283 { attr_layer, NEW(layer_new), NULL, NULL, NULL, NULL, ADD(layer_add_attr) },
284 { attr_layout, NEW(layout_new), NULL, NULL, NULL, NULL, ADD(layout_add_attr) },
285 { attr_log, NEW(log_new) },
286 { attr_map, NEW(map_new) },
287 { attr_mapset, NEW(mapset_new), NULL, NULL, NULL, NULL, ADD(mapset_add_attr_name) },
288 { attr_navigation, NEW(navigation_new), GET(navigation_get_attr) },
289 { attr_navit, NEW(navit_new), GET(navit_get_attr), ITERN(navit_attr_iter_new), ITERD(navit_attr_iter_destroy), SET(navit_set_attr), ADD(navit_add_attr), REMOVE(navit_remove_attr), INIT(navit_init), DESTROY(navit_destroy) },
290 { attr_osd, NEW(osd_new) },
291 { attr_plugins, NEW(plugins_new), NULL, NULL, NULL, NULL, NULL, NULL, INIT(plugins_init) },
292 { attr_plugin, NEW(plugin_new) },
293 { attr_polygon, NEW(polygon_new), NULL, NULL, NULL, NULL, ADD(element_add_attr) },
294 { attr_polyline, NEW(polyline_new), NULL, NULL, NULL, NULL, ADD(element_add_attr) },
295 { attr_roadprofile, NEW(roadprofile_new), GET(roadprofile_get_attr), NULL, NULL, SET(roadprofile_set_attr), ADD(roadprofile_add_attr) },
296 { attr_route, NEW(route_new), GET(route_get_attr), NULL, NULL, SET(route_set_attr), ADD(route_add_attr), REMOVE(route_remove_attr) },
297 { attr_speech, NEW(speech_new), GET(speech_get_attr), NULL, NULL, SET(speech_set_attr) },
298 { attr_text, NEW(text_new) },
299 { attr_tracking, NEW(tracking_new) },
300 { attr_vehicle, NEW(vehicle_new), GET(vehicle_get_attr), NULL, NULL, SET(vehicle_set_attr), ADD(vehicle_add_attr), REMOVE(vehicle_remove_attr) },
301 { attr_vehicleprofile, NEW(vehicleprofile_new), GET(vehicleprofile_get_attr), NULL, NULL, SET(vehicleprofile_set_attr), ADD(vehicleprofile_add_attr) }, };
302
303 struct object_func *
304 object_func_lookup(enum attr_type type)
305 {
306 int i;
307 for (i = 0; i < sizeof(object_funcs) / sizeof(struct object_func); i++)
308 {
309 if (object_funcs[i].type == type)
310 return &object_funcs[i];
311 }
312 return NULL;
313 }
314
315 struct element_func
316 {
317 char *name;
318 char *parent;
319 int (*func)(struct xmlstate *state);
320 enum attr_type type;
321 };
322 struct element_func *elements;
323
324 static char *attr_fixme_itemgra[] =
325 { "type", "item_types", NULL, NULL, };
326
327 static char *attr_fixme_text[] =
328 { "label_size", "text_size", NULL, NULL, };
329
330 static char *attr_fixme_circle[] =
331 { "label_size", "text_size", NULL, NULL, };
332
333 static struct attr_fixme attr_fixmes[] =
334 {
335 { "item", attr_fixme_itemgra },
336 { "itemgra", attr_fixme_itemgra },
337 { "text", attr_fixme_text },
338 { "label", attr_fixme_text },
339 { "circle", attr_fixme_circle },
340 { NULL, NULL }, };
341
342 static char *element_fixmes[] =
343 { "item", "itemgra", "label", "text", NULL, NULL, };
344
345 static void initStatic(void)
346 {
347 elements=g_new0(struct element_func,40); //39 is a number of elements + ending NULL element
348
349 elements[0].name = "config";
350 elements[0].parent = NULL;
351 elements[0].func = NULL;
352 elements[0].type = attr_config;
353
354 elements[1].name = "announce";
355 elements[1].parent = "navigation";
356 elements[1].func = xmlconfig_announce;
357
358 elements[2].name = "speech";
359 elements[2].parent = "navit";
360 elements[2].func = NULL;
361 elements[2].type = attr_speech;
362
363 elements[3].name = "tracking";
364 elements[3].parent = "navit";
365 elements[3].func = NULL;
366 elements[3].type = attr_tracking;
367
368 elements[4].name = "route";
369 elements[4].parent = "navit";
370 elements[4].func = NULL;
371 elements[4].type = attr_route;
372
373 elements[5].name = "mapset";
374 elements[5].parent = "navit";
375 elements[5].func = NULL;
376 elements[5].type = attr_mapset;
377
378 elements[6].name = "map";
379 elements[6].parent = "mapset";
380 elements[6].func = NULL;
381 elements[6].type = attr_map;
382
383 elements[7].name = "debug";
384 elements[7].parent = "config";
385 elements[7].func = NULL;
386 elements[7].type = attr_debug;
387
388 elements[8].name = "osd";
389 elements[8].parent = "navit";
390 elements[8].func = NULL;
391 elements[8].type = attr_osd;
392
393 elements[9].name = "navigation";
394 elements[9].parent = "navit";
395 elements[9].func = NULL;
396 elements[9].type = attr_navigation;
397
398 elements[10].name = "navit";
399 elements[10].parent = "config";
400 elements[10].func = NULL;
401 elements[10].type = attr_navit;
402
403 elements[11].name = "graphics";
404 elements[11].parent = "navit";
405 elements[11].func = NULL;
406 elements[11].type = attr_graphics;
407
408 elements[12].name = "gui";
409 elements[12].parent = "navit";
410 elements[12].func = NULL;
411 elements[12].type = attr_gui;
412
413 elements[13].name = "layout";
414 elements[13].parent = "navit";
415 elements[13].func = NULL;
416 elements[13].type = attr_layout;
417
418 elements[14].name = "cursor";
419 elements[14].parent = "layout";
420 elements[14].func = NULL;
421 elements[14].type = attr_cursor;
422
423 elements[15].name = "layer";
424 elements[15].parent = "layout";
425 elements[15].func = NULL;
426 elements[15].type = attr_layer;
427
428 elements[16].name = "itemgra";
429 elements[16].parent = "layer";
430 elements[16].func = NULL;
431 elements[16].type = attr_itemgra;
432
433 elements[17].name = "circle";
434 elements[17].parent = "itemgra";
435 elements[17].func = NULL;
436 elements[17].type = attr_circle;
437
438 elements[18].name = "coord";
439 elements[18].parent = "circle";
440 elements[18].func = NULL;
441 elements[18].type = attr_coord;
442
443 elements[19].name = "icon";
444 elements[19].parent = "itemgra";
445 elements[19].func = NULL;
446 elements[19].type = attr_icon;
447
448 elements[20].name = "coord";
449 elements[20].parent = "icon";
450 elements[20].func = NULL;
451 elements[20].type = attr_coord;
452
453 elements[21].name = "image";
454 elements[21].parent = "itemgra";
455 elements[21].func = NULL;
456 elements[21].type = attr_image;
457
458 elements[22].name = "text";
459 elements[22].parent = "itemgra";
460 elements[22].func = NULL;
461 elements[22].type = attr_text;
462
463 elements[23].name = "polygon";
464 elements[23].parent = "itemgra";
465 elements[23].func = NULL;
466 elements[23].type = attr_polygon;
467
468 elements[24].name = "coord";
469 elements[24].parent = "polygon";
470 elements[24].func = NULL;
471 elements[24].type = attr_coord;
472
473 elements[25].name = "polyline";
474 elements[25].parent = "itemgra";
475 elements[25].func = NULL;
476 elements[25].type = attr_polyline;
477
478 elements[26].name = "coord";
479 elements[26].parent = "polyline";
480 elements[26].func = NULL;
481 elements[26].type = attr_coord;
482
483 elements[27].name = "arrows";
484 elements[27].parent = "itemgra";
485 elements[27].func = NULL;
486 elements[27].type = attr_arrows;
487
488 elements[28].name = "vehicle";
489 elements[28].parent = "navit";
490 elements[28].func = NULL;
491 elements[28].type = attr_vehicle;
492
493 elements[29].name = "vehicleprofile";
494 elements[29].parent = "navit";
495 elements[29].func = NULL;
496 elements[29].type = attr_vehicleprofile;
497
498 elements[30].name = "roadprofile";
499 elements[30].parent = "vehicleprofile";
500 elements[30].func = NULL;
501 elements[30].type = attr_roadprofile;
502
503 elements[31].name = "announcement";
504 elements[31].parent = "roadprofile";
505 elements[31].func = NULL;
506 elements[31].type = attr_announcement;
507
508 elements[32].name = "cursor";
509 elements[32].parent = "vehicle";
510 elements[32].func = NULL;
511 elements[32].type = attr_cursor;
512
513 elements[33].name = "itemgra";
514 elements[33].parent = "cursor";
515 elements[33].func = NULL;
516 elements[33].type = attr_itemgra;
517
518 elements[34].name = "log";
519 elements[34].parent = "vehicle";
520 elements[34].func = NULL;
521 elements[34].type = attr_log;
522
523 elements[35].name = "log";
524 elements[35].parent = "navit";
525 elements[35].func = NULL;
526 elements[35].type = attr_log;
527
528 elements[36].name = "plugins";
529 elements[36].parent = "config";
530 elements[36].func = NULL;
531 elements[36].type = attr_plugins;
532
533 elements[37].name = "plugin";
534 elements[37].parent = "plugins";
535 elements[37].func = NULL;
536 elements[37].type = attr_plugin;
537 }
538
539 /**
540 * * Parse the opening tag of a config element
541 * *
542 * * @param context document parse context
543 * * @param element_name the current tag name
544 * * @param attribute_names ptr to return the set of attribute names
545 * * @param attribute_values ptr return the set of attribute values
546 * * @param user_data ptr to xmlstate structure
547 * * @param error ptr return error context
548 * * @returns nothing
549 * */
550
551 static void start_element(GMarkupParseContext *context, const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, gpointer user_data, xmlerror **error)
552 {
553 struct xmlstate *new = NULL, **parent = user_data;
554 struct element_func *e = elements, *func = NULL;
555 struct attr_fixme *attr_fixme = attr_fixmes;
556 char **element_fixme = element_fixmes;
557 int found = 0;
558 static int fixme_count;
559 const char *parent_name = NULL;
560 char *s, *sep = "", *possible_parents;
561 struct attr *parent_attr;
562 // dbg(2, "name='%s' parent='%s'\n", element_name, *parent ? (*parent)->element : NULL);
563
564 if (!strcmp(element_name, "xml"))
565 return;
566 /* determine if we have to fix any attributes */
567 while (attr_fixme[0].element)
568 {
569 if (!strcmp(element_name, attr_fixme[0].element))
570 break;
571 attr_fixme++;
572 }
573 if (!attr_fixme[0].element)
574 attr_fixme = NULL;
575
576 /* tell user to fix deprecated element names */
577 while (element_fixme[0])
578 {
579 if (!strcmp(element_name, element_fixme[0]))
580 {
581 element_name = element_fixme[1];
582 if (fixme_count++ < 10)
583 {
584 // dbg(0, "Please change <%s /> to <%s /> in config file\n",element_fixme[0], element_fixme[1]);
585 }
586 }
587 element_fixme += 2;
588 }
589 /* validate that this element is valid
590 * and that the element has a valid parent */
591 possible_parents = g_strdup("");
592 if (*parent)
593 parent_name = (*parent)->element;
594 while (e->name)
595 {
596 if (!g_ascii_strcasecmp(element_name, e->name))
597 {
598 found = 1;
599 s = g_strconcat(possible_parents, sep, e->parent, NULL);
600 g_free(possible_parents);
601 possible_parents = s;
602 sep = ",";
603 if ((parent_name && e->parent && !g_ascii_strcasecmp(parent_name, e->parent)) || (!parent_name && !e->parent))
604 func = e;
605 }
606 e++;
607 }
608 if (!found)
609 {
610 g_set_error(error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT, "Unknown element '%s'", element_name);
611 g_free(possible_parents);
612 return;
613 }
614 if (!func)
615 {
616 g_set_error(error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, "Element '%s' within unexpected context '%s'. Expected '%s'%s", element_name, parent_name, possible_parents,
617 !strcmp(possible_parents, "config") ? "\nPlease add <config> </config> tags at the beginning/end of your navit.xml" : "");
618 g_free(possible_parents);
619 return;
620 }
621 g_free(possible_parents);
622
623 new=g_new(struct xmlstate, 1);
624 new->attribute_names = attribute_names;
625 new->attribute_values = attribute_values;
626 new->parent = *parent;
627 new->element_attr.u.data = NULL;
628 new->element = element_name;
629 new->error = error;
630 new->func = func;
631 new->object_func = NULL;
632 *parent = new;
633 if (!find_boolean(new, "enabled", 1, 0))
634 return;
635 if (new->parent && !new->parent->element_attr.u.data)
636 return;
637 if (func->func)
638 {
639 if (!func->func(new))
640 {
641 return;
642 }
643 }
644 else
645 {
646 struct attr **attrs;
647
648 new->object_func = object_func_lookup(func->type);
649 if (!new->object_func)
650 return;
651 attrs = convert_to_attrs(new, attr_fixme);
652 new->element_attr.type = attr_none;
653 if (!new->parent || new->parent->element_attr.type == attr_none)
654 parent_attr = NULL;
655 else
656 parent_attr = &new->parent->element_attr;
657 new->element_attr.u.data = new->object_func->create(parent_attr, attrs);
658 if (!new->element_attr.u.data)
659 return;
660 new->element_attr.type = attr_from_name(element_name);
661 if (new->element_attr.type == attr_none)
662 {
663 // dbg(0, "failed to create object of type '%s'\n", element_name);
664 }
665 if (new->parent && new->parent->object_func && new->parent->object_func->add_attr)
666 new->parent->object_func->add_attr(new->parent->element_attr.u.data, &new->element_attr);
667 }
668 return;
669 }
670
671 /* Called for close tags </foo> */
672 static void end_element(GMarkupParseContext *context, const gchar *element_name, gpointer user_data, xmlerror **error)
673 {
674 struct xmlstate *curr, **state = user_data;
675
676 if (!strcmp(element_name, "xml"))
677 {
678 return;
679 }
680 // dbg(2, "name='%s'\n", element_name);
681 curr = *state;
682 if (curr->object_func && curr->object_func->init)
683 curr->object_func->init(curr->element_attr.u.data);
684 *state = curr->parent;
685 g_free(curr);
686 }
687
688 static gboolean parse_file(struct xmldocument *document, xmlerror **error);
689
690 static void xinclude(GMarkupParseContext *context, const gchar **attribute_names, const gchar **attribute_values, struct xmldocument *doc_old, xmlerror **error)
691 {
692 struct xmldocument doc_new;
693 struct file_wordexp *we;
694 int i, count;
695 const char *href = NULL;
696 char **we_files;
697
698 if (doc_old->level >= 16)
699 {
700 g_set_error(error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, "xi:include recursion too deep");
701 return;
702 }
703 memset(&doc_new, 0, sizeof(doc_new));
704 i = 0;
705 while (attribute_names[i])
706 {
707 if (!g_ascii_strcasecmp("href", attribute_names[i]))
708 {
709 if (!href)
710 href = attribute_values[i];
711 else
712 {
713 g_set_error(error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, "xi:include has more than one href");
714 return;
715 }
716 }
717 else if (!g_ascii_strcasecmp("xpointer", attribute_names[i]))
718 {
719 if (!doc_new.xpointer)
720 doc_new.xpointer = attribute_values[i];
721 else
722 {
723 g_set_error(error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, "xi:include has more than one xpointer");
724 return;
725 }
726 }
727 else
728 {
729 g_set_error(error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, "xi:include has invalid attributes");
730 return;
731 }
732 i++;
733 }
734 if (!doc_new.xpointer && !href)
735 {
736 g_set_error(error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, "xi:include has neither href nor xpointer");
737 return;
738 }
739 doc_new.level = doc_old->level + 1;
740 doc_new.user_data = doc_old->user_data;
741 if (!href)
742 {
743 //dbg(1, "no href, using '%s'\n", doc_old->href);
744 doc_new.href = doc_old->href;
745 if (file_exists(doc_new.href))
746 {
747 parse_file(&doc_new, error);
748 }
749 else
750 {
751 //dbg(0, "Unable to include %s\n", doc_new.href);
752 }
753 }
754 else
755 {
756 //dbg(1, "expanding '%s'\n", href);
757 we = file_wordexp_new(href);
758 we_files = file_wordexp_get_array(we);
759 count = file_wordexp_get_count(we);
760 //dbg(1, "%d results\n", count);
761 if (file_exists(we_files[0]))
762 {
763 for (i = 0; i < count; i++)
764 {
765 //dbg(1, "result[%d]='%s'\n", i, we_files[i]);
766 doc_new.href = we_files[i];
767 parse_file(&doc_new, error);
768 }
769 }
770 else
771 {
772 //dbg(0, "Unable to include %s\n", we_files[0]);
773 }
774 file_wordexp_destroy(we);
775
776 }
777
778 }
779
780 static int strncmp_len(const char *s1, int s1len, const char *s2)
781 {
782 int ret;
783 #if 0
784 char c[s1len+1];
785 strncpy(c, s1, s1len);
786 c[s1len]='\0';
787 //dbg(0,"'%s' vs '%s'\n", c, s2);
788 #endif
789
790 ret = strncmp(s1, s2, s1len);
791 if (ret)
792 return ret;
793 return strlen(s2) - s1len;
794 }
795
796 static int xpointer_value(const char *test, int len, struct xistate *elem, const char **out, int out_len)
797 {
798 int i, ret = 0;
799 if (len <= 0 || out_len <= 0)
800 {
801 return 0;
802 }
803 if (!(strncmp_len(test, len, "name(.)")))
804 {
805 out[0] = elem->element;
806 return 1;
807 }
808 if (test[0] == '@')
809 {
810 i = 0;
811 while (elem->attribute_names[i] && out_len > 0)
812 {
813 if (!strncmp_len(test + 1, len - 1, elem->attribute_names[i]))
814 {
815 out[ret++] = elem->attribute_values[i];
816 out_len--;
817 }
818 i++;
819 }
820 return ret;
821 }
822 return 0;
823 }
824
825 static int xpointer_test(const char *test, int len, struct xistate *elem)
826 {
827 int eq, i, count, vlen, cond_req = 1, cond = 0;
828 char c;
829 const char *tmp[16];
830 #if 0
831 char test2[len+1];
832
833 strncpy(test2, test, len);
834 test2[len]='\0';
835 //dbg(0,"%s\n", test2);
836 #endif
837 if (!len)
838 return 0;
839 c = test[len - 1];
840 if (c != '\'' && c != '"')
841 return 0;
842 eq = strcspn(test, "=");
843 if (eq >= len || test[eq + 1] != c)
844 return 0;
845 vlen = eq;
846 if (eq > 0 && test[eq - 1] == '!')
847 {
848 cond_req = 0;
849 vlen--;
850 }
851 count = xpointer_value(test, vlen, elem, tmp, 16);
852 for (i = 0; i < count; i++)
853 {
854 if (!strncmp_len(test + eq + 2, len - eq - 3, tmp[i]))
855 cond = 1;
856 }
857 if (cond == cond_req)
858 return 1;
859 return 0;
860 }
861
862 static int xpointer_element_match(const char *xpointer, int len, struct xistate *elem)
863 {
864 int start, tlen, tlen2;
865 #if 0
866 char test2[len+1];
867
868 strncpy(test2, xpointer, len);
869 test2[len]='\0';
870 //dbg(0,"%s\n", test2);
871 #endif
872 start = strcspn(xpointer, "[");
873 if (start > len)
874 start = len;
875 if (strncmp_len(xpointer, start, elem->element) && (start != 1 || xpointer[0] != '*'))
876 return 0;
877 if (start == len)
878 return 1;
879 if (xpointer[len - 1] != ']')
880 return 0;
881 tlen = len - start - 2;
882 for (;;)
883 {
884 start++;
885 tlen2 = strcspn(xpointer + start, "]");
886 if (start + tlen2 > len)
887 return 1;
888 if (!xpointer_test(xpointer + start, tlen2, elem))
889 return 0;
890 start += tlen2 + 1;
891 }
892 }
893
894 static int xpointer_xpointer_match(const char *xpointer, int len, struct xistate *first)
895 {
896 const char *c;
897 int s;
898 //dbg(2, "%s\n", xpointer);
899 if (xpointer[0] != '/')
900 return 0;
901 c = xpointer + 1;
902 len--;
903 do
904 {
905 s = strcspn(c, "/");
906 if (s > len)
907 s = len;
908 if (!xpointer_element_match(c, s, first))
909 return 0;
910 first = first->child;
911 c += s + 1;
912 len -= s + 1;
913 }
914 while (len > 0 && first);
915 if (len > 0)
916 return 0;
917 return 1;
918 }
919
920 static int xpointer_match(const char *xpointer, struct xistate *first)
921 {
922 char *prefix = "xpointer(";
923 int len;
924 if (!xpointer)
925 return 1;
926 len = strlen(xpointer);
927 if (strncmp(xpointer, prefix, strlen(prefix)))
928 return 0;
929 if (xpointer[len - 1] != ')')
930 return 0;
931 return xpointer_xpointer_match(xpointer + strlen(prefix), len - strlen(prefix) - 1, first);
932
933 }
934
935 static void xi_start_element(GMarkupParseContext *context, const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, gpointer user_data, xmlerror **error)
936 {
937 struct xmldocument *doc = user_data;
938 struct xistate *xistate;
939 int i, count = 0;
940 while (attribute_names[count++ * ATTR_DISTANCE])
941 ;xistate=g_new0(struct xistate, 1);
942 xistate->element = element_name;
943 xistate->attribute_names=g_new0(const char *, count);
944 xistate->attribute_values=g_new0(const char *, count);
945 for (i = 0; i < count; i++)
946 {
947 if (attribute_names[i * ATTR_DISTANCE] && attribute_values[i * ATTR_DISTANCE])
948 {
949 xistate->attribute_names[i] = g_strdup(attribute_names[i * ATTR_DISTANCE]);
950 xistate->attribute_values[i] = g_strdup(attribute_values[i * ATTR_DISTANCE]);
951 }
952 }
953 xistate->parent = doc->last;
954
955 if (doc->last)
956 {
957 doc->last->child = xistate;
958 }
959 else
960 doc->first = xistate;
961 doc->last = xistate;
962 if (doc->active > 0 || xpointer_match(doc->xpointer, doc->first))
963 {
964 if (!g_ascii_strcasecmp("xi:include", element_name))
965 {
966 xinclude(context, xistate->attribute_names, xistate->attribute_values, doc, error);
967 return;
968 }
969 start_element(context, element_name, xistate->attribute_names, xistate->attribute_values, doc->user_data, error);
970 doc->active++;
971 }
972
973 }
974 /**
975 * * Reached closing tag of a config element
976 * *
977 * * @param context
978 * * @param element name
979 * * @param user_data ptr to xmldocument
980 * * @param error ptr to struct for error information
981 * * @returns nothing
982 * */
983
984 static void xi_end_element(GMarkupParseContext *context, const gchar *element_name, gpointer user_data, xmlerror **error)
985 {
986 struct xmldocument *doc = user_data;
987 struct xistate *xistate = doc->last;
988 int i = 0;
989 doc->last = doc->last->parent;
990 if (!doc->last)
991 doc->first = NULL;
992 else
993 doc->last->child = NULL;
994 if (doc->active > 0)
995 {
996 if (!g_ascii_strcasecmp("xi:include", element_name))
997 {
998 return;
999 }
1000 end_element(context, element_name, doc->user_data, error);
1001 doc->active--;
1002 }
1003 while (xistate->attribute_names[i])
1004 {
1005 g_free((char *) (xistate->attribute_names[i]));
1006 g_free((char *) (xistate->attribute_values[i]));
1007 i++;
1008 }
1009 g_free(xistate->attribute_names);
1010 g_free(xistate->attribute_values);
1011 g_free(xistate);
1012 }
1013
1014 /* Called for character data */
1015 /* text is not nul-terminated */
1016 static void xi_text(GMarkupParseContext *context, const gchar *text, gsize text_len, gpointer user_data, xmlerror **error)
1017 {
1018 struct xmldocument *doc = user_data;
1019 int i;
1020 if (doc->active)
1021 {
1022 for (i = 0; i < text_len; i++)
1023 {
1024 if (!isspace(text[i]))
1025 {
1026 struct xmldocument *doc = user_data;
1027 struct xmlstate *curr, **state = doc->user_data;
1028 struct attr attr;
1029 char *text_dup = malloc(text_len + 1);
1030
1031 curr = *state;
1032 strncpy(text_dup, text, text_len);
1033 text_dup[text_len] = '\0';
1034 attr.type = attr_xml_text;
1035 attr.u.str = text_dup;
1036 if (curr->object_func && curr->object_func->add_attr && curr->element_attr.u.data)
1037 curr->object_func->add_attr(curr->element_attr.u.data, &attr);
1038 free(text_dup);
1039 return;
1040 }
1041 }
1042 }
1043 }
1044
1045 #ifndef HAVE_GLIB
1046 static void parse_node_text(ezxml_t node, void *data, void(*start)(void *, const char *, const char **, const char **, void *, void *), void(*end)(void *, const char *, void *, void *), void(*text)(void *, const char *, int, void *, void *))
1047 {
1048 while (node)
1049 {
1050 if (start)
1051 start(NULL, node->name, (const char **) node->attr, (const char **) (node->attr + 1), data, NULL);
1052 if (text && node->txt)
1053 text(NULL, node->txt, strlen(node->txt), data, NULL);
1054 if (node->child)
1055 parse_node_text(node->child, data, start, end, text);
1056 if (end)
1057 end(NULL, node->name, data, NULL);
1058 node = node->ordered;
1059 }
1060 }
1061 #endif
1062
1063 void xml_parse_text(const char *document, void *data, void(*start)(void *, const char *, const char **, const char **, void *, void *), void(*end)(void *, const char *, void *, void *), void(*text)(void *, const char *, int, void *, void *))
1064 {
1065 #ifdef HAVE_GLIB
1066 GMarkupParser parser =
1067 { start, end, text, NULL, NULL};
1068 GMarkupParseContext *context;
1069 gboolean result;
1070
1071 context = g_markup_parse_context_new (&parser, 0, data, NULL);
1072 result = g_markup_parse_context_parse (context, document, strlen(document), NULL);
1073 g_markup_parse_context_free (context);
1074 #else
1075 char *str = g_strdup(document);
1076 ezxml_t root = ezxml_parse_str(str, strlen(str));
1077 if (!root)
1078 return;
1079 parse_node_text(root, data, start, end, text);
1080 ezxml_free(root);
1081 g_free(str);
1082 #endif
1083 }
1084
1085 #ifdef HAVE_GLIB
1086
1087 static const GMarkupParser parser =
1088 {
1089 xi_start_element,
1090 xi_end_element,
1091 xi_text,
1092 NULL,
1093 NULL
1094 };
1095 /**
1096 * * Parse the contents of the configuration file
1097 * *
1098 * * @param document struct holding info about the config file
1099 * * @param error info on any errors detected
1100 * * @returns boolean TRUE or FALSE
1101 * */
1102
1103 static gboolean
1104 parse_file(struct xmldocument *document, xmlerror **error)
1105 {
1106 //dbg(0,"EEnter\n");
1107
1108 GMarkupParseContext *context;
1109 gchar *contents, *message;
1110 gsize len;
1111 gint line, chr;
1112 gboolean result;
1113 char *xmldir,*newxmldir,*xmlfile,*newxmlfile,*sep;
1114
1115 //dbg(1,"enter filename='%s'\n", document->href);
1116 #if GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION < 12
1117 #define G_MARKUP_TREAT_CDATA_AS_TEXT 0
1118 #endif
1119
1120 context = g_markup_parse_context_new(&parser, G_MARKUP_TREAT_CDATA_AS_TEXT, document, NULL);
1121
1122 if (!g_file_get_contents(document->href, &contents, &len, error))
1123 {
1124 g_markup_parse_context_free(context);
1125 return FALSE;
1126 }
1127 xmldir=getenv("XMLDIR");
1128 xmlfile=getenv("XMLFILE");
1129 newxmlfile=g_strdup(document->href);
1130 newxmldir=g_strdup(document->href);
1131 if ((sep=strrchr(newxmldir,'/')))
1132 *sep='\0';
1133 else
1134 {
1135 g_free(newxmldir);
1136 newxmldir=g_strdup(".");
1137 }
1138 setenv("XMLDIR",newxmldir,1);
1139 setenv("XMLFILE",newxmlfile,1);
1140 document->active=document->xpointer ? 0:1;
1141 document->first=NULL;
1142 document->last=NULL;
1143 result = g_markup_parse_context_parse(context, contents, len, error);
1144 if (!result && error && *error)
1145 {
1146 g_markup_parse_context_get_position(context, &line, &chr);
1147 message=g_strdup_printf("%s at line %d, char %d\n", (*error)->message, line, chr);
1148 g_free((*error)->message);
1149 (*error)->message=message;
1150 }
1151 g_markup_parse_context_free(context);
1152 g_free (contents);
1153 if (xmldir)
1154 setenv("XMLDIR",xmldir,1);
1155 else
1156 #ifndef __MINGW32__
1157 unsetenv("XMLDIR");
1158 #else
1159 putenv("XMLDIR=");
1160 #endif /* __MINGW32__ */
1161 if (xmlfile)
1162 setenv("XMLFILE",xmlfile,1);
1163 else
1164 #ifndef __MINGW32__
1165 unsetenv("XMLFILE");
1166 #else
1167 putenv("XMLFILE=");
1168 #endif /* __MINGW32__ */
1169 g_free(newxmldir);
1170 g_free(newxmlfile);
1171 //dbg(1,"return %d\n", result);
1172
1173 return result;
1174 }
1175 #else
1176
1177 // this is used on android!!!!
1178 // this is used on android!!!!
1179 // this is used on android!!!!
1180 static void parse_node(struct xmldocument *document, ezxml_t node)
1181 {
1182
1183 //dbg(0,"EEnter\n");
1184
1185 while (node)
1186 {
1187 //dbg(0,"name:%s\n", node->name);
1188
1189 xi_start_element(NULL, node->name, node->attr, node->attr + 1, document, NULL);
1190 if (node->txt)
1191 {
1192 //dbg(0," txt:start\n");
1193 xi_text(NULL, node->txt, strlen(node->txt), document, NULL);
1194 //dbg(0," txt:end\n");
1195 }
1196
1197 if (node->child)
1198 {
1199 parse_node(document, node->child);
1200 }
1201
1202 xi_end_element(NULL, node->name, document, NULL);
1203 node = node->ordered;
1204 }
1205 }
1206
1207
1208
1209
1210
1211 // this is used on android!!!!
1212 // this is used on android!!!!
1213 // this is used on android!!!!
1214 // this is used on android!!!!
1215 static gboolean parse_file(struct xmldocument *document, xmlerror **error)
1216 {
1217 //dbg(0,"EEnter 2\n");
1218
1219 FILE *f;
1220 ezxml_t root;
1221
1222 f = fopen(document->href, "rb");
1223 if (!f)
1224 return FALSE;
1225 root = ezxml_parse_fp(f);
1226 fclose(f);
1227 if (!root)
1228 return FALSE;
1229 document->active = document->xpointer ? 0 : 1;
1230 document->first = NULL;
1231 document->last = NULL;
1232
1233 parse_node(document, root);
1234
1235 return TRUE;
1236 }
1237 #endif
1238
1239 /**
1240 * * Load and parse the master config file
1241 * *
1242 * * @param filename FQFN of the file
1243 * * @param error ptr to error details, if any
1244 * * @returns boolean TRUE or FALSE (if error detected)
1245 * */
1246
1247 gboolean config_load(const char *filename, xmlerror **error)
1248 {
1249 struct xmldocument document;
1250 struct xmlstate *curr = NULL;
1251 gboolean result;
1252
1253 initStatic();
1254
1255 dbg(0, "XML-----XML-----enter filename='%s'\n", filename);
1256 memset(&document, 0, sizeof(document));
1257 document.href = filename;
1258 document.user_data = &curr;
1259 result = parse_file(&document, error);
1260 if (result && curr)
1261 {
1262 g_set_error(error, G_MARKUP_ERROR, G_MARKUP_ERROR_PARSE, "element '%s' not closed", curr->element);
1263 result = FALSE;
1264 }
1265 dbg(0, "XML-----XML-----return %d\n", result);
1266 return result;
1267 }
1268

   
Visit the ZANavi Wiki