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 |
#include <string.h>
|
21 |
#include <stdio.h>
|
22 |
#include <stdlib.h>
|
23 |
#include <glib.h>
|
24 |
#include "debug.h"
|
25 |
#include "item.h"
|
26 |
#include "country.h"
|
27 |
#include "search.h"
|
28 |
#include "navit_nls.h"
|
29 |
|
30 |
|
31 |
|
32 |
struct country {
|
33 |
int id;
|
34 |
char *car;
|
35 |
char *iso2;
|
36 |
char *iso3;
|
37 |
char *name;
|
38 |
};
|
39 |
|
40 |
#include "country_countrytable.h"
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
struct country_search {
|
46 |
struct attr search;
|
47 |
int len;
|
48 |
int partial;
|
49 |
struct item item;
|
50 |
int count;
|
51 |
struct country *country;
|
52 |
enum attr_type attr_next;
|
53 |
};
|
54 |
|
55 |
static int
|
56 |
country_attr_get(void *priv_data, enum attr_type attr_type, struct attr *attr)
|
57 |
{
|
58 |
struct country_search *this_=priv_data;
|
59 |
struct country *country=this_->country;
|
60 |
|
61 |
attr->type=attr_type;
|
62 |
switch (attr_type) {
|
63 |
case attr_any:
|
64 |
while (this_->attr_next != attr_none) {
|
65 |
if (country_attr_get(this_, this_->attr_next, attr))
|
66 |
return 1;
|
67 |
}
|
68 |
return 0;
|
69 |
case attr_label:
|
70 |
attr->u.str=gettext(country->name);
|
71 |
this_->attr_next=attr_country_id;
|
72 |
return 1;
|
73 |
case attr_country_id:
|
74 |
attr->u.num=country->id;
|
75 |
this_->attr_next=country->car ? attr_country_car : attr_country_iso2;
|
76 |
return 1;
|
77 |
case attr_country_car:
|
78 |
attr->u.str=country->car;
|
79 |
this_->attr_next=attr_country_iso2;
|
80 |
return attr->u.str ? 1 : 0;
|
81 |
case attr_country_iso2:
|
82 |
attr->u.str=country->iso2;
|
83 |
this_->attr_next=attr_country_iso3;
|
84 |
return 1;
|
85 |
case attr_country_iso3:
|
86 |
attr->u.str=country->iso3;
|
87 |
this_->attr_next=attr_country_name;
|
88 |
return 1;
|
89 |
case attr_country_name:
|
90 |
attr->u.str=gettext(country->name);
|
91 |
this_->attr_next=attr_none;
|
92 |
return 1;
|
93 |
default:
|
94 |
return 0;
|
95 |
}
|
96 |
}
|
97 |
|
98 |
|
99 |
|
100 |
struct item_methods country_meth = {
|
101 |
NULL, /* coord_rewind */
|
102 |
NULL, /* coord_get */
|
103 |
NULL, /* attr_rewind */
|
104 |
country_attr_get, /* attr_get */
|
105 |
};
|
106 |
|
107 |
struct country_search *
|
108 |
country_search_new(struct attr *search, int partial)
|
109 |
{
|
110 |
struct country_search *ret=g_new(struct country_search, 1);
|
111 |
ret->search=*search;
|
112 |
if (search->type != attr_country_id)
|
113 |
ret->len=strlen(search->u.str);
|
114 |
else
|
115 |
ret->len=0;
|
116 |
ret->partial=partial;
|
117 |
ret->count=0;
|
118 |
|
119 |
ret->item.type=type_country_label;
|
120 |
ret->item.id_hi=0;
|
121 |
ret->item.map=NULL;
|
122 |
ret->item.meth=&country_meth;
|
123 |
ret->item.priv_data=ret;
|
124 |
|
125 |
return ret;
|
126 |
}
|
127 |
|
128 |
char* str_tolower(char *name)
|
129 |
{
|
130 |
int i;
|
131 |
char *str;
|
132 |
str = g_strdup(name);
|
133 |
for(i = 0; str[i]; i++)
|
134 |
{
|
135 |
str[i] = tolower(str[i]);
|
136 |
}
|
137 |
return str;
|
138 |
}
|
139 |
|
140 |
static int
|
141 |
match(struct country_search *this_, enum attr_type type, const char *name)
|
142 |
{
|
143 |
int ret;
|
144 |
char *s1 = NULL;
|
145 |
char *s2 = NULL;
|
146 |
|
147 |
if (!name)
|
148 |
return 0;
|
149 |
|
150 |
if (!this_->search.u.str)
|
151 |
{
|
152 |
return 0;
|
153 |
}
|
154 |
|
155 |
if (this_->search.type != type && this_->search.type != attr_country_all)
|
156 |
return 0;
|
157 |
|
158 |
|
159 |
//fprintf(stderr, "1 name=%s str=%s\n", name, this_->search.u.str);
|
160 |
|
161 |
//s1=linguistics_casefold(this_->search.u.str);
|
162 |
s1=str_tolower(this_->search.u.str);
|
163 |
|
164 |
//fprintf(stderr, "2 name=%s str=%s\n", name, this_->search.u.str);
|
165 |
//fprintf(stderr, "3 s1=%s\n", s1);
|
166 |
|
167 |
//s2=linguistics_casefold(name);
|
168 |
s2=str_tolower(name);
|
169 |
|
170 |
//fprintf(stderr, "4 name=%s str=%s\n", name, this_->search.u.str);
|
171 |
//fprintf(stderr, "5 s2=%s\n", s2);
|
172 |
|
173 |
//if ((s1 == NULL)||(s2 == NULL))
|
174 |
//{
|
175 |
// // ok i give up, i cant find the damn bug. so here is a stupid!! stupid!! workaround
|
176 |
// fprintf(stderr, "2 name=%s str=%s\n", name, this_->search.u.str);
|
177 |
// fprintf(stderr, "3 s1=%s\n", s1);
|
178 |
// fprintf(stderr, "5 s2=%s\n", s2);
|
179 |
// return 0;
|
180 |
//}
|
181 |
//else
|
182 |
//{
|
183 |
ret=linguistics_compare(s2,s1,this_->partial)==0;
|
184 |
//}
|
185 |
//fprintf(stderr, "6 s1=%s\n", s1);
|
186 |
//fprintf(stderr, "7 s2=%s\n", s2);
|
187 |
|
188 |
if (s1)
|
189 |
{
|
190 |
g_free(s1);
|
191 |
}
|
192 |
|
193 |
if (s2)
|
194 |
{
|
195 |
g_free(s2);
|
196 |
}
|
197 |
|
198 |
return ret;
|
199 |
}
|
200 |
|
201 |
|
202 |
struct item *
|
203 |
country_search_get_item(struct country_search *this_)
|
204 |
{
|
205 |
for (;;) {
|
206 |
if (this_->count >= sizeof(country)/sizeof(struct country))
|
207 |
return NULL;
|
208 |
this_->country=&country[this_->count++];
|
209 |
if ((this_->search.type == attr_country_id && this_->search.u.num == this_->country->id) ||
|
210 |
match(this_, attr_country_iso3, this_->country->iso3) ||
|
211 |
match(this_, attr_country_iso2, this_->country->iso2) ||
|
212 |
match(this_, attr_country_car, this_->country->car) ||
|
213 |
match(this_, attr_country_name, gettext(this_->country->name))) {
|
214 |
this_->item.id_lo=this_->country->id;
|
215 |
return &this_->item;
|
216 |
}
|
217 |
}
|
218 |
}
|
219 |
|
220 |
static struct attr country_default_attr;
|
221 |
static char iso2[3];
|
222 |
|
223 |
struct attr *
|
224 |
country_default(void)
|
225 |
{
|
226 |
char *lang;
|
227 |
if (country_default_attr.u.str)
|
228 |
return &country_default_attr;
|
229 |
lang=getenv("LANG");
|
230 |
if (!lang || strlen(lang) < 5)
|
231 |
return NULL;
|
232 |
strncpy(iso2, lang+3, 2);
|
233 |
country_default_attr.type=attr_country_iso2;
|
234 |
country_default_attr.u.str=iso2;
|
235 |
return &country_default_attr;
|
236 |
}
|
237 |
|
238 |
void
|
239 |
country_search_destroy(struct country_search *this_)
|
240 |
{
|
241 |
g_free(this_);
|
242 |
}
|