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

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

   
Visit the ZANavi Wiki