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

Contents of /navit/navit/mapset.c

Parent Directory Parent Directory | Revision Log Revision Log


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

   
Visit the ZANavi Wiki