Add:Core:Added svn version in navit -v output
[navit-package] / navit / item.c
1 /**
2  * Navit, a modular navigation system.
3  * Copyright (C) 2005-2008 Navit Team
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 #include <string.h>
21 #include <glib.h>
22 #include "coord.h"
23 #include "debug.h"
24 #include "item.h"
25
26 struct item_name {
27         enum item_type item;
28         char *name;
29 };
30
31
32 struct item_name item_names[]={
33 #define ITEM2(x,y) ITEM(y)
34 #define ITEM(x) { type_##x, #x },
35 #include "item_def.h"
36 #undef ITEM2
37 #undef ITEM
38 };
39
40 void
41 item_coord_rewind(struct item *it)
42 {
43         it->meth->item_coord_rewind(it->priv_data);
44 }
45
46 int
47 item_coord_get(struct item *it, struct coord *c, int count)
48 {
49         return it->meth->item_coord_get(it->priv_data, c, count);
50 }
51
52 int
53 item_coord_get_pro(struct item *it, struct coord *c, int count, enum projection to)
54 {
55         int ret=item_coord_get(it, c, count);
56         int i;
57         enum projection from=map_projection(it->map);
58         if (from != to) 
59                 for (i = 0 ; i < count ; i++) 
60                         transform_from_to(c+i, from, c+i, to);
61         return ret;
62 }
63
64 int 
65 item_coord_is_segment(struct item *it)
66 {
67         if (it->meth->item_coord_is_segment)
68                 return it->meth->item_coord_is_segment(it->priv_data);
69         return 0;
70 }
71
72 void
73 item_attr_rewind(struct item *it)
74 {
75         it->meth->item_attr_rewind(it->priv_data);
76 }
77 int
78 item_attr_get(struct item *it, enum attr_type attr_type, struct attr *attr)
79 {
80         return it->meth->item_attr_get(it->priv_data, attr_type, attr);
81 }
82
83 struct item * item_new(char *type, int zoom)
84 {
85         struct item * it;
86
87         it = g_new0(struct item, 1);
88
89         /* FIXME evaluate arguments */
90
91         return it;
92 }
93
94 enum item_type
95 item_from_name(const char *name)
96 {
97         int i;
98
99         for (i=0 ; i < sizeof(item_names)/sizeof(struct item_name) ; i++) {
100                 if (! strcmp(item_names[i].name, name))
101                         return item_names[i].item;
102         }
103         return type_none;
104 }
105
106 char *
107 item_to_name(enum item_type item)
108 {
109         int i;
110
111         for (i=0 ; i < sizeof(item_names)/sizeof(struct item_name) ; i++) {
112                 if (item_names[i].item == item)
113                         return item_names[i].name;
114         }
115         return NULL; 
116 }
117
118 struct item_hash {
119         GHashTable *h;
120 };
121
122 static guint
123 item_hash_hash(gconstpointer key)
124 {
125         const struct item *itm=key;
126         gconstpointer hashkey=(gconstpointer)(itm->id_hi^itm->id_lo^((int) itm->map));
127         return g_direct_hash(hashkey);
128 }
129
130 static gboolean
131 item_hash_equal(gconstpointer a, gconstpointer b)
132 {
133         const struct item *itm_a=a;
134         const struct item *itm_b=b;
135         if (item_is_equal(*itm_a, *itm_b))
136                 return TRUE;
137         return FALSE;
138 }
139
140
141
142 struct item_hash *
143 item_hash_new(void)
144 {
145         struct item_hash *ret=g_new(struct item_hash, 1);
146
147         ret->h=g_hash_table_new_full(item_hash_hash, item_hash_equal, g_free, NULL);
148         return ret;
149 }
150
151 void
152 item_hash_insert(struct item_hash *h, struct item *item, void *val)
153 {
154         struct item *hitem=g_new(struct item, 1);
155         *hitem=*item;
156         dbg(2,"inserting (0x%x,0x%x) into %p\n", item->id_hi, item->id_lo, h->h);
157         g_hash_table_insert(h->h, hitem, val);
158 }
159
160 int
161 item_hash_remove(struct item_hash *h, struct item *item)
162 {
163         int ret;
164
165         dbg(2,"removing (0x%x,0x%x) from %p\n", item->id_hi, item->id_lo, h->h);
166         ret=g_hash_table_remove(h->h, item);
167         dbg(2,"ret=%d\n", ret);
168
169         return ret;
170 }
171
172 void *
173 item_hash_lookup(struct item_hash *h, struct item *item)
174 {
175         return g_hash_table_lookup(h->h, item);
176 }
177
178
179 void
180 item_hash_destroy(struct item_hash *h)
181 {
182         g_hash_table_destroy(h->h);
183         g_free(h);
184 }