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

Contents of /navit/navit/search.c

Parent Directory Parent Directory | Revision Log Revision Log


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

   
Visit the ZANavi Wiki