/work/workdir/UnpackedTarball/mythes/mythes.cxx
Line | Count | Source |
1 | | #include "COPYING" |
2 | | #include <stdio.h> |
3 | | #include <string.h> |
4 | | #include <stdlib.h> |
5 | | #include <errno.h> |
6 | | #include <limits> |
7 | | #include <vector> |
8 | | |
9 | | #include "mythes.hxx" |
10 | | |
11 | | #ifdef _WIN32 |
12 | | #include <windows.h> |
13 | | #include <wchar.h> |
14 | | #endif |
15 | | |
16 | | MyThes::MyThes(const char* idxpath, const char * datpath) |
17 | 0 | { |
18 | 0 | nw = 0; |
19 | 0 | encoding = NULL; |
20 | 0 | list = NULL; |
21 | 0 | offst = NULL; |
22 | 0 | pdfile = NULL; |
23 | |
|
24 | 0 | if (thInitialize(idxpath, datpath) != 1) { |
25 | 0 | fprintf(stderr,"Error - can't open %s or %s\n",idxpath, datpath); |
26 | 0 | fflush(stderr); |
27 | 0 | thCleanup(); |
28 | | // did not initialize properly - throw exception? |
29 | 0 | } |
30 | 0 | } |
31 | | |
32 | | |
33 | | MyThes::~MyThes() |
34 | 0 | { |
35 | 0 | thCleanup(); |
36 | 0 | } |
37 | | |
38 | | |
39 | | int MyThes::thInitialize(const char* idxpath, const char* datpath) |
40 | 0 | { |
41 | | |
42 | | // open the index file |
43 | 0 | FILE * pifile = myfopen(idxpath,"r"); |
44 | 0 | if (!pifile) { |
45 | 0 | return 0; |
46 | 0 | } |
47 | | |
48 | | // parse in encoding and index size |
49 | 0 | std::vector<char> buffer(MAX_WD_LEN); |
50 | 0 | char * wrd = &buffer[0]; |
51 | 0 | readLine(pifile,wrd,MAX_WD_LEN); |
52 | 0 | encoding = mystrdup(wrd); |
53 | 0 | readLine(pifile,wrd,MAX_WD_LEN); |
54 | 0 | errno = 0; |
55 | 0 | char *endptr = NULL; |
56 | 0 | long idxsz_l = strtol(wrd, &endptr, 10); |
57 | 0 | if (errno != 0 || endptr == wrd || idxsz_l <= 0 || |
58 | 0 | idxsz_l > std::numeric_limits<int>::max() / static_cast<long>(sizeof(char*))) { |
59 | 0 | fprintf(stderr,"Error - bad index %ld\n", idxsz_l); |
60 | 0 | fclose(pifile); |
61 | 0 | return 0; |
62 | 0 | } |
63 | 0 | int idxsz = static_cast<int>(idxsz_l); |
64 | | |
65 | | // now allocate list, offst for the given size |
66 | 0 | list = (char**) calloc(idxsz,sizeof(char*)); |
67 | 0 | offst = (unsigned int*) calloc(idxsz,sizeof(unsigned int)); |
68 | |
|
69 | 0 | if ( (!(list)) || (!(offst)) ) { |
70 | 0 | fprintf(stderr,"Error - bad memory allocation\n"); |
71 | 0 | fclose(pifile); |
72 | 0 | return 0; |
73 | 0 | } |
74 | | |
75 | | // now parse the remaining lines of the index |
76 | 0 | int len = readLine(pifile,wrd,MAX_WD_LEN); |
77 | 0 | while (len > 0) |
78 | 0 | { |
79 | 0 | int np = mystr_indexOfChar(wrd,'|'); |
80 | 0 | if (nw < idxsz) { |
81 | 0 | if (np >= 0) { |
82 | 0 | *(wrd+np) = '\0'; |
83 | 0 | list[nw] = (char *)calloc(1,(np+1)); |
84 | 0 | if (!list[nw]) { |
85 | 0 | fprintf(stderr,"Error - bad memory allocation\n"); |
86 | 0 | fflush(stderr); |
87 | 0 | fclose(pifile); |
88 | 0 | return 0; |
89 | 0 | } |
90 | 0 | memcpy((list[nw]),wrd,np); |
91 | 0 | errno = 0; |
92 | 0 | char *off_end = NULL; |
93 | 0 | unsigned long off = strtoul(wrd+np+1, &off_end, 10); |
94 | 0 | if (errno != 0 || off > std::numeric_limits<unsigned int>::max()) |
95 | 0 | off = 0; |
96 | 0 | offst[nw] = static_cast<unsigned int>(off); |
97 | 0 | nw++; |
98 | 0 | } |
99 | 0 | } |
100 | 0 | len = readLine(pifile,wrd,MAX_WD_LEN); |
101 | 0 | } |
102 | | |
103 | 0 | fclose(pifile); |
104 | | |
105 | | /* next open the data file */ |
106 | 0 | pdfile = myfopen(datpath,"r"); |
107 | 0 | if (!pdfile) { |
108 | 0 | return 0; |
109 | 0 | } |
110 | | |
111 | 0 | return 1; |
112 | 0 | } |
113 | | |
114 | | |
115 | | void MyThes::thCleanup() |
116 | 0 | { |
117 | | /* first close the data file */ |
118 | 0 | if (pdfile) { |
119 | 0 | fclose(pdfile); |
120 | 0 | pdfile=NULL; |
121 | 0 | } |
122 | |
|
123 | 0 | if (list) |
124 | 0 | { |
125 | | /* now free up all the allocated strings on the list */ |
126 | 0 | for (int i=0; i < nw; i++) |
127 | 0 | { |
128 | 0 | if (list[i]) { |
129 | 0 | free(list[i]); |
130 | 0 | list[i] = 0; |
131 | 0 | } |
132 | 0 | } |
133 | 0 | free((void*)list); |
134 | 0 | } |
135 | |
|
136 | 0 | if (encoding) free((void*)encoding); |
137 | 0 | if (offst) free((void*)offst); |
138 | |
|
139 | 0 | encoding = NULL; |
140 | 0 | list = NULL; |
141 | 0 | offst = NULL; |
142 | 0 | nw = 0; |
143 | 0 | } |
144 | | |
145 | | |
146 | | |
147 | | // lookup text in index and count of meanings and a list of meaning entries |
148 | | // with each entry having a synonym count and pointer to an |
149 | | // array of char * (i.e the synonyms) |
150 | | // |
151 | | // note: calling routine should call CleanUpAfterLookup with the original |
152 | | // meaning point and count to properly deallocate memory |
153 | | |
154 | | int MyThes::Lookup(const char * pText, int len, mentry** pme) |
155 | 0 | { |
156 | |
|
157 | 0 | *pme = NULL; |
158 | | |
159 | | // handle the case of missing file or file related errors |
160 | 0 | if (! pdfile) return 0; |
161 | | |
162 | | // check we can allocate enough memory for the string |
163 | 0 | std::vector<char> buffer; |
164 | 0 | if (len < 1 || len > buffer.max_size() - 1) { |
165 | 0 | return 0; |
166 | 0 | } |
167 | | |
168 | | /* copy search word and make sure null terminated */ |
169 | 0 | buffer.resize(len+1); |
170 | 0 | char * wrd = &buffer[0]; |
171 | 0 | memcpy(wrd,pText,len); |
172 | | |
173 | | /* find it in the list */ |
174 | 0 | int idx = nw > 0 ? binsearch(wrd,list,nw) : -1; |
175 | 0 | if (idx < 0) return 0; |
176 | | |
177 | | // now seek to the offset |
178 | 0 | long offset = (long) offst[idx]; |
179 | 0 | int rc = fseek(pdfile,offset,SEEK_SET); |
180 | 0 | if (rc) { |
181 | 0 | return 0; |
182 | 0 | } |
183 | | |
184 | | // grab the count of the number of meanings |
185 | | // and allocate a list of meaning entries |
186 | 0 | char * buf = (char *) malloc(MAX_LN_LEN); |
187 | 0 | if (!buf) return 0; |
188 | 0 | if (readLine(pdfile, buf, (MAX_LN_LEN-1)) < 0) { |
189 | 0 | free(buf); |
190 | 0 | return 0; |
191 | 0 | } |
192 | 0 | int np = mystr_indexOfChar(buf,'|'); |
193 | 0 | if (np < 0) { |
194 | 0 | free(buf); |
195 | 0 | return 0; |
196 | 0 | } |
197 | 0 | errno = 0; |
198 | 0 | char *nm_end = NULL; |
199 | 0 | long nm = strtol(buf+np+1, &nm_end, 10); |
200 | 0 | int nmeanings = (errno != 0 || nm < 0 || nm > MAX_MEANINGS) ? 0 : static_cast<int>(nm); |
201 | 0 | *pme = (mentry*)(nmeanings ? malloc(nmeanings * sizeof(mentry)) : NULL); |
202 | 0 | if (!(*pme)) { |
203 | 0 | free(buf); |
204 | 0 | return 0; |
205 | 0 | } |
206 | | |
207 | | // now read in each meaning and parse it to get defn, count and synonym lists |
208 | 0 | mentry* pm = *(pme); |
209 | 0 | char dfn[MAX_WD_LEN]; |
210 | |
|
211 | 0 | for (int j = 0; j < nmeanings; j++) { |
212 | 0 | if (readLine(pdfile, buf, (MAX_LN_LEN-1)) < 0) { |
213 | | // .dat ended before nmeanings lines were read; return what we got |
214 | 0 | nmeanings = j; |
215 | 0 | break; |
216 | 0 | } |
217 | | |
218 | 0 | pm->count = 0; |
219 | 0 | pm->psyns = NULL; |
220 | 0 | pm->defn = NULL; |
221 | | |
222 | | // store away the part of speech for later use |
223 | 0 | char * p = buf; |
224 | 0 | char * pos = NULL; |
225 | 0 | np = mystr_indexOfChar(p,'|'); |
226 | 0 | if (np >= 0) { |
227 | 0 | *(buf+np) = '\0'; |
228 | 0 | pos = mystrdup(p); |
229 | 0 | p = p + np + 1; |
230 | 0 | } else { |
231 | 0 | pos = mystrdup(""); |
232 | 0 | } |
233 | | |
234 | | // count the number of fields in the remaining line |
235 | 0 | int nf = 1; |
236 | 0 | char * d = p; |
237 | 0 | np = mystr_indexOfChar(d,'|'); |
238 | 0 | while ( np >= 0 ) { |
239 | 0 | nf++; |
240 | 0 | d = d + np + 1; |
241 | 0 | np = mystr_indexOfChar(d,'|'); |
242 | 0 | } |
243 | 0 | pm->count = nf; |
244 | 0 | pm->psyns = (char **) malloc(nf*sizeof(char*)); |
245 | | |
246 | | // fill in the synonym list |
247 | 0 | d = p; |
248 | 0 | for (int jj = 0; jj < nf; jj++) |
249 | 0 | { |
250 | 0 | np = mystr_indexOfChar(d,'|'); |
251 | 0 | if (np > 0) |
252 | 0 | { |
253 | 0 | *(d+np) = '\0'; |
254 | 0 | pm->psyns[jj] = mystrdup(d); |
255 | 0 | d = d + np + 1; |
256 | 0 | } |
257 | 0 | else |
258 | 0 | { |
259 | 0 | pm->psyns[jj] = mystrdup(d); |
260 | 0 | } |
261 | 0 | } |
262 | | |
263 | | // add pos to first synonym to create the definition |
264 | 0 | if (pm->psyns[0]) |
265 | 0 | { |
266 | 0 | int k = strlen(pos); |
267 | 0 | int m = strlen(pm->psyns[0]); |
268 | 0 | if ((k+m) < (MAX_WD_LEN - 1)) { |
269 | 0 | memcpy(dfn,pos,k); |
270 | 0 | *(dfn+k) = ' '; |
271 | 0 | memcpy((dfn+k+1),(pm->psyns[0]),m+1); |
272 | 0 | pm->defn = mystrdup(dfn); |
273 | 0 | } else { |
274 | 0 | pm->defn = mystrdup(pm->psyns[0]); |
275 | 0 | } |
276 | 0 | } |
277 | 0 | free(pos); |
278 | 0 | pm++; |
279 | |
|
280 | 0 | } |
281 | 0 | free(buf); |
282 | |
|
283 | 0 | if (nmeanings == 0) { |
284 | 0 | free(*pme); |
285 | 0 | *pme = NULL; |
286 | 0 | } |
287 | |
|
288 | 0 | return nmeanings; |
289 | 0 | } |
290 | | |
291 | | |
292 | | |
293 | | void MyThes::CleanUpAfterLookup(mentry ** pme, int nmeanings) |
294 | 0 | { |
295 | |
|
296 | 0 | if (nmeanings == 0) return; |
297 | 0 | if ((*pme) == NULL) return; |
298 | | |
299 | 0 | mentry * pm = *pme; |
300 | | |
301 | 0 | for (int i = 0; i < nmeanings; i++) { |
302 | 0 | int count = pm->count; |
303 | 0 | for (int j = 0; j < count; j++) { |
304 | 0 | if (pm->psyns[j]) free(pm->psyns[j]); |
305 | 0 | pm->psyns[j] = NULL; |
306 | 0 | } |
307 | 0 | if (pm->psyns) free(pm->psyns); |
308 | 0 | pm->psyns = NULL; |
309 | 0 | if (pm->defn) free(pm->defn); |
310 | 0 | pm->defn = NULL; |
311 | 0 | pm->count = 0; |
312 | 0 | pm++; |
313 | 0 | } |
314 | 0 | pm = *pme; |
315 | 0 | free(pm); |
316 | 0 | *pme = NULL; |
317 | 0 | return; |
318 | 0 | } |
319 | | |
320 | | |
321 | | // read a line of text from a text file stripping |
322 | | // off the line terminator and replacing it with |
323 | | // a null string terminator. |
324 | | // returns: -1 on error or the number of characters in |
325 | | // in the returning string |
326 | | |
327 | | // A maximum of nc characters will be returned |
328 | | |
329 | | int MyThes::readLine(FILE * pf, char * buf, int nc) |
330 | 0 | { |
331 | | |
332 | 0 | if (fgets(buf,nc,pf)) { |
333 | 0 | mychomp(buf); |
334 | 0 | return strlen(buf); |
335 | 0 | } |
336 | 0 | return -1; |
337 | 0 | } |
338 | | |
339 | | |
340 | | |
341 | | // performs a binary search on null terminated character |
342 | | // strings |
343 | | // |
344 | | // returns: -1 on not found |
345 | | // index of wrd in the list[] |
346 | | |
347 | | int MyThes::binsearch(char * sw, char* _list[], int nlst) |
348 | 0 | { |
349 | 0 | int lp, up, mp, j, indx; |
350 | 0 | lp = 0; |
351 | 0 | up = nlst-1; |
352 | 0 | indx = -1; |
353 | 0 | if (strcmp(sw,_list[lp]) < 0) return -1; |
354 | 0 | if (strcmp(sw,_list[up]) > 0) return -1; |
355 | 0 | while (indx < 0 ) { |
356 | 0 | mp = (int)((lp+up) >> 1); |
357 | 0 | j = strcmp(sw,_list[mp]); |
358 | 0 | if ( j > 0) { |
359 | 0 | lp = mp + 1; |
360 | 0 | } else if (j < 0 ) { |
361 | 0 | up = mp - 1; |
362 | 0 | } else { |
363 | 0 | indx = mp; |
364 | 0 | } |
365 | 0 | if (lp > up) return -1; |
366 | 0 | } |
367 | 0 | return indx; |
368 | 0 | } |
369 | | |
370 | | char * MyThes::get_th_encoding() |
371 | 0 | { |
372 | 0 | return encoding; |
373 | 0 | } |
374 | | |
375 | | |
376 | | // string duplication routine |
377 | | char * MyThes::mystrdup(const char * s) |
378 | 0 | { |
379 | 0 | char * d = NULL; |
380 | 0 | if (s) { |
381 | 0 | int sl = strlen(s)+1; |
382 | 0 | d = (char *) malloc(sl); |
383 | 0 | if (d) memcpy(d,s,sl); |
384 | 0 | } |
385 | 0 | return d; |
386 | 0 | } |
387 | | |
388 | | // remove cross-platform text line end characters |
389 | | void MyThes::mychomp(char * s) |
390 | 0 | { |
391 | 0 | int k = strlen(s); |
392 | 0 | if ((k > 0) && ((*(s+k-1)=='\r') || (*(s+k-1)=='\n'))) *(s+k-1) = '\0'; |
393 | 0 | if ((k > 1) && (*(s+k-2) == '\r')) *(s+k-2) = '\0'; |
394 | 0 | } |
395 | | |
396 | | |
397 | | // return index of char in string |
398 | | int MyThes::mystr_indexOfChar(const char * d, int c) |
399 | 0 | { |
400 | 0 | char * p = strchr((char *)d,c); |
401 | 0 | if (p) return (int)(p-d); |
402 | 0 | return -1; |
403 | 0 | } |
404 | | |
405 | 0 | FILE * MyThes::myfopen(const char * path, const char * mode) { |
406 | | #ifdef _WIN32 |
407 | | #define WIN32_LONG_PATH_PREFIX "\\\\?\\" |
408 | | if (strncmp(path, WIN32_LONG_PATH_PREFIX, 4) == 0) { |
409 | | int len = MultiByteToWideChar(CP_UTF8, 0, path, -1, NULL, 0); |
410 | | wchar_t *buff = (wchar_t *) malloc(len * sizeof(wchar_t)); |
411 | | wchar_t *buff2 = (wchar_t *) malloc(len * sizeof(wchar_t)); |
412 | | FILE * f = NULL; |
413 | | MultiByteToWideChar(CP_UTF8, 0, path, -1, buff, len); |
414 | | if (_wfullpath( buff2, buff, len ) != NULL) { |
415 | | f = _wfopen(buff2, (strcmp(mode, "r") == 0) ? L"r" : L"rb"); |
416 | | } |
417 | | free(buff); |
418 | | free(buff2); |
419 | | return f; |
420 | | } |
421 | | #endif |
422 | 0 | return fopen(path, mode); |
423 | 0 | } |