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

Contents of /navit/navit/mapset.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 31 - (show annotations) (download)
Mon Feb 4 17:41:59 2013 UTC (6 years, 7 months ago) by zoff99
File MIME type: text/plain
File size: 12413 byte(s)
new map version, lots of fixes and experimental new features
1 /**
2 * ZANavi, Zoff Android Navigation system.
3 * Copyright (C) 2011-2012 Zoff <zoff@zoff.cc>
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 /**
21 * Navit, a modular navigation system.
22 * Copyright (C) 2005-2008 Navit Team
23 *
24 * This program is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU General Public License
26 * version 2 as published by the Free Software Foundation.
27 *
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the
35 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
36 * Boston, MA 02110-1301, USA.
37 */
38
39 /** @file
40 *
41 * @brief Contains code used for loading more than one map
42 *
43 * The code in this file introduces "mapsets", which are collections of several maps.
44 * This enables navit to operate on more than one map at once. See map.c / map.h to learn
45 * how maps are handled.
46 */
47
48 #include <string.h>
49 #include <glib.h>
50 #include <glib/gprintf.h>
51 #include "debug.h"
52 #include "item.h"
53 #include "mapset.h"
54 #include "projection.h"
55 #include "map.h"
56
57 /**
58 * @brief A mapset
59 *
60 * This structure holds a complete mapset
61 */
62 struct mapset
63 {
64 GList *maps; /**< Linked list of all the maps in the mapset */
65 };
66
67 struct attr_iter
68 {
69 GList *last;
70 };
71
72 /**
73 * @brief Creates a new, empty mapset
74 *
75 * @return The new mapset
76 */
77 struct mapset *mapset_new(struct attr *parent, struct attr **attrs)
78 {
79 #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
80 dbg(0,"+#+:enter\n");
81 #endif
82 struct mapset *ms;
83
84 ms=g_new0(struct mapset, 1);
85
86 return ms;
87 }
88
89 struct attr_iter *
90 mapset_attr_iter_new(void)
91 {
92 #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
93 dbg(0,"+#+:enter\n");
94 #endif
95 return g_new0(struct attr_iter, 1);
96 }
97
98 void mapset_attr_iter_destroy(struct attr_iter *iter)
99 {
100 #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
101 dbg(0,"+#+:enter\n");
102 #endif
103 g_free(iter);
104 }
105
106 /**
107 * @brief Adds a map to a mapset
108 *
109 * @param ms The mapset to add the map to
110 * @param m The map to be added
111 */
112 int mapset_add_attr(struct mapset *ms, struct attr *attr)
113 {
114 #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
115 dbg(0,"+#+:enter\n");
116 #endif
117 switch (attr->type)
118 {
119 case attr_map:
120 ms->maps = g_list_append(ms->maps, attr->u.map);
121 map_ref(attr->u.map);
122 return 1;
123 default:
124 return 0;
125 }
126 }
127
128 int mapset_add_attr_name(struct mapset *ms, struct attr *attr)
129 {
130 #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
131 dbg(0,"+#+:enter\n");
132 #endif
133 struct attr map_name;
134
135 dbg(0, "****** ADD MAP from sdcard ******");
136 switch (attr->type)
137 {
138 case attr_map:
139 ms->maps = g_list_append(ms->maps, attr->u.map);
140 map_ref(attr->u.map);
141
142 struct map *m;
143 m=g_new0(struct map, 1);
144 m = attr->u.map;
145 struct attr *map_file_name = attr_search(m->attrs, NULL, attr_data);
146 map_name.type = attr_name;
147 map_name.u.str = g_strconcat("_ms_sdcard_map:", map_file_name->u.str, NULL);
148
149 dbg(0, "====== map name1=%s", map_name.u.str);
150 map_set_attr(attr->u.map, &map_name);
151
152 return 1;
153 default:
154 return 0;
155 }
156 }
157
158 int mapset_add_attr_name_str(struct mapset *ms, struct attr *attr, char *map_name_str)
159 {
160 #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
161 dbg(0,"+#+:enter\n");
162 #endif
163 struct attr map_name;
164
165 dbg(0, "****** ADD MAP from sdcard - with name str ******");
166 switch (attr->type)
167 {
168 case attr_map:
169 ms->maps = g_list_append(ms->maps, attr->u.map);
170 map_ref(attr->u.map);
171
172 struct map *m;
173 m=g_new0(struct map, 1);
174 m = attr->u.map;
175 struct attr *map_file_name = attr_search(m->attrs, NULL, attr_data);
176 map_name.type = attr_name;
177 map_name.u.str = g_strconcat("_ms_sdcard_map:", map_name_str, NULL);
178
179 dbg(0, "====== map name2=%s", map_name.u.str);
180 map_set_attr(attr->u.map, &map_name);
181
182 return 1;
183 default:
184 return 0;
185 }
186 }
187
188 int mapset_remove_attr(struct mapset *ms, struct attr *attr)
189 {
190 #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
191 dbg(0,"+#+:enter\n");
192 #endif
193 switch (attr->type)
194 {
195 case attr_map:
196 ms->maps = g_list_remove(ms->maps, attr->u.map);
197 return 1;
198 default:
199 return 0;
200 }
201 }
202
203 int mapset_get_attr(struct mapset *ms, enum attr_type type, struct attr *attr, struct attr_iter *iter)
204 {
205 #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
206 dbg(0,"+#+:enter\n");
207 #endif
208 GList *map;
209 map = ms->maps;
210 attr->type = type;
211 switch (type)
212 {
213 case attr_map:
214 while (map)
215 {
216 if (!iter || iter->last == g_list_previous(map))
217 {
218 attr->u.map = map->data;
219 if (iter)
220 iter->last = map;
221 return 1;
222 }
223 map = g_list_next(map);
224 }
225 break;
226 default:
227 break;
228 }
229 return 0;
230 }
231
232 #if 0
233 static void mapset_maps_free(struct mapset *ms)
234 {
235 /* todo */
236 }
237 #endif
238
239 /**
240 * @brief Destroys a mapset.
241 *
242 * This destroys a mapset. Please note that it does not touch the contained maps
243 * in any way.
244 *
245 * @param ms The mapset to be destroyed
246 */
247 void mapset_destroy(struct mapset *ms)
248 {
249 #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
250 dbg(0,"+#+:enter\n");
251 #endif
252 GList *map;
253 map = ms->maps;
254 while (map)
255 {
256 map_destroy(map->data);
257 map = g_list_next(map);
258 }
259 g_free(ms);
260 }
261
262 /**
263 * @brief Handle for a mapset in use
264 *
265 * This struct is used for a mapset that is in use. With this it is possible to iterate
266 * all maps in a mapset.
267 */
268 struct mapset_handle
269 {
270 GList *l; /**< Pointer to the current (next) map */
271 };
272
273 /**
274 * @brief Returns a new handle for a mapset
275 *
276 * This returns a new handle for an existing mapset. The new handle points to the first
277 * map in the set.
278 *
279 * @param ms The mapset to get a handle of
280 * @return The new mapset handle
281 */
282 struct mapset_handle *
283 mapset_open(struct mapset *ms)
284 {
285 #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
286 dbg(0,"+#+:enter\n");
287 #endif
288 struct mapset_handle *ret = NULL;
289 if (ms)
290 {
291 ret=g_new(struct mapset_handle, 1);
292 ret->l = ms->maps;
293 }
294
295 return ret;
296 }
297
298 /**
299 * @brief Gets the next map from a mapset handle
300 *
301 * If you set active to true, this function will not return any maps that
302 * have the attr_active attribute associated with them and set to false.
303 *
304 * @param msh The mapset handle to get the next map of
305 * @param active Set to true to only get active maps (See description)
306 * @return The next map
307 */
308 struct map * mapset_next(struct mapset_handle *msh, int active)
309 {
310 #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
311 dbg(0,"+#+:enter\n");
312 #endif
313 struct map *ret;
314 struct attr active_attr;
315
316 for (;;)
317 {
318 if (!msh || !msh->l)
319 return NULL;
320 ret = msh->l->data;
321 msh->l = g_list_next(msh->l);
322 if (!active)
323 return ret;
324 if (active == 2 && map_get_attr(ret, attr_route_active, &active_attr, NULL))
325 {
326 if (active_attr.u.num)
327 return ret;
328 else
329 continue;
330 }
331 if (active == 3 && map_get_attr(ret, attr_search_active, &active_attr, NULL))
332 {
333 if (active_attr.u.num)
334 return ret;
335 else
336 continue;
337 }
338 if (!map_get_attr(ret, attr_active, &active_attr, NULL))
339 return ret;
340 if (active_attr.u.num)
341 return ret;
342 }
343 }
344
345 /**
346 * @brief Gets a map from the mapset by name
347 *
348 * @param ms The map
349 * @param map_name the map name used by the search
350 * @return The next map
351 */
352 struct map *
353 mapset_get_map_by_name(struct mapset *ms, char *map_name)
354 {
355 #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
356 dbg(0,"+#+:enter\n");
357 #endif
358 struct mapset_handle*msh;
359 struct map*curr_map;
360 struct attr map_attr;
361 if (!ms || !map_name)
362 {
363 return NULL;
364 }
365 msh = mapset_open(ms);
366 while ((curr_map = mapset_next(msh, 1)))
367 {
368 //get map name
369 if (map_get_attr(curr_map, attr_name, &map_attr, NULL))
370 {
371 if (!strcmp(map_attr.u.str, map_name))
372 {
373 break;
374 }
375 }
376 }
377 return curr_map;
378 }
379
380 /**
381 * @brief Closes a mapset handle after it is no longer used
382 *
383 * @param msh Mapset handle to be closed
384 */
385 void mapset_close(struct mapset_handle *msh)
386 {
387 #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
388 dbg(0,"+#+:enter\n");
389 #endif
390 g_free(msh);
391 }
392
393 /**
394 * @brief Holds information about a search in a mapset
395 *
396 * This struct holds information about a search (e.g. for a street) in a mapset.
397 *
398 * @sa For a more detailed description see the documentation of mapset_search_new().
399 */
400 struct mapset_search
401 {
402 GList *map; /**< The list of maps to be searched within */
403 struct map_search *ms; /**< A map search struct for the map currently active */
404 struct item *item; /**< "Superior" item. */
405 struct attr *search_attr; /**< Attribute to be searched for. */
406 int partial; /**< Indicates if one would like to have partial matches */
407 };
408
409 /**
410 * @brief Starts a search on a mapset
411 *
412 * This function starts a search on a mapset. What attributes one can search for depends on the
413 * map plugin. See the description of map_search_new() in map.c for details.
414 *
415 * If you enable partial matches bear in mind that the search matches only the begin of the
416 * strings - a search for a street named "street" would match to "streetfoo", but not to
417 * "somestreet". Search is case insensitive.
418 *
419 * The item passed to this function specifies a "superior item" to "search within" - e.g. a town
420 * in which we want to search for a street, or a country in which to search for a town.
421 *
422 * @param ms The mapset that should be searched
423 * @param item Specifies a superior item to "search within" (see description)
424 * @param search_attr Attribute specifying what to search for. See description.
425 * @param partial Set this to true to also have partial matches. See description.
426 * @return A new mapset search struct for this search
427 */
428 struct mapset_search *
429 mapset_search_new(struct mapset *ms, struct item *item, struct attr *search_attr, int partial)
430 {
431 #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
432 dbg(0,"+#+:enter\n");
433 #endif
434 struct mapset_search *this;
435 //dbg(0,"enter(%p,%p,%p,%d)\n", ms, item, search_attr, partial);
436 this=g_new0(struct mapset_search,1);
437 if (this != NULL && ms != NULL)
438 {
439 this->map = ms->maps;
440 this->item = item;
441 this->search_attr = search_attr;
442 this->partial = partial;
443 this->ms = map_search_new(this->map->data, item, search_attr, partial);
444 return this;
445 }
446 else
447 {
448 return NULL;
449 }
450 }
451
452 /**
453 * @brief Returns the next found item from a mapset search
454 *
455 * This function returns the next item from a mapset search or NULL if there are no more items found.
456 * It automatically iterates through all the maps in the mapset. Please note that maps which have the
457 * attr_active attribute associated with them and set to false are not searched.
458 *
459 * @param this The mapset search to return an item from
460 * @return The next found item or NULL if there are no more items found
461 */
462 struct item *
463 mapset_search_get_item(struct mapset_search *this_)
464 {
465 #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
466 dbg(0,"+#+:enter\n");
467 #endif
468 struct item *ret = NULL;
469 struct attr active_attr;
470
471 while ((this_) && (!this_->ms || !(ret = map_search_get_item(this_->ms))))
472 { /* The current map has no more items to be returned */
473 if (this_->search_attr->type >= attr_country_all && this_->search_attr->type <= attr_country_name)
474 break;
475 for (;;)
476 {
477 this_->map = g_list_next(this_->map);
478 if (!this_->map)
479 break;
480 if (map_get_attr(this_->map->data, attr_search_active, &active_attr, NULL))
481 {
482 if (!active_attr.u.num)
483 continue;
484 }
485 if (!map_get_attr(this_->map->data, attr_active, &active_attr, NULL))
486 break;
487 if (active_attr.u.num)
488 break;
489 }
490 if (!this_->map)
491 break;
492 map_search_destroy(this_->ms);
493 this_->ms = map_search_new(this_->map->data, this_->item, this_->search_attr, this_->partial);
494 }
495 return ret;
496 }
497
498 /**
499 * @brief Destroys a mapset search
500 *
501 * @param this The mapset search to be destroyed
502 */
503 void mapset_search_destroy(struct mapset_search *this_)
504 {
505 #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
506 dbg(0,"+#+:enter\n");
507 #endif
508 if (this_)
509 {
510 map_search_destroy(this_->ms);
511 g_free(this_);
512 }
513 }

   
Visit the ZANavi Wiki