a6846c5091692ff2f6b0933d37704a7dbf17289e
[navit-package] / navit / mapset.c
1 /**
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                 return 1;
91         default:
92                 return 0;
93         }
94 }
95
96 int
97 mapset_get_attr(struct mapset *ms, enum attr_type type, struct attr *attr, struct attr_iter *iter)
98 {
99         GList *map;
100         map=ms->maps;
101         attr->type=type;
102         switch (type) {
103         case attr_map:
104                 while (map) {
105                         if (!iter || iter->last == g_list_previous(map)) {
106                                 attr->u.map=map->data;
107                                 if (iter)
108                                         iter->last=map;
109                                 return 1;
110                         }
111                         map=g_list_next(map);
112                 }
113                 break;
114         default:
115                 break;
116         }
117         return 0;
118 }
119
120
121 #if 0
122 static void mapset_maps_free(struct mapset *ms)
123 {
124         /* todo */
125 }
126 #endif
127
128 /**
129  * @brief Destroys a mapset. 
130  *
131  * This destroys a mapset. Please note that it does not touch the contained maps
132  * in any way.
133  *
134  * @param ms The mapset to be destroyed
135  */
136 void mapset_destroy(struct mapset *ms)
137 {
138         g_free(ms);
139 }
140
141 /**
142  * @brief Handle for a mapset in use
143  *
144  * This struct is used for a mapset that is in use. With this it is possible to iterate
145  * all maps in a mapset.
146  */
147 struct mapset_handle {
148         GList *l;       /**< Pointer to the current (next) map */
149 };
150
151 /**
152  * @brief Returns a new handle for a mapset
153  *
154  * This returns a new handle for an existing mapset. The new handle points to the first
155  * map in the set.
156  *
157  * @param ms The mapset to get a handle of
158  * @return The new mapset handle
159  */
160 struct mapset_handle *
161 mapset_open(struct mapset *ms)
162 {
163         struct mapset_handle *ret=NULL;
164         if(ms)
165         {
166                 ret=g_new(struct mapset_handle, 1);
167                 ret->l=ms->maps;
168         }
169
170         return ret;
171 }
172
173 /**
174  * @brief Gets the next map from a mapset handle
175  *
176  * If you set active to true, this function will not return any maps that
177  * have the attr_active attribute associated with them and set to false.
178  *
179  * @param msh The mapset handle to get the next map of
180  * @param active Set to true to only get active maps (See description)
181  * @return The next map
182  */
183 struct map * mapset_next(struct mapset_handle *msh, int active)
184 {
185         struct map *ret;
186         struct attr active_attr;
187
188         for (;;) {
189                 if (!msh || !msh->l)
190                         return NULL;
191                 ret=msh->l->data;
192                 msh->l=g_list_next(msh->l);
193                 if (!active)
194                         return ret;                     
195                 dbg(0,"active=%d\n",active);
196                 if (active == 2 && map_get_attr(ret, attr_route_active, &active_attr, NULL)) {
197                         if (active_attr.u.num)
198                                 return ret;
199                         else
200                                 continue;
201                 }
202                 if (!map_get_attr(ret, attr_active, &active_attr, NULL))
203                         return ret;
204                 if (active_attr.u.num)
205                         return ret;
206         }
207 }
208
209 /**
210  * @brief Closes a mapset handle after it is no longer used
211  *
212  * @param msh Mapset handle to be closed
213  */
214 void 
215 mapset_close(struct mapset_handle *msh)
216 {
217         g_free(msh);
218 }
219
220 /**
221  * @brief Holds information about a search in a mapset
222  *
223  * This struct holds information about a search (e.g. for a street) in a mapset. 
224  *
225  * @sa For a more detailed description see the documentation of mapset_search_new().
226  */
227 struct mapset_search {
228         GList *map;                                     /**< The list of maps to be searched within */
229         struct map_search *ms;          /**< A map search struct for the map currently active */
230         struct item *item;                      /**< "Superior" item. */
231         struct attr *search_attr;       /**< Attribute to be searched for. */
232         int partial;                            /**< Indicates if one would like to have partial matches */
233 };
234
235 /**
236  * @brief Starts a search on a mapset
237  *
238  * This function starts a search on a mapset. What attributes one can search for depends on the
239  * map plugin. See the description of map_search_new() in map.c for details.
240  *
241  * If you enable partial matches bear in mind that the search matches only the begin of the
242  * strings - a search for a street named "street" would match to "streetfoo", but not to
243  * "somestreet". Search is case insensitive.
244  *
245  * The item passed to this function specifies a "superior item" to "search within" - e.g. a town 
246  * in which we want to search for a street, or a country in which to search for a town.
247  *
248  * @param ms The mapset that should be searched
249  * @param item Specifies a superior item to "search within" (see description)
250  * @param search_attr Attribute specifying what to search for. See description.
251  * @param partial Set this to true to also have partial matches. See description.
252  * @return A new mapset search struct for this search
253  */
254 struct mapset_search *
255 mapset_search_new(struct mapset *ms, struct item *item, struct attr *search_attr, int partial)
256 {
257         struct mapset_search *this;
258         dbg(1,"enter(%p,%p,%p,%d)\n", ms, item, search_attr, partial);
259         this=g_new0(struct mapset_search,1);
260         if(this != NULL && ms!=NULL )
261         {
262                 this->map=ms->maps;
263                 this->item=item;
264                 this->search_attr=search_attr;
265                 this->partial=partial;
266                 this->ms=map_search_new(this->map->data, item, search_attr, partial);
267                 return this;
268         }
269         else
270         {
271                 return NULL;
272         }
273 }
274
275 /**
276  * @brief Returns the next found item from a mapset search
277  *
278  * This function returns the next item from a mapset search or NULL if there are no more items found.
279  * It automatically iterates through all the maps in the mapset. Please note that maps which have the
280  * attr_active attribute associated with them and set to false are not searched.
281  *
282  * @param this The mapset search to return an item from
283  * @return The next found item or NULL if there are no more items found
284  */
285 struct item *
286 mapset_search_get_item(struct mapset_search *this)
287 {
288         struct item *ret=NULL;
289         struct attr active_attr;
290
291         while ((this) && (!this->ms || !(ret=map_search_get_item(this->ms)))) { /* The current map has no more items to be returned */
292                 if (this->search_attr->type >= attr_country_all && this->search_attr->type <= attr_country_name)
293                         break;
294                 for (;;) {
295                         this->map=g_list_next(this->map);
296                         if (! this->map)
297                                 break;
298                         if (!map_get_attr(this->map->data, attr_active, &active_attr, NULL))
299                                 break;
300                         if (active_attr.u.num)
301                                 break;
302                 }
303                 if (! this->map)
304                         break;
305                 map_search_destroy(this->ms);
306                 this->ms=map_search_new(this->map->data, this->item, this->search_attr, this->partial);
307         }
308         return ret;
309 }
310
311 /**
312  * @brief Destroys a mapset search
313  *
314  * @param this The mapset search to be destroyed
315  */
316 void
317 mapset_search_destroy(struct mapset_search *this)
318 {
319         if (this) {
320                 map_search_destroy(this->ms);
321                 g_free(this);
322         }
323 }