Merge with modular_map
[navit-package] / src / item.c
1 #include <string.h>
2 #include <glib.h>
3 #include "attr.h"
4 #include "coord.h"
5 #include "item.h"
6
7 struct item_name {
8         enum item_type item;
9         char *name;
10 };
11
12
13 struct item_name item_names[]={
14 #define ITEM2(x,y) ITEM(y)
15 #define ITEM(x) { type_##x, #x },
16 #include "item_def.h"
17 #undef ITEM2
18 #undef ITEM
19 };
20
21 void
22 item_coord_rewind(struct item *it)
23 {
24         it->meth->item_coord_rewind(it->priv_data);
25 }
26
27 int
28 item_coord_get(struct item *it, struct coord *c, int count)
29 {
30         return it->meth->item_coord_get(it->priv_data, c, count);
31 }
32
33 void
34 item_attr_rewind(struct item *it)
35 {
36         it->meth->item_attr_rewind(it->priv_data);
37 }
38 int
39 item_attr_get(struct item *it, enum attr_type attr_type, struct attr *attr)
40 {
41         return it->meth->item_attr_get(it->priv_data, attr_type, attr);
42 }
43
44 struct item * item_new(char *type, int zoom)
45 {
46         struct item * it;
47
48         it = g_new0(struct item, 1);
49
50         /* FIXME evaluate arguments */
51
52         return it;
53 }
54
55 enum item_type
56 item_from_name(char *name)
57 {
58         int i;
59
60         for (i=0 ; i < sizeof(item_names)/sizeof(struct item_name) ; i++) {
61                 if (! strcmp(item_names[i].name, name))
62                         return item_names[i].item;
63         }
64         return type_none;
65 }
66
67 char *
68 item_to_name(enum item_type item)
69 {
70         int i;
71
72         for (i=0 ; i < sizeof(item_names)/sizeof(struct item_name) ; i++) {
73                 if (item_names[i].item == item)
74                         return item_names[i].name;
75         }
76         return NULL; 
77 }
78
79 struct item_hash {
80         GHashTable *h;
81 };
82
83 static guint
84 item_hash_hash(gconstpointer key)
85 {
86         const struct item *itm=key;
87         gconstpointer hashkey=(gconstpointer)(itm->id_hi^itm->id_lo^((int) itm->map));
88         return g_direct_hash(hashkey);
89 }
90
91 static gboolean
92 item_hash_equal(gconstpointer a, gconstpointer b)
93 {
94         const struct item *itm_a=a;
95         const struct item *itm_b=b;
96         if (item_is_equal(*itm_a, *itm_b))
97                 return TRUE;
98         return FALSE;
99 }
100
101
102
103 struct item_hash *
104 item_hash_new(void)
105 {
106         struct item_hash *ret=g_new(struct item_hash, 1);
107
108         ret->h=g_hash_table_new_full(item_hash_hash, item_hash_equal, g_free, NULL);
109         return ret;
110 }
111
112 void
113 item_hash_insert(struct item_hash *h, struct item *item, void *val)
114 {
115         struct item *hitem=g_new(struct item, 1);
116         *hitem=*item;
117         g_hash_table_insert(h->h, hitem, val);
118 }
119
120 void *
121 item_hash_lookup(struct item_hash *h, struct item *item)
122 {
123          return g_hash_table_lookup(h->h, item);
124 }
125
126
127 void
128 item_hash_destroy(struct item_hash *h)
129 {
130         g_hash_table_destroy(h->h);
131         g_free(h);
132 }