Fix:data_binfile:Fixed a few search bugs
[navit-package] / navit / mapset.c
1 #include <string.h>
2 #include <glib.h>
3 #include <glib/gprintf.h>
4 #include "debug.h"
5 #include "item.h"
6 #include "mapset.h"
7 #include "projection.h"
8 #include "map.h"
9
10 struct mapset {
11         GList *maps;
12 };
13
14 struct mapset *mapset_new(void)
15 {
16         struct mapset *ms;
17
18         ms=g_new0(struct mapset, 1);
19
20         return ms;
21 }
22
23 void mapset_add(struct mapset *ms, struct map *m)
24 {
25         ms->maps=g_list_append(ms->maps, m);
26 }
27
28 #if 0
29 static void mapset_maps_free(struct mapset *ms)
30 {
31         /* todo */
32 }
33 #endif
34
35 void mapset_destroy(struct mapset *ms)
36 {
37         g_free(ms);
38 }
39
40 struct mapset_handle {
41         GList *l;
42 };
43
44 struct mapset_handle *
45 mapset_open(struct mapset *ms)
46 {
47         struct mapset_handle *ret;
48
49         ret=g_new(struct mapset_handle, 1);
50         ret->l=ms->maps;
51
52         return ret;
53 }
54
55 struct map * mapset_next(struct mapset_handle *msh, int active)
56 {
57         struct map *ret;
58
59         for (;;) {
60                 if (!msh->l)
61                         return NULL;
62                 ret=msh->l->data;
63                 msh->l=g_list_next(msh->l);
64                 if (!active || map_get_active(ret))
65                         return ret;
66         }
67 }
68
69 void 
70 mapset_close(struct mapset_handle *msh)
71 {
72         g_free(msh);
73 }
74
75 struct mapset_search {
76         GList *map;
77         struct map_search *ms;
78         struct item *item;
79         struct attr *search_attr;
80         int partial;
81 };
82
83 struct mapset_search *
84 mapset_search_new(struct mapset *ms, struct item *item, struct attr *search_attr, int partial)
85 {
86         struct mapset_search *this;
87         dbg(1,"enter(%p,%p,%p,%d)\n", ms, item, search_attr, partial);
88         this=g_new0(struct mapset_search,1);
89         this->map=ms->maps;
90         this->item=item;
91         this->search_attr=search_attr;
92         this->partial=partial;
93         this->ms=map_search_new(this->map->data, item, search_attr, partial);
94         return this;
95 }
96
97 struct item *
98 mapset_search_get_item(struct mapset_search *this)
99 {
100         struct item *ret=NULL;
101         while (!this->ms || !(ret=map_search_get_item(this->ms))) {
102                 if (this->search_attr->type >= attr_country_all && this->search_attr->type <= attr_country_name)
103                         break;
104                 do {
105                         this->map=g_list_next(this->map);
106                 } while (this->map && ! map_get_active(this->map->data));
107                 if (! this->map)
108                         break;
109                 map_search_destroy(this->ms);
110                 this->ms=map_search_new(this->map->data, this->item, this->search_attr, this->partial);
111         }
112         return ret;
113 }
114
115 void
116 mapset_search_destroy(struct mapset_search *this)
117 {
118         if (this) {
119                 map_search_destroy(this->ms);
120                 g_free(this);
121         }
122 }