14
|
1 |
#include <stdio.h>
|
|
2 |
#include <sys/stat.h>
|
|
3 |
#include <sys/types.h>
|
|
4 |
#include <sys/mman.h>
|
|
5 |
#include <fcntl.h>
|
|
6 |
#include "dictre.h"
|
|
7 |
|
17
|
8 |
const static char indexext[] = ".index";
|
|
9 |
const static char dictext[] = ".dict";
|
14
|
10 |
|
|
11 |
int get_filesize(const char *fname)
|
|
12 |
{
|
|
13 |
struct stat st;
|
|
14 |
int res;
|
|
15 |
res = stat(fname, &st);
|
|
16 |
if (res == -1)
|
|
17 |
{
|
|
18 |
fprintf(stderr, "Problem stating the file %s\n", fname);
|
|
19 |
perror("Error:");
|
|
20 |
exit(-1);
|
|
21 |
}
|
|
22 |
|
|
23 |
return st.st_size;
|
|
24 |
}
|
|
25 |
|
17
|
26 |
void init_dictionary(struct Dict *d, const char *base)
|
14
|
27 |
{
|
17
|
28 |
char *filename;
|
|
29 |
|
|
30 |
filename = (char *) malloc(strlen(base) + 10);
|
|
31 |
|
|
32 |
/* Prepare .index filename and open it*/
|
|
33 |
strcpy(filename, base);
|
|
34 |
strcat(filename, indexext);
|
|
35 |
|
|
36 |
d->indexsize = get_filesize(filename);
|
|
37 |
d->indexfd = open(filename, O_RDONLY);
|
|
38 |
if (d->indexfd == -1)
|
14
|
39 |
{
|
17
|
40 |
fprintf(stderr, "Problem opening the file %s\n", filename);
|
14
|
41 |
perror("Error:");
|
|
42 |
exit(-1);
|
|
43 |
}
|
17
|
44 |
d->index = (unsigned char *) mmap(0, d->indexsize, PROT_READ, MAP_SHARED,
|
|
45 |
d->indexfd, 0);
|
14
|
46 |
|
17
|
47 |
/* Prepare .dict filename and open it*/
|
|
48 |
strcpy(filename, base);
|
|
49 |
strcat(filename, dictext);
|
|
50 |
d->defs = fopen(filename, "r");
|
|
51 |
if (d->defs == 0)
|
14
|
52 |
{
|
17
|
53 |
fprintf(stderr, "Problem opening the file %s\n", filename);
|
14
|
54 |
perror("Error:");
|
|
55 |
exit(-1);
|
|
56 |
}
|
|
57 |
|
21
|
58 |
d->trim_first_line = 0;
|
|
59 |
d->trim_last_newlines = 0;
|
|
60 |
|
17
|
61 |
free(filename);
|
14
|
62 |
}
|
|
63 |
|
17
|
64 |
void end_dictionary(struct Dict *d)
|
14
|
65 |
{
|
17
|
66 |
munmap(d->index, d->indexsize);
|
|
67 |
close(d->indexfd);
|
|
68 |
fclose(d->defs);
|
14
|
69 |
}
|
|
70 |
|
21
|
71 |
static int trim_first_line(char *def, int len)
|
|
72 |
{
|
|
73 |
int new_line_pos;
|
|
74 |
int i,j;
|
|
75 |
for(i=0; i < len; ++i)
|
|
76 |
{
|
|
77 |
if (def[i] == '\n')
|
|
78 |
{
|
|
79 |
/* Break */
|
|
80 |
memmove(def, def + i + 1 /* \n */,
|
|
81 |
len - i - 1);
|
|
82 |
def[len-i-1] = 0;
|
22
|
83 |
return len-i-2/*\n*/+1/*\0*/;
|
21
|
84 |
}
|
|
85 |
}
|
|
86 |
return len;
|
|
87 |
}
|
|
88 |
|
|
89 |
static int trim_last_newlines(char *def, int len)
|
|
90 |
{
|
|
91 |
int new_line_pos;
|
|
92 |
int i,j;
|
22
|
93 |
if (len < 2)
|
|
94 |
return len;
|
|
95 |
|
|
96 |
for(i=len-2; i >= 0; --i)
|
21
|
97 |
{
|
|
98 |
if (def[i] != '\n' && def[i] != '\r')
|
|
99 |
{
|
|
100 |
def[i+1] = '\0';
|
|
101 |
return i + 1;
|
|
102 |
}
|
|
103 |
}
|
|
104 |
return len;
|
|
105 |
}
|
|
106 |
|
17
|
107 |
static void fill_def(struct Dict *d, int offset, int length, char * def)
|
14
|
108 |
{
|
17
|
109 |
fseek(d->defs, offset, SEEK_SET);
|
|
110 |
fread(def, 1, length, d->defs);
|
21
|
111 |
def[length] = 0;
|
|
112 |
if (d->trim_first_line)
|
|
113 |
{
|
|
114 |
length = trim_first_line(def, length + 1/*\0*/) - 1 /*\0*/;
|
|
115 |
}
|
|
116 |
if (d->trim_last_newlines)
|
|
117 |
{
|
|
118 |
length = trim_last_newlines(def, length+1) - 1; /* math as above*/
|
|
119 |
}
|
17
|
120 |
}
|
|
121 |
|
21
|
122 |
static int pointer_at_end(struct Dict *d, const unsigned char *ptr)
|
17
|
123 |
{
|
|
124 |
if (ptr >= (d->index + d->indexsize))
|
14
|
125 |
return 1;
|
|
126 |
return 0;
|
|
127 |
}
|
|
128 |
|
21
|
129 |
static const char * skip_until_newline(struct Dict *d, const char *from)
|
14
|
130 |
{
|
17
|
131 |
if (pointer_at_end(d, from))
|
14
|
132 |
return 0;
|
|
133 |
while(*from != '\n' && *from != 0)
|
|
134 |
{
|
|
135 |
++from;
|
17
|
136 |
if(pointer_at_end(d, from))
|
14
|
137 |
return 0;
|
|
138 |
}
|
|
139 |
return from;
|
|
140 |
}
|
|
141 |
|
|
142 |
static int compare(const unsigned char *word, const unsigned char *test)
|
|
143 |
{
|
|
144 |
int i;
|
|
145 |
|
|
146 |
/*printf("Comparing %s to %.20s\n", word, test);*/
|
|
147 |
for(i=0; word[i] != 0 && test[i] != 0; ++i)
|
|
148 |
{
|
|
149 |
if (word[i] != test[i])
|
|
150 |
{
|
|
151 |
break;
|
|
152 |
}
|
|
153 |
}
|
|
154 |
if (word[i] == 0 && test[i] == '\t')
|
|
155 |
return 0;
|
|
156 |
else if (word[i] == 0)
|
|
157 |
return -1;
|
|
158 |
else if (test[i] == '\t')
|
|
159 |
return 1;
|
|
160 |
else if (word[i] > test[i])
|
|
161 |
return 1;
|
|
162 |
else if (word[i] < test[i])
|
|
163 |
return -1;
|
|
164 |
|
|
165 |
/* It should never reach this. */
|
|
166 |
return -1;
|
|
167 |
}
|
|
168 |
|
21
|
169 |
static const char * search_next(struct Dict *d, const char *word, const char *from)
|
|
170 |
{
|
|
171 |
const char *ret;
|
|
172 |
ret = skip_until_newline(d, from);
|
|
173 |
if (compare(from, word) == 0)
|
|
174 |
return ret;
|
|
175 |
return 0;
|
|
176 |
}
|
|
177 |
|
|
178 |
static const char * bin_search(struct Dict *d, const char *word)
|
14
|
179 |
{
|
|
180 |
int step, pivot;
|
21
|
181 |
const char *ret;
|
|
182 |
const char *test;
|
|
183 |
int comparision;
|
|
184 |
int found_once = 0;
|
14
|
185 |
|
17
|
186 |
pivot = d->indexsize / 2;
|
|
187 |
step = d->indexsize / 2;
|
14
|
188 |
|
|
189 |
do
|
|
190 |
{
|
17
|
191 |
test = d->index + pivot;
|
|
192 |
test = skip_until_newline(d, test);
|
14
|
193 |
if (test == 0)
|
|
194 |
return 0;
|
|
195 |
test += 1; /* skip exactly the new line */
|
|
196 |
|
|
197 |
comparision = compare(word, test);
|
21
|
198 |
if (comparision <= 0)
|
14
|
199 |
{
|
21
|
200 |
if (comparision == 0)
|
|
201 |
found_once = 1;
|
|
202 |
/* If == 0, we don't know that it's the FIRST
|
|
203 |
* match possible in the dictionary.
|
|
204 |
* We want all possible matches. */
|
14
|
205 |
step = step / 2;
|
|
206 |
pivot = pivot - step;
|
|
207 |
} else if (comparision > 0)
|
|
208 |
{
|
|
209 |
step = step / 2;
|
|
210 |
pivot = pivot + step;
|
|
211 |
}
|
|
212 |
} while(step > 0);
|
21
|
213 |
|
|
214 |
if (!found_once)
|
|
215 |
return 0;
|
|
216 |
|
|
217 |
if (comparision == 0) /* last comparision */
|
|
218 |
{
|
|
219 |
ret = skip_until_newline(d, d->index + pivot) + 1;
|
|
220 |
} else
|
|
221 |
{
|
|
222 |
ret = skip_until_newline(d, test) + 1;
|
|
223 |
}
|
|
224 |
return ret;
|
14
|
225 |
}
|
|
226 |
|
21
|
227 |
static int my_get_int(const char **pos)
|
14
|
228 |
{
|
|
229 |
int i;
|
21
|
230 |
const char *start;
|
14
|
231 |
int val;
|
|
232 |
|
|
233 |
start = *pos;
|
|
234 |
for(i=0; start[i] != '\t' && start[i] != '\n'; ++i)
|
|
235 |
;
|
|
236 |
val = str2int_len(start, i);
|
|
237 |
*pos += i + 1;
|
|
238 |
return val;
|
|
239 |
}
|
|
240 |
|
17
|
241 |
void find_def(struct Dict *d, const char *word, char * def)
|
14
|
242 |
{
|
|
243 |
int offset, len;
|
21
|
244 |
const char *found, *pos;
|
|
245 |
int wordlen;
|
14
|
246 |
|
21
|
247 |
def[0] = 0;
|
|
248 |
/* we will get a pointer to the offset for the ints*/
|
|
249 |
found = bin_search(d, word);
|
|
250 |
if (found == 0)
|
|
251 |
return;
|
|
252 |
wordlen = strlen(word);
|
|
253 |
do
|
14
|
254 |
{
|
21
|
255 |
found += wordlen+1;
|
|
256 |
pos = found;
|
|
257 |
offset = my_get_int(&pos); /* increments pos */
|
|
258 |
len = my_get_int(&pos); /* increments pos */
|
|
259 |
fill_def(d, offset, len, def);
|
|
260 |
found = search_next(d, word, found);
|
|
261 |
if (!found)
|
|
262 |
break;
|
|
263 |
strcat(def, ", ");
|
|
264 |
def += strlen(def);
|
|
265 |
} while(1);
|
14
|
266 |
}
|