Fix:Core:Some more searching improvements
authormartin-s <martin-s@ffa7fe5e-494d-0410-b361-a75ebd5db220>
Fri, 21 Mar 2008 19:38:48 +0000 (19:38 +0000)
committermartin-s <martin-s@ffa7fe5e-494d-0410-b361-a75ebd5db220>
Fri, 21 Mar 2008 19:38:48 +0000 (19:38 +0000)
git-svn-id: https://navit.svn.sourceforge.net/svnroot/navit/trunk@954 ffa7fe5e-494d-0410-b361-a75ebd5db220

navit/src/country.c
navit/src/data/mg/map.c
navit/src/data/mg/mg.h
navit/src/data/mg/town.c
navit/src/search.c

index c2977e5..0fa7a08 100644 (file)
@@ -20,7 +20,7 @@ struct country {
 #define _(STRING)    gettext(STRING)
 #define _n(STRING)    gettext_noop(STRING)
 
-struct country country[]= {
+static struct country country[]= {
   { 20,        "AND",  "AD", "AND", /* 020 */ _n("Andorra")},
   {784,        "UAE",  "AE", "ARE", /* 784 */ _n("United Arab Emirates")},
   {  4,        "AFG",  "AF", "AFG", /* 004 */ _n("Afghanistan")},
index 53e9c04..843516d 100644 (file)
@@ -7,9 +7,11 @@
 #include "mg.h"
 
 
-struct country_isonum {
+static struct country_isonum {
        int country;
        int isonum;
+       int postal_len;
+       char *postal_prefix;
 } country_isonums[]={
   {  1,203},
   {  2,703},
@@ -38,7 +40,7 @@ struct country_isonum {
   { 46,752},
   { 47,578},
   { 48,616},
-  { 49,276},
+  { 49,276,5,"D@@"},
   { 50,292},
   { 51,620},
   { 52,442},
@@ -117,6 +119,24 @@ int mg_country_to_isonum(int country)
        return 0;
 }
 
+int mg_country_postal_len(int country)
+{
+       int i;
+       for (i = 0 ; i < sizeof(country_isonums)/sizeof(struct country_isonum) ; i++)
+               if (country_isonums[i].country == country)
+                       return country_isonums[i].postal_len;
+       return 0;
+}
+
+static char *mg_country_postal_prefix(int isonum)
+{
+       int i;
+       for (i = 0 ; i < sizeof(country_isonums)/sizeof(struct country_isonum) ; i++)
+               if (country_isonums[i].isonum == isonum)
+                       return country_isonums[i].postal_prefix;
+       return NULL;
+}
+
 static int
 file_next(struct map_rect_priv *mr)
 {
@@ -316,6 +336,7 @@ static struct map_search_priv *
 map_search_new_mg(struct map_priv *map, struct item *item, struct attr *search, int partial)
 {
        struct map_rect_priv *mr=g_new0(struct map_rect_priv, 1);
+       char *prefix;
        dbg(1,"id_lo=0x%x\n", item->id_lo);
        dbg(1,"search=%s\n", search->u.str);
        mr->m=map;
@@ -324,9 +345,13 @@ map_search_new_mg(struct map_priv *map, struct item *item, struct attr *search,
        case attr_town_postal:
                if (item->type != type_country_label)
                        return NULL;
+               prefix=mg_country_postal_prefix(item->id_lo);
+               if (! prefix)
+                       return NULL;
                tree_search_init(map->dirname, "town.b1", &mr->ts, 0);
                mr->current_file=file_town_twn;
-               mr->search_str=g_strdup(search->u.str);
+               mr->search_str=g_strdup_printf("%s%s",prefix,search->u.str);
+               dbg(0,"search_str='%s'\n",mr->search_str);
                mr->search_country=mg_country_from_isonum(item->id_lo);
                break;
        case attr_town_name:
@@ -362,6 +387,7 @@ map_search_destroy_mg(struct map_search_priv *ms)
 {
        struct map_rect_priv *mr=(struct map_rect_priv *)ms;
 
+       dbg(1,"mr=%p\n", mr);
        if (! mr)
                return;
        g_free(mr->search_str);
index 217b94b..2600e3c 100644 (file)
@@ -49,6 +49,7 @@ struct town_priv {
        int aidx;
        enum attr_type attr_next;
        char debug[256];
+       char postal[32];
        struct item town_attr_item;
 };
 
@@ -272,6 +273,7 @@ struct map_rect_priv {
 
 int mg_country_from_isonum(int isonum);
 int mg_country_to_isonum(int country);
+int mg_country_postal_len(int country);
 
 int block_init(struct map_rect_priv *mr);
 int block_next(struct map_rect_priv *mr);
index abadc32..92bd0e7 100644 (file)
@@ -38,6 +38,7 @@ static int
 town_attr_get(void *priv_data, enum attr_type attr_type, struct attr *attr)
 {
        struct town_priv *twn=priv_data;
+       int len;
 
        attr->type=attr_type;
        switch (attr_type) {
@@ -59,7 +60,12 @@ town_attr_get(void *priv_data, enum attr_type attr_type, struct attr *attr)
                twn->attr_next=attr_town_postal;
                return ((attr->u.str && attr->u.str[0]) ? 1:0);
        case attr_town_postal:
-               attr->u.str=twn->postal_code1;
+               strncpy(twn->postal, twn->postal_code1, 32);
+               attr->u.str=twn->postal;
+               len=mg_country_postal_len(twn->country);
+               if (!len)
+                       len=31;
+               twn->postal[len]='\0';
                twn->attr_next=attr_district_name;
                return ((attr->u.str && attr->u.str[0]) ? 1:0);
        case attr_district_name:
@@ -225,19 +231,25 @@ town_search_get_item(struct map_rect_priv *mr)
                if (! mr->search_linear) {
                        while ((leaf=tree_search_next(&mr->ts, &mr->search_p, dir)) != -1) {
                                dir=town_search_compare(&mr->search_p, mr);
-                               if (! dir && (mr->search_partial == 0 || leaf)) {
+                               if (! dir && leaf) {
                                        mr->search_linear=1;
                                        mr->search_p=NULL;
                                        break;
                                }
                        }
-                       if (! mr->search_linear)
+                       if (! mr->search_linear) {
+                               dbg(1,"not found\n");
                                return NULL;
+                       }
                }
-               if (! tree_search_next_lin(&mr->ts, &mr->search_p))
+               if (! tree_search_next_lin(&mr->ts, &mr->search_p)) {
+                       dbg(1,"linear not found\n");
                        return NULL;
-               if (town_search_compare(&mr->search_p, mr))
+               }
+               if (town_search_compare(&mr->search_p, mr)) {
+                       dbg(1,"no match\n");
                        return NULL;
+               }
                dbg(1,"found %d blocks\n",mr->search_blk_count);
        }
        if (! mr->search_blk_count)
index a0deeea..d088c5f 100644 (file)
@@ -16,8 +16,6 @@ struct search_list_level {
        struct mapset_search *search;
        GHashTable *hash;
        GList *list,*curr,*last;
-       int complete;
-       
 };
 
 struct search_list {
@@ -142,7 +140,7 @@ search_list_town_new(struct item *item)
        }
        else
                ret->item=*item;
-       if (item_attr_get(item, attr_label, &attr))
+       if (item_attr_get(item, attr_town_name, &attr))
                ret->name=map_convert_string(item->map,attr.u.str);
        if (item_attr_get(item, attr_town_postal, &attr))
                ret->postal=map_convert_string(item->map,attr.u.str);
@@ -221,9 +219,12 @@ search_list_search_free(struct search_list *sl, int level)
                mapset_search_destroy(le->search);
                le->search=NULL;
        }
-       if (le->search) {
+#if 0 /* FIXME */
+       if (le->hash) {
                g_hash_table_destroy(le->hash);
+               le->hash=NULL;
        }
+#endif
        curr=le->list;
        while (curr) {
                search_list_result_destroy(level, curr->data);
@@ -234,19 +235,22 @@ search_list_search_free(struct search_list *sl, int level)
        le->list=NULL;
        le->curr=NULL;
        le->last=NULL;
-       le->complete=0;
 
 }
 
 static int
 search_add_result(struct search_list_level *le, void *p)
 {
+#if 0
        if (! g_hash_table_lookup(le->hash, p)) {
+#endif
                g_hash_table_insert(le->hash, p, (void *)1);    
                le->list=g_list_append(le->list, p);
                return 1;
+#if 0
        }
        return 0;
+#endif
 }
 
 struct search_list_result *
@@ -259,7 +263,7 @@ search_list_get_result(struct search_list *this_)
        dbg(1,"enter\n");
        le=&this_->levels[level];
        dbg(1,"le=%p\n", le);
-       while(! le->complete) {
+       for (;;) {
                dbg(1,"le->search=%p\n", le->search);
                if (! le->search) {
                        dbg(1,"partial=%d\n", le->partial);
@@ -273,14 +277,18 @@ search_list_get_result(struct search_list *this_)
                                leu->last=leu->curr;
                                leu->curr=g_list_next(leu->curr);
                        }
+                       if (le->parent)
+                               dbg(1,"mapset_search_new with item(%d,%d)\n", le->parent->id_hi, le->parent->id_lo);
+                       dbg(1,"attr=%s\n", attr_to_name(le->attr.type));        
                        le->search=mapset_search_new(this_->ms, le->parent, &le->attr, le->partial);
-                       le->hash=g_hash_table_new_full(search_item_hash_hash, search_item_hash_equal, g_free, NULL);
+                       le->hash=g_hash_table_new(search_item_hash_hash, search_item_hash_equal);
                }
                dbg(1,"le->search=%p\n", le->search);
                item=mapset_search_get_item(le->search);
                dbg(1,"item=%p\n", item);
                if (item) {
                        void *p=NULL;
+                       dbg(1,"id_hi=%d id_lo=%d\n", item->id_hi, item->id_lo);
                        this_->result.country=NULL;
                        this_->result.town=NULL;
                        this_->result.street=NULL;
@@ -310,8 +318,13 @@ search_list_get_result(struct search_list *this_)
                                else 
                                        search_list_result_destroy(level, p);
                        }
-               } else 
-                       le->complete=1;
+               } else {
+                       mapset_search_destroy(le->search);
+                       le->search=NULL;
+                       g_hash_table_destroy(le->hash);
+                       if (! level)
+                               break;
+               }
        }
        return NULL;
 }