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

Contents of /navit/navit/search.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 15 - (show annotations) (download)
Mon Nov 21 20:54:48 2011 UTC (12 years, 5 months ago) by zoff99
File MIME type: text/plain
File size: 53282 byte(s)
better search, make search interuptable, new translations
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 <stdlib.h>
21 #include <glib.h>
22 #include <string.h>
23 #include <math.h>
24 #include "debug.h"
25 #include "projection.h"
26 #include "item.h"
27 #include "map.h"
28 #include "mapset.h"
29 #include "coord.h"
30 #include "transform.h"
31 #include "search.h"
32 #include "country.h"
33 #include "navit.h"
34
35 #if HAVE_API_ANDROID
36 #include "android.h"
37 #endif
38 #include "layout.h"
39 #include "map.h"
40 #include "linguistics.h"
41
42 struct search_list_level {
43 struct mapset *ms;
44 struct search_list_common *parent;
45 struct attr *attr;
46 int partial;
47 int selected;
48 struct mapset_search *search;
49 GHashTable *hash;
50 GList *list,*curr,*last;
51 };
52
53 struct interpolation {
54 int side, mode, rev;
55 char *first, *last, *curr;
56 };
57
58 struct search_list {
59 struct mapset *ms;
60 struct item *item;
61 int level;
62 struct search_list_level levels[4];
63 struct search_list_result result;
64 struct search_list_result last_result;
65 int last_result_valid;
66 char *postal;
67 struct interpolation inter;
68 };
69
70 static guint
71 search_item_hash_hash(gconstpointer key)
72 {
73 const struct item *itm=key;
74 gconstpointer hashkey=(gconstpointer)GINT_TO_POINTER(itm->id_hi^itm->id_lo);
75 return g_direct_hash(hashkey);
76 }
77
78 static gboolean
79 search_item_hash_equal(gconstpointer a, gconstpointer b)
80 {
81 const struct item *itm_a=a;
82 const struct item *itm_b=b;
83 if (item_is_equal_id(*itm_a, *itm_b))
84 return TRUE;
85 return FALSE;
86 }
87
88 struct search_list *
89 search_list_new(struct mapset *ms)
90 {
91 struct search_list *ret;
92
93 ret=g_new0(struct search_list, 1);
94 ret->ms=ms;
95
96 return ret;
97 }
98
99 static void search_list_search_free(struct search_list *sl, int level);
100
101 static int
102 search_list_level(enum attr_type attr_type)
103 {
104 switch(attr_type) {
105 case attr_country_all:
106 case attr_country_id:
107 case attr_country_iso2:
108 case attr_country_iso3:
109 case attr_country_car:
110 case attr_country_name:
111 return 0;
112 case attr_town_postal:
113 return 1;
114 case attr_town_name:
115 case attr_district_name:
116 case attr_town_or_district_name:
117 return 1;
118 case attr_street_name:
119 return 2;
120 case attr_house_number:
121 return 3;
122 case attr_postal:
123 return -1;
124 default:
125 dbg(0,"unknown search '%s'\n",attr_to_name(attr_type));
126 return -1;
127 }
128 }
129
130 static void
131 interpolation_clear(struct interpolation *inter)
132 {
133 inter->mode=inter->side=0;
134 g_free(inter->first);
135 g_free(inter->last);
136 g_free(inter->curr);
137 inter->first=inter->last=inter->curr=NULL;
138 }
139
140 void
141 search_list_search(struct search_list *this_, struct attr *search_attr, int partial)
142 {
143 struct search_list_level *le;
144 int level=search_list_level(search_attr->type);
145 this_->item=NULL;
146 interpolation_clear(&this_->inter);
147 //dbg(0,"## enter\n");
148 //dbg(0,"## level=%d\n", level);
149 if (level != -1)
150 {
151 this_->result.id=0;
152 this_->level=level;
153 le=&this_->levels[level];
154 search_list_search_free(this_, level);
155 le->attr=attr_dup(search_attr);
156 le->partial=partial;
157 if (level > 0)
158 {
159 le=&this_->levels[level-1];
160 le->curr=le->list;
161 }
162 //dbg(0,"## le=%p partial=%d\n", le, partial);
163 }
164 else if (search_attr->type == attr_postal)
165 {
166 g_free(this_->postal);
167 this_->postal=g_strdup(search_attr->u.str);
168 }
169 //dbg(0,"## return\n");
170 }
171
172 struct search_list_common *
173 search_list_select(struct search_list *this_, enum attr_type attr_type, int id, int mode)
174 {
175 int level=search_list_level(attr_type);
176 int num=0;
177 struct search_list_level *le;
178 struct search_list_common *slc;
179 GList *curr;
180 le=&this_->levels[level];
181 curr=le->list;
182 if (mode > 0 || !id)
183 le->selected=mode;
184 //dbg(0,"enter level=%d %d %d %p\n", level, id, mode, curr);
185 while (curr) {
186 num++;
187 if (! id || num == id) {
188 slc=curr->data;
189 slc->selected=mode;
190 if (id) {
191 le->last=curr;
192 //dbg(0,"found\n");
193 return slc;
194 }
195 }
196 curr=g_list_next(curr);
197 }
198 //dbg(0,"not found\n");
199 return NULL;
200 }
201
202 static void
203 search_list_common_new(struct item *item, struct search_list_common *common)
204 {
205 struct attr attr;
206 if (item_attr_get(item, attr_town_name, &attr))
207 common->town_name=map_convert_string(item->map, attr.u.str);
208 else
209 common->town_name=NULL;
210 if (item_attr_get(item, attr_county_name, &attr))
211 common->county_name=map_convert_string(item->map, attr.u.str);
212 else
213 common->county_name=NULL;
214 if (item_attr_get(item, attr_district_name, &attr))
215 common->district_name=map_convert_string(item->map, attr.u.str);
216 else
217 common->district_name=NULL;
218 if (item_attr_get(item, attr_postal, &attr))
219 common->postal=map_convert_string(item->map, attr.u.str);
220 else if (item_attr_get(item, attr_town_postal, &attr))
221 common->postal=map_convert_string(item->map, attr.u.str);
222 else
223 common->postal=NULL;
224 if (item_attr_get(item, attr_postal_mask, &attr))
225 common->postal_mask=map_convert_string(item->map, attr.u.str);
226 else
227 common->postal_mask=NULL;
228 }
229
230 static void
231 search_list_common_destroy(struct search_list_common *common)
232 {
233 map_convert_free(common->town_name);
234 map_convert_free(common->district_name);
235 map_convert_free(common->county_name);
236 map_convert_free(common->postal);
237 map_convert_free(common->postal_mask);
238 }
239
240 static struct search_list_country *
241 search_list_country_new(struct item *item)
242 {
243 struct search_list_country *ret=g_new0(struct search_list_country, 1);
244 struct attr attr;
245
246 ret->common.item=ret->common.unique=*item;
247 if (item_attr_get(item, attr_country_car, &attr))
248 ret->car=g_strdup(attr.u.str);
249 if (item_attr_get(item, attr_country_iso2, &attr)) {
250 #if HAVE_API_ANDROID
251 ret->iso2=g_malloc(strlen(attr.u.str)+1);
252 strtolower(ret->iso2, attr.u.str);
253 #else
254 ret->iso2=g_strdup(attr.u.str);
255 #endif
256 ret->flag=g_strdup_printf("country_%s", ret->iso2);
257 }
258 if (item_attr_get(item, attr_country_iso3, &attr))
259 ret->iso3=g_strdup(attr.u.str);
260 if (item_attr_get(item, attr_country_name, &attr))
261 ret->name=g_strdup(attr.u.str);
262 return ret;
263 }
264
265 static void
266 search_list_country_destroy(struct search_list_country *this_)
267 {
268 g_free(this_->car);
269 g_free(this_->iso2);
270 g_free(this_->iso3);
271 g_free(this_->flag);
272 g_free(this_->name);
273 g_free(this_);
274 }
275
276 static struct search_list_town *
277 search_list_town_new(struct item *item)
278 {
279 struct search_list_town *ret=g_new0(struct search_list_town, 1);
280 struct attr attr;
281 struct coord c;
282
283 ret->itemt=*item;
284 ret->common.item=ret->common.unique=*item;
285 if (item_attr_get(item, attr_town_streets_item, &attr)) {
286 dbg(1,"town_assoc 0x%x 0x%x\n", attr.u.item->id_hi, attr.u.item->id_lo);
287 ret->common.unique=*attr.u.item;
288 }
289 search_list_common_new(item, &ret->common);
290 if (item_attr_get(item, attr_county_name, &attr))
291 ret->county=map_convert_string(item->map,attr.u.str);
292 else
293 ret->county=NULL;
294 if (item_coord_get(item, &c, 1)) {
295 ret->common.c=g_new(struct pcoord, 1);
296 ret->common.c->x=c.x;
297 ret->common.c->y=c.y;
298 ret->common.c->pro = map_projection(item->map);
299 }
300 return ret;
301 }
302
303 static void
304 search_list_town_destroy(struct search_list_town *this_)
305 {
306 map_convert_free(this_->county);
307 search_list_common_destroy(&this_->common);
308 if (this_->common.c)
309 g_free(this_->common.c);
310 g_free(this_);
311 }
312
313
314 static struct search_list_street *
315 search_list_street_new(struct item *item)
316 {
317 struct search_list_street *ret=g_new0(struct search_list_street, 1);
318 struct attr attr;
319 struct coord c;
320
321 ret->common.item=ret->common.unique=*item;
322 if (item_attr_get(item, attr_street_name, &attr))
323 ret->name=map_convert_string(item->map, attr.u.str);
324 else
325 ret->name=NULL;
326 search_list_common_new(item, &ret->common);
327 if (item_coord_get(item, &c, 1)) {
328 ret->common.c=g_new(struct pcoord, 1);
329 ret->common.c->x=c.x;
330 ret->common.c->y=c.y;
331 ret->common.c->pro = map_projection(item->map);
332 }
333 return ret;
334 }
335
336
337 static void
338 search_list_street_destroy(struct search_list_street *this_)
339 {
340 map_convert_free(this_->name);
341 search_list_common_destroy(&this_->common);
342 if (this_->common.c)
343 {
344 g_free(this_->common.c);
345 }
346 g_free(this_);
347 }
348
349 static char *
350 search_interpolate(struct interpolation *inter)
351 {
352 dbg(1,"interpolate %s-%s %s\n",inter->first,inter->last,inter->curr);
353 if (!inter->first || !inter->last)
354 return NULL;
355 if (!inter->curr)
356 inter->curr=g_strdup(inter->first);
357 else {
358 if (strcmp(inter->curr, inter->last)) {
359 int next=atoi(inter->curr)+(inter->mode?2:1);
360 g_free(inter->curr);
361 if (next == atoi(inter->last))
362 inter->curr=g_strdup(inter->last);
363 else
364 inter->curr=g_strdup_printf("%d",next);
365 } else {
366 g_free(inter->curr);
367 inter->curr=NULL;
368 }
369 }
370 dbg(1,"interpolate result %s\n",inter->curr);
371 return inter->curr;
372 }
373
374 static void
375 search_interpolation_split(char *str, struct interpolation *inter)
376 {
377 char *pos=strchr(str,'-');
378 char *first,*last;
379 int len;
380 if (!pos) {
381 inter->first=g_strdup(str);
382 inter->last=g_strdup(str);
383 inter->rev=0;
384 return;
385 }
386 len=pos-str;
387 first=g_malloc(len+1);
388 strncpy(first, str, len);
389 first[len]='\0';
390 last=g_strdup(pos+1);
391 dbg(1,"%s = %s - %s\n",str, first, last);
392 if (atoi(first) > atoi(last)) {
393 inter->first=last;
394 inter->last=first;
395 inter->rev=1;
396 } else {
397 inter->first=first;
398 inter->last=last;
399 inter->rev=0;
400 }
401 }
402
403 static int
404 search_setup_interpolation(struct item *item, enum attr_type i0, enum attr_type i1, enum attr_type i2, struct interpolation *inter)
405 {
406 struct attr attr;
407 g_free(inter->first);
408 g_free(inter->last);
409 g_free(inter->curr);
410 inter->first=inter->last=inter->curr=NULL;
411 dbg(1,"setup %s\n",attr_to_name(i0));
412 if (item_attr_get(item, i0, &attr)) {
413 search_interpolation_split(attr.u.str, inter);
414 inter->mode=0;
415 } else if (item_attr_get(item, i1, &attr)) {
416 search_interpolation_split(attr.u.str, inter);
417 inter->mode=1;
418 } else if (item_attr_get(item, i2, &attr)) {
419 search_interpolation_split(attr.u.str, inter);
420 inter->mode=2;
421 } else
422 return 0;
423 return 1;
424 }
425
426 static int
427 search_match(char *str, char *search, int partial)
428 {
429 if (!partial)
430 return (!g_strcasecmp(str, search));
431 else
432 return (!g_strncasecmp(str, search, strlen(search)));
433 }
434
435 static struct pcoord *
436 search_house_number_coordinate(struct item *item, struct interpolation *inter)
437 {
438 struct pcoord *ret=g_new(struct pcoord, 1);
439 ret->pro = map_projection(item->map);
440 if (item_is_point(*item)) {
441 struct coord c;
442 if (item_coord_get(item, &c, 1)) {
443 ret->x=c.x;
444 ret->y=c.y;
445 } else {
446 g_free(ret);
447 ret=NULL;
448 }
449 } else {
450 int count,max=1024;
451 int hn_pos,hn_length;
452 struct coord *c=g_alloca(sizeof(struct coord)*max);
453 item_coord_rewind(item);
454 count=item_coord_get(item, c, max);
455 hn_length=atoi(inter->last)-atoi(inter->first);
456 if (inter->rev)
457 hn_pos=atoi(inter->last)-atoi(inter->curr);
458 else
459 hn_pos=atoi(inter->curr)-atoi(inter->first);
460 if (count) {
461 int i,distance_sum=0,hn_distance;
462 int *distances=g_alloca(sizeof(int)*(count-1));
463 dbg(1,"count=%d hn_length=%d hn_pos=%d (%s of %s-%s)\n",count,hn_length,hn_pos,inter->curr,inter->first,inter->last);
464 if (!hn_length) {
465 hn_length=2;
466 hn_pos=1;
467 }
468 if (count == max)
469 dbg(0,"coordinate overflow\n");
470 for (i = 0 ; i < count-1 ; i++) {
471 distances[i]=navit_sqrt(transform_distance_sq(&c[i],&c[i+1]));
472 distance_sum+=distances[i];
473 dbg(1,"distance[%d]=%d\n",i,distances[i]);
474 }
475 dbg(1,"sum=%d\n",distance_sum);
476 hn_distance=distance_sum*hn_pos/hn_length;
477 dbg(1,"hn_distance=%d\n",hn_distance);
478 i=0;
479 while (i < count-1 && hn_distance > distances[i])
480 hn_distance-=distances[i++];
481 dbg(1,"remaining distance=%d from %d\n",hn_distance,distances[i]);
482 ret->x=(c[i+1].x-c[i].x)*hn_distance/distances[i]+c[i].x;
483 ret->y=(c[i+1].y-c[i].y)*hn_distance/distances[i]+c[i].y;
484 }
485 }
486 return ret;
487 }
488
489 static struct search_list_house_number *
490 search_list_house_number_new(struct item *item, struct interpolation *inter, char *inter_match, int inter_partial)
491 {
492 struct search_list_house_number *ret=g_new0(struct search_list_house_number, 1);
493 struct attr attr;
494 char *hn;
495
496 // dbg(0,"@@@@ enter @@@@\n");
497
498 ret->common.item=ret->common.unique=*item;
499 //if (item_attr_get(item, attr_street_name, &attr))
500 // dbg(0,"xx1 %s\n",attr.u.str);
501 if (item_attr_get(item, attr_house_number, &attr))
502 ret->house_number=map_convert_string(item->map, attr.u.str);
503 else {
504 //if (item_attr_get(item, attr_street_name, &attr))
505 // dbg(0,"xx2 %s\n",attr.u.str);
506 for (;;) {
507 //dbg(0,"interpolate 11");
508 ret->interpolation=1;
509 switch(inter->side) {
510 case 0:
511 //dbg(0,"interpolate 11 0");
512 inter->side=-1;
513 search_setup_interpolation(item, attr_house_number_left, attr_house_number_left_odd, attr_house_number_left_even, inter);
514 case -1:
515 //dbg(0,"interpolate 11 -1");
516 if ((hn=search_interpolate(inter)))
517 break;
518 inter->side=1;
519 search_setup_interpolation(item, attr_house_number_right, attr_house_number_right_odd, attr_house_number_right_even, inter);
520 case 1:
521 //dbg(0,"interpolate 11 1");
522 if ((hn=search_interpolate(inter)))
523 break;
524 default:
525 //dbg(0,"interpolate 11 default");
526 g_free(ret);
527 return NULL;
528 }
529 if (search_match(hn, inter_match, inter_partial))
530 {
531 //dbg(0,"interpolate 22");
532 //dbg(0,"match %s %s-%s\n",hn, inter->first, inter->last);
533 ret->house_number=map_convert_string(item->map, hn);
534 break;
535 }
536 }
537 }
538 //dbg(0,"interpolate 33");
539 search_list_common_new(item, &ret->common);
540 ret->common.c=search_house_number_coordinate(item, ret->interpolation?inter:NULL);
541 //dbg(0,"interpolate 44");
542 return ret;
543 }
544
545 static void
546 search_list_house_number_destroy(struct search_list_house_number *this_)
547 {
548 map_convert_free(this_->house_number);
549 search_list_common_destroy(&this_->common);
550 if (this_->common.c)
551 g_free(this_->common.c);
552 g_free(this_);
553 }
554
555 static void
556 search_list_result_destroy(int level, void *p)
557 {
558 switch (level) {
559 case 0:
560 search_list_country_destroy(p);
561 break;
562 case 1:
563 search_list_town_destroy(p);
564 break;
565 case 2:
566 search_list_street_destroy(p);
567 break;
568 case 3:
569 search_list_house_number_destroy(p);
570 break;
571 }
572 }
573
574 static void
575 search_list_search_free(struct search_list *sl, int level)
576 {
577 //dbg(0,"enter\n");
578
579 struct search_list_level *le=&sl->levels[level];
580 GList *next,*curr;
581 if (le->search)
582 {
583 mapset_search_destroy(le->search);
584 le->search=NULL;
585 }
586 #if 0 /* FIXME */
587 if (le->hash) {
588 g_hash_table_destroy(le->hash);
589 le->hash=NULL;
590 }
591 #endif
592 curr=le->list;
593 while (curr)
594 {
595 search_list_result_destroy(level, curr->data);
596 next=g_list_next(curr);
597 curr=next;
598 }
599 attr_free(le->attr);
600 g_list_free(le->list);
601 le->list=NULL;
602 le->curr=NULL;
603 le->last=NULL;
604
605 //dbg(0,"return\n");
606 }
607
608 char *
609 search_postal_merge(char *mask, char *new)
610 {
611 int i;
612 char *ret=NULL;
613 dbg(1,"enter %s %s\n", mask, new);
614 if (!new)
615 return NULL;
616 if (!mask)
617 return g_strdup(new);
618 i=0;
619 while (mask[i] && new[i]) {
620 if (mask[i] != '.' && mask[i] != new[i])
621 break;
622 i++;
623
624 }
625 if (mask[i]) {
626 ret=g_strdup(mask);
627 while (mask[i])
628 ret[i++]='.';
629 }
630 dbg(1,"merged %s with %s as %s\n", mask, new, ret);
631 return ret;
632 }
633
634 char *
635 search_postal_merge_replace(char *mask, char *new)
636 {
637 char *ret=search_postal_merge(mask, new);
638 if (!ret)
639 return mask;
640 g_free(mask);
641 return ret;
642 }
643
644
645 static int
646 postal_match(char *postal, char *mask)
647 {
648 for (;;) {
649 if ((*postal != *mask) && (*mask != '.'))
650 return 0;
651 if (!*postal) {
652 if (!*mask)
653 return 1;
654 else
655 return 0;
656 }
657 postal++;
658 mask++;
659 }
660 }
661
662 static int
663 search_add_result(struct search_list_level *le, struct search_list_common *slc)
664 {
665 struct search_list_common *slo;
666 char *merged;
667
668 //dbg(0,"enter\n");
669
670 //slo=g_hash_table_lookup(le->hash, &slc->unique);
671 //if (!slo) {
672 //g_hash_table_insert(le->hash, &slc->unique, slc);
673 if (slc->postal && !slc->postal_mask)
674 {
675 slc->postal_mask=g_strdup(slc->postal);
676 }
677 // ******
678 g_list_free(le->list);
679 le->list=NULL;
680 // ******
681 le->list=g_list_append(le->list, slc);
682 return 1;
683 //}
684 merged=search_postal_merge(slo->postal_mask, slc->postal);
685 if (merged)
686 {
687 g_free(slo->postal_mask);
688 slo->postal_mask=merged;
689 }
690 return 0;
691 }
692
693 struct search_list_result *
694 search_list_get_result(struct search_list *this_)
695 {
696 struct search_list_level *le,*leu;
697 int level=this_->level;
698 struct attr attr2;
699 int has_street_name=0;
700
701 //dbg(0,"******* enter *******\n");
702 le=&this_->levels[level];
703 //dbg(0,"le=%p\n", le);
704 for (;;)
705 {
706 //dbg(0,"le->search=%p\n", le->search);
707 if (! le->search)
708 {
709 //dbg(0,"partial=%d level=%d\n", le->partial, level);
710 if (! level)
711 le->parent=NULL;
712 else
713 {
714 leu=&this_->levels[level-1];
715 //dbg(0,"leu->curr=%p\n", leu->curr);
716 for (;;)
717 {
718 //dbg(0,"*********########");
719
720 struct search_list_common *slc;
721 if (! leu->curr)
722 {
723 return NULL;
724 }
725 le->parent=leu->curr->data;
726 leu->last=leu->curr;
727 leu->curr=g_list_next(leu->curr);
728 slc=(struct search_list_common *)(le->parent);
729 if (!slc)
730 break;
731 if (slc->selected == leu->selected)
732 break;
733 }
734 }
735 #if 0
736 if (le->parent)
737 {
738 dbg(0,"mapset_search_new with item(%d,%d)\n", le->parent->item.id_hi, le->parent->item.id_lo);
739 }
740 else
741 {
742 dbg(0,"NO parent!!\n");
743 }
744 dbg(0,"############## attr=%s\n", attr_to_name(le->attr->type));
745 #endif
746 le->search=mapset_search_new(this_->ms, &le->parent->item, le->attr, le->partial);
747 // ** DOC ** mapset_search_new(struct mapset *ms, struct item *item, struct attr *search_attr, int partial)
748 le->hash=g_hash_table_new(search_item_hash_hash, search_item_hash_equal);
749 }
750
751 //dbg(0,"le->search=%p\n", le->search);
752
753 if (!this_->item)
754 {
755 //dbg(0,"sssss 1");
756 this_->item=mapset_search_get_item(le->search);
757 //dbg(0,"sssss 1 %p\n",this_->item);
758 }
759
760 if (this_->item)
761 {
762 void *p=NULL;
763 //dbg(0,"id_hi=%d id_lo=%d\n", this_->item->id_hi, this_->item->id_lo);
764 if (this_->postal)
765 {
766 struct attr postal;
767 if (item_attr_get(this_->item, attr_postal_mask, &postal)) {
768 if (!postal_match(this_->postal, postal.u.str))
769 continue;
770 } else if (item_attr_get(this_->item, attr_postal, &postal)) {
771 if (strcmp(this_->postal, postal.u.str))
772 continue;
773 }
774 }
775 this_->result.country=NULL;
776 this_->result.town=NULL;
777 this_->result.street=NULL;
778 this_->result.c=NULL;
779 //dbg(0,"case x LEVEL start %d\n",level);
780 switch (level)
781 {
782 case 0:
783 //dbg(0,"case 0 COUNTRY");
784 p=search_list_country_new(this_->item);
785 this_->result.country=p;
786 this_->result.country->common.parent=NULL;
787 this_->item=NULL;
788 break;
789 case 1:
790 //dbg(0,"case 1 TOWN");
791 p=search_list_town_new(this_->item);
792 this_->result.town=p;
793 this_->result.town->common.parent=this_->levels[0].last->data;
794 this_->result.country=this_->result.town->common.parent;
795 this_->result.c=this_->result.town->common.c;
796 this_->item=NULL;
797 break;
798 case 2:
799 //dbg(0,"case 2 STREET");
800 p=search_list_street_new(this_->item);
801 this_->result.street=p;
802 this_->result.street->common.parent=this_->levels[1].last->data;
803 this_->result.town=this_->result.street->common.parent;
804 this_->result.country=this_->result.town->common.parent;
805 this_->result.c=this_->result.street->common.c;
806 this_->item=NULL;
807 break;
808 case 3:
809 //dbg(0,"case 3 HOUSENUMBER");
810 has_street_name=0;
811
812 // if this housenumber has a streetname tag, set the name now
813 if (item_attr_get(this_->item, attr_street_name, &attr2))
814 {
815 // dbg(0,"streetname: %s\n",attr2.u.str);
816 has_street_name=1;
817 }
818
819 //dbg(0,"case 3 XXXX 1\n");
820 p=search_list_house_number_new(this_->item, &this_->inter, le->attr->u.str, le->partial);
821 //dbg(0,"case 3 XXXX 2\n");
822 if (!p)
823 {
824 interpolation_clear(&this_->inter);
825 this_->item=NULL;
826 continue;
827 }
828 //dbg(0,"case 3 XXXX 3\n");
829 this_->result.house_number=p;
830 if (!this_->result.house_number->interpolation)
831 {
832 this_->item=NULL;
833 }
834
835 this_->result.house_number->common.parent=this_->levels[2].last->data;
836 this_->result.street=this_->result.house_number->common.parent;
837 this_->result.town=this_->result.street->common.parent;
838 this_->result.country=this_->result.town->common.parent;
839 this_->result.c=this_->result.house_number->common.c;
840
841 //dbg(0,"case 3 XXXX 4\n");
842 if (has_street_name==1)
843 {
844 gchar *tmp_name=g_strdup(attr2.u.str);
845 this_->result.street->name=tmp_name;
846 //dbg(0,"res streetname=%s\n",this_->result.street->name);
847 }
848 else
849 {
850 //
851 // this crashes all the time -> so dont use!
852 //static struct search_list_street null_street;
853 //this_->result.street=&null_street;
854 // this crashes all the time -> so dont use!
855 //
856 this_->result.street->name=NULL;
857 }
858 //dbg(0,"case 3 XXXX 5\n");
859 break;
860 }
861 // CASE END *********
862
863
864 if (p)
865 {
866 if (search_add_result(le, p))
867 {
868 //** this_->result.id++;
869 this_->result.id=0;
870 //dbg(0,"++++ return result\n");
871 return &this_->result;
872 }
873 else
874 {
875 search_list_result_destroy(level, p);
876 // return &this_->result;
877 }
878 }
879 }
880 else
881 {
882 mapset_search_destroy(le->search);
883 le->search=NULL;
884 g_hash_table_destroy(le->hash);
885 if (! level)
886 {
887 break;
888 }
889 }
890 }
891 return NULL;
892 }
893
894 void
895 search_list_destroy(struct search_list *this_)
896 {
897 g_free(this_->postal);
898 g_free(this_);
899 }
900
901 void
902 search_init(void)
903 {
904 }
905
906
907 static char *
908 search_fix_spaces(char *str)
909 {
910 int i;
911 int len=strlen(str);
912 char c,*s,*d,*ret=g_strdup(str);
913
914 for (i = 0 ; i < len ; i++) {
915 if (ret[i] == ',' || ret[i] == ',' || ret[i] == '/')
916 ret[i]=' ';
917 }
918 s=ret;
919 d=ret;
920 len=0;
921 do {
922 c=*s++;
923 if (c != ' ' || len != 0) {
924 *d++=c;
925 len++;
926 }
927 while (c == ' ' && *s == ' ')
928 s++;
929 if (c == ' ' && *s == '\0') {
930 d--;
931 len--;
932 }
933 } while (c);
934 return ret;
935 }
936
937 static GList *
938 search_split_phrases(char *str)
939 {
940 char *tmp,*s,*d;
941 GList *ret=NULL;
942 s=str;
943 do {
944 tmp=g_strdup(s);
945 d=tmp+strlen(s)-1;
946 ret=g_list_append(ret, g_strdup(s));
947 while (d >= tmp) {
948 if (*d == ' ') {
949 *d = '\0';
950 ret=g_list_append(ret, g_strdup(tmp));
951 }
952 d--;
953 }
954 g_free(tmp);
955 do {
956 s++;
957 if (*s == ' ') {
958 s++;
959 break;
960 }
961 } while (*s != '\0');
962 } while (*s != '\0');
963 return ret;
964 }
965
966 static GList *
967 search_address_housenumber_real(GList *result_list, struct search_list *sl, char *street_name, GList *phrases, GList *exclude1, GList *exclude2, GList *exclude3, int partial, struct jni_object *jni)
968 {
969 struct search_list_result *slr;
970 struct coord_geo g;
971 struct coord c;
972
973 //dbg(0,"enter\n");
974
975 while ((slr=search_list_get_result(sl)))
976 {
977 if (offline_search_break_searching==1)
978 {
979 break;
980 }
981
982 // does the streetname of the housenumber match the street we want?
983 if (slr != NULL)
984 {
985 if (slr->street != NULL)
986 if ((street_name != NULL)&&(slr->street->name != NULL))
987 {
988
989 if (strcmp(slr->street->name, street_name)==0)
990 {
991 char *buffer;
992 // coords of result
993 c.x=slr->house_number->common.c->x;
994 c.y=slr->house_number->common.c->y;
995 transform_to_geo(slr->house_number->common.c->pro, &c, &g);
996 // SHN -> street with house number
997 // return a string like: "SHN:H111L5555:16.766:48.76:full address name is at the end"
998 // ca. 9 chars : ca. 9 chars : max. 100 max. 100 max. 100 max. 15 chars -> this sould be max. about 335 chars long
999 if (slr->town->common.postal == NULL)
1000 {
1001 buffer=g_strdup_printf("SHN:H%dL%d:%f:%f:%.101s,%.101s, %.101s %.15s",slr->street->common.item.id_hi,slr->street->common.item.id_lo,g.lat,g.lng,slr->country->name,slr->town->common.town_name,slr->street->name,slr->house_number->house_number);
1002 }
1003 else
1004 {
1005 buffer=g_strdup_printf("SHN:H%dL%d:%f:%f:%.101s,%.7s %.101s, %.101s %.15s",slr->street->common.item.id_hi,slr->street->common.item.id_lo,g.lat,g.lng,slr->country->name,slr->town->common.postal,slr->town->common.town_name,slr->street->name,slr->house_number->house_number);
1006 }
1007
1008 //dbg(0,"res=%s\n",buffer);
1009
1010 // deactivated now * result_list=g_list_prepend(result_list,g_strdup(buffer));
1011 #ifdef HAVE_API_ANDROID
1012 // return results to android as they come in ...
1013 android_return_search_result(jni,buffer);
1014 #endif
1015 g_free(buffer);
1016 }
1017 }
1018 }
1019 }
1020
1021 //dbg(0,"return 2\n");
1022 return result_list;
1023 }
1024
1025 static GList *
1026 search_address__street(GList *result_list, struct search_list *sl, GList *phrases, GList *exclude1, GList *exclude2, GList *exclude3, int partial, struct jni_object *jni)
1027 {
1028 //dbg(0,"enter\n");
1029
1030 struct search_list_result *slr = NULL;
1031 GList *tmp=phrases;
1032 int count=0;
1033 struct coord_geo g;
1034 struct coord c;
1035 struct attr attr2;
1036
1037 struct item *save_item;
1038 int save_level;
1039 int save_last_result_valid;
1040
1041 while ((slr=search_list_get_result(sl)))
1042 {
1043 char *buffer;
1044 char *buffer2;
1045
1046 if (offline_search_break_searching==1)
1047 {
1048 break;
1049 }
1050
1051 if (slr->street)
1052 {
1053 // coords of result
1054 c.x=slr->street->common.c->x;
1055 c.y=slr->street->common.c->y;
1056 transform_to_geo(slr->street->common.c->pro, &c, &g);
1057
1058 // STR -> street
1059 // return a string like: "STR:H1111L5555:16.766:-48.76:full address name is at the end"
1060 // ca. 9 chars : ca. 9 chars : max. 100 max. 100 max. 100 chars -> this sould be max. about 320 chars long
1061 if (slr->town->common.postal == NULL)
1062 {
1063 buffer=g_strdup_printf("STR:H%dL%d:%f:%f:%.101s,%.101s, %.101s",slr->street->common.item.id_hi,slr->street->common.item.id_lo,g.lat,g.lng,slr->country->name,slr->town->common.town_name,slr->street->name);
1064 }
1065 else
1066 {
1067 buffer=g_strdup_printf("STR:H%dL%d:%f:%f:%.101s,%.7s %.101s, %.101s",slr->street->common.item.id_hi,slr->street->common.item.id_lo,g.lat,g.lng,slr->country->name,slr->town->common.postal,slr->town->common.town_name,slr->street->name);
1068 }
1069 // deactivated now * result_list=g_list_prepend(result_list,g_strdup(buffer));
1070
1071 //dbg(0,"res=%s\n",buffer);
1072
1073 #ifdef HAVE_API_ANDROID
1074 // return results to android as they come in ...
1075 android_return_search_result(jni,buffer);
1076 #endif
1077 count++;
1078
1079 buffer2=g_strdup_printf("%s", slr->street->name);
1080 //dbg(0,"b2:%s\n",buffer2);
1081
1082
1083 save_item=sl->item;
1084 save_level=sl->level;
1085 save_last_result_valid=sl->last_result_valid;
1086
1087
1088 #if 1
1089 // put words back to start!!
1090 tmp=phrases;
1091 while (tmp)
1092 {
1093 //dbg(0,"s0=%s\n",tmp->data);
1094 if (tmp != exclude1 && tmp != exclude2 && tmp != exclude3)
1095 {
1096 //dbg(0,"s=%s\n",tmp->data);
1097
1098 attr2.type=attr_house_number;
1099 attr2.u.str=tmp->data;
1100 search_list_search(sl, &attr2, partial);
1101
1102 result_list=search_address_housenumber_real(result_list, sl, buffer2, phrases, exclude1, exclude2, exclude3, partial, jni);
1103 }
1104 tmp=g_list_next(tmp);
1105 }
1106 #endif
1107
1108 // restore again
1109 sl->item=save_item;
1110 sl->level=save_level;
1111 sl->last_result_valid=save_last_result_valid;
1112
1113 }
1114
1115 if (buffer2)
1116 {
1117 g_free(buffer2);
1118 }
1119
1120 if (buffer)
1121 {
1122 g_free(buffer);
1123 }
1124 }
1125
1126 //dbg(0,"return 2\n");
1127 return result_list;
1128 }
1129
1130 static GList *
1131 search_address__town(GList *result_list, struct search_list *sl, GList *phrases, GList *exclude1, GList *exclude2, int partial, struct jni_object *jni)
1132 {
1133 //dbg(0,"enter\n");
1134 struct search_list_result *slr;
1135 GList *tmp=phrases;
1136 int count=0;
1137 struct coord_geo g;
1138 struct coord c;
1139 struct attr attr;
1140 struct attr attrx;
1141 // struct search_list *sl2=search_list_new(sl->ms);
1142 struct item *save_item;
1143 int save_level;
1144 int save_last_result_valid;
1145 // int first=1;
1146
1147 while ((slr=search_list_get_result(sl)))
1148 {
1149 char *buffer;
1150
1151 if (offline_search_break_searching==1)
1152 {
1153 break;
1154 }
1155
1156 // coords of result
1157 c.x=slr->town->common.c->x;
1158 c.y=slr->town->common.c->y;
1159 transform_to_geo(slr->town->common.c->pro, &c, &g);
1160
1161 // TWN -> town
1162 if (slr->town->common.postal == NULL)
1163 {
1164 buffer=g_strdup_printf("TWN:H%dL%d:%f:%f:%.101s,%.101s",slr->town->common.item.id_hi,slr->town->common.item.id_lo,g.lat,g.lng,slr->country->name,slr->town->common.town_name);
1165 }
1166 else
1167 {
1168 buffer=g_strdup_printf("TWN:H%dL%d:%f:%f:%.101s,%.7s %.101s",slr->town->common.item.id_hi,slr->town->common.item.id_lo,g.lat,g.lng,slr->country->name,slr->town->common.postal,slr->town->common.town_name);
1169 }
1170
1171 //dbg(0,"**res=%s\n",buffer);
1172
1173 // deactivated now * result_list=g_list_prepend(result_list,g_strdup(buffer));
1174 #ifdef HAVE_API_ANDROID
1175 // return results to android as they come in ...
1176 android_return_search_result(jni,buffer);
1177 #endif
1178
1179 count++;
1180 if (buffer)
1181 g_free(buffer);
1182
1183
1184 save_item=sl->item;
1185 save_level=sl->level;
1186 save_last_result_valid=sl->last_result_valid;
1187
1188 // put words back to start!!
1189 tmp=phrases;
1190
1191 while (tmp)
1192 {
1193 if (tmp != exclude1 && tmp != exclude2)
1194 {
1195 //dbg(0,"s=%s\n",tmp->data);
1196 attr.type=attr_street_name;
1197 attr.u.str=tmp->data;
1198 search_list_search(sl, &attr, partial);
1199 result_list=search_address__street(result_list, sl, phrases, exclude1, exclude2, tmp, partial, jni);
1200 }
1201 tmp=g_list_next(tmp);
1202 }
1203
1204 // restore again
1205 sl->item=save_item;
1206 sl->level=save_level;
1207 sl->last_result_valid=save_last_result_valid;
1208 }
1209
1210 //dbg(0,"return 2\n");
1211 return result_list;
1212 }
1213
1214 static GList *
1215 search_address__country(GList *result_list, struct search_list *sl, GList *phrases, GList *exclude, int partial, struct jni_object *jni)
1216 {
1217 GList *tmp=phrases;
1218 int count=0;
1219 struct attr attr;
1220 struct search_list_result *slr;
1221 //dbg(0,"enter\n");
1222
1223 while ((slr=search_list_get_result(sl)))
1224 {
1225 //dbg(0,"1 slr=%p\n",slr->country);
1226 //dbg(0,"2 slr=%s\n",slr->country->name);
1227 //dbg(0,"3 slr=%s\n",slr->country->iso2);
1228 count++;
1229 }
1230 //dbg(0,"count %d\n",count);
1231 if (!count)
1232 {
1233 //dbg(0,"return 1");
1234 return result_list;
1235 }
1236
1237 while (tmp)
1238 {
1239 if (tmp != exclude)
1240 {
1241 //dbg(0,"Is=%s\n",tmp->data);
1242 attr.type=attr_town_or_district_name;
1243 attr.u.str=tmp->data;
1244 search_list_search(sl, &attr, partial);
1245 result_list=search_address__town(result_list, sl, phrases, exclude, tmp, partial, jni);
1246 }
1247 //else
1248 //{
1249 //dbg(0,"Xs=%s\n",tmp->data);
1250 //}
1251 tmp=g_list_next(tmp);
1252 }
1253 //dbg(0,"return 2");
1254 return result_list;
1255 }
1256
1257
1258 struct country2 {
1259 int id;
1260 char *car;
1261 char *iso2;
1262 char *iso3;
1263 char *name;
1264 };
1265
1266 static struct country2 all_country_list[]= {
1267 { 20, "AND", "AD", "AND", /* 020 */ "Andorra"},
1268 {784, "UAE", "AE", "ARE", /* 784 */ "United Arab Emirates"},
1269 { 4, "AFG", "AF", "AFG", /* 004 */ "Afghanistan"},
1270 { 28, "AG", "AG", "ATG", /* 028 */ "Antigua and Barbuda"},
1271 {660, NULL, "AI", "AIA", /* 660 */ "Anguilla"},
1272 { 8, "AL", "AL", "ALB", /* 008 */ "Albania"},
1273 { 51, "ARM", "AM", "ARM", /* 051 */ "Armenia"},
1274 {530, "NA", "AN", "ANT", /* 530 */ "Netherlands Antilles"},
1275 { 24, "ANG", "AO", "AGO", /* 024 */ "Angola"},
1276 { 10, NULL, "AQ", "ATA", /* 010 */ "Antarctica"},
1277 { 32, "RA", "AR", "ARG", /* 032 */ "Argentina"},
1278 { 16, NULL, "AS", "ASM", /* 016 */ "American Samoa"},
1279 { 40, "A", "AT", "AUT", /* 040 */ "Austria"},
1280 { 36, "AUS", "AU", "AUS", /* 036 */ "Australia"},
1281 {533, "ARU", "AW", "ABW", /* 533 */ "Aruba"},
1282 {248, "AX", "AX", "ALA", /* 248 */ "Aland Islands"},
1283 { 31, "AZ", "AZ", "AZE", /* 031 */ "Azerbaijan"},
1284 { 70, "BiH", "BA", "BIH", /* 070 */ "Bosnia and Herzegovina"},
1285 { 52, "BDS", "BB", "BRB", /* 052 */ "Barbados"},
1286 { 50, "BD", "BD", "BGD", /* 050 */ "Bangladesh"},
1287 { 56, "B", "BE", "BEL", /* 056 */ "Belgium"},
1288 {854, "BF", "BF", "BFA", /* 854 */ "Burkina Faso"},
1289 {100, "BG", "BG", "BGR", /* 100 */ "Bulgaria"},
1290 { 48, "BRN", "BH", "BHR", /* 048 */ "Bahrain"},
1291 {108, "RU", "BI", "BDI", /* 108 */ "Burundi"},
1292 {204, "BJ", "BJ", "BEN", /* 204 */ "Benin"},
1293 {652, NULL, "BL", "BLM", /* 652 */ "Saint Barthelemy"},
1294 { 60, NULL, "BM", "BMU", /* 060 */ "Bermuda"},
1295 { 96, "BRU", "BN", "BRN", /* 096 */ "Brunei Darussalam"},
1296 { 68, "BOL", "BO", "BOL", /* 068 */ "Bolivia"},
1297 { 76, "BR", "BR", "BRA", /* 076 */ "Brazil"},
1298 { 44, "BS", "BS", "BHS", /* 044 */ "Bahamas"},
1299 { 64, "BHT", "BT", "BTN", /* 064 */ "Bhutan"},
1300 { 74, NULL, "BV", "BVT", /* 074 */ "Bouvet Island"},
1301 { 72, "RB", "BW", "BWA", /* 072 */ "Botswana"},
1302 {112, "BY", "BY", "BLR", /* 112 */ "Belarus"},
1303 { 84, "BZ", "BZ", "BLZ", /* 084 */ "Belize"},
1304 {124, "CDN", "CA", "CAN", /* 124 */ "Canada"},
1305 {166, NULL, "CC", "CCK", /* 166 */ "Cocos (Keeling) Islands"},
1306 {180, "CGO", "CD", "COD", /* 180 */ "Congo, Democratic Republic of the"},
1307 {140, "RCA", "CF", "CAF", /* 140 */ "Central African Republic"},
1308 {178, NULL, "CG", "COG", /* 178 */ "Congo"},
1309 {756, "CH", "CH", "CHE", /* 756 */ "Switzerland"},
1310 {384, "CI", "CI", "CIV", /* 384 */ "Cote d'Ivoire"},
1311 {184, NULL, "CK", "COK", /* 184 */ "Cook Islands"},
1312 {152, "RCH", "CL", "CHL", /* 152 */ "Chile"},
1313 {120, "CAM", "CM", "CMR", /* 120 */ "Cameroon"},
1314 {156, "RC", "CN", "CHN", /* 156 */ "China"},
1315 {170, "CO", "CO", "COL", /* 170 */ "Colombia"},
1316 {188, "CR", "CR", "CRI", /* 188 */ "Costa Rica"},
1317 {192, "C", "CU", "CUB", /* 192 */ "Cuba"},
1318 {132, "CV", "CV", "CPV", /* 132 */ "Cape Verde"},
1319 {162, NULL, "CX", "CXR", /* 162 */ "Christmas Island"},
1320 {196, "CY", "CY", "CYP", /* 196 */ "Cyprus"},
1321 {203, "CZ", "CZ", "CZE", /* 203 */ "Czech Republic"},
1322 {276, "D", "DE", "DEU", /* 276 */ "Germany"},
1323 {262, "DJI", "DJ", "DJI", /* 262 */ "Djibouti"},
1324 {208, "DK", "DK", "DNK", /* 208 */ "Denmark"},
1325 {212, "WD", "DM", "DMA", /* 212 */ "Dominica"},
1326 {214, "DOM", "DO", "DOM", /* 214 */ "Dominican Republic"},
1327 { 12, "DZ", "DZ", "DZA", /* 012 */ "Algeria"},
1328 {218, "EC", "EC", "ECU", /* 218 */ "Ecuador"},
1329 {233, "EST", "EE", "EST", /* 233 */ "Estonia"},
1330 {818, "ET", "EG", "EGY", /* 818 */ "Egypt"},
1331 {732, "WSA", "EH", "ESH", /* 732 */ "Western Sahara"},
1332 {232, "ER", "ER", "ERI", /* 232 */ "Eritrea"},
1333 {724, "E", "ES", "ESP", /* 724 */ "Spain"},
1334 {231, "ETH", "ET", "ETH", /* 231 */ "Ethiopia"},
1335 {246, "FIN", "FI", "FIN", /* 246 */ "Finland"},
1336 {242, "FJI", "FJ", "FJI", /* 242 */ "Fiji"},
1337 {238, NULL, "FK", "FLK", /* 238 */ "Falkland Islands (Malvinas)"},
1338 {583, "FSM", "FM", "FSM", /* 583 */ "Micronesia, Federated States of"},
1339 {234, "FO", "FO", "FRO", /* 234 */ "Faroe Islands"},
1340 {250, "F", "FR", "FRA", /* 250 */ "France"},
1341 {266, "G", "GA", "GAB", /* 266 */ "Gabon"},
1342 {826, "GB", "GB", "GBR", /* 826 */ "United Kingdom"},
1343 {308, "WG", "GD", "GRD", /* 308 */ "Grenada"},
1344 {268, "GE", "GE", "GEO", /* 268 */ "Georgia"},
1345 {254, NULL, "GF", "GUF", /* 254 */ "French Guiana"},
1346 {831, NULL, "GG", "GGY", /* 831 */ "Guernsey"},
1347 {288, "GH", "GH", "GHA", /* 288 */ "Ghana"},
1348 {292, "GBZ", "GI", "GIB", /* 292 */ "Gibraltar"},
1349 {304, "KN", "GL", "GRL", /* 304 */ "Greenland"},
1350 {270, "WAG", "GM", "GMB", /* 270 */ "Gambia"},
1351 {324, "RG", "GN", "GIN", /* 324 */ "Guinea"},
1352 {312, NULL, "GP", "GLP", /* 312 */ "Guadeloupe"},
1353 {226, "GQ", "GQ", "GNQ", /* 226 */ "Equatorial Guinea"},
1354 {300, "GR", "GR", "GRC", /* 300 */ "Greece"},
1355 {239, NULL, "GS", "SGS", /* 239 */ "South Georgia and the South Sandwich Islands"},
1356 {320, "GCA", "GT", "GTM", /* 320 */ "Guatemala"},
1357 {316, NULL, "GU", "GUM", /* 316 */ "Guam"},
1358 {624, "GUB", "GW", "GNB", /* 624 */ "Guinea-Bissau"},
1359 {328, "GUY", "GY", "GUY", /* 328 */ "Guyana"},
1360 {344, "HK", "HK", "HKG", /* 344 */ "Hong Kong"},
1361 {334, NULL, "HM", "HMD", /* 334 */ "Heard Island and McDonald Islands"},
1362 {340, "HN", "HN", "HND", /* 340 */ "Honduras"},
1363 {191, "HR", "HR", "HRV", /* 191 */ "Croatia"},
1364 {332, "RH", "HT", "HTI", /* 332 */ "Haiti"},
1365 {348, "H", "HU", "HUN", /* 348 */ "Hungary"},
1366 {360, "RI", "ID", "IDN", /* 360 */ "Indonesia"},
1367 {372, "IRL", "IE", "IRL", /* 372 */ "Ireland"},
1368 {376, "IL", "IL", "ISR", /* 376 */ "Israel"},
1369 {833, NULL, "IM", "IMN", /* 833 */ "Isle of Man"},
1370 {356, "IND", "IN", "IND", /* 356 */ "India"},
1371 { 86, NULL, "IO", "IOT", /* 086 */ "British Indian Ocean Territory"},
1372 {368, "IRQ", "IQ", "IRQ", /* 368 */ "Iraq"},
1373 {364, "IR", "IR", "IRN", /* 364 */ "Iran, Islamic Republic of"},
1374 {352, "IS", "IS", "ISL", /* 352 */ "Iceland"},
1375 {380, "I", "IT", "ITA", /* 380 */ "Italy"},
1376 {832, NULL, "JE", "JEY", /* 832 */ "Jersey"},
1377 {388, "JA", "JM", "JAM", /* 388 */ "Jamaica"},
1378 {400, "JOR", "JO", "JOR", /* 400 */ "Jordan"},
1379 {392, "J", "JP", "JPN", /* 392 */ "Japan"},
1380 {404, "EAK", "KE", "KEN", /* 404 */ "Kenya"},
1381 {417, "KS", "KG", "KGZ", /* 417 */ "Kyrgyzstan"},
1382 {116, "K", "KH", "KHM", /* 116 */ "Cambodia"},
1383 {296, "KIR", "KI", "KIR", /* 296 */ "Kiribati"},
1384 {174, "COM", "KM", "COM", /* 174 */ "Comoros"},
1385 {659, "KAN", "KN", "KNA", /* 659 */ "Saint Kitts and Nevis"},
1386 {408, "KP", "KP", "PRK", /* 408 */ "Korea, Democratic People's Republic of"},
1387 {410, "ROK", "KR", "KOR", /* 410 */ "Korea, Republic of"},
1388 {414, "KWT", "KW", "KWT", /* 414 */ "Kuwait"},
1389 {136, NULL, "KY", "CYM", /* 136 */ "Cayman Islands"},
1390 {398, "KZ", "KZ", "KAZ", /* 398 */ "Kazakhstan"},
1391 {418, "LAO", "LA", "LAO", /* 418 */ "Lao People's Democratic Republic"},
1392 {422, "RL", "LB", "LBN", /* 422 */ "Lebanon"},
1393 {662, "WL", "LC", "LCA", /* 662 */ "Saint Lucia"},
1394 {438, "FL", "LI", "LIE", /* 438 */ "Liechtenstein"},
1395 {144, "CL", "LK", "LKA", /* 144 */ "Sri Lanka"},
1396 {430, "LB", "LR", "LBR", /* 430 */ "Liberia"},
1397 {426, "LS", "LS", "LSO", /* 426 */ "Lesotho"},
1398 {440, "LT", "LT", "LTU", /* 440 */ "Lithuania"},
1399 {442, "L", "LU", "LUX", /* 442 */ "Luxembourg"},
1400 {428, "LV", "LV", "LVA", /* 428 */ "Latvia"},
1401 {434, "LAR", "LY", "LBY", /* 434 */ "Libyan Arab Jamahiriya"},
1402 {504, "MA", "MA", "MAR", /* 504 */ "Morocco"},
1403 {492, "MC", "MC", "MCO", /* 492 */ "Monaco"},
1404 {498, "MD", "MD", "MDA", /* 498 */ "Moldova, Republic of"},
1405 {499, "MNE", "ME", "MNE", /* 499 */ "Montenegro"},
1406 {663, NULL, "MF", "MAF", /* 663 */ "Saint Martin (French part)"},
1407 {450, "RM", "MG", "MDG", /* 450 */ "Madagascar"},
1408 {584, "MH", "MH", "MHL", /* 584 */ "Marshall Islands"},
1409 {807, "MK", "MK", "MKD", /* 807 */ "Macedonia, the former Yugoslav Republic of"},
1410 {466, "RMM", "ML", "MLI", /* 466 */ "Mali"},
1411 {104, "MYA", "MM", "MMR", /* 104 */ "Myanmar"},
1412 {496, "MGL", "MN", "MNG", /* 496 */ "Mongolia"},
1413 {446, NULL, "MO", "MAC", /* 446 */ "Macao"},
1414 {580, NULL, "MP", "MNP", /* 580 */ "Northern Mariana Islands"},
1415 {474, NULL, "MQ", "MTQ", /* 474 */ "Martinique"},
1416 {478, "RIM", "MR", "MRT", /* 478 */ "Mauritania"},
1417 {500, NULL, "MS", "MSR", /* 500 */ "Montserrat"},
1418 {470, "M", "MT", "MLT", /* 470 */ "Malta"},
1419 {480, "MS", "MU", "MUS", /* 480 */ "Mauritius"},
1420 {462, "MV", "MV", "MDV", /* 462 */ "Maldives"},
1421 {454, "MW", "MW", "MWI", /* 454 */ "Malawi"},
1422 {484, "MEX", "MX", "MEX", /* 484 */ "Mexico"},
1423 {458, "MAL", "MY", "MYS", /* 458 */ "Malaysia"},
1424 {508, "MOC", "MZ", "MOZ", /* 508 */ "Mozambique"},
1425 {516, "NAM", "NA", "NAM", /* 516 */ "Namibia"},
1426 {540, "NCL", "NC", "NCL", /* 540 */ "New Caledonia"},
1427 {562, "RN", "NE", "NER", /* 562 */ "Niger"},
1428 {574, NULL, "NF", "NFK", /* 574 */ "Norfolk Island"},
1429 {566, "NGR", "NG", "NGA", /* 566 */ "Nigeria"},
1430 {558, "NIC", "NI", "NIC", /* 558 */ "Nicaragua"},
1431 {528, "NL", "NL", "NLD", /* 528 */ "Netherlands"},
1432 {578, "N", "NO", "NOR", /* 578 */ "Norway"},
1433 {524, "NEP", "NP", "NPL", /* 524 */ "Nepal"},
1434 {520, "NAU", "NR", "NRU", /* 520 */ "Nauru"},
1435 {570, NULL, "NU", "NIU", /* 570 */ "Niue"},
1436 {554, "NZ", "NZ", "NZL", /* 554 */ "New Zealand"},
1437 {512, "OM", "OM", "OMN", /* 512 */ "Oman"},
1438 {591, "PA", "PA", "PAN", /* 591 */ "Panama"},
1439 {604, "PE", "PE", "PER", /* 604 */ "Peru"},
1440 {258, NULL, "PF", "PYF", /* 258 */ "French Polynesia"},
1441 {598, "PNG", "PG", "PNG", /* 598 */ "Papua New Guinea"},
1442 {608, "RP", "PH", "PHL", /* 608 */ "Philippines"},
1443 {586, "PK", "PK", "PAK", /* 586 */ "Pakistan"},
1444 {616, "PL", "PL", "POL", /* 616 */ "Poland"},
1445 {666, NULL, "PM", "SPM", /* 666 */ "Saint Pierre and Miquelon"},
1446 {612, NULL, "PN", "PCN", /* 612 */ "Pitcairn"},
1447 {630, "PRI", "PR", "PRI", /* 630 */ "Puerto Rico"},
1448 {275, "AUT", "PS", "PSE", /* 275 */ "Palestinian Territory, Occupied"},
1449 {620, "P", "PT", "PRT", /* 620 */ "Portugal"},
1450 {585, "PAL", "PW", "PLW", /* 585 */ "Palau"},
1451 {600, "PY", "PY", "PRY", /* 600 */ "Paraguay"},
1452 {634, "Q", "QA", "QAT", /* 634 */ "Qatar"},
1453 {638, NULL, "RE", "REU", /* 638 */ "Reunion"},
1454 {642, "RO", "RO", "ROU", /* 642 */ "Romania"},
1455 {688, "SRB", "RS", "SRB", /* 688 */ "Serbia"},
1456 {643, "RUS", "RU", "RUS", /* 643 */ "Russian Federation"},
1457 {646, "RWA", "RW", "RWA", /* 646 */ "Rwanda"},
1458 {682, "KSA", "SA", "SAU", /* 682 */ "Saudi Arabia"},
1459 { 90, "SOL", "SB", "SLB", /* 090 */ "Solomon Islands"},
1460 {690, "SY", "SC", "SYC", /* 690 */ "Seychelles"},
1461 {736, "SUD", "SD", "SDN", /* 736 */ "Sudan"},
1462 {752, "S", "SE", "SWE", /* 752 */ "Sweden"},
1463 {702, "SGP", "SG", "SGP", /* 702 */ "Singapore"},
1464 {654, NULL, "SH", "SHN", /* 654 */ "Saint Helena"},
1465 {705, "SLO", "SI", "SVN", /* 705 */ "Slovenia"},
1466 {744, NULL, "SJ", "SJM", /* 744 */ "Svalbard and Jan Mayen"},
1467 {703, "SK", "SK", "SVK", /* 703 */ "Slovakia"},
1468 {694, "WAL", "SL", "SLE", /* 694 */ "Sierra Leone"},
1469 {674, "RSM", "SM", "SMR", /* 674 */ "San Marino"},
1470 {686, "SN", "SN", "SEN", /* 686 */ "Senegal"},
1471 {706, "SO", "SO", "SOM", /* 706 */ "Somalia"},
1472 {740, "SME", "SR", "SUR", /* 740 */ "Suriname"},
1473 {678, "STP", "ST", "STP", /* 678 */ "Sao Tome and Principe"},
1474 {222, "ES", "SV", "SLV", /* 222 */ "El Salvador"},
1475 {760, "SYR", "SY", "SYR", /* 760 */ "Syrian Arab Republic"},
1476 {748, "SD", "SZ", "SWZ", /* 748 */ "Swaziland"},
1477 {796, NULL, "TC", "TCA", /* 796 */ "Turks and Caicos Islands"},
1478 {148, "TD", "TD", "TCD", /* 148 */ "Chad"},
1479 {260, "ARK", "TF", "ATF", /* 260 */ "French Southern Territories"},
1480 {768, "RT", "TG", "TGO", /* 768 */ "Togo"},
1481 {764, "T", "TH", "THA", /* 764 */ "Thailand"},
1482 {762, "TJ", "TJ", "TJK", /* 762 */ "Tajikistan"},
1483 {772, NULL, "TK", "TKL", /* 772 */ "Tokelau"},
1484 {626, "TL", "TL", "TLS", /* 626 */ "Timor-Leste"},
1485 {795, "TM", "TM", "TKM", /* 795 */ "Turkmenistan"},
1486 {788, "TN", "TN", "TUN", /* 788 */ "Tunisia"},
1487 {776, "TON", "TO", "TON", /* 776 */ "Tonga"},
1488 {792, "TR", "TR", "TUR", /* 792 */ "Turkey"},
1489 {780, "TT", "TT", "TTO", /* 780 */ "Trinidad and Tobago"},
1490 {798, "TUV", "TV", "TUV", /* 798 */ "Tuvalu"},
1491 {158, NULL, "TW", "TWN", /* 158 */ "Taiwan, Province of China"},
1492 {834, "EAT", "TZ", "TZA", /* 834 */ "Tanzania, United Republic of"},
1493 {804, "UA", "UA", "UKR", /* 804 */ "Ukraine"},
1494 {800, "EAU", "UG", "UGA", /* 800 */ "Uganda"},
1495 {581, NULL, "UM", "UMI", /* 581 */ "United States Minor Outlying Islands"},
1496 {840, "USA", "US", "USA", /* 840 */ "United States"},
1497 {858, "ROU", "UY", "URY", /* 858 */ "Uruguay"},
1498 {860, "UZ", "UZ", "UZB", /* 860 */ "Uzbekistan"},
1499 {336, "SCV", "VA", "VAT", /* 336 */ "Holy See (Vatican City State)"},
1500 {670, "WV", "VC", "VCT", /* 670 */ "Saint Vincent and the Grenadines"},
1501 {862, "YV", "VE", "VEN", /* 862 */ "Venezuela"},
1502 { 92, NULL, "VG", "VGB", /* 092 */ "Virgin Islands, British"},
1503 {850, NULL, "VI", "VIR", /* 850 */ "Virgin Islands, U.S."},
1504 {704, "VN", "VN", "VNM", /* 704 */ "Viet Nam"},
1505 {548, "VAN", "VU", "VUT", /* 548 */ "Vanuatu"},
1506 {876, NULL, "WF", "WLF", /* 876 */ "Wallis and Futuna"},
1507 {882, "WS", "WS", "WSM", /* 882 */ "Samoa"},
1508 {887, "YAR", "YE", "YEM", /* 887 */ "Yemen"},
1509 {175, NULL, "YT", "MYT", /* 175 */ "Mayotte"},
1510 {710, "ZA", "ZA", "ZAF", /* 710 */ "South Africa"},
1511 {894, "Z", "ZM", "ZMB", /* 894 */ "Zambia"},
1512 {716, "ZW", "ZW", "ZWE", /* 716 */ "Zimbabwe"},
1513 {999, "*", "*", "*", "Unknown"},
1514 };
1515
1516
1517 static int
1518 ascii_cmp_local(char *name, char *match, int partial)
1519 {
1520 char *s1=linguistics_casefold(name);
1521 char *s2=linguistics_casefold(match);
1522 int ret=linguistics_compare(s1,s2,partial);
1523 g_free(s1);
1524 g_free(s2);
1525 return ret;
1526 }
1527
1528 struct navit *global_navit;
1529
1530
1531 void
1532 search_full_world(char *addr, int partial, int search_order, struct jni_object *jni,struct coord_geo *search_center, int search_radius)
1533 {
1534 struct item *item;
1535 struct map_rect *mr=NULL;
1536 struct mapset *ms;
1537 struct mapset_handle *msh;
1538 struct map* map = NULL;
1539 struct attr map_name_attr;
1540 struct attr attr;
1541
1542 char *str=search_fix_spaces(addr);
1543 GList *phrases=search_split_phrases(str);
1544 GList *phrases_first;
1545 phrases_first=phrases;
1546
1547 ms=global_navit->mapsets->data;
1548 msh=mapset_open(ms);
1549
1550 struct pcoord center99;
1551 int search_radius_this=0;
1552 dbg(0,"in lat=%f,lng=%f\n",search_center->lat,search_center->lng);
1553 if ((search_center->lat == 0)&&(search_center->lng == 0))
1554 {
1555 center99.x=0;
1556 center99.y=0;
1557 search_radius_this=21000000;
1558 }
1559 else
1560 {
1561 struct coord c99;
1562 transform_from_geo(projection_mg, search_center, &c99);
1563 center99.x=c99.x;
1564 center99.y=c99.y;
1565 search_radius_this=search_radius;
1566 }
1567 dbg(0,"out x=%d,y=%d,r=%d\n",center99.x,center99.y,search_radius_this);
1568
1569 struct map_selection *sel=map_selection_rect_new(&center99, search_radius_this, search_order);
1570 sel->range.min=type_town_label;
1571 sel->range.max=type_area;
1572
1573 while (msh && (map=mapset_next(msh, 0)))
1574 {
1575 if(map_get_attr(map,attr_name, &map_name_attr,NULL))
1576 {
1577 if (strncmp("_ms_sdcard_map:", map_name_attr.u.str, 15) == 0)
1578 {
1579 if (strncmp("_ms_sdcard_map:/sdcard/zanavi/maps/navitmap", map_name_attr.u.str, 38) == 0)
1580 {
1581 // its an sdcard map
1582 //dbg(0,"map name=%s",map_name_attr.u.str);
1583 // mr=map_rect_new(map, NULL);
1584 mr=map_rect_new(map, sel);
1585 if (mr)
1586 {
1587 char *streetname_last=NULL;
1588
1589 while ((item=map_rect_get_item(mr)))
1590 {
1591 if ( (item_is_town(*item)) || (item_is_district(*item)) )
1592 {
1593 struct search_list_town *p=NULL;
1594
1595 if (item_attr_get(item, attr_town_name, &attr))
1596 {
1597 p=search_list_town_new(item);
1598 char *buffer=NULL;
1599 // coords of result
1600 struct coord_geo g;
1601 struct coord c;
1602 c.x=p->common.c->x;
1603 c.y=p->common.c->y;
1604 int calc_geo=0;
1605
1606 // dbg(0,"town name=%s\n", attr.u.str);
1607
1608 phrases=phrases_first;
1609 while (phrases)
1610 {
1611 if (!ascii_cmp_local(attr.u.str, phrases->data, partial))
1612 {
1613 // dbg(0,"matched town name=%s want=%s\n", attr.u.str, phrases->data);
1614 if (calc_geo==0)
1615 {
1616 transform_to_geo(p->common.c->pro, &c, &g);
1617 // TWN -> town
1618 calc_geo=1;
1619 }
1620 if (p->common.postal == NULL)
1621 {
1622 buffer=g_strdup_printf("TWN:H%dL%d:%f:%f:%.101s",p->common.item.id_hi,p->common.item.id_lo,g.lat,g.lng,p->common.town_name);
1623 }
1624 else
1625 {
1626 buffer=g_strdup_printf("TWN:H%dL%d:%f:%f:%.7s %.101s",p->common.item.id_hi,p->common.item.id_lo,g.lat,g.lng,p->common.postal,p->common.town_name);
1627 }
1628 #ifdef HAVE_API_ANDROID
1629 // return results to android as they come in ...
1630 android_return_search_result(jni,buffer);
1631 #endif
1632 }
1633 phrases=g_list_next(phrases);
1634
1635 }
1636 if (buffer)
1637 {
1638 g_free(buffer);
1639 }
1640 search_list_town_destroy(p);
1641 }
1642 }
1643 else if (item_is_street(*item))
1644 {
1645
1646 struct search_list_street *p=NULL;
1647
1648 if (item_attr_get(item, attr_label, &attr))
1649 {
1650 // dbg(0,"street1=%s\n",map_convert_string(item->map, attr.u.str));
1651 if ( (streetname_last==NULL) || (strcmp(streetname_last,attr.u.str) != 0) )
1652 {
1653 // dbg(0,"street2=%s\n",map_convert_string(item->map, attr.u.str));
1654 streetname_last=g_strdup_printf("%s",attr.u.str);
1655
1656 p=search_list_street_new(item);
1657 char *buffer=NULL;
1658 // coords of result
1659 struct coord_geo g;
1660 struct coord c;
1661 c.x=p->common.c->x;
1662 c.y=p->common.c->y;
1663 int calc_geo=0;
1664
1665 phrases=phrases_first;
1666 while (phrases)
1667 {
1668 if (!ascii_cmp_local(attr.u.str, phrases->data, partial))
1669 {
1670 if (calc_geo==0)
1671 {
1672 transform_to_geo(p->common.c->pro, &c, &g);
1673 calc_geo=1;
1674 }
1675 if (p->common.postal == NULL)
1676 {
1677 buffer=g_strdup_printf("STR:H%dL%d:%f:%f:%.101s",p->common.item.id_hi,p->common.item.id_lo,g.lat,g.lng,attr.u.str);
1678 }
1679 else
1680 {
1681 buffer=g_strdup_printf("STR:H%dL%d:%f:%f:%.7s %.101s",p->common.item.id_hi,p->common.item.id_lo,g.lat,g.lng,p->common.postal,attr.u.str);
1682 }
1683 //dbg(0,"street3=%s\n",buffer);
1684 #ifdef HAVE_API_ANDROID
1685 // return results to android as they come in ...
1686 android_return_search_result(jni,buffer);
1687 #endif
1688 }
1689 phrases=g_list_next(phrases);
1690 }
1691 if (buffer)
1692 {
1693 g_free(buffer);
1694 }
1695 search_list_street_destroy(p);
1696 }
1697 }
1698 else if (item_attr_get(item, attr_street_name_systematic, &attr))
1699 {
1700 //dbg(0,"street systematic=%s\n",map_convert_string(item->map, attr.u.str));
1701
1702 p=search_list_street_new(item);
1703 char *buffer=NULL;
1704 // coords of result
1705 struct coord_geo g;
1706 struct coord c;
1707 c.x=p->common.c->x;
1708 c.y=p->common.c->y;
1709 int calc_geo=0;
1710
1711 phrases=phrases_first;
1712 while (phrases)
1713 {
1714 if (!ascii_cmp_local(attr.u.str, phrases->data, partial))
1715 {
1716 if (calc_geo==0)
1717 {
1718 transform_to_geo(p->common.c->pro, &c, &g);
1719 calc_geo=1;
1720 }
1721 if (p->common.postal == NULL)
1722 {
1723 buffer=g_strdup_printf("STR:H%dL%d:%f:%f:%.101s",p->common.item.id_hi,p->common.item.id_lo,g.lat,g.lng,attr.u.str);
1724 }
1725 else
1726 {
1727 buffer=g_strdup_printf("STR:H%dL%d:%f:%f:%.7s %.101s",p->common.item.id_hi,p->common.item.id_lo,g.lat,g.lng,p->common.postal,attr.u.str);
1728 }
1729 #ifdef HAVE_API_ANDROID
1730 // return results to android as they come in ...
1731 android_return_search_result(jni,buffer);
1732 #endif
1733 }
1734 phrases=g_list_next(phrases);
1735 }
1736 if (buffer)
1737 {
1738 g_free(buffer);
1739 }
1740 search_list_street_destroy(p);
1741
1742 }
1743 }
1744 }
1745 g_free(streetname_last);
1746 map_rect_destroy(mr);
1747 }
1748 }
1749 }
1750 }
1751 }
1752
1753 map_selection_destroy(sel);
1754
1755 if (phrases)
1756 {
1757 g_list_free(phrases);
1758 }
1759 g_free(str);
1760
1761 mapset_close(msh);
1762 }
1763
1764
1765 GList *
1766 search_by_address(GList *result_list,struct mapset *ms, char *addr, int partial, struct jni_object *jni, int search_country_flags, char *search_country_string)
1767 {
1768 char *str=search_fix_spaces(addr);
1769 GList *tmp,*phrases=search_split_phrases(str);
1770 GList *phrases_first;
1771 GList *ret = NULL;
1772 struct search_list *sl;
1773 struct attr attr;
1774 attr.type=attr_country_all;
1775 tmp=phrases;
1776 phrases_first=phrases;
1777 sl=search_list_new(ms);
1778
1779 dbg(0,"-- START --\n");
1780
1781 // normal search stuff -------- START ----------
1782 if (search_country_flags == 1)
1783 {
1784 dbg(0,"-- country default start --\n");
1785 //while (phrases)
1786 //{
1787 // dbg(0,"s=%s\n",phrases->data);
1788 // set default country
1789 search_list_search(sl, country_default(), 0);
1790 ret=search_address__country(ret, sl, phrases, NULL, partial, jni);
1791 // phrases=g_list_next(phrases);
1792 //}
1793 dbg(0,"-- country default end --\n");
1794 }
1795 else if (search_country_flags == 2)
1796 {
1797 dbg(0,"-- country sel:%s start --\n",search_country_string);
1798 // set a country
1799 struct attr country;
1800 country.type=attr_country_iso2;
1801 country.u.str=search_country_string;
1802 //while (phrases)
1803 //{
1804 // dbg(0,"s=%s\n",phrases->data);
1805 search_list_search(sl, &country, 0);
1806 // set a country
1807 ret=search_address__country(ret, sl, phrases, NULL, partial, jni);
1808 //phrases=g_list_next(phrases);
1809 //}
1810 dbg(0,"-- country sel:%s end --\n",search_country_string);
1811 }
1812 else // flags==3
1813 {
1814 dbg(0,"-- country all start --\n");
1815 // search all countries!! could take a really long time!!
1816 struct attr country;
1817 int j2=sizeof(all_country_list) / sizeof(all_country_list[0]);
1818 int j1;
1819 for(j1=0;j1 < j2;j1++)
1820 {
1821 if (all_country_list[j1].iso2 != NULL)
1822 {
1823 phrases=phrases_first;
1824 //while (phrases)
1825 //{
1826 // dbg(0,"s country=%s\n",all_country_list[j1].iso2);
1827 // dbg(0,"s=%s\n",phrases->data);
1828 country.type=attr_country_iso2;
1829 country.u.str=all_country_list[j1].iso2;
1830 search_list_search(sl, &country, 0);
1831 ret=search_address__country(ret, sl, phrases, NULL, partial, jni);
1832 //phrases=g_list_next(phrases);
1833 //}
1834
1835 if (offline_search_break_searching==1)
1836 {
1837 break;
1838 }
1839 }
1840 }
1841 dbg(0,"-- country all end --\n");
1842 }
1843 // normal search stuff -------- END ----------
1844
1845 if (phrases_first)
1846 {
1847 g_list_free(phrases_first);
1848 }
1849
1850 dbg(0,"-- END --\n");
1851
1852 g_free(str);
1853 return ret;
1854 }
1855
1856

   
Visit the ZANavi Wiki