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

Diff of /navit/navit/item.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 26 Revision 27
24#include "debug.h" 24#include "debug.h"
25#include "item.h" 25#include "item.h"
26#include "map.h" 26#include "map.h"
27#include "transform.h" 27#include "transform.h"
28 28
29struct item_name { 29struct item_name
30{
30 enum item_type item; 31 enum item_type item;
31 char *name; 32 char *name;
32}; 33};
33 34
34struct item_range item_range_all = { type_none, type_last }; 35struct item_range item_range_all =
36{ type_none, type_last };
35 37
36struct default_flags { 38struct default_flags
39{
37 enum item_type type; 40 enum item_type type;
38 int flags; 41 int flags;
39}; 42};
40 43
41struct item busy_item; 44struct item busy_item;
42 45
43struct default_flags default_flags2[]={ 46struct default_flags default_flags2[] =
47{
44 {type_street_nopass, AF_PBH}, 48{ type_street_nopass, AF_PBH },
45 {type_street_0, AF_ALL}, 49{ type_street_0, AF_ALL },
46 {type_street_1_city, AF_ALL}, 50{ type_street_1_city, AF_ALL },
47 {type_street_2_city, AF_ALL}, 51{ type_street_2_city, AF_ALL },
48 {type_street_3_city, AF_ALL}, 52{ type_street_3_city, AF_ALL },
49 {type_street_4_city, AF_ALL}, 53{ type_street_4_city, AF_ALL },
50 {type_highway_city, AF_MOTORIZED_FAST}, 54{ type_highway_city, AF_MOTORIZED_FAST },
51 {type_street_1_land, AF_ALL}, 55{ type_street_1_land, AF_ALL },
52 {type_street_2_land, AF_ALL}, 56{ type_street_2_land, AF_ALL },
53 {type_street_3_land, AF_ALL}, 57{ type_street_3_land, AF_ALL },
54 {type_street_4_land, AF_ALL}, 58{ type_street_4_land, AF_ALL },
55 {type_street_n_lanes, AF_MOTORIZED_FAST}, 59{ type_street_n_lanes, AF_MOTORIZED_FAST },
56 {type_highway_land, AF_MOTORIZED_FAST}, 60{ type_highway_land, AF_MOTORIZED_FAST },
57 {type_ramp, AF_MOTORIZED_FAST}, 61{ type_ramp, AF_MOTORIZED_FAST },
58 {type_roundabout, AF_ALL}, 62{ type_roundabout, AF_ALL },
59 {type_ferry, AF_ALL}, 63{ type_ferry, AF_ALL },
60 {type_cycleway, AF_PBH}, 64{ type_cycleway, AF_PBH },
61 {type_track_paved, AF_ALL}, 65{ type_track_paved, AF_ALL },
62 {type_track_gravelled, AF_ALL}, 66{ type_track_gravelled, AF_ALL },
63 {type_track_unpaved, AF_ALL}, 67{ type_track_unpaved, AF_ALL },
64 {type_track_ground, AF_ALL}, 68{ type_track_ground, AF_ALL },
65 {type_track_grass, AF_ALL}, 69{ type_track_grass, AF_ALL },
66 {type_footway, AF_PBH}, 70{ type_footway, AF_PBH },
67 {type_living_street, AF_ALL}, 71{ type_living_street, AF_ALL },
68 {type_street_service, AF_ALL}, 72{ type_street_service, AF_ALL },
69 {type_bridleway, AF_PBH}, 73{ type_bridleway, AF_PBH },
70 {type_path, AF_PBH}, 74{ type_path, AF_PBH },
71 {type_steps, AF_PBH}, 75{ type_steps, AF_PBH },
72 {type_street_pedestrian, AF_PBH}, 76{ type_street_pedestrian, AF_PBH }, };
73};
74 77
75
76
77struct item_name item_names[]={ 78struct item_name item_names[] =
79{
78#define ITEM2(x,y) ITEM(y) 80#define ITEM2(x,y) ITEM(y)
79#define ITEM(x) { type_##x, #x }, 81#define ITEM(x) { type_##x, #x },
80#include "item_def.h" 82#include "item_def.h"
81#undef ITEM2 83#undef ITEM2
82#undef ITEM 84#undef ITEM
83}; 85 };
84 86
85static GHashTable *default_flags_hash; 87static GHashTable *default_flags_hash;
86 88
87int * 89int *
88item_get_default_flags(enum item_type type) 90item_get_default_flags(enum item_type type)
89{ 91{
90 if (!default_flags_hash) { 92 if (!default_flags_hash)
93 {
91 int i; 94 int i;
92 default_flags_hash=g_hash_table_new(NULL, NULL); 95 default_flags_hash = g_hash_table_new(NULL, NULL);
93 for (i = 0 ; i < sizeof(default_flags2)/sizeof(struct default_flags); i++) { 96 for (i = 0; i < sizeof(default_flags2) / sizeof(struct default_flags); i++)
97 {
94 g_hash_table_insert(default_flags_hash, (void *)(long)default_flags2[i].type, &default_flags2[i].flags); 98 g_hash_table_insert(default_flags_hash, (void *) (long) default_flags2[i].type, &default_flags2[i].flags);
95 } 99 }
96 } 100 }
97 return g_hash_table_lookup(default_flags_hash, (void *)(long)type); 101 return g_hash_table_lookup(default_flags_hash, (void *) (long) type);
98} 102}
99 103
100void
101item_cleanup(void) 104void item_cleanup(void)
102{ 105{
103 if (default_flags_hash) 106 if (default_flags_hash)
104 g_hash_table_destroy(default_flags_hash); 107 g_hash_table_destroy(default_flags_hash);
105} 108}
106 109
107void
108item_coord_rewind(struct item *it) 110void item_coord_rewind(struct item *it)
109{ 111{
110 it->meth->item_coord_rewind(it->priv_data); 112 it->meth->item_coord_rewind(it->priv_data);
111} 113}
112 114
113int
114item_coord_get(struct item *it, struct coord *c, int count) 115int item_coord_get(struct item *it, struct coord *c, int count)
115{ 116{
116 return it->meth->item_coord_get(it->priv_data, c, count); 117 return it->meth->item_coord_get(it->priv_data, c, count);
117} 118}
118 119
119int
120item_coord_set(struct item *it, struct coord *c, int count, enum change_mode mode) 120int item_coord_set(struct item *it, struct coord *c, int count, enum change_mode mode)
121{ 121{
122 if (!it->meth->item_coord_set) 122 if (!it->meth->item_coord_set)
123 return 0; 123 return 0;
124 return it->meth->item_coord_set(it->priv_data, c, count, mode); 124 return it->meth->item_coord_set(it->priv_data, c, count, mode);
125} 125}
126 126
127int
128item_coord_get_within_selection(struct item *it, struct coord *c, int count, struct map_selection *sel) 127int item_coord_get_within_selection(struct item *it, struct coord *c, int count, struct map_selection *sel)
129{ 128{
130 int i,ret=it->meth->item_coord_get(it->priv_data, c, count); 129 int i, ret = it->meth->item_coord_get(it->priv_data, c, count);
131 struct coord_rect r; 130 struct coord_rect r;
132 struct map_selection *curr; 131 struct map_selection *curr;
133 if (ret <= 0 || !sel) 132 if (ret <= 0 || !sel)
134 return ret; 133 return ret;
135 r.lu=c[0]; 134 r.lu = c[0];
136 r.rl=c[0]; 135 r.rl = c[0];
137 for (i = 1 ; i < ret ; i++) { 136 for (i = 1; i < ret; i++)
137 {
138 if (r.lu.x > c[i].x) 138 if (r.lu.x > c[i].x)
139 r.lu.x=c[i].x; 139 r.lu.x = c[i].x;
140 if (r.rl.x < c[i].x) 140 if (r.rl.x < c[i].x)
141 r.rl.x=c[i].x; 141 r.rl.x = c[i].x;
142 if (r.rl.y > c[i].y) 142 if (r.rl.y > c[i].y)
143 r.rl.y=c[i].y; 143 r.rl.y = c[i].y;
144 if (r.lu.y < c[i].y) 144 if (r.lu.y < c[i].y)
145 r.lu.y=c[i].y; 145 r.lu.y = c[i].y;
146 } 146 }
147 curr=sel; 147 curr = sel;
148 while (curr) { 148 while (curr)
149 {
149 struct coord_rect *sr=&curr->u.c_rect; 150 struct coord_rect *sr = &curr->u.c_rect;
150 if (r.lu.x <= sr->rl.x && r.rl.x >= sr->lu.x && 151 if (r.lu.x <= sr->rl.x && r.rl.x >= sr->lu.x && r.lu.y >= sr->rl.y && r.rl.y <= sr->lu.y)
151 r.lu.y >= sr->rl.y && r.rl.y <= sr->lu.y)
152 return ret; 152 return ret;
153 curr=curr->next; 153 curr = curr->next;
154 } 154 }
155 return 0; 155 return 0;
156} 156}
157 157
158int
159item_coord_get_pro(struct item *it, struct coord *c, int count, enum projection to) 158int item_coord_get_pro(struct item *it, struct coord *c, int count, enum projection to)
160{ 159{
161 int ret=item_coord_get(it, c, count); 160 int ret = item_coord_get(it, c, count);
162 int i; 161 int i;
163 enum projection from=map_projection(it->map); 162 enum projection from = map_projection(it->map);
164 if (from != to) 163 if (from != to)
165 for (i = 0 ; i < count ; i++) 164 for (i = 0; i < count; i++)
166 transform_from_to(c+i, from, c+i, to); 165 transform_from_to(c + i, from, c + i, to);
167 return ret; 166 return ret;
168} 167}
169 168
170int
171item_coord_is_node(struct item *it) 169int item_coord_is_node(struct item *it)
172{ 170{
173 if (it->meth->item_coord_is_node) 171 if (it->meth->item_coord_is_node)
174 return it->meth->item_coord_is_node(it->priv_data); 172 return it->meth->item_coord_is_node(it->priv_data);
175 return 0; 173 return 0;
176} 174}
177 175
178void
179item_attr_rewind(struct item *it) 176void item_attr_rewind(struct item *it)
180{ 177{
181 it->meth->item_attr_rewind(it->priv_data); 178 it->meth->item_attr_rewind(it->priv_data);
182} 179}
183 180
184int
185item_attr_get(struct item *it, enum attr_type attr_type, struct attr *attr) 181int item_attr_get(struct item *it, enum attr_type attr_type, struct attr *attr)
186{ 182{
187 if (it->meth) 183 if (it->meth)
188 { 184 {
189 return it->meth->item_attr_get(it->priv_data, attr_type, attr); 185 return it->meth->item_attr_get(it->priv_data, attr_type, attr);
190 } 186 }
191 else 187 else
192 { 188 {
193 dbg(0,"not method found\n"); 189 dbg(0, "not method found\n");
194 return 0; 190 return 0;
195 } 191 }
196} 192}
197 193
198int
199item_attr_set(struct item *it, struct attr *attr, enum change_mode mode) 194int item_attr_set(struct item *it, struct attr *attr, enum change_mode mode)
200{ 195{
201 if (!it->meth->item_attr_set) 196 if (!it->meth->item_attr_set)
202 return 0; 197 return 0;
203 return it->meth->item_attr_set(it->priv_data, attr, mode); 198 return it->meth->item_attr_set(it->priv_data, attr, mode);
204} 199}
212 /* FIXME evaluate arguments */ 207 /* FIXME evaluate arguments */
213 208
214 return it; 209 return it;
215} 210}
216 211
217enum item_type
218item_from_name(const char *name) 212enum item_type item_from_name(const char *name)
219{ 213{
220 int i; 214 int i;
221 215
222 for (i=0 ; i < sizeof(item_names)/sizeof(struct item_name) ; i++) { 216 for (i = 0; i < sizeof(item_names) / sizeof(struct item_name); i++)
217 {
223 if (! strcmp(item_names[i].name, name)) 218 if (!strcmp(item_names[i].name, name))
224 return item_names[i].item; 219 return item_names[i].item;
225 } 220 }
226 return type_none; 221 return type_none;
227} 222}
228 223
229char * 224char *
230item_to_name(enum item_type item) 225item_to_name(enum item_type item)
231{ 226{
232 int i; 227 int i;
233 228
234 for (i=0 ; i < sizeof(item_names)/sizeof(struct item_name) ; i++) { 229 for (i = 0; i < sizeof(item_names) / sizeof(struct item_name); i++)
230 {
235 if (item_names[i].item == item) 231 if (item_names[i].item == item)
236 return item_names[i].name; 232 return item_names[i].name;
237 } 233 }
238 return NULL; 234 return NULL;
239} 235}
240 236
241struct item_hash { 237struct item_hash
238{
242 GHashTable *h; 239 GHashTable *h;
243}; 240};
244 241
245static guint
246item_hash_hash(gconstpointer key) 242static guint item_hash_hash(gconstpointer key)
247{ 243{
248 const struct item *itm=key; 244 const struct item *itm = key;
249 gconstpointer hashkey=(gconstpointer)GINT_TO_POINTER(itm->id_hi^itm->id_lo^(GPOINTER_TO_INT(itm->map))); 245 gconstpointer hashkey = (gconstpointer) GINT_TO_POINTER(itm->id_hi ^ itm->id_lo ^ (GPOINTER_TO_INT(itm->map)));
250 return g_direct_hash(hashkey); 246 return g_direct_hash(hashkey);
251} 247}
252 248
253static gboolean
254item_hash_equal(gconstpointer a, gconstpointer b) 249static gboolean item_hash_equal(gconstpointer a, gconstpointer b)
255{ 250{
256 const struct item *itm_a=a; 251 const struct item *itm_a = a;
257 const struct item *itm_b=b; 252 const struct item *itm_b = b;
258 if (item_is_equal(*itm_a, *itm_b)) 253 if (item_is_equal(*itm_a, *itm_b))
259 return TRUE; 254 return TRUE;
260 return FALSE; 255 return FALSE;
261} 256}
262 257
263unsigned int
264item_id_hash(const void *key) 258unsigned int item_id_hash(const void *key)
265{ 259{
266 const struct item_id *id=key; 260 const struct item_id *id = key;
267 return id->id_hi^id->id_lo; 261 return id->id_hi ^ id->id_lo;
268} 262}
269 263
270int
271item_id_equal(const void *a, const void *b) 264int item_id_equal(const void *a, const void *b)
272{ 265{
273 const struct item_id *id_a=a; 266 const struct item_id *id_a = a;
274 const struct item_id *id_b=b; 267 const struct item_id *id_b = b;
275 return (id_a->id_hi == id_b->id_hi && id_a->id_lo == id_b->id_lo); 268 return (id_a->id_hi == id_b->id_hi && id_a->id_lo == id_b->id_lo);
276} 269}
277
278
279 270
280struct item_hash * 271struct item_hash *
281item_hash_new(void) 272item_hash_new(void)
282{ 273{
283 struct item_hash *ret=g_new(struct item_hash, 1); 274 struct item_hash *ret=g_new(struct item_hash, 1);
284 275
285 ret->h=g_hash_table_new_full(item_hash_hash, item_hash_equal, g_free, NULL); 276 ret->h = g_hash_table_new_full(item_hash_hash, item_hash_equal, g_free, NULL);
286 return ret; 277 return ret;
287} 278}
288 279
289void
290item_hash_insert(struct item_hash *h, struct item *item, void *val) 280void item_hash_insert(struct item_hash *h, struct item *item, void *val)
291{ 281{
292 struct item *hitem=g_new(struct item, 1); 282 struct item *hitem=g_new(struct item, 1);
293 *hitem=*item; 283 *hitem = *item;
294 dbg(2,"inserting (0x%x,0x%x) into %p\n", item->id_hi, item->id_lo, h->h); 284 dbg(2, "inserting (0x%x,0x%x) into %p\n", item->id_hi, item->id_lo, h->h);
295 g_hash_table_insert(h->h, hitem, val); 285 g_hash_table_insert(h->h, hitem, val);
296} 286}
297 287
298int
299item_hash_remove(struct item_hash *h, struct item *item) 288int item_hash_remove(struct item_hash *h, struct item *item)
300{ 289{
301 int ret; 290 int ret;
302 291
303 dbg(2,"removing (0x%x,0x%x) from %p\n", item->id_hi, item->id_lo, h->h); 292 dbg(2, "removing (0x%x,0x%x) from %p\n", item->id_hi, item->id_lo, h->h);
304 ret=g_hash_table_remove(h->h, item); 293 ret = g_hash_table_remove(h->h, item);
305 dbg(2,"ret=%d\n", ret); 294 dbg(2, "ret=%d\n", ret);
306 295
307 return ret; 296 return ret;
308} 297}
309 298
310void * 299void *
311item_hash_lookup(struct item_hash *h, struct item *item) 300item_hash_lookup(struct item_hash *h, struct item *item)
312{ 301{
313 return g_hash_table_lookup(h->h, item); 302 return g_hash_table_lookup(h->h, item);
314} 303}
315 304
316
317void
318item_hash_destroy(struct item_hash *h) 305void item_hash_destroy(struct item_hash *h)
319{ 306{
320 g_hash_table_destroy(h->h); 307 g_hash_table_destroy(h->h);
321 g_free(h); 308 g_free(h);
322} 309}
323 310
324int
325item_range_intersects_range(struct item_range *range1, struct item_range *range2) 311int item_range_intersects_range(struct item_range *range1, struct item_range *range2)
326{ 312{
327 if (range1->max < range2->min) 313 if (range1->max < range2->min)
328 return 0; 314 return 0;
329 if (range1->min > range2->max) 315 if (range1->min > range2->max)
330 return 0; 316 return 0;
331 return 1; 317 return 1;
332} 318}
333int
334item_range_contains_item(struct item_range *range, enum item_type type) 319int item_range_contains_item(struct item_range *range, enum item_type type)
335{ 320{
336 if (type >= range->min && type <= range->max) 321 if (type >= range->min && type <= range->max)
337 return 1; 322 return 1;
338 return 0; 323 return 0;
339} 324}
340 325
341void
342item_dump_attr_stdout(struct item *item, struct map *map) 326void item_dump_attr_stdout(struct item *item, struct map *map)
343{ 327{
344 struct attr attr; 328 struct attr attr;
345 dbg(0,"type=%s\n", item_to_name(item->type)); 329 dbg(0, "type=%s\n", item_to_name(item->type));
346 while (item_attr_get(item, attr_any, &attr)) 330 while (item_attr_get(item, attr_any, &attr))
347 { 331 {
348 dbg(0," %s='%s'", attr_to_name(attr.type), attr_to_text(&attr, map, 1)); 332 dbg(0, " %s='%s'", attr_to_name(attr.type), attr_to_text(&attr, map, 1));
349 // dbg(0," %s\n", attr_to_name(attr.type)); 333 // dbg(0," %s\n", attr_to_name(attr.type));
350 } 334 }
351} 335}
352 336
353
354void
355item_dump_attr(struct item *item, struct map *map, FILE *out) 337void item_dump_attr(struct item *item, struct map *map, FILE *out)
356{ 338{
357 struct attr attr; 339 struct attr attr;
358 fprintf(out,"type=%s", item_to_name(item->type)); 340 fprintf(out, "type=%s", item_to_name(item->type));
359 while (item_attr_get(item, attr_any, &attr)) 341 while (item_attr_get(item, attr_any, &attr))
360 fprintf(out," %s='%s'", attr_to_name(attr.type), attr_to_text(&attr, map, 1)); 342 fprintf(out, " %s='%s'", attr_to_name(attr.type), attr_to_text(&attr, map, 1));
361} 343}
362 344
363void
364item_dump_filedesc(struct item *item, struct map *map, FILE *out) 345void item_dump_filedesc(struct item *item, struct map *map, FILE *out)
365{ 346{
366 347
367 int i,count,max=16384; 348 int i, count, max = 16384;
368 struct coord *ca=g_alloca(sizeof(struct coord)*max); 349 struct coord *ca = g_alloca(sizeof(struct coord) * max);
369 350
370 count=item_coord_get(item, ca, item->type < type_line ? 1: max); 351 count = item_coord_get(item, ca, item->type < type_line ? 1 : max);
371 if (item->type < type_line) 352 if (item->type < type_line)
372 fprintf(out,"mg:0x%x 0x%x ", ca[0].x, ca[0].y); 353 fprintf(out, "mg:0x%x 0x%x ", ca[0].x, ca[0].y);
373 item_dump_attr(item, map, out); 354 item_dump_attr(item, map, out);
374 fprintf(out,"\n"); 355 fprintf(out, "\n");
375 if (item->type >= type_line) 356 if (item->type >= type_line)
376 for (i = 0 ; i < count ; i++) 357 for (i = 0; i < count; i++)
377 fprintf(out,"mg:0x%x 0x%x\n", ca[i].x, ca[i].y); 358 fprintf(out, "mg:0x%x 0x%x\n", ca[i].x, ca[i].y);
378} 359}

Legend:
Removed from v.26  
changed lines
  Added in v.27

   
Visit the ZANavi Wiki