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

Contents of /navit/navit/xmlconfig.c

Parent Directory Parent Directory | Revision Log Revision Log


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

   
Visit the ZANavi Wiki