/
search.c
1693 lines (1541 loc) · 49.2 KB
/
search.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2009 Openmoko Inc.
*
* Authors Holger Hans Peter Freyther <zecke@openmoko.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <inttypes.h>
#include <grifo.h>
#include "search.h"
#include "keyboard.h"
#include "guilib.h"
#include "glyph.h"
#include "ustring.h"
#include "Alloc.h"
#include "Bra.h"
#include "LzmaDec.h"
#include "wikilib.h"
#include "lcd_buf_draw.h"
#include "bigram.h"
#include "history.h"
#include "wikilib.h"
#include "search_fnd.h"
#include "wiki_info.h"
#include "utf8.h"
#include "languages.h"
#define DBG_SEARCH 0
#define DELAYED_SEARCH_TIME 0.3
unsigned long time_search_string_changed = 0;
bool search_string_changed = false;
bool search_string_changed_remove = false;
int more_search_results = 0;
extern ARTICLE_LINK articleLink[MAX_ARTICLE_LINKS];
extern int article_link_count;
extern long saved_idx_article;
int search_interrupted = 0;
int b_type_a_word_cleared = 0;
typedef struct _search_results {
unsigned char title[NUMBER_OF_FIRST_PAGE_RESULTS][MAX_TITLE_ACTUAL];
unsigned char title_search[NUMBER_OF_FIRST_PAGE_RESULTS][MAX_TITLE_SEARCH];
uint32_t idx_article[NUMBER_OF_FIRST_PAGE_RESULTS]; // index (wiki.idx) for loading the article
uint32_t offset_list[NUMBER_OF_FIRST_PAGE_RESULTS]; // offset (wiki.fnd) of each search title in list
uint32_t offset_next; // offset (wiki.fnd) of the next title after the list
uint32_t count;
uint32_t result_populated;
int32_t cur_selected; // -1 when no selection.
} SEARCH_RESULTS;
static SEARCH_RESULTS *result_list = NULL;
typedef struct _search_info {
int32_t inited;
int32_t fd_pfx;
int32_t fd_idx;
uint32_t max_article_idx;
uint32_t *prefix_index_table; // each SEARCH_INFO used to have private prefix_index_table.
//now it points to the common g_prefix_index_table for all SEARCH_INFO
uint32_t b_prefix_index_block_loaded[SEARCH_CHR_COUNT];
unsigned char *buf; // buf correspond to result_list
uint32_t buf_len;
uint32_t offset_current; // offset (wiki.fnd) of the content of buffer
} SEARCH_INFO;
static SEARCH_INFO *search_info = NULL;
uint32_t *g_prefix_index_table = NULL; // common prefix_index_table
unsigned char *g_search_info_buf = NULL; // common SEARCH_INFO buf
//#define SIZE_PREFIX_INDEX_TABLE SEARCH_CHR_COUNT * SEARCH_CHR_COUNT * SEARCH_CHR_COUNT * sizeof(long)
//static struct search_state state;
//static struct search_state last_first_hit;
static unsigned char search_string[MAX_TITLE_SEARCH];
static int search_string_pos[MAX_TITLE_SEARCH];
static unsigned char search_string_per_language[MAX_TITLE_SEARCH * 3];
static int search_str_len = 0;
static int search_str_per_language_len = 0;
static unsigned char temp_search_string[MAX_TITLE_SEARCH];
static int temp_search_string_pos[MAX_TITLE_SEARCH];
static unsigned char temp_search_string_per_language[MAX_TITLE_SEARCH * 3];
static int temp_search_str_len = 0;
static int temp_search_str_per_language_len = 0;
//static char s_find_first = 1;
static void *SzAlloc(void *p, size_t size) { p = p; return memory_allocate(size, "search0"); }
static void SzFree(void *p, void *address) { p = p; if (address) memory_free(address, "search0"); }
static ISzAlloc g_Alloc = { SzAlloc, SzFree };
void backup_search_criteria()
{
memcpy(temp_search_string, search_string, sizeof(search_string));
memcpy(temp_search_string_pos, search_string_pos, sizeof(search_string_pos));
memcpy(temp_search_string_per_language, search_string_per_language, sizeof(search_string_per_language));
temp_search_str_len = search_str_len;
temp_search_str_per_language_len = search_str_per_language_len;
}
void restore_search_criteria()
{
memcpy(search_string, temp_search_string, sizeof(search_string));
memcpy(search_string_pos, temp_search_string_pos, sizeof(search_string_pos));
memcpy(search_string_per_language, temp_search_string_per_language, sizeof(search_string_per_language));
search_str_len = temp_search_str_len;
search_str_per_language_len = temp_search_str_per_language_len;
}
long result_list_offset_next(void)
{
return result_list->offset_next;
}
long result_list_next_result(long offset_next, long *idxArticle, unsigned char *sTitleActual)
{
TITLE_SEARCH titleSearch;
unsigned char sTitleSearch[MAX_TITLE_SEARCH];
copy_fnd_to_buf(offset_next, (void *)&titleSearch, sizeof(TITLE_SEARCH));
retrieve_titles_from_fnd(offset_next, sTitleSearch, sTitleActual);
if (!search_string_cmp(sTitleSearch, search_string, search_str_len)) // match!
{
*idxArticle = titleSearch.idxArticle;
offset_next += sizeof(titleSearch.idxArticle) + ustrlen(titleSearch.sTitleSearch) + 2; // now point to the actual title
offset_next += ustrlen(&titleSearch.sTitleSearch[ustrlen(titleSearch.sTitleSearch) + 1]) + 1; // skip the actual title to the next titleSearch
return offset_next;
}
else
return 0;
}
void get_article_title_from_idx(long idx, unsigned char *title)
{
ARTICLE_PTR article_ptr;
TITLE_SEARCH title_search;
int wiki_id;
int wiki_idx;
int nTmpeCurrentWiki = nCurrentWiki;
unsigned char sTitleSearch[MAX_TITLE_SEARCH];
title[0] = '\0';
wiki_id = idx >> 24;
idx &= 0x00FFFFFF;
if (wiki_id > 0)
{
wiki_idx = get_wiki_idx_from_id(wiki_id);
if (wiki_idx < 0) // wiki not loaded
return;
nCurrentWiki = wiki_idx;
}
file_lseek(search_info[nCurrentWiki].fd_idx, (idx - 1) * sizeof(ARTICLE_PTR) + 4);
file_read(search_info[nCurrentWiki].fd_idx, (void *)&article_ptr, sizeof(article_ptr));
if (article_ptr.offset_fnd)
{
copy_fnd_to_buf(article_ptr.offset_fnd, (unsigned char *)&title_search, sizeof(title_search));
retrieve_titles_from_fnd(article_ptr.offset_fnd, sTitleSearch, title);
title[MAX_TITLE_ACTUAL - 1] = '\0';
}
nCurrentWiki = nTmpeCurrentWiki;
}
void load_prefix_index(int nWikiIdx)
{
if (!search_info[nWikiIdx].inited)
{
search_info[nWikiIdx].fd_pfx = file_open(get_wiki_file_path(nWikiIdx, "wiki.pfx"), FILE_OPEN_READ);
search_info[nWikiIdx].fd_idx = file_open(get_wiki_file_path(nWikiIdx, "wiki.idx"), FILE_OPEN_READ);
search_info[nWikiIdx].offset_current = -1;
if (search_info[nWikiIdx].fd_pfx >= 0 && search_info[nWikiIdx].fd_idx >= 0)
{
file_read(search_info[nWikiIdx].fd_idx, (void *)&search_info[nWikiIdx].max_article_idx, sizeof(search_info[nWikiIdx].max_article_idx));
memset(search_info[nWikiIdx].b_prefix_index_block_loaded, 0, sizeof(search_info[nWikiIdx].b_prefix_index_block_loaded));
search_info[nWikiIdx].inited = 1;
}
else
fatal_error("index file open error");
}
}
void reset_search_info(int nWikiIdx)
{
load_prefix_index(nWikiIdx);
search_info[nWikiIdx].offset_current = -1;
memset(search_info[nWikiIdx].b_prefix_index_block_loaded, 0, sizeof(search_info[nWikiIdx].b_prefix_index_block_loaded));
}
void search_init()
{
int i;
int nWikiCount = get_wiki_count();
if (!result_list)
{
result_list = (SEARCH_RESULTS *)memory_allocate(sizeof(SEARCH_RESULTS), "search1");
if (!result_list)
fatal_error("search_init malloc error");
}
if (!search_info)
{
search_info = (SEARCH_INFO *)memory_allocate(sizeof(SEARCH_INFO) * nWikiCount, "search2");
if (!search_info)
fatal_error("search_init malloc error");
else
{
g_prefix_index_table = (uint32_t *)memory_allocate(sizeof(uint32_t) * SEARCH_CHR_COUNT * SEARCH_CHR_COUNT * SEARCH_CHR_COUNT, "search3");
g_search_info_buf = (unsigned char *)memory_allocate(NUMBER_OF_FIRST_PAGE_RESULTS * sizeof(TITLE_SEARCH), "search4");
if (!g_prefix_index_table || !g_search_info_buf)
fatal_error("search_init malloc error");
for (i = 0; i < nWikiCount; i++)
{
search_info[i].inited = 0;
search_info[i].prefix_index_table = g_prefix_index_table;
search_info[i].buf = g_search_info_buf;
}
}
}
load_prefix_index(nCurrentWiki);
result_list->count = 0;
init_search_fnd();
}
void memrcpy(char *dest, char *src, int len) // memory copy starting from the last byte
{
if (len >= 0)
{
dest += len - 1;
src += len - 1;
while (len--)
{
*dest = *src;
--dest;
--src;
}
}
}
unsigned char article_error[100] = "";
unsigned char article_error2[100] = "";
static void print_article_error()
{
unsigned char msg[100];
sprintf((char *)msg, "The article, %lx, failed to load.", saved_idx_article);
guilib_fb_lock();
guilib_clear();
render_string(SEARCH_LIST_FONT_IDX, -1, 84, msg, ustrlen(msg), 0);
render_string(SEARCH_LIST_FONT_IDX, -1, 104, (const unsigned char *)"Please restart your WikiReader and", 34, 0);
render_string(SEARCH_LIST_FONT_IDX, -1, 124, (const unsigned char *)"try again.", 10, 0);
//render_string(SEARCH_LIST_FONT_IDX, -1, 124, article_error, ustrlen(article_error), 0);
//render_string(SEARCH_LIST_FONT_IDX, -1, 144, article_error2, ustrlen(article_error2), 0);
guilib_fb_unlock();
}
// check if null terminator exists
int is_proper_string(const unsigned char *s, int len)
{
while (len >= 0)
{
if (!*s)
return 1;
++s;
len--;
}
return 0;
}
char *strnstr(char *s1, char *s2, int len)
{
int bFound = 0;
int s2_len = ustrlen(s2);
while (!bFound && len >= s2_len)
{
if (!memcmp(s1, s2, s2_len))
bFound = 1;
else
{
s1++;
len--;
}
}
if (bFound)
return s1;
else
return NULL;
}
// locate the patterh of the beginning of a TITLE_SEARCH structure
// the continuous 8 bytes should look like the following:
// 0xXX00 (null terminated last two bytes of the last TITLE_SEARCH.sTitleActual where XX is not 00) +
// 0xXXXXXX00 (TITLE_SEARCH.idxArticle where XXXXXX is not 000000) +
// 0x00 (TITLE_SEARCH.cZero) +
// 0xXX (Non-zero of TITLE_SEARCH.sTitleSearch)
bool is_title_search_pattern(const unsigned char *pBuf)
{
return pBuf[0] && !pBuf[1] && (pBuf[2] || pBuf[3] || pBuf[4]) && !pBuf[5] && !pBuf[6] && pBuf[7];
}
TITLE_SEARCH *locate_proper_title_search(unsigned char *buf_middle, int len)
{
unsigned char *pBuf = buf_middle - 2; // including the possible null terminated last two bytes of the last TITLE_SEARCH
// for finding the proper TITLE_SEARCH pattern
int i = 0;
bool bFound = false;
while (!bFound && i < len + 2 - 8) // the pattern consists of 8 bytes
{
if (is_title_search_pattern(&pBuf[i]))
bFound = true;
else
i++;
}
if (bFound)
return (TITLE_SEARCH *)(pBuf + i + 2);
else
return NULL;
}
uint32_t get_article_idx_from_offset_range(const unsigned char *sInputTitleActual, long offset_fnd_start, long offset_fnd_end)
{
unsigned int len;
int rc;
uint32_t article_idx = 0;
unsigned char sTitleSearch[MAX_TITLE_SEARCH];
unsigned char sTitleActual[MAX_TITLE_ACTUAL];
unsigned char buf_middle[sizeof(TITLE_SEARCH) * 2];
long offset_middle;
static TITLE_SEARCH *pTitleSearch;
unsigned int offsetNextTitleSearch = 0;
bool bFound = false;
while (!bFound && offset_fnd_start >= 0)
{
if (search_info[nCurrentWiki].offset_current != (uint32_t)offset_fnd_start)
{
search_info[nCurrentWiki].buf_len = copy_fnd_to_buf(offset_fnd_start, search_info[nCurrentWiki].buf, NUMBER_OF_FIRST_PAGE_RESULTS * sizeof(TITLE_SEARCH));
search_info[nCurrentWiki].offset_current = offset_fnd_start;
if (search_interrupted)
{
goto out;
}
if (search_info[nCurrentWiki].buf_len < sizeof(pTitleSearch->idxArticle) + sizeof(pTitleSearch->cZero) + 2) // at lease 2 chars for pTitleSearch->sTitleSearch
{
goto out;
}
}
pTitleSearch = (TITLE_SEARCH *)&search_info[nCurrentWiki].buf[offsetNextTitleSearch];
if (offsetNextTitleSearch < search_info[nCurrentWiki].buf_len &&
is_proper_string(pTitleSearch->sTitleSearch, search_info[nCurrentWiki].buf_len - offsetNextTitleSearch -
sizeof(pTitleSearch->idxArticle) - sizeof(pTitleSearch->cZero)))
{
retrieve_titles_from_fnd(offset_fnd_start + offsetNextTitleSearch,
sTitleSearch, sTitleActual);
rc = search_string_cmp(sTitleSearch, search_string, search_str_len);
if (!rc) // match!
{
if (!ustrcmp(sTitleActual, sInputTitleActual))
{
article_idx = pTitleSearch->idxArticle;
bFound = true;
}
else
{
offset_fnd_end = -1; // start sequential search
offsetNextTitleSearch += sizeof(pTitleSearch->idxArticle) + ustrlen(pTitleSearch->sTitleSearch) +
ustrlen(&pTitleSearch->sTitleSearch[ustrlen(pTitleSearch->sTitleSearch) + 1]) + 3;
offset_fnd_start = offset_fnd_start + offsetNextTitleSearch;
offsetNextTitleSearch = 0;
}
}
else if (rc < 0)
{
if (offset_fnd_end <= 0)
{
offsetNextTitleSearch += sizeof(pTitleSearch->idxArticle) + ustrlen(pTitleSearch->sTitleSearch) +
ustrlen(&pTitleSearch->sTitleSearch[ustrlen(pTitleSearch->sTitleSearch) + 1]) + 3;
offset_fnd_start = offset_fnd_start + offsetNextTitleSearch;
}
else // binary search
{
offset_middle = offset_fnd_start + (offset_fnd_end - offset_fnd_start) / 2; // position to the middle of the range
if (offset_middle <= offset_fnd_start)
offset_fnd_end = -1;
else
{
len = copy_fnd_to_buf(offset_middle, buf_middle, sizeof(buf_middle));
if (search_interrupted)
{
goto out;
}
if (len >= sizeof(pTitleSearch->idxArticle) + sizeof(pTitleSearch->cZero) + 2) // at lease 2 chars for pTitleSearch->sTitleSearch
{
pTitleSearch = locate_proper_title_search(buf_middle, len);
if (pTitleSearch)
{
unsigned char local_title_search[MAX_TITLE_SEARCH];
unsigned char sTitleActual[MAX_TITLE_ACTUAL];
offset_middle += (const unsigned char *)pTitleSearch - buf_middle;
retrieve_titles_from_fnd(offset_middle, local_title_search, sTitleActual);
rc = search_string_cmp(local_title_search, search_string, search_str_len);
if (rc >= 0) // the first mactch will be in front or at offset_middle
{
if (offset_middle == offset_fnd_end) // search from offset_fnd_start directly
offset_fnd_end = -1;
else
offset_fnd_end = offset_middle;
}
else // the first mactch will be after offset_middle
{
offset_fnd_start = offset_middle;
search_info[nCurrentWiki].buf_len = copy_fnd_to_buf(offset_fnd_start, search_info[nCurrentWiki].buf, NUMBER_OF_FIRST_PAGE_RESULTS * sizeof(TITLE_SEARCH));
search_info[nCurrentWiki].offset_current = offset_fnd_start;
if (search_interrupted)
{
goto out;
}
if (search_info[nCurrentWiki].buf_len <= 0)
{
goto out;
}
offsetNextTitleSearch = 0;
}
}
}
else
offset_fnd_end = -1;
}
}
offsetNextTitleSearch = 0;
}
else
{
bFound = true; // return 0
goto out;
}
}
else
{
offset_fnd_start = offset_fnd_start + offsetNextTitleSearch;
search_info[nCurrentWiki].buf_len = copy_fnd_to_buf(offset_fnd_start, search_info[nCurrentWiki].buf, NUMBER_OF_FIRST_PAGE_RESULTS * sizeof(TITLE_SEARCH));
search_info[nCurrentWiki].offset_current = offset_fnd_start;
if (search_interrupted)
{
goto out;
}
if (search_info[nCurrentWiki].buf_len <= 0)
{
goto out;
}
offsetNextTitleSearch = 0;
}
}
out:
return article_idx;
}
int fetch_search_result(long input_offset_fnd_start, long input_offset_fnd_end, int bInit)
{
unsigned int len;
int rc;
unsigned char buf_middle[sizeof(TITLE_SEARCH) * 2];
long offset_middle;
static TITLE_SEARCH *pTitleSearch;
static unsigned int offsetNextTitleSearch = 0;
static long offset_fnd_start = -1;
static long offset_fnd_end = -1;
if (bInit)
{
result_list->result_populated = 0;
offset_fnd_start = input_offset_fnd_start;
offset_fnd_end = input_offset_fnd_end;
result_list->count = 0;
offsetNextTitleSearch = 0;
is_title_in_result_list(0, NULL);
}
if (result_list->result_populated || offset_fnd_start < 0)
return 0;
if (search_info[nCurrentWiki].offset_current != (uint32_t)offset_fnd_start)
{
search_info[nCurrentWiki].buf_len = copy_fnd_to_buf(offset_fnd_start, search_info[nCurrentWiki].buf, NUMBER_OF_FIRST_PAGE_RESULTS * sizeof(TITLE_SEARCH));
search_info[nCurrentWiki].offset_current = offset_fnd_start;
if (search_interrupted)
{
search_interrupted = 7;
goto interrupted;
}
if (search_info[nCurrentWiki].buf_len < sizeof(pTitleSearch->idxArticle) + sizeof(pTitleSearch->cZero) + 2) // at lease 2 chars for pTitleSearch->sTitleSearch
{
result_list->result_populated = 1;
goto out;
}
}
if (!result_list->result_populated)
{
pTitleSearch = (TITLE_SEARCH *)&search_info[nCurrentWiki].buf[offsetNextTitleSearch];
if (offsetNextTitleSearch < search_info[nCurrentWiki].buf_len &&
is_proper_string(pTitleSearch->sTitleSearch, search_info[nCurrentWiki].buf_len - offsetNextTitleSearch -
sizeof(pTitleSearch->idxArticle) - sizeof(pTitleSearch->cZero)))
{
retrieve_titles_from_fnd(offset_fnd_start + offsetNextTitleSearch,
result_list->title_search[result_list->count], result_list->title[result_list->count]);
rc = search_string_cmp(result_list->title_search[result_list->count], search_string, search_str_len);
if (!rc) // match!
{
if (!result_list->count)
{
offset_fnd_start = offset_fnd_start + offsetNextTitleSearch;
search_info[nCurrentWiki].buf_len = copy_fnd_to_buf(offset_fnd_start, search_info[nCurrentWiki].buf, NUMBER_OF_FIRST_PAGE_RESULTS * sizeof(TITLE_SEARCH));
search_info[nCurrentWiki].offset_current = offset_fnd_start;
if (search_interrupted)
{
search_interrupted = 8;
goto interrupted;
}
if (search_info[nCurrentWiki].buf_len < sizeof(pTitleSearch->idxArticle) + sizeof(pTitleSearch->cZero) + 2) // at lease 2 chars for pTitleSearch->sTitleSearch
{
result_list->result_populated = 1;
goto out;
}
offsetNextTitleSearch = 0;
pTitleSearch = (TITLE_SEARCH *)&search_info[nCurrentWiki].buf[offsetNextTitleSearch];
}
// use memcpy to avoid "Unaligned data access"
memcpy((void *)&result_list->idx_article[result_list->count],
(void *)&pTitleSearch->idxArticle,
sizeof(result_list->idx_article[result_list->count]));
result_list->offset_list[result_list->count] = offset_fnd_start + offsetNextTitleSearch;
offsetNextTitleSearch += sizeof(pTitleSearch->idxArticle) + ustrlen(pTitleSearch->sTitleSearch) +
ustrlen(&pTitleSearch->sTitleSearch[ustrlen(pTitleSearch->sTitleSearch) + 1]) + 3;
result_list->offset_next = offset_fnd_start + offsetNextTitleSearch;
if (!is_title_in_result_list(result_list->idx_article[result_list->count],
result_list->title[result_list->count]))
{ // if the title is not in the list, add it
result_list->count++;
}
if (result_list->count >= NUMBER_OF_FIRST_PAGE_RESULTS)
{
result_list->result_populated = 1;
goto out;
}
}
else if (rc < 0)
{
if (offset_fnd_end <= 0)
{
offsetNextTitleSearch += sizeof(pTitleSearch->idxArticle) + ustrlen(pTitleSearch->sTitleSearch) +
ustrlen(&pTitleSearch->sTitleSearch[ustrlen(pTitleSearch->sTitleSearch) + 1]) + 3;
offset_fnd_start = offset_fnd_start + offsetNextTitleSearch;
}
else // binary search
{
offset_middle = offset_fnd_start + (offset_fnd_end - offset_fnd_start) / 2; // position to the middle of the range
if (offset_middle <= offset_fnd_start)
offset_fnd_end = -1;
else
{
len = copy_fnd_to_buf(offset_middle, buf_middle, sizeof(buf_middle));
if (search_interrupted)
{
search_interrupted = 9;
goto interrupted;
}
if (len >= sizeof(pTitleSearch->idxArticle) + sizeof(pTitleSearch->cZero) + 2) // at lease 2 chars for pTitleSearch->sTitleSearch
{
pTitleSearch = locate_proper_title_search(buf_middle, len);
if (pTitleSearch)
{
unsigned char local_title_search[MAX_TITLE_SEARCH];
unsigned char sTitleActual[MAX_TITLE_ACTUAL];
offset_middle += (unsigned char *)pTitleSearch - buf_middle;
retrieve_titles_from_fnd(offset_middle, local_title_search, sTitleActual);
rc = search_string_cmp(local_title_search, search_string, search_str_len);
if (rc >= 0) // the first mactch will be in front or at offset_middle
{
if (offset_middle == offset_fnd_end) // search from offset_fnd_start directly
offset_fnd_end = -1;
else
offset_fnd_end = offset_middle;
}
else // the first mactch will be after offset_middle
{
offset_fnd_start = offset_middle;
search_info[nCurrentWiki].buf_len = copy_fnd_to_buf(offset_fnd_start, search_info[nCurrentWiki].buf, NUMBER_OF_FIRST_PAGE_RESULTS * sizeof(TITLE_SEARCH));
search_info[nCurrentWiki].offset_current = offset_fnd_start;
if (search_interrupted)
{
search_interrupted = 10;
goto interrupted;
}
if (search_info[nCurrentWiki].buf_len <= 0)
{
result_list->result_populated = 1;
goto out;
}
offsetNextTitleSearch = 0;
}
}
}
else
offset_fnd_end = -1;
}
}
offsetNextTitleSearch = 0;
}
else
{
result_list->result_populated = 1;
goto out;
}
}
else
{
offset_fnd_start = offset_fnd_start + offsetNextTitleSearch;
search_info[nCurrentWiki].buf_len = copy_fnd_to_buf(offset_fnd_start, search_info[nCurrentWiki].buf, NUMBER_OF_FIRST_PAGE_RESULTS * sizeof(TITLE_SEARCH));
search_info[nCurrentWiki].offset_current = offset_fnd_start;
if (search_interrupted)
{
search_interrupted = 11;
goto interrupted;
}
if (search_info[nCurrentWiki].buf_len <= 0)
{
result_list->result_populated = 1;
goto out;
}
offsetNextTitleSearch = 0;
}
}
out:
if (result_list->result_populated)
{
if (!bInit) // just completed search result
search_to_be_reloaded(SEARCH_TO_BE_RELOADED_SET, SEARCH_RELOAD_NORMAL);
return 0;
}
else
{
return 1;
}
interrupted:
return 1;
}
long get_prefix_index_table(int idx_prefix_index_table)
{
int idxBlock = idx_prefix_index_table / (SEARCH_CHR_COUNT * SEARCH_CHR_COUNT);
event_t ev;
load_prefix_index(nCurrentWiki);
if (!search_info[nCurrentWiki].b_prefix_index_block_loaded[idxBlock])
{
if (event_peek(&ev) != EVENT_NONE)
search_interrupted = 1;
file_lseek(search_info[nCurrentWiki].fd_pfx,
idxBlock * SEARCH_CHR_COUNT * SEARCH_CHR_COUNT * sizeof(uint32_t));
file_read(search_info[nCurrentWiki].fd_pfx,
&(search_info[nCurrentWiki].prefix_index_table[idxBlock * SEARCH_CHR_COUNT * SEARCH_CHR_COUNT]),
SEARCH_CHR_COUNT * SEARCH_CHR_COUNT * sizeof(uint32_t));
search_info[nCurrentWiki].b_prefix_index_block_loaded[idxBlock]++;
}
return search_info[nCurrentWiki].prefix_index_table[idx_prefix_index_table];
}
long get_search_result_start()
{
long offset_search_result_start = -1;
int idx_prefix_index_table;
char c1, c2, c3;
//int found = 0;
long offset;
switch(search_str_len)
{
case 1:
c1 = search_string[0];
c2 = '\0';
c3 = '\0';
break;
case 2:
c1 = search_string[0];
c2 = search_string[1];
c3 = '\0';
break;
default:
c1 = search_string[0];
c2 = search_string[1];
c3 = search_string[2];
break;
}
idx_prefix_index_table = bigram_char_idx(c1) * SEARCH_CHR_COUNT * SEARCH_CHR_COUNT +
bigram_char_idx(c2) * SEARCH_CHR_COUNT + bigram_char_idx(c3);
if ((offset = get_prefix_index_table(idx_prefix_index_table)))
{
//found = 1;
offset_search_result_start = offset;
}
if (search_interrupted)
{
search_interrupted = 14;
goto interrupted;
}
interrupted:
return offset_search_result_start;
}
int next_search_string(char *local_search_string, int len_local_search_string)
{
char *pSupportedChars = SUPPORTED_SEARCH_CHARS;
int idxChar;
if (len_local_search_string > MAX_SEARCH_STRING_HASHED_LEN)
len_local_search_string = MAX_SEARCH_STRING_HASHED_LEN;
if (len_local_search_string > 0)
{
idxChar = 0;
while (idxChar < ustrlen(pSupportedChars) && pSupportedChars[idxChar] != local_search_string[len_local_search_string - 1])
idxChar++;
if (idxChar >= ustrlen(pSupportedChars) - 1)
{
len_local_search_string--;
len_local_search_string = next_search_string(local_search_string, len_local_search_string);
}
else
local_search_string[len_local_search_string - 1] = pSupportedChars[idxChar + 1];
}
return len_local_search_string;
}
long get_search_result_end()
{
long offset_search_result_end = -1;
char local_search_string[MAX_TITLE_SEARCH];
int len_local_search_string;
int last_len_local_search_string;
int found = 0;
int idx_prefix_index_table;
char c1, c2, c3;
long offset;
if (search_str_len > MAX_SEARCH_STRING_ALL_HASHED_LEN)
len_local_search_string = MAX_SEARCH_STRING_ALL_HASHED_LEN;
else
len_local_search_string = search_str_len;
memcpy(local_search_string, search_string, len_local_search_string);
/* disable hashing
while (!found && len_local_search_string > 3)
{
last_len_local_search_string = len_local_search_string;
len_local_search_string = next_search_string(local_search_string, len_local_search_string);
if (!memcmp(search_string, local_search_string, len_local_search_string))
found = 1; // returns -1 directly
else
{
offset_search_result_end = get_search_offset_fnd(local_search_string, len_local_search_string);
if (search_interrupted)
{
search_interrupted = 15;
goto interrupted;
}
if (offset_search_result_end > 0)
{
found = 1;
}
}
len_local_search_string = last_len_local_search_string - 1;
}
*/
if (!found)
{
if (len_local_search_string > 3)
len_local_search_string = 3;
while (!found && len_local_search_string > 0)
{
last_len_local_search_string = len_local_search_string;
len_local_search_string = next_search_string(local_search_string, len_local_search_string);
if (!memcmp(search_string, local_search_string, len_local_search_string))
found = 1; // returns -1 directly
else
{
switch(len_local_search_string)
{
case 1:
c1 = local_search_string[0];
c2 = '\0';
c3 = '\0';
break;
case 2:
c1 = local_search_string[0];
c2 = local_search_string[1];
c3 = '\0';
break;
default:
c1 = local_search_string[0];
c2 = local_search_string[1];
c3 = local_search_string[2];
break;
}
idx_prefix_index_table = bigram_char_idx(c1) * SEARCH_CHR_COUNT * SEARCH_CHR_COUNT +
bigram_char_idx(c2) * SEARCH_CHR_COUNT + bigram_char_idx(c3);
if ((offset = get_prefix_index_table(idx_prefix_index_table)))
{
found = 1;
offset_search_result_end = offset;
}
if (search_interrupted)
{
search_interrupted = 14;
goto interrupted;
}
// if (search_info[nCurrentWiki].prefix_index_table[idx_prefix_index_table])
// {
// found = 1;
// offset_search_result_end = search_info[nCurrentWiki].prefix_index_table[idx_prefix_index_table];
// }
}
len_local_search_string = last_len_local_search_string - 1;
}
}
interrupted:
return offset_search_result_end;
}
uint32_t get_article_idx_by_title(unsigned char *titleSearch, unsigned char *titleActual)
{
uint32_t article_idx = 0;
long offset_search_result_start = -1;
long offset_search_result_end = -1;
int i = 0;
backup_search_criteria();
search_str_len = 0;
search_str_per_language_len = 0;
while (titleSearch[i] && titleSearch[i] != CHAR_LANGUAGE_LINK_TITLE_DELIMITER && search_str_len < MAX_TITLE_SEARCH - 1)
{
if (is_supported_search_char(titleSearch[i]))
{
if ('A' <= titleSearch[i] && titleSearch[i] <= 'Z')
search_string[search_str_len++] = 'a' + (titleSearch[i] - 'A');
else
search_string[search_str_len++] = titleSearch[i];
}
i++;
}
if (search_str_len > 0)
{
search_string[search_str_len] = '\0';
offset_search_result_start = get_search_result_start();
if (!search_interrupted && offset_search_result_start > 0)
{
offset_search_result_end = get_search_result_end();
if (!search_interrupted)
article_idx = get_article_idx_from_offset_range(titleActual, offset_search_result_start, offset_search_result_end);
}
}
restore_search_criteria();
return article_idx;
}
int search_populate_result()
{
int found = 0;
long offset_search_result_start = -1;
long offset_search_result_end = -1;
search_string_changed = false;
result_list->count = 0;
result_list->result_populated = 0;
result_list->cur_selected = -1;
if (search_str_len > 0)
{
offset_search_result_start = get_search_result_start();
if (search_interrupted)
{
search_interrupted = 16;
goto interrupted;
}
if (offset_search_result_start > 0)
{
found = 1;
offset_search_result_end = get_search_result_end();
if (search_interrupted)
{
search_interrupted = 17;
goto interrupted;
}
fetch_search_result(offset_search_result_start, offset_search_result_end, 1);
if (search_interrupted)
{
search_interrupted = 18;
goto interrupted;
}
}
else
{
result_list->result_populated = 1;
}
}
return found;
interrupted:
search_string_changed = true;
return found;
}
void capitalize(unsigned char *out_str, unsigned char *in_str, int len)
{
unsigned char utf8_c[5];
unsigned char capital_utf8_c[5];
if (wiki_is_korean())
{
english_to_korean(out_str, MAX_TITLE_SEARCH * 3, in_str, &len);
}
else if (wiki_keyboard_conversion_needed())
{
int i = 0;
int j = 0;
int used_len;
unsigned char *s;
while (i < len)
{
s = full_alphabet_to_half(&in_str[i], &used_len);
memcpy(&out_str[j], s, ustrlen(s));
j += ustrlen(s);
i += used_len;
}
out_str[j] = '\0';
}
else
{
memcpy(out_str, in_str, len + 1);
}
get_first_utf8_char(utf8_c, out_str, ustrlen(out_str));
utf8_char_toupper(capital_utf8_c, utf8_c);
memcpy(out_str, capital_utf8_c, ustrlen(capital_utf8_c)); // assuming the lower case and upper case charasters got the same number of bytes
}
void search_reload(int flag)
{
unsigned int screen_display_count = keyboard_get_mode() == KEYBOARD_NONE ?
NUMBER_OF_FIRST_PAGE_RESULTS : NUMBER_OF_RESULTS_KEYBOARD;
unsigned int y_pos;
int start_x_search = 0;
unsigned int end_y_pos;
unsigned char *title;
unsigned char temp_search_string[MAX_TITLE_SEARCH * 3];
static int bNoResultLastTime = 0;
int keyboard_mode = keyboard_get_mode();
const unsigned char *pMsg;
more_search_results = 0;
guilib_fb_lock();
if (keyboard_mode == KEYBOARD_NONE)
{
if (result_list->result_populated || flag == SEARCH_RELOAD_KEEP_REFRESH)
{
if (flag == SEARCH_RELOAD_KEEP_RESULT)
guilib_clear_area(0, LCD_HEIGHT - KEYBOARD_HEIGHT, 239, LCD_HEIGHT - 1);
else
guilib_clear();