/[zanavi_public1]/navit/navit/s_index.h_full
ZANavi

Contents of /navit/navit/s_index.h_full

Parent Directory Parent Directory | Revision Log Revision Log


Revision 56 - (show annotations) (download)
Sun Mar 19 08:44:36 2017 UTC (7 years ago) by zoff99
File size: 33768 byte(s)
updates
1 /**
2 * ZANavi, Zoff Android Navigation system.
3 * Copyright (C) 2015 Zoff <zoff@zoff.cc>
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
21 #define MAX_INDEXSEARCH_TOWNNAME 300
22
23
24 // IN_BUF_SIZE2 is the size of the file read buffer.
25 // IN_BUF_SIZE2 must be >= 1
26 //#define IN_BUF_SIZE2 (1024*16)
27 //x/ int IN_BUF_SIZE2 = sizeof(struct streets_index_data_block) * 1024;
28 //x/ int t_IN_BUF_SIZE2 = sizeof(struct town_index_data_block) * 1024;
29 //x/ static uint8 s_inbuf[(sizeof(struct streets_index_data_block) * 1024)];
30 //x/ static uint8 t_s_inbuf[(sizeof(struct town_index_data_block) * 1024)];
31 //static uint8 s_inbuf[IN_BUF_SIZE2];
32
33 int IN_BUF_SIZE2 = sizeof(struct streets_index_data_block) * 2048;
34 int t_IN_BUF_SIZE2 = sizeof(struct town_index_data_block) * 2048;
35 uint8 *s_inbuf;
36 uint8 *t_s_inbuf;
37
38 // OUT_BUF_SIZE2 is the size of the output buffer used during decompression.
39 // OUT_BUF_SIZE2 must be a power of 2 >= TINFL_LZ_DICT_SIZE(==32768) (because the low-level decompressor
40 // not only writes, but reads from the output buffer as it decompresses)
41 //#define OUT_BUF_SIZE2 (TINFL_LZ_DICT_SIZE)
42 //#define OUT_BUF_SIZE2 (1024*32)
43
44
45 // -- OLD --
46 //int OUT_BUF_SIZE2 = sizeof(struct streets_index_data_block) * 1024;
47 //int t_OUT_BUF_SIZE2 = sizeof(struct town_index_data_block) * 1024;
48 //static uint8 s_outbuf[(sizeof(struct streets_index_data_block) * 1024)];
49 //static uint8 t_s_outbuf[(sizeof(struct town_index_data_block) * 1024)];
50 // -- OLD --
51 // -- NEW --
52 //x/ int OUT_BUF_SIZE2 = TINFL_LZ_DICT_SIZE * 32;
53 //x/ int t_OUT_BUF_SIZE2 = TINFL_LZ_DICT_SIZE * 32;
54 //x/ static uint8 s_outbuf[(sizeof(struct streets_index_data_block) * 1024 * 32)];
55 //x/ static uint8 t_s_outbuf[(sizeof(struct town_index_data_block) * 1024 * 32)];
56 // -- NEW --
57
58 // TINFL_LZ_DICT_SIZE = 32768
59 int OUT_BUF_SIZE2 = (sizeof(struct streets_index_data_block) * 512);
60 int t_OUT_BUF_SIZE2 = (sizeof(struct town_index_data_block) * 512);
61 uint8 *s_outbuf;
62 uint8 *t_s_outbuf;
63
64 //static uint8 s_outbuf[OUT_BUF_SIZE2];
65
66 static long long street_index_size = 0; // this is the offset for town index start
67 // street index starts at "+sizeof(long long)"
68
69 #define my_min(a,b) (((a) < (b)) ? (a) : (b))
70 #define NUMBER_OF_TOWNS_TO_CACHE 1024
71
72 static struct town_index_data_block_c *town_lookup_cache = NULL;
73 static struct town_index_data_block_c *town_lookup_cache_cur = NULL;
74 static struct town_index_data_block_c *town_lookup_cache_found = NULL;
75 static int town_lookup_cache_items = 0;
76 static int town_lookup_cache_cur_item = 0;
77
78
79 // func definition
80 static int ascii_cmp_local_faster(char *name, char *match, int partial);
81 static int ascii_cmp_local_faster_DEBUG(char *name, char *match, int partial);
82 // func definition
83
84
85 static int search_address_housenumber_for_street(char *hn_name_match, char *street_name_match, char *town_string, struct coord *c, int partial, struct jni_object *jni);
86
87
88
89 void town_index_init_cache()
90 {
91 //dbg(0,"+#+:enter\n");
92
93 int s = sizeof(struct town_index_data_block_c) * NUMBER_OF_TOWNS_TO_CACHE;
94 dbg(0, "cache size=%d\n", s);
95
96 town_lookup_cache = g_malloc(s);
97 town_lookup_cache_cur = town_lookup_cache;
98 town_lookup_cache_found = NULL;
99 town_lookup_cache_items = 0;
100 town_lookup_cache_cur_item = 0;
101
102 //dbg(0,"+#+:leave\n");
103 }
104
105 void town_index_insert_cache(struct town_index_data_block* t, char* townname_long)
106 {
107 //dbg(0,"+#+:enter\n");
108
109 if (town_lookup_cache_items < NUMBER_OF_TOWNS_TO_CACHE)
110 {
111 // fill up cache until all slots are filled
112 town_lookup_cache_cur->town_id = t->town_id;
113 town_lookup_cache_cur->country_id = t->country_id;
114 sprintf(town_lookup_cache_cur->town_name, "%s", townname_long);
115 town_lookup_cache_items++;
116 }
117 else
118 {
119 // just fill cache and rotate if at end
120 town_lookup_cache_cur->town_id = t->town_id;
121 town_lookup_cache_cur->country_id = t->country_id;
122 sprintf(town_lookup_cache_cur->town_name, "%s", townname_long);
123 }
124
125 if (town_lookup_cache_items == NUMBER_OF_TOWNS_TO_CACHE)
126 {
127 town_lookup_cache_cur_item = 0;
128 town_lookup_cache_cur = town_lookup_cache;
129 }
130 else
131 {
132 town_lookup_cache_cur_item++;
133 town_lookup_cache_cur++;
134 }
135 }
136
137 int town_index_lookup_cache(long long townid)
138 {
139 //dbg(0,"+#+:enter\n");
140
141 int i;
142 struct town_index_data_block_c* t;
143
144 if (town_lookup_cache_items < 1)
145 {
146 return 0;
147 }
148
149 t = town_lookup_cache;
150 for (i = 0; i < town_lookup_cache_items; i++)
151 {
152 if (t->town_id == townid)
153 {
154 // set pointer to found datablock
155 town_lookup_cache_found = t;
156 return 1;
157 }
158 t++;
159 }
160
161 return 0;
162 }
163
164 char* town_index_lookup(struct street_index_head *sih, long long townid)
165 {
166 //dbg(0,"+#+:enter\n");
167
168 char *townname = NULL;
169 char *townname2 = NULL;
170 int found = 0;
171 int i;
172 int split = 0;
173 int split_count = 0;
174 long long save_town_id;
175 int save_country_id;
176
177 if (townid == 0)
178 {
179 return townname;
180 }
181
182 if (town_lookup_cache == NULL)
183 {
184 town_index_init_cache();
185 }
186
187 if (town_index_lookup_cache(townid) == 1)
188 {
189 townname = g_strdup_printf("%s", town_lookup_cache_found->town_name);
190
191 // dbg(0, "TWNAME:idx:01:t_id %s\n", town_lookup_cache_found->town_name);
192
193 return townname;
194 }
195
196 sih->ti_ib = sih->ti_ib_mem;
197
198 // find townid block
199 found = sih->ti_ibs.count_of_index_blocks - 1; // set to last block
200 for (i = 0; i < sih->ti_ibs.count_of_index_blocks; i++)
201 {
202 //dbg(0, "i=%d %lld %lld num idx blocks=%d\n", i, townid, sih->ti_ib->first_id, sih->ti_ibs.count_of_index_blocks);
203
204 if (townid < sih->ti_ib->first_id)
205 {
206 found = i - 1;
207 break;
208 }
209
210 sih->ti_ib++;
211 }
212
213 if (found != -1)
214 {
215 //dbg(0, "found town block num=%d\n", found);
216
217 town_index_setpos(sih, found); // move to correct index block
218
219 while (town_index_read_data(sih))
220 {
221 //dbg(0, "id=%lld\n", sih->ti_db_ptr->town_id);
222
223 if (offline_search_break_searching == 1)
224 {
225 break;
226 }
227
228 if (sih->ti_db_ptr->town_id == townid)
229 {
230 townname = g_strdup_printf("%s", sih->ti_db_ptr->town_name);
231 //dbg(0,"found town:%s\n", townname);
232 save_town_id = sih->ti_db_ptr->town_id;
233 save_country_id = sih->ti_db_ptr->country_id;
234 split = 1;
235 split_count = 0;
236 while ((town_index_read_data(sih))&&(split == 1))
237 {
238 split_count++;
239 if ((split_count + 1) > MAX_TOWNNAME_SPLIT)
240 {
241 break;
242 }
243
244 if (sih->ti_db_ptr->town_id == 0)
245 {
246 //dbg(0," town-split:%s\n", sih->ti_db_ptr->town_name);
247 townname2 = g_strdup_printf("%s%s", townname, sih->ti_db_ptr->town_name);
248 g_free(townname);
249 townname = townname2;
250 }
251 else
252 {
253 split = 0;
254 }
255 }
256 break;
257 }
258 }
259 }
260
261 if (townname != NULL)
262 {
263 sih->ti_db_ptr->town_id = save_town_id; // set town and country to values before we read the "split"-blocks!
264 sih->ti_db_ptr->country_id = save_country_id;
265 town_index_insert_cache(sih->ti_db_ptr, townname);
266 }
267
268 //dbg(0, "return\n");
269
270 return townname;
271 }
272
273
274
275 struct street_index_head* street_index_init(const char* idxfile_name)
276 {
277 //dbg(0,"+#+:enter\n");
278
279 struct street_index_head *ret=g_new0(struct street_index_head, 1);
280 long s1;
281 int b;
282 char *index_file;
283
284 index_file = g_strdup_printf("%s%s", navit_maps_dir, idxfile_name);
285 ret->sif = fopen(index_file, "rb");
286 g_free(index_file);
287
288 fread(&street_index_size, sizeof(struct streets_index_index_block_start), 1, ret->sif);
289 //dbg(0, "street_index_size=%lld\n", street_index_size);
290
291 b = fread(&ret->si_ibs, sizeof(struct streets_index_index_block_start), 1, ret->sif);
292 //dbg(0, "ftell=%d\n", ftell(ret->sif));
293 //dbg(0, "items read=%d\n", b);
294
295 //dbg(0, "struct size=%d\n", sizeof(struct streets_index_data_block));
296
297 //dbg(0, "index entries=%d\n", ret->si_ibs.count_of_index_blocks);
298 //dbg(0, "index entry size=%d\n", sizeof(struct streets_index_index_block));
299 s1 = sizeof(struct streets_index_index_block) * ret->si_ibs.count_of_index_blocks;
300 //dbg(0, "s1=%ld\n", s1);
301
302 ret->si_ib_mem = g_malloc(s1);
303 ret->si_ib = ret->si_ib_mem;
304
305 ret->comp_status = 0;
306 ret->t_comp_status = 0;
307
308 //dbg(0, "ftell=%d\n", ftell(ret->sif));
309 fread(ret->si_ib_mem, sizeof(struct streets_index_index_block), ret->si_ibs.count_of_index_blocks, ret->sif);
310 //dbg(0, "ftell=%d\n", ftell(ret->sif));
311
312 //dbg(0, "len=%lld\n", ret->si_ib->len);
313 //dbg(0, "offset=%lld\n", ret->si_ib->offset);
314
315 fseek(ret->sif, street_index_size + sizeof(long long), SEEK_SET); // seek to townindex header
316 fread(&ret->ti_ibs, sizeof(struct town_index_index_block_start), 1, ret->sif);
317 //dbg(0, "len=%lld\n", ret->ti_ibs.count_of_index_blocks);
318
319 //dbg(0, "town index entries=%d\n", ret->ti_ibs.count_of_index_blocks);
320 //dbg(0, "town index entry size=%d\n", sizeof(struct town_index_index_block));
321 s1 = sizeof(struct town_index_index_block) * ret->ti_ibs.count_of_index_blocks;
322 //dbg(0, "s1=%ld\n", s1);
323
324 ret->ti_ib_mem = g_malloc(s1);
325 ret->ti_ib = ret->ti_ib_mem;
326
327 //dbg(0, "ftell=%d\n", ftell(ret->sif));
328 fread(ret->ti_ib_mem, sizeof(struct town_index_index_block), ret->ti_ibs.count_of_index_blocks, ret->sif);
329 //dbg(0, "ftell=%d\n", ftell(ret->sif));
330
331 //dbg(0, "town len=%lld\n", ret->ti_ib->len);
332 //dbg(0, "town offset=%lld\n", ret->ti_ib->offset);
333
334 return ret;
335 }
336
337 void town_index_setpos(struct street_index_head *sih, int town_data_block_num)
338 {
339 //dbg(0,"+#+:enter\n");
340
341 if (sih->t_comp_status == 1)
342 {
343 town_index_close_compr(sih);
344 }
345
346 sih->ti_ib = (sih->ti_ib_mem + town_data_block_num);
347
348 town_index_init_compr(sih, sih->ti_ib->len);
349
350 //dbg(0, "len=%lld\n", sih->ti_ib->len);
351 //dbg(0, "fid=%lld\n", sih->ti_ib->first_id);
352 //dbg(0, "off=%lld\n", sih->ti_ib->offset);
353
354 // if (sih->ti_ib->len >= sizeof(struct town_index_data_block))
355 if (sih->ti_ib->len > 1)
356 {
357 //dbg(0, "fpos1=%d\n", ftell(sih->sif));
358 fseek(sih->sif, sih->ti_ib->offset + sizeof(long long) + street_index_size, SEEK_SET);
359 //dbg(0, "fpos2=%d\n", ftell(sih->sif));
360
361 sih->t_data_count = 0;
362 // move ptr to first data
363 sih->ti_db_ptr = t_s_outbuf;
364 }
365
366 }
367
368 void street_index_setpos(struct street_index_head *sih, int data_block_num)
369 {
370 //dbg(0,"+#+:enter\n");
371
372 if (sih->comp_status == 1)
373 {
374 street_index_close_compr(sih);
375 }
376
377 sih->si_ib = (sih->si_ib_mem + data_block_num);
378
379 //dbg(0, "len=%lld\n", sih->si_ib->len);
380 //dbg(0, "fl=%c off=%lld\n", sih->si_ib->first_letter, sih->si_ib->offset);
381
382 street_index_init_compr(sih, sih->si_ib->len);
383
384 //if (sih->si_ib->len >= sizeof(struct streets_index_data_block))
385 if (sih->si_ib->len > 1) // what is the minimum compressed block size? (about 55 bytes now)
386 {
387 //dbg(0, "mem start=%d, cur pos=%d\n", sih->si_ib_mem, sih->si_ib);
388 //dbg(0, "file offset=%d\n", sih->si_ib->offset);
389 //dbg(0, "fpos s1=%d\n", ftell(sih->sif));
390 fseek(sih->sif, sih->si_ib->offset + sizeof(long long), SEEK_SET); // add the "long long" from start of file to offset
391 //dbg(0, "fpos s2=%d\n", ftell(sih->sif));
392
393 sih->data_count = 0;
394 // move ptr to first data
395 sih->si_db_ptr = s_outbuf;
396 }
397 }
398
399 int street_index_read_data(struct street_index_head *sih)
400 {
401 //dbg(0,"+#+:enter\n");
402
403 // fread(&sih->si_db, sizeof(struct streets_index_data_block), 1, sih->sif);
404
405
406 //if (sih->si_ib->len < sizeof(struct streets_index_data_block))
407 if (sih->si_ib->len <= 1) // minimum size of compressed block?
408 {
409 //dbg(0, "len=%d sof=%d\n", sih->si_ib->len, sizeof(struct streets_index_data_block));
410 // no data for this letter
411 return 0;
412 }
413
414 if (sih->data_count == 0)
415 {
416 // init
417 sih->next_out = s_outbuf;
418 sih->avail_out = OUT_BUF_SIZE2;
419
420 // read data
421 sih->data_count = street_index_decompress_data_block(sih);
422 //dbg(0, "stat=%d\n", sih->data_count);
423
424 if (sih->data_count <= 0)
425 {
426 // end of data
427 return 0;
428 }
429
430 // move ptr to next data
431 sih->si_db_ptr = s_outbuf;
432 }
433 else
434 {
435 sih->data_count = sih->data_count - sizeof(struct streets_index_data_block);
436
437 if (sih->data_count > 0)
438 {
439 sih->si_db_ptr++;
440 //dbg(0, "dc=%d ptr=%p\n", sih->data_count, sih->si_db_ptr);
441 }
442 else
443 {
444 // init
445 sih->next_out = s_outbuf;
446 sih->avail_out = OUT_BUF_SIZE2;
447
448 // read data
449 sih->data_count = street_index_decompress_data_block(sih);
450 //dbg(0, "stat2=%d\n", sih->data_count);
451
452 if (sih->data_count <= 0)
453 {
454 // end of data
455 return 0;
456 }
457
458 // move ptr to next data
459 sih->si_db_ptr = s_outbuf;
460 }
461 }
462
463 //dbg(0, "data=%s, %d, %d, %lld\n", sih->si_db_ptr->street_name, sih->si_db_ptr->lat, sih->si_db_ptr->lon, sih->si_db_ptr->town_id);
464
465 return 1;
466
467 //if (ftell(sih->sif) > (sih->si_ib->offset + sih->si_ib->len))
468 //{
469 // // end of data
470 // return 0;
471 //}
472
473 // more data found
474 // return 1;
475 }
476
477 int town_index_read_data(struct street_index_head *sih)
478 {
479 //dbg(0,"+#+:enter\n");
480
481 // fread(&sih->si_db, sizeof(struct streets_index_data_block), 1, sih->sif);
482
483
484 // if (sih->ti_ib->len < sizeof(struct town_index_data_block))
485 if (sih->ti_ib->len <= 1)
486 {
487 // no data for this block
488 //fprintf(stderr, "no data for this block\n");
489 return 0;
490 }
491
492 if (sih->t_data_count == 0)
493 {
494 // init
495 sih->t_next_out = t_s_outbuf;
496 sih->t_avail_out = t_OUT_BUF_SIZE2;
497
498 // read data
499 sih->t_data_count = town_index_decompress_data_block(sih);
500 //dbg(0, "stat=%d\n", sih->data_count);
501
502 if (sih->t_data_count <= 0)
503 {
504 // end of data
505 //fprintf(stderr, "end of data\n");
506 return 0;
507 }
508
509 // move ptr to next data
510 sih->ti_db_ptr = t_s_outbuf;
511 }
512 else
513 {
514 sih->t_data_count = sih->t_data_count - sizeof(struct town_index_data_block);
515
516 if (sih->t_data_count > 0)
517 {
518 sih->ti_db_ptr++;
519 //dbg(0, "dc=%d ptr=%p\n", sih->data_count, sih->si_db_ptr);
520 }
521 else
522 {
523 // init
524 sih->t_next_out = t_s_outbuf;
525 sih->t_avail_out = t_OUT_BUF_SIZE2;
526
527 // read data
528 sih->t_data_count = town_index_decompress_data_block(sih);
529 //dbg(0, "stat2=%d\n", sih->data_count);
530
531 if (sih->t_data_count <= 0)
532 {
533 // end of data
534 //fprintf(stderr, "end of data (2)\n");
535 return 0;
536 }
537
538 // move ptr to next data
539 sih->ti_db_ptr = t_s_outbuf;
540 }
541 }
542
543 //dbg(0, "data=%s, %d, %d, %lld\n", sih->si_db_ptr->street_name, sih->si_db_ptr->lat, sih->si_db_ptr->lon, sih->si_db_ptr->town_id);
544
545 return 1;
546
547 //if (ftell(sih->sif) > (sih->si_ib->offset + sih->si_ib->len))
548 //{
549 // // end of data
550 // return 0;
551 //}
552
553 // more data found
554 // return 1;
555 }
556
557 void street_index_close_compr(struct street_index_head *sih)
558 {
559 //dbg(0,"+#+:enter\n");
560
561 g_free(sih->inflator);
562 sih->comp_status = 2;
563
564 //dbg(0,"+#+:leave\n");
565 }
566
567 void town_index_close_compr(struct street_index_head *sih)
568 {
569 //dbg(0,"+#+:enter\n");
570
571 g_free(sih->t_inflator);
572 sih->t_comp_status = 2;
573
574 //dbg(0,"+#+:leave\n");
575 }
576
577 void street_index_init_compr(struct street_index_head *sih, long long size)
578 {
579 //dbg(0,"+#+:enter\n");
580
581 // decompress structure
582 sih->inflator = g_new0(tinfl_decompressor, 1);
583 sih->comp_status = 1;
584 // decompress structure
585
586 // Decompression.
587 sih->infile_size = (uint) size;
588 sih->infile_remaining = sih->infile_size;
589
590 sih->next_in = s_inbuf;
591 sih->avail_in = 0;
592 sih->next_out = s_outbuf;
593 sih->avail_out = OUT_BUF_SIZE2;
594
595 sih->data_count = 0;
596
597 tinfl_init(sih->inflator);
598
599 //dbg(0,"+#+:leave\n");
600 }
601
602 void town_index_init_compr(struct street_index_head *sih, long long size)
603 {
604 //dbg(0,"+#+:enter\n");
605
606 // decompress structure
607 sih->t_inflator = g_new0(tinfl_decompressor, 1);
608 sih->t_comp_status = 1;
609 // decompress structure
610
611 // Decompression.
612 sih->t_infile_size = (uint) size;
613 sih->t_infile_remaining = sih->t_infile_size;
614
615 sih->t_next_in = t_s_inbuf;
616 sih->t_avail_in = 0;
617 sih->t_next_out = t_s_outbuf;
618 sih->t_avail_out = t_OUT_BUF_SIZE2;
619
620 sih->t_data_count = 0;
621
622 tinfl_init(sih->t_inflator);
623
624 //dbg(0,"+#+:leave\n");
625 }
626
627 int street_index_decompress_data_block(struct street_index_head *sih)
628 {
629 //dbg(0,"+#+:enter\n");
630
631 // size_t total_in = 0, total_out = 0;
632 // long file_loc;
633
634 // Decompression.
635 for (;;)
636 {
637 sih->in_bytes = 0;
638 sih->out_bytes = 0;
639 if (!sih->avail_in)
640 {
641 // Input buffer is empty, so read more bytes from input file.
642 uint n = my_min(IN_BUF_SIZE2, sih->infile_remaining);
643
644 //dbg(0, "reading bytes:%d remain=%d\n", n, sih->infile_remaining);
645
646 if (fread(s_inbuf, 1, n, sih->sif) != n)
647 {
648 //printf("Failed reading from input file!\n");
649 dbg(0, "Failed reading from input file!\n");
650 //g_free(sih->inflator);
651 return -1;
652 }
653
654 sih->next_in = s_inbuf;
655 sih->avail_in = n;
656
657 sih->infile_remaining -= n;
658 }
659
660 sih->in_bytes = sih->avail_in;
661 sih->out_bytes = sih->avail_out;
662 sih->miniz_status = tinfl_decompress(sih->inflator, (const mz_uint8 *) sih->next_in, &sih->in_bytes, s_outbuf, (mz_uint8 *) sih->next_out, &sih->out_bytes, (sih->infile_remaining ? TINFL_FLAG_HAS_MORE_INPUT : 0) | TINFL_FLAG_PARSE_ZLIB_HEADER);
663
664 sih->avail_in -= sih->in_bytes;
665 sih->next_in = (const mz_uint8 *) sih->next_in + sih->in_bytes;
666 //total_in += sih->in_bytes;
667
668 sih->avail_out -= sih->out_bytes;
669 sih->next_out = (mz_uint8 *) sih->next_out + sih->out_bytes;
670 //total_out += sih->out_bytes;
671
672 if ((sih->miniz_status <= TINFL_STATUS_DONE) || (!sih->avail_out))
673 {
674 // Output buffer is full, or decompression is done, so write buffer to output file.
675 uint n = OUT_BUF_SIZE2 - (uint) sih->avail_out;
676
677 //dbg(0, "decompr: start=%p len=%d\n", (void *) sih->next_out, (int) n);
678 //dbg(0, "decompr: start=%p len=%d\n", (void *) s_outbuf, (int) n);
679 //dbg(0, "decompr: av in=%d av out=%d\n", sih->avail_in, sih->avail_out);
680 //dbg(0, "decompr: nx in=%d nx out=%d\n", sih->next_in, sih->next_out);
681
682 //struct streets_index_data_block *tmp = (struct streets_index_data_block *)s_outbuf;
683 //dbg(0,"data=%s, %d, %d, %lld\n", tmp->street_name, tmp->lat, tmp->lon, tmp->town_id);
684
685 //sih->next_out = s_outbuf;
686 //sih->avail_out = OUT_BUF_SIZE2;
687
688 return (int) n;
689
690 //if (fwrite(s_outbuf, 1, n, pOutfile) != n)
691 //{
692 // // printf("Failed writing to output file!\n");
693 // //g_free(inflator);
694 // return;
695 //}
696 }
697
698 // If sih->miniz_status is <= TINFL_STATUS_DONE then either decompression is done or something went wrong.
699 if (sih->miniz_status <= TINFL_STATUS_DONE)
700 {
701 if (sih->miniz_status == TINFL_STATUS_DONE)
702 {
703 // Decompression completed successfully.
704 //dbg(0, "Decompression completed successfully\n");
705 //break;
706 return -2;
707 }
708 else
709 {
710 // Decompression failed.
711 //printf("tinfl_decompress() failed with status %i!\n", sih->miniz_status);
712 dbg(0, "tinfl_decompress() failed with status %i!\n", sih->miniz_status);
713
714 //g_free(inflator);
715 return -1;
716 }
717 }
718 }
719
720 //g_free(inflator);
721 return -3;
722
723 }
724
725
726
727 int town_index_decompress_data_block(struct street_index_head *sih)
728 {
729 //dbg(0,"+#+:enter\n");
730
731 // size_t total_in = 0, total_out = 0;
732 // long file_loc;
733
734 // Decompression.
735 for (;;)
736 {
737 sih->t_in_bytes = 0;
738 sih->t_out_bytes = 0;
739 if (!sih->t_avail_in)
740 {
741 // Input buffer is empty, so read more bytes from input file.
742 uint n = my_min(t_IN_BUF_SIZE2, sih->t_infile_remaining);
743
744 //dbg(0, "reading bytes:%d remain=%d\n", n, sih->t_infile_remaining);
745
746 if (fread(t_s_inbuf, 1, n, sih->sif) != n)
747 {
748 //printf("Failed reading from input file!\n");
749 dbg(0, "Failed reading from input file!\n");
750 //g_free(sih->inflator);
751 return -1;
752 }
753
754 sih->t_next_in = t_s_inbuf;
755 sih->t_avail_in = n;
756
757 sih->t_infile_remaining -= n;
758 }
759
760 sih->t_in_bytes = sih->t_avail_in;
761 sih->t_out_bytes = sih->t_avail_out;
762 sih->t_miniz_status = tinfl_decompress(sih->t_inflator, (const mz_uint8 *) sih->t_next_in, &sih->t_in_bytes, t_s_outbuf, (mz_uint8 *) sih->t_next_out, &sih->t_out_bytes, (sih->t_infile_remaining ? TINFL_FLAG_HAS_MORE_INPUT : 0) | TINFL_FLAG_PARSE_ZLIB_HEADER);
763
764 sih->t_avail_in -= sih->t_in_bytes;
765 sih->t_next_in = (const mz_uint8 *) sih->t_next_in + sih->t_in_bytes;
766 //total_in += sih->in_bytes;
767
768 sih->t_avail_out -= sih->t_out_bytes;
769 sih->t_next_out = (mz_uint8 *) sih->t_next_out + sih->t_out_bytes;
770 //total_out += sih->out_bytes;
771
772 if ((sih->t_miniz_status <= TINFL_STATUS_DONE) || (!sih->t_avail_out))
773 {
774 // Output buffer is full, or decompression is done, so write buffer to output file.
775 uint n = t_OUT_BUF_SIZE2 - (uint) sih->t_avail_out;
776
777 //dbg(0, "decompr: start=%p len=%d\n", (void *) sih->next_out, (int) n);
778 //dbg(0, "decompr: start=%p len=%d\n", (void *) s_outbuf, (int) n);
779 //dbg(0, "decompr: av in=%d av out=%d\n", sih->avail_in, sih->avail_out);
780 //dbg(0, "decompr: nx in=%d nx out=%d\n", sih->next_in, sih->next_out);
781
782 //struct town_index_data_block *tmpt = (struct town_index_data_block *)t_s_outbuf;
783 //dbg(0,"data=%lld %s\n", tmpt->town_id, tmpt->town_name);
784
785 //sih->next_out = s_outbuf;
786 //sih->avail_out = t_OUT_BUF_SIZE2;
787
788 return (int) n;
789
790 //if (fwrite(s_outbuf, 1, n, pOutfile) != n)
791 //{
792 // // printf("Failed writing to output file!\n");
793 // //g_free(inflator);
794 // return;
795 //}
796 }
797
798 // If sih->miniz_status is <= TINFL_STATUS_DONE then either decompression is done or something went wrong.
799 if (sih->t_miniz_status <= TINFL_STATUS_DONE)
800 {
801 if (sih->t_miniz_status == TINFL_STATUS_DONE)
802 {
803 // Decompression completed successfully.
804 //dbg(0, "Decompression completed successfully\n");
805 //break;
806 return -2;
807 }
808 else
809 {
810 // Decompression failed.
811 //printf("tinfl_decompress() failed with status %i!\n", sih->miniz_status);
812 dbg(0, "tinfl_decompress() failed with status %i!\n", sih->t_miniz_status);
813
814 //g_free(inflator);
815 return -1;
816 }
817 }
818 }
819
820 //g_free(inflator);
821 return -3;
822
823 }
824
825 void street_index_close(struct street_index_head *sih)
826 {
827 //dbg(0,"+#+:enter\n");
828
829 g_free(sih->si_ib_mem);
830 g_free(sih->ti_ib_mem);
831 if (town_lookup_cache)
832 {
833 g_free(town_lookup_cache);
834 town_lookup_cache = NULL;
835 }
836 fclose(sih->sif);
837 g_free(sih);
838
839 //dbg(0,"+#+:leave\n");
840 }
841
842
843
844 #include <sys/types.h>
845 #include <dirent.h>
846
847 void spill_index()
848 {
849 }
850
851 void search_v2(char *addr, char *town, char* hn, int partial, struct jni_object *jni)
852 {
853 //dbg(0,"+#+:enter\n");
854
855 int len;
856 int len2;
857 int len3;
858 char *last_four;
859 DIR* dirp;
860 struct dirent *dp;
861 int result_count = 0;
862 int this_res_count = 0;
863 void *p1;
864 void *p2;
865 void *p3;
866 void *p4;
867
868 s_outbuf = g_malloc(OUT_BUF_SIZE2);
869 t_s_outbuf = g_malloc(t_OUT_BUF_SIZE2);
870 p1 = s_outbuf;
871 p2 = t_s_outbuf;
872
873 s_inbuf = g_malloc(IN_BUF_SIZE2);
874 t_s_inbuf = g_malloc(t_IN_BUF_SIZE2);
875 p3 = s_inbuf;
876 p4 = t_s_inbuf;
877
878
879 len2 = strlen("navitmap_");
880 len = len2 + 11; // should be 21 'navitmap_0%%.bin.idx'
881
882 if ((town == NULL) || (strlen(town) == 0))
883 {
884 result_count = 0;
885 this_res_count = 0;
886
887 // ------------- SEARCH LOOP -------------
888 // look for all the navitmap_0**.bin.idx files in mapdir, then call search in all of them
889 // we dont have a TOWN name, so just search for "addr"
890 dirp = opendir(navit_maps_dir);
891 while ((dp = readdir(dirp)) != NULL)
892 {
893 if ((strlen(dp->d_name) == len) && (!strncmp(dp->d_name, "navitmap_", len2)))
894 {
895 len3 = strlen(dp->d_name);
896 last_four = &dp->d_name[len3 - 4];
897 if (!strcmp(last_four, ".idx"))
898 {
899 this_res_count = search_v2_work(addr, NULL, hn, partial, jni, dp->d_name);
900 //dbg(0, "SLOOP:s=%s t=%s c=%i\n", addr, NULL, this_res_count);
901 result_count = result_count + this_res_count;
902 }
903 }
904 }
905 closedir(dirp);
906 //dbg(0, "SLOOP:sumc=%i\n", result_count)
907 // ------------- SEARCH LOOP -------------
908 }
909 else
910 {
911 result_count = 0;
912 this_res_count = 0;
913
914 // ------------- SEARCH LOOP 1 -------------
915 // look for all the navitmap_0**.bin.idx files in mapdir, then call search in all of them
916 // first search for "addr<space>town" concated as street name
917 char *addr_concat = g_strdup_printf("%s %s", addr, town);
918 dirp = opendir(navit_maps_dir);
919 while ((dp = readdir(dirp)) != NULL)
920 {
921 if ((strlen(dp->d_name) == len) && (!strncmp(dp->d_name, "navitmap_", len2)))
922 {
923 len3 = strlen(dp->d_name);
924 last_four = &dp->d_name[len3 - 4];
925 if (!strcmp(last_four, ".idx"))
926 {
927 this_res_count = search_v2_work(addr_concat, NULL, hn, partial, jni, dp->d_name);
928 //dbg(0, "SLOOP1:s=%s t=%s c=%i\n", addr_concat, NULL, this_res_count);
929 result_count = result_count + this_res_count;
930 }
931 }
932 }
933 closedir(dirp);
934 //dbg(0, "SLOOP1:sumc=%i\n", result_count)
935 g_free(addr_concat);
936 // ------------- SEARCH LOOP 1 -------------
937
938 // ------------- SEARCH LOOP 2 -------------
939 // look for all the navitmap_0**.bin.idx files in mapdir, then call search in all of them
940 // then search for "addr" and "town"
941 dirp = opendir(navit_maps_dir);
942 while ((dp = readdir(dirp)) != NULL)
943 {
944 if ((strlen(dp->d_name) == len) && (!strncmp(dp->d_name, "navitmap_", len2)))
945 {
946 len3 = strlen(dp->d_name);
947 last_four = &dp->d_name[len3 - 4];
948 if (!strcmp(last_four, ".idx"))
949 {
950 this_res_count = search_v2_work(addr, town, hn, partial, jni, dp->d_name);
951 //dbg(0, "SLOOP2:s=%s t=%s c=%i\n", addr, town, this_res_count);
952 result_count = result_count + this_res_count;
953 }
954 }
955 }
956 closedir(dirp);
957 //dbg(0, "SLOOP2:sumc=%i\n", result_count)
958 // ------------- SEARCH LOOP 2 -------------
959
960 // if still no result then search only for "addr" as streetname
961 if (result_count == 0)
962 {
963 // ------------- SEARCH LOOP 3 -------------
964 // look for all the navitmap_0**.bin.idx files in mapdir, then call search in all of them
965 dirp = opendir(navit_maps_dir);
966 while ((dp = readdir(dirp)) != NULL)
967 {
968 if ((strlen(dp->d_name) == len) && (!strncmp(dp->d_name, "navitmap_", len2)))
969 {
970 len3 = strlen(dp->d_name);
971 last_four = &dp->d_name[len3 - 4];
972 if (!strcmp(last_four, ".idx"))
973 {
974 this_res_count = search_v2_work(addr, NULL, hn, partial, jni, dp->d_name);
975 //dbg(0, "SLOOP3:s=%s t=%s c=%i\n", addr, NULL, this_res_count);
976 result_count = result_count + this_res_count;
977 }
978 }
979 }
980 closedir(dirp);
981 //dbg(0, "SLOOP3:sumc=%i\n", result_count)
982 // ------------- SEARCH LOOP 3 -------------
983 }
984
985 }
986
987 g_free(p1);
988 g_free(p2);
989
990 g_free(p3);
991 g_free(p4);
992
993 //dbg(0,"+#+:leave\n");
994 }
995
996 int search_v2_work(char *addr, char *town, char* hn, int partial, struct jni_object *jni, const char* idxfile_name)
997 {
998 //dbg(0,"+#+:enter\n");
999
1000 char *buffer = NULL;
1001 float lat;
1002 float lng;
1003 char *address;
1004 char *address2;
1005 char *townname = NULL;
1006 char *addr_copy;
1007 char *addr2;
1008 char *addr3;
1009 char *addr3a;
1010 char *town_fold;
1011 char *tt;
1012 int i;
1013 int j;
1014 int br;
1015 int charlen;
1016 int found;
1017 int starts_with_utf8 = 0;
1018 int nd_with_utf8 = 0;
1019 int want_result;
1020 struct coord c3;
1021 static const char *alpha = "abcdefghijklmnopqrstuvwxyz";
1022 // char tmp_letter[STREET_INDEX_STREET_NAME_SIZE - 1];
1023 struct street_index_head *sih;
1024 int res_hn = 0;
1025 int search_results_found_ = 0;
1026 long long pos_now_pre;
1027 long long pos_now_aft;
1028 int max_townname_plus_1 = MAX_INDEXSEARCH_TOWNNAME + 1;
1029
1030
1031 if ((!addr) || (strlen(addr) < 1))
1032 {
1033 return NULL;
1034 }
1035
1036 // prepare search string
1037 addr2 = linguistics_casefold(addr);
1038 if (addr2)
1039 {
1040 addr3 = linguistics_remove_all_specials(addr2);
1041 if (addr3)
1042 {
1043 g_free(addr2);
1044 addr2 = addr3;
1045 }
1046 addr3 = linguistics_expand_special(addr2, 1);
1047 if (addr3)
1048 {
1049 g_free(addr2);
1050 addr2 = addr3;
1051 }
1052 addr_copy = addr2;
1053 }
1054 else
1055 {
1056 addr2 = g_strdup(addr);
1057 addr_copy = addr2;
1058 }
1059
1060 // prepare town search string
1061 town_fold = linguistics_fold_and_prepare_complete(town, 0);
1062
1063
1064 //dbg(0,"town=%s townfold=%s street=%s hn=%s\n", town, town_fold, addr_copy, hn);
1065
1066 sih = street_index_init(idxfile_name);
1067
1068 // is first letter ascii or UTF-8?
1069 addr3 = g_utf8_find_next_char(addr_copy, NULL);
1070 charlen = addr3 - addr_copy;
1071 if (charlen > 1)
1072 {
1073 starts_with_utf8 = 1;
1074 }
1075 //dbg(0, "charlen=%d starts_with_utf8=%d\n", charlen, starts_with_utf8);
1076
1077 // is second letter ascii or UTF-8?
1078 addr3a = g_utf8_find_next_char(addr3, NULL);
1079 charlen = addr3a - addr3;
1080 if (charlen > 1)
1081 {
1082 nd_with_utf8 = 1;
1083 }
1084 //dbg(0, "charlen=%d nd_with_utf8=%d\n", charlen, nd_with_utf8);
1085
1086
1087 // find starting letter of search string
1088 found = (703 - 1); // 26+1 letters ((26+1)*26 + 1) = 703
1089 br = 0;
1090 if (starts_with_utf8 == 0)
1091 {
1092 // check the first letter
1093 for (i = 0; i < 26; i++)
1094 {
1095 if (addr_copy[0] == alpha[i])
1096 {
1097 if ((strlen(addr_copy) > 1) && (nd_with_utf8 == 0))
1098 {
1099 //dbg(0, "i=%d\n", i);
1100 // check the second letter
1101 for (j = 0; j < 26; j++)
1102 {
1103 //dbg(0, "j=%d\n", j);
1104 if (addr_copy[1] == alpha[j])
1105 {
1106 found = (27 * i) + j;
1107 br = 1;
1108 break;
1109 }
1110 }
1111 if (br == 0)
1112 {
1113 // second letter has no match, use generic first letter block
1114 found = (27 * i) + 26;
1115 }
1116 br = 1;
1117 break;
1118 }
1119 else
1120 {
1121 // use generic first letter block
1122 found = (27 * i) + 26;
1123 br = 1;
1124 break;
1125 }
1126 }
1127
1128 if (br)
1129 {
1130 break;
1131 }
1132 }
1133 }
1134
1135 // dbg(0, "found pos=%d\n", found);
1136
1137 street_index_setpos(sih, found);
1138 int found_data = 0;
1139 //int ddd = 0;
1140
1141 while (street_index_read_data(sih))
1142 {
1143 // ------------------------------------------
1144 // ------------------------------------------
1145 // what streettypes do we want to include in search:
1146 // 1 -> normal street in native language
1147 // 2 -> english streetname
1148 // 3 -> alternative name
1149 // 4 -> town name
1150 // 40 -> POI
1151 // ------------------------------------------
1152 // ------------------------------------------
1153 //
1154 // dbg(0,"STREETINDEX:type=%d name=%s\n", (int)sih->si_db_ptr->street_type, sih->si_db_ptr->street_name);
1155 #if 0
1156 if ((int)sih->si_db_ptr->street_type == 2)
1157 {
1158 continue;
1159 }
1160
1161 if ((int)sih->si_db_ptr->street_type == 3)
1162 {
1163 continue;
1164 }
1165 #endif
1166 //
1167 // ------------------------------------------
1168 // ------------------------------------------
1169
1170 //ddd++;
1171
1172 //if (ddd > 3)
1173 //{
1174 // break;
1175 //}
1176
1177 if (offline_search_break_searching == 1)
1178 {
1179 break;
1180 }
1181
1182
1183 // -- SANITY CHECK -- ----------------------
1184 if (! sih->si_db_ptr->street_name)
1185 {
1186 // the end is here
1187 break;
1188 }
1189
1190 if (strlen(sih->si_db_ptr->street_name) == 0)
1191 {
1192 // the end is here
1193 break;
1194 }
1195 // -- SANITY CHECK -- ----------------------
1196
1197
1198 //dbg(0,"data=%s addr=%s\n", sih->si_db_ptr->street_name, addr_copy);
1199 if (!ascii_cmp_local_faster(sih->si_db_ptr->street_name, addr_copy, 1))
1200 {
1201 found_data = 1;
1202 if ((partial == 1) || (!ascii_cmp_local_faster(sih->si_db_ptr->street_name, addr_copy, partial)))
1203 {
1204 // dbg(0, "TWNAME:00:t_id=%d\n", (int)sih->si_db_ptr->town_id);
1205
1206 // townname = NULL;
1207 pos_now_pre = ftello(sih->sif); // 64bit
1208 townname = town_index_lookup(sih, sih->si_db_ptr->town_id);
1209 pos_now_aft = ftello(sih->sif); // 64bit
1210 if (pos_now_pre != pos_now_aft)
1211 {
1212 // dbg(0, "correct seek position\n");
1213 fseeko(sih->sif, (off_t)pos_now_pre, SEEK_SET);
1214 }
1215
1216 // if we also have a search-town-name then check here
1217 if ((!town_fold)||(strlen(town_fold) < 1))
1218 {
1219 want_result = 1;
1220 }
1221 else
1222 {
1223 tt = linguistics_fold_and_prepare_complete(townname, 0);
1224 want_result = 1-(linguistics_compare_anywhere(tt, town_fold));
1225 //dbg(0, "want_result=%d tt=%s\n", want_result, tt);
1226 }
1227
1228 if (want_result == 1)
1229 {
1230
1231 // check for housenumber
1232 if ((hn != NULL) && (strlen(hn) > 0))
1233 {
1234 // now set coord of this item/street
1235 c3.y = sih->si_db_ptr->lat;
1236 c3.x = sih->si_db_ptr->lon;
1237 res_hn = search_address_housenumber_for_street(hn, sih->si_db_ptr->street_name, townname, &c3, partial, jni);
1238 search_results_found_ = search_results_found_ + res_hn;
1239 }
1240
1241 // dbg(0, "TWNAME:00:t=%p\n", townname);
1242
1243 if ((townname)&&(strcmp(townname, "(null)")))
1244 {
1245 address = g_strdup_printf("%s, %s", sih->si_db_ptr->street_name, townname);
1246
1247 // dbg(0, "TWNAME:01:t=%s\n", townname);
1248 }
1249 else
1250 {
1251 address = g_strdup_printf("%s", sih->si_db_ptr->street_name);
1252 }
1253
1254 if (strlen(address) > MAX_INDEXSEARCH_TOWNNAME)
1255 {
1256 address2 = address;
1257 address2[max_townname_plus_1] = '\0';
1258 address2 = linguistics_check_utf8_string(address2);
1259 }
1260 else
1261 {
1262 address2 = address;
1263 }
1264
1265 if ((int)sih->si_db_ptr->street_type == 4)
1266 {
1267 // result is town/district
1268 buffer = g_strdup_printf("TWN:H0L0:%d:%d:%.*s", sih->si_db_ptr->lat, sih->si_db_ptr->lon, max_townname_plus_1, address2);
1269 }
1270 else if ((int)sih->si_db_ptr->street_type == 40)
1271 {
1272 // result is town/district
1273 buffer = g_strdup_printf("POI:H0L0:%d:%d:%.*s", sih->si_db_ptr->lat, sih->si_db_ptr->lon, max_townname_plus_1, address2);
1274 }
1275 else
1276 {
1277 // result is street
1278 buffer = g_strdup_printf("STR:H0L0:%d:%d:%.*s", sih->si_db_ptr->lat, sih->si_db_ptr->lon, max_townname_plus_1, address2);
1279 }
1280 // dbg(0,"buffer=%s\n", buffer);
1281 #ifdef HAVE_API_ANDROID
1282 // return results to android as they come in ...
1283 android_return_search_result(jni, buffer);
1284 search_results_found_++;
1285 #endif
1286 if (townname)
1287 {
1288 g_free(townname);
1289 townname = NULL;
1290 }
1291
1292 if (address)
1293 {
1294 g_free(address);
1295 address = NULL;
1296 }
1297
1298 if (buffer)
1299 {
1300 g_free(buffer);
1301 buffer = NULL;
1302 }
1303 }
1304 }
1305 }
1306 else
1307 {
1308 if (found_data == 1)
1309 {
1310 // no more matching data, stop searching
1311 break;
1312 }
1313 }
1314 }
1315 g_free(addr_copy);
1316 street_index_close(sih);
1317
1318 //dbg(0,"+#+:leave\n");
1319
1320 return search_results_found_;
1321 }
1322
1323

   
Visit the ZANavi Wiki