diff options
author | martin-s <martin-s@ffa7fe5e-494d-0410-b361-a75ebd5db220> | 2009-03-06 09:56:34 +0000 |
---|---|---|
committer | martin-s <martin-s@ffa7fe5e-494d-0410-b361-a75ebd5db220> | 2009-03-06 09:56:34 +0000 |
commit | 52b80c8828e1c1c9c48a145064a6228d2a63e41e (patch) | |
tree | 5fdc06bc73f76aba6e68b8bcba25661163e37bf3 | |
parent | 818b39c6a62e2fb1169c1acafccb40aaf6b71d19 (diff) | |
download | navit-52b80c8828e1c1c9c48a145064a6228d2a63e41e.tar.gz |
Fix:Core:Cleaned up a bit
git-svn-id: http://svn.code.sf.net/p/navit/code/trunk/navit@2082 ffa7fe5e-494d-0410-b361-a75ebd5db220
-rw-r--r-- | navit/command.c | 30 | ||||
-rw-r--r-- | navit/main.c | 2 | ||||
-rw-r--r-- | navit/navit.c | 6 | ||||
-rw-r--r-- | navit/navit.h | 1 | ||||
-rw-r--r-- | navit/xmlconfig.c | 131 | ||||
-rw-r--r-- | navit/xmlconfig.h | 14 |
6 files changed, 121 insertions, 63 deletions
diff --git a/navit/command.c b/navit/command.c index 2a84998c8..8a387ceb4 100644 --- a/navit/command.c +++ b/navit/command.c @@ -3,7 +3,9 @@ #include <string.h> #include <stdarg.h> #include <stdlib.h> +#include <glib.h> #include "item.h" +#include "xmlconfig.h" #include "main.h" #include "navit.h" #include "vehicle.h" @@ -107,18 +109,10 @@ command_attr_type(struct result *res) static int command_object_get_attr(struct attr *object, enum attr_type attr_type, struct attr *ret) { - switch (object->type) { - case attr_gui: - return gui_get_attr(object->u.gui, attr_type, ret, NULL); - case attr_navit: - return navit_get_attr(object->u.navit, attr_type, ret, NULL); - case attr_speech: - return speech_get_attr(object->u.speech, attr_type, ret, NULL); - case attr_vehicle: - return vehicle_get_attr(object->u.vehicle, attr_type, ret, NULL); - default: + struct object_func *func=object_func_lookup(object->type); + if (!func || !func->get_attr) return 0; - } + return func->get_attr(object->u.data, attr_type, ret, NULL); } static void @@ -145,17 +139,11 @@ command_set_attr(struct context *ctx, struct result *res, struct result *newres) { int result=0; enum attr_type attr_type=command_attr_type(res); + struct object_func *func=object_func_lookup(res->attr.type); + if (!func || !func->set_attr) + return; newres->attr.type=attr_type; - switch (res->attr.type) { - case attr_navit: - result=navit_set_attr(res->attr.u.navit, &newres->attr); - break; - case attr_speech: - result=speech_set_attr(res->attr.u.speech, &newres->attr); - break; - default: - break; - } + result=func->set_attr(res->attr.u.data, &newres->attr); *res=*newres; } diff --git a/navit/main.c b/navit/main.c index cdf71fda3..b361fecc1 100644 --- a/navit/main.c +++ b/navit/main.c @@ -38,8 +38,8 @@ #include "main.h" #include "navit.h" #include "gui.h" -#include "xmlconfig.h" #include "item.h" +#include "xmlconfig.h" #include "coord.h" #include "route.h" #include "navigation.h" diff --git a/navit/navit.c b/navit/navit.c index 70555223e..91f3ccb54 100644 --- a/navit/navit.c +++ b/navit/navit.c @@ -1785,6 +1785,12 @@ navit_add_attr(struct navit *this_, struct attr *attr) return ret; } +int +navit_remove_attr(struct navit *this_, struct attr *attr) +{ + return 0; +} + struct attr_iter * navit_attr_iter_new() { diff --git a/navit/navit.h b/navit/navit.h index dcd212b9a..1a8b8df25 100644 --- a/navit/navit.h +++ b/navit/navit.h @@ -80,6 +80,7 @@ void navit_set_center_screen(struct navit *this_, struct point *p); int navit_set_attr(struct navit *this_, struct attr *attr); int navit_get_attr(struct navit *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter); int navit_add_attr(struct navit *this_, struct attr *attr); +int navit_remove_attr(struct navit *this_, struct attr *attr); struct attr_iter *navit_attr_iter_new(void); void navit_attr_iter_destroy(struct attr_iter *iter); void navit_add_callback(struct navit *this_, struct callback *cb); diff --git a/navit/xmlconfig.c b/navit/xmlconfig.c index 9d3edd569..e85c9f8ff 100644 --- a/navit/xmlconfig.c +++ b/navit/xmlconfig.c @@ -86,6 +86,7 @@ struct xmlstate { const gchar *element; xmlerror **error; struct element_func *func; + struct object_func *object_func; struct xmldocument *document; }; @@ -255,53 +256,97 @@ xmlconfig_announce(struct xmlstate *state) * */ #define NEW(x) (void *(*)(struct attr *, struct attr **))(x) +#define GET(x) (int (*)(void *, enum attr_type type, struct attr *attr, struct attr_iter *iter))(x) +#define ITERN(x) (struct attr_iter * (*)(void *))(x) +#define ITERD(x) (void (*)(struct attr_iter *iter))(x) +#define SET(x) (int (*)(void *, struct attr *attr))(x) #define ADD(x) (int (*)(void *, struct attr *attr))(x) +#define REMOVE(x) (int (*)(void *, struct attr *attr))(x) #define INIT(x) (int (*)(void *))(x) #define DESTROY(x) (void (*)(void *))(x) + +static struct object_func object_funcs[] = { + { attr_arrows, NEW(arrows_new)}, + { attr_circle, NEW(circle_new), NULL, NULL, NULL, NULL, ADD(element_add_attr)}, + { attr_coord, NEW(coord_new_from_attrs)}, + { attr_cursor, NEW(cursor_new), NULL, NULL, NULL, NULL, ADD(cursor_add_attr)}, + { attr_debug, NEW(debug_new)}, + { attr_graphics, NEW(graphics_new)}, + { attr_gui, NEW(gui_new), GET(gui_get_attr)}, + { attr_icon, NEW(icon_new), NULL, NULL, NULL, NULL, ADD(element_add_attr)}, + { attr_image, NEW(image_new)}, + { attr_itemgra, NEW(itemgra_new), NULL, NULL, NULL, NULL, ADD(itemgra_add_attr)}, + { attr_layer, NEW(layer_new), NULL, NULL, NULL, NULL, ADD(layer_add_attr)}, + { attr_layout, NEW(layout_new), NULL, NULL, NULL, NULL, ADD(layout_add_attr)}, + { attr_log, NEW(log_new)}, + { attr_map, NEW(map_new)}, + { attr_mapset, NEW(mapset_new), NULL, NULL, NULL, NULL, ADD(mapset_add_attr)}, + { attr_navigation, NEW(navigation_new)}, + { 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)}, + { attr_osd, NEW(osd_new)}, + { attr_plugins, NEW(plugins_new), NULL, NULL, NULL, NULL, NULL, NULL, INIT(plugins_init)}, + { attr_plugin, NEW(plugin_new)}, + { attr_polygon, NEW(polygon_new), NULL, NULL, NULL, NULL, ADD(element_add_attr)}, + { attr_polyline, NEW(polyline_new), NULL, NULL, NULL, NULL, ADD(element_add_attr)}, + { attr_route, NEW(route_new)}, + { attr_speech, NEW(speech_new), GET(speech_get_attr), NULL, NULL, SET(speech_set_attr)}, + { attr_text, NEW(text_new)}, + { attr_tracking, NEW(tracking_new)}, + { attr_vehicle, NEW(vehicle_new), GET(vehicle_get_attr), NULL, NULL, NULL, ADD(vehicle_add_attr) }, +}; + +struct object_func * +object_func_lookup(enum attr_type type) +{ + int i; + for (i = 0 ; i < sizeof(object_funcs)/sizeof(struct object_func); i++) { + if (object_funcs[i].type == type) + return &object_funcs[i]; + } + return NULL; +} + struct element_func { char *name; char *parent; int (*func)(struct xmlstate *state); - void *(*new)(struct attr *parent, struct attr **attrs); - int (*add_attr)(void *, struct attr *attr); - int (*init)(void *); - void (*destroy)(void *); + enum attr_type type; } elements[] = { { "config", NULL, xmlconfig_config}, { "announce", "navigation", xmlconfig_announce}, - { "speech", "navit", NULL, NEW(speech_new)}, - { "tracking", "navit", NULL, NEW(tracking_new)}, - { "route", "navit", NULL, NEW(route_new)}, + { "speech", "navit", NULL, attr_speech}, + { "tracking", "navit", NULL, attr_tracking}, + { "route", "navit", NULL, attr_route}, { "speed", "route", xmlconfig_speed}, - { "mapset", "navit", NULL, NEW(mapset_new), ADD(mapset_add_attr)}, - { "map", "mapset", NULL, NEW(map_new)}, - { "debug", "config", NULL, NEW(debug_new)}, - { "osd", "navit", NULL, NEW(osd_new)}, - { "navigation", "navit", NULL, NEW(navigation_new)}, - { "navit", "config", NULL, NEW(navit_new), ADD(navit_add_attr), INIT(navit_init), DESTROY(navit_destroy)}, - { "graphics", "navit", NULL, NEW(graphics_new)}, - { "gui", "navit", NULL, NEW(gui_new)}, - { "layout", "navit", NULL, NEW(layout_new), ADD(layout_add_attr)}, - { "layer", "layout", NULL, NEW(layer_new), ADD(layer_add_attr)}, - { "itemgra", "layer", NULL, NEW(itemgra_new), ADD(itemgra_add_attr)}, - { "circle", "itemgra", NULL, NEW(circle_new), ADD(element_add_attr)}, - { "coord", "circle", NULL, NEW(coord_new_from_attrs)}, - { "icon", "itemgra", NULL, NEW(icon_new), ADD(element_add_attr)}, - { "coord", "icon", NULL, NEW(coord_new_from_attrs)}, - { "image", "itemgra", NULL, NEW(image_new)}, - { "text", "itemgra", NULL, NEW(text_new)}, - { "polygon", "itemgra", NULL, NEW(polygon_new), ADD(element_add_attr)}, - { "coord", "polygon", NULL, NEW(coord_new_from_attrs)}, - { "polyline", "itemgra", NULL, NEW(polyline_new), ADD(element_add_attr)}, - { "coord", "polyline", NULL, NEW(coord_new_from_attrs)}, - { "arrows", "itemgra", NULL, NEW(arrows_new)}, - { "vehicle", "navit", NULL, NEW(vehicle_new), ADD(vehicle_add_attr) }, - { "cursor", "vehicle", NULL, NEW(cursor_new), ADD(cursor_add_attr)}, - { "itemgra", "cursor", NULL, NEW(itemgra_new), ADD(itemgra_add_attr)}, - { "log", "vehicle", NULL, NEW(log_new)}, - { "log", "navit", NULL, NEW(log_new)}, - { "plugins", "config", NULL, NEW(plugins_new), NULL, INIT(plugins_init)}, - { "plugin", "plugins", NULL, NEW(plugin_new)}, + { "mapset", "navit", NULL, attr_mapset}, + { "map", "mapset", NULL, attr_map}, + { "debug", "config", NULL, attr_debug}, + { "osd", "navit", NULL, attr_osd}, + { "navigation", "navit", NULL, attr_navigation}, + { "navit", "config", NULL, attr_navit}, + { "graphics", "navit", NULL, attr_graphics}, + { "gui", "navit", NULL, attr_gui}, + { "layout", "navit", NULL, attr_layout}, + { "layer", "layout", NULL, attr_layer}, + { "itemgra", "layer", NULL, attr_itemgra}, + { "circle", "itemgra", NULL, attr_circle}, + { "coord", "circle", NULL, attr_coord}, + { "icon", "itemgra", NULL, attr_icon}, + { "coord", "icon", NULL, attr_coord}, + { "image", "itemgra", NULL, attr_image}, + { "text", "itemgra", NULL, attr_text}, + { "polygon", "itemgra", NULL, attr_polygon}, + { "coord", "polygon", NULL, attr_coord}, + { "polyline", "itemgra", NULL, attr_polyline}, + { "coord", "polyline", NULL, attr_coord}, + { "arrows", "itemgra", NULL, attr_arrows}, + { "vehicle", "navit", NULL, attr_vehicle}, + { "cursor", "vehicle", NULL, attr_cursor}, + { "itemgra", "cursor", NULL, attr_itemgra}, + { "log", "vehicle", NULL, attr_log}, + { "log", "navit", NULL, attr_log}, + { "plugins", "config", NULL, attr_plugins}, + { "plugin", "plugins", NULL, attr_plugin}, {}, }; @@ -425,6 +470,7 @@ start_element(GMarkupParseContext *context, new->element=element_name; new->error=error; new->func=func; + new->object_func=NULL; *parent=new; if (!find_boolean(new, "enabled", 1, 0)) return; @@ -437,16 +483,19 @@ start_element(GMarkupParseContext *context, } else { struct attr **attrs; + new->object_func=object_func_lookup(func->type); + if (! new->object_func) + return; attrs=convert_to_attrs(new,attr_fixme); new->element_attr.type=attr_none; - new->element_attr.u.data = func->new(&new->parent->element_attr, attrs); + new->element_attr.u.data = new->object_func->new(&new->parent->element_attr, attrs); if (! new->element_attr.u.data) return; new->element_attr.type=attr_from_name(element_name); if (new->element_attr.type == attr_none) dbg(0,"failed to create object of type '%s'\n", element_name); - if (new->parent->func->add_attr) - new->parent->func->add_attr(new->parent->element_attr.u.data, &new->element_attr); + if (new->parent->object_func && new->parent->object_func->add_attr) + new->parent->object_func->add_attr(new->parent->element_attr.u.data, &new->element_attr); } return; } @@ -463,8 +512,8 @@ end_element (GMarkupParseContext *context, dbg(2,"name='%s'\n", element_name); curr=*state; - if (curr->func->init) - curr->func->init(curr->element_attr.u.data); + if (curr->object_func && curr->object_func->init) + curr->object_func->init(curr->element_attr.u.data); *state=curr->parent; g_free(curr); } diff --git a/navit/xmlconfig.h b/navit/xmlconfig.h index 15918f184..0e5e49311 100644 --- a/navit/xmlconfig.h +++ b/navit/xmlconfig.h @@ -20,9 +20,23 @@ #ifndef NAVIT_XMLCONFIG_H #define NAVIT_XMLCONFIG_H +struct object_func { + enum attr_type type; + void *(*new)(struct attr *parent, struct attr **attrs); + int (*get_attr)(void *, enum attr_type type, struct attr *attr, struct attr_iter *iter); + struct attr_iter *(*iter_new)(void *); + void (*iter_destroy)(struct attr_iter *); + int (*set_attr)(void *, struct attr *attr); + int (*add_attr)(void *, struct attr *attr); + int (*remove_attr)(void *, struct attr *attr); + int (*init)(void *); + void (*destroy)(void *); +}; + typedef GError xmlerror; struct container; gboolean config_load(const char *filename, xmlerror **error); +struct object_func *object_func_lookup(enum attr_type type); #endif |