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

Contents of /navit/navit/mapset.c

Parent Directory Parent Directory | Revision Log Revision Log


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

   
Visit the ZANavi Wiki