/
bigram.c
137 lines (121 loc) · 2.63 KB
/
bigram.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
/*
* Copyright (c) 2009 Openmoko Inc.
*
* 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 <stdlib.h>
#include <grifo.h>
#include "ustring.h"
#include "bigram.h"
#include "lcd_buf_draw.h"
#include "wiki_info.h"
typedef struct _bitram {
unsigned char char_pairs[128][2];
} BIGRAM, *PBIGRAM;
PBIGRAM aBigram = NULL;
int32_t aCharIdx[128];
void init_bigram(int fd)
{
if (!aBigram)
{
aBigram = (PBIGRAM)memory_allocate(sizeof(BIGRAM) * get_wiki_count(), "bigram");
}
init_char_idx();
file_read(fd, &aBigram[nCurrentWiki], sizeof(BIGRAM));
}
void init_char_idx()
{
char c;
int i;
int idx = 1;
static int inited = 0;
if (inited)
return;
inited = 1;
memset(aCharIdx, 0, sizeof(aCharIdx));
for (i = 0; i < 128; i++)
{
c = (char)i;
if (is_supported_search_char(c))
{
if ('a' <= c && c <= 'z')
aCharIdx[i] = aCharIdx[(int)'A' + (c - 'a')];
else
aCharIdx[i] = idx++;
}
}
}
int bigram_char_idx(unsigned char c)
{
return aCharIdx[(int)c];
}
void bigram_decode(unsigned char *outStr, const unsigned char *inStr, int lenMax)
{
unsigned char c;
while (lenMax > 1 && (c = *inStr++) != '\0')
{
if (c >= 128 && aBigram[nCurrentWiki].char_pairs[c-128][0])
{
*outStr = aBigram[nCurrentWiki].char_pairs[c-128][0];
outStr++;
lenMax--;
if (lenMax > 1)
{
*outStr = aBigram[nCurrentWiki].char_pairs[c-128][1];
outStr++;
lenMax--;
}
}
else
{
*outStr = c;
outStr++;
lenMax--;
}
}
*outStr = '\0';
}
int is_supported_search_char(unsigned char c)
{
return c && (strchr(SUPPORTED_SEARCH_CHARS, c) || ('A' <= c && c <= 'Z'));
}
int search_string_cmp(const unsigned char *title, const unsigned char *search, int len) // assuming search consists of lowercase only
{
int rc = 0;
unsigned char c = 0;
while (!rc && len > 0)
{
c = *title;
if (c && !is_supported_search_char(c))
{
title++;
}
else
{
if ('A' <= c && c <= 'Z')
c += 32;
if (c == *search)
{
title++;
search++;
len--;
}
else if (c > *search)
rc = 1;
else
rc = -1;
}
}
return rc;
}