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

Contents of /navit/navit/mapset.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (hide annotations) (download)
Fri Oct 28 21:19:04 2011 UTC (12 years, 5 months ago) by zoff99
File MIME type: text/plain
File size: 10396 byte(s)
import files
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     // copied from map.c !!!!!!!!!
98     // copied from map.c !!!!!!!!!
99     struct map {
100     struct map_methods meth; /**< Structure with pointers to the map plugin's functions */
101     struct map_priv *priv; /**< Private data of the map, only known to the map plugin */
102     struct attr **attrs; /**< Attributes of this map */
103     struct callback_list *attr_cbl; /**< List of callbacks that are called when attributes change */
104     int refcount;
105     };
106     // copied from map.c !!!!!!!!!
107     // copied from map.c !!!!!!!!!
108    
109     int
110     mapset_add_attr_name(struct mapset *ms, struct attr *attr)
111     {
112     struct attr map_name;
113    
114     dbg(0,"****** ADD MAP from sdcard ******");
115     switch (attr->type) {
116     case attr_map:
117     ms->maps=g_list_append(ms->maps, attr->u.map);
118     map_ref(attr->u.map);
119    
120     struct map *m;
121     m=g_new0(struct map, 1);
122     m=attr->u.map;
123     struct attr *map_file_name=attr_search(m->attrs, NULL, attr_data);
124     map_name.type=attr_name;
125     // map_name.u.str="_ms_sdcard_map";
126     map_name.u.str=g_strconcat("_ms_sdcard_map:",map_file_name->u.str,NULL);
127     // map_name.u.str=g_strdup(map_file_name->u.str);
128    
129     dbg(0,"====== map name=%s",map_name.u.str);
130     map_set_attr(attr->u.map, &map_name);
131    
132     return 1;
133     default:
134     return 0;
135     }
136     }
137    
138    
139     int
140     mapset_remove_attr(struct mapset *ms, struct attr *attr)
141     {
142     switch (attr->type) {
143     case attr_map:
144     ms->maps=g_list_remove(ms->maps, attr->u.map);
145     return 1;
146     default:
147     return 0;
148     }
149     }
150    
151     int
152     mapset_get_attr(struct mapset *ms, enum attr_type type, struct attr *attr, struct attr_iter *iter)
153     {
154     GList *map;
155     map=ms->maps;
156     attr->type=type;
157     switch (type) {
158     case attr_map:
159     while (map) {
160     if (!iter || iter->last == g_list_previous(map)) {
161     attr->u.map=map->data;
162     if (iter)
163     iter->last=map;
164     return 1;
165     }
166     map=g_list_next(map);
167     }
168     break;
169     default:
170     break;
171     }
172     return 0;
173     }
174    
175    
176     #if 0
177     static void mapset_maps_free(struct mapset *ms)
178     {
179     /* todo */
180     }
181     #endif
182    
183     /**
184     * @brief Destroys a mapset.
185     *
186     * This destroys a mapset. Please note that it does not touch the contained maps
187     * in any way.
188     *
189     * @param ms The mapset to be destroyed
190     */
191     void mapset_destroy(struct mapset *ms)
192     {
193     GList *map;
194     map=ms->maps;
195     while (map) {
196     map_destroy(map->data);
197     map=g_list_next(map);
198     }
199     g_free(ms);
200     }
201    
202     /**
203     * @brief Handle for a mapset in use
204     *
205     * This struct is used for a mapset that is in use. With this it is possible to iterate
206     * all maps in a mapset.
207     */
208     struct mapset_handle {
209     GList *l; /**< Pointer to the current (next) map */
210     };
211    
212     /**
213     * @brief Returns a new handle for a mapset
214     *
215     * This returns a new handle for an existing mapset. The new handle points to the first
216     * map in the set.
217     *
218     * @param ms The mapset to get a handle of
219     * @return The new mapset handle
220     */
221     struct mapset_handle *
222     mapset_open(struct mapset *ms)
223     {
224     struct mapset_handle *ret=NULL;
225     if(ms)
226     {
227     ret=g_new(struct mapset_handle, 1);
228     ret->l=ms->maps;
229     }
230    
231     return ret;
232     }
233    
234     /**
235     * @brief Gets the next map from a mapset handle
236     *
237     * If you set active to true, this function will not return any maps that
238     * have the attr_active attribute associated with them and set to false.
239     *
240     * @param msh The mapset handle to get the next map of
241     * @param active Set to true to only get active maps (See description)
242     * @return The next map
243     */
244     struct map * mapset_next(struct mapset_handle *msh, int active)
245     {
246     struct map *ret;
247     struct attr active_attr;
248    
249     for (;;) {
250     if (!msh || !msh->l)
251     return NULL;
252     ret=msh->l->data;
253     msh->l=g_list_next(msh->l);
254     if (!active)
255     return ret;
256     if (active == 2 && map_get_attr(ret, attr_route_active, &active_attr, NULL)) {
257     if (active_attr.u.num)
258     return ret;
259     else
260     continue;
261     }
262     if (active == 3 && map_get_attr(ret, attr_search_active, &active_attr, NULL)) {
263     if (active_attr.u.num)
264     return ret;
265     else
266     continue;
267     }
268     if (!map_get_attr(ret, attr_active, &active_attr, NULL))
269     return ret;
270     if (active_attr.u.num)
271     return ret;
272     }
273     }
274    
275     /**
276     * @brief Gets a map from the mapset by name
277     *
278     * @param ms The map
279     * @param map_name the map name used by the search
280     * @return The next map
281     */
282     struct map *
283     mapset_get_map_by_name(struct mapset *ms, char*map_name)
284     {
285     struct mapset_handle*msh;
286     struct map*curr_map;
287     struct attr map_attr;
288     if( !ms || !map_name ) {
289     return NULL;
290     }
291     msh=mapset_open(ms);
292     while ((curr_map=mapset_next(msh, 1))) {
293     //get map name
294     if(map_get_attr(curr_map,attr_name, &map_attr,NULL)) {
295     if( ! strcmp(map_attr.u.str, map_name)) {
296     break;
297     }
298     }
299     }
300     return curr_map;
301     }
302    
303     /**
304     * @brief Closes a mapset handle after it is no longer used
305     *
306     * @param msh Mapset handle to be closed
307     */
308     void
309     mapset_close(struct mapset_handle *msh)
310     {
311     g_free(msh);
312     }
313    
314     /**
315     * @brief Holds information about a search in a mapset
316     *
317     * This struct holds information about a search (e.g. for a street) in a mapset.
318     *
319     * @sa For a more detailed description see the documentation of mapset_search_new().
320     */
321     struct mapset_search {
322     GList *map; /**< The list of maps to be searched within */
323     struct map_search *ms; /**< A map search struct for the map currently active */
324     struct item *item; /**< "Superior" item. */
325     struct attr *search_attr; /**< Attribute to be searched for. */
326     int partial; /**< Indicates if one would like to have partial matches */
327     };
328    
329     /**
330     * @brief Starts a search on a mapset
331     *
332     * This function starts a search on a mapset. What attributes one can search for depends on the
333     * map plugin. See the description of map_search_new() in map.c for details.
334     *
335     * If you enable partial matches bear in mind that the search matches only the begin of the
336     * strings - a search for a street named "street" would match to "streetfoo", but not to
337     * "somestreet". Search is case insensitive.
338     *
339     * The item passed to this function specifies a "superior item" to "search within" - e.g. a town
340     * in which we want to search for a street, or a country in which to search for a town.
341     *
342     * @param ms The mapset that should be searched
343     * @param item Specifies a superior item to "search within" (see description)
344     * @param search_attr Attribute specifying what to search for. See description.
345     * @param partial Set this to true to also have partial matches. See description.
346     * @return A new mapset search struct for this search
347     */
348     struct mapset_search *
349     mapset_search_new(struct mapset *ms, struct item *item, struct attr *search_attr, int partial)
350     {
351     struct mapset_search *this;
352     dbg(1,"enter(%p,%p,%p,%d)\n", ms, item, search_attr, partial);
353     this=g_new0(struct mapset_search,1);
354     if(this != NULL && ms!=NULL )
355     {
356     this->map=ms->maps;
357     this->item=item;
358     this->search_attr=search_attr;
359     this->partial=partial;
360     this->ms=map_search_new(this->map->data, item, search_attr, partial);
361     return this;
362     }
363     else
364     {
365     return NULL;
366     }
367     }
368    
369     /**
370     * @brief Returns the next found item from a mapset search
371     *
372     * This function returns the next item from a mapset search or NULL if there are no more items found.
373     * It automatically iterates through all the maps in the mapset. Please note that maps which have the
374     * attr_active attribute associated with them and set to false are not searched.
375     *
376     * @param this The mapset search to return an item from
377     * @return The next found item or NULL if there are no more items found
378     */
379     struct item *
380     mapset_search_get_item(struct mapset_search *this_)
381     {
382     struct item *ret=NULL;
383     struct attr active_attr;
384    
385     while ((this_) && (!this_->ms || !(ret=map_search_get_item(this_->ms)))) { /* The current map has no more items to be returned */
386     if (this_->search_attr->type >= attr_country_all && this_->search_attr->type <= attr_country_name)
387     break;
388     for (;;) {
389     this_->map=g_list_next(this_->map);
390     if (! this_->map)
391     break;
392     if (map_get_attr(this_->map->data, attr_search_active, &active_attr, NULL)) {
393     if (!active_attr.u.num)
394     continue;
395     }
396     if (!map_get_attr(this_->map->data, attr_active, &active_attr, NULL))
397     break;
398     if (active_attr.u.num)
399     break;
400     }
401     if (! this_->map)
402     break;
403     map_search_destroy(this_->ms);
404     this_->ms=map_search_new(this_->map->data, this_->item, this_->search_attr, this_->partial);
405     }
406     return ret;
407     }
408    
409     /**
410     * @brief Destroys a mapset search
411     *
412     * @param this The mapset search to be destroyed
413     */
414     void
415     mapset_search_destroy(struct mapset_search *this_)
416     {
417     if (this_) {
418     map_search_destroy(this_->ms);
419     g_free(this_);
420     }
421     }

   
Visit the ZANavi Wiki