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