-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxdict.c
executable file
·393 lines (350 loc) · 7.58 KB
/
xdict.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
/**
* @file xdict.c (dictionary query)
* @author Hightman Mar
* @editor set number ; syntax on ; set autoindent ; set tabstop=4 (vim)
* $Id$
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifdef WIN32
# include "config_win32.h"
#endif
#include "xdict.h"
#include "xtree.h"
#include "xdb.h"
#include "crc32.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef WIN32
# include <sys/param.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
/* temp file format for TEXT xdb */
#if !defined(PATH_MAX) || (PATH_MAX < 1024)
# define XDICT_PATH_MAX 1024
#else
# define XDICT_PATH_MAX PATH_MAX
#endif
#ifdef HAVE_STRTOK_R
# define _strtok_r strtok_r
#else
static char *_strtok_r(char *s, char *delim, char **lasts)
{
register char *spanp;
register int c, sc;
char *tok;
if (s == NULL && (s = *lasts) == NULL)
return NULL;
/*
* Skip (span) leading delimiters (s += strspn(s, delim), sort of).
*/
cont:
c = *s++;
for (spanp = (char *) delim; (sc = *spanp++) != 0;)
{
if (c == sc) goto cont;
}
if (c == 0)
{ /* no non-delimiter characters */
*lasts = NULL;
return NULL;
}
tok = s - 1;
/*
* Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
* Note that delim must have one NUL; we stop if we see that, too.
*/
for (;;)
{
c = *s++;
spanp = (char *) delim;
do
{
if ((sc = *spanp++) == c)
{
if (c == 0) s = NULL;
else s[-1] = '\0';
*lasts = s;
return tok;
}
}
while (sc != 0);
}
}
#endif
#ifdef WIN32
# include <direct.h>
static void _realpath(const char *src, char *dst)
{
int len = strlen(src);
if (strchr(src, ':') != NULL)
memcpy(dst, src, len + 1);
else
{
char *ptr;
getcwd(dst, XDICT_PATH_MAX - len - 2);
ptr = dst + strlen(dst);
*ptr++ = '/';
memcpy(ptr, src, len + 1);
}
}
#else
# define _realpath realpath
#endif
/* open the text dict */
static xdict_t _xdict_open_txt(const char *fpath, int mode, unsigned char *ml)
{
xdict_t xd;
xtree_t xt;
char buf[XDICT_PATH_MAX], tmpfile[XDICT_PATH_MAX];
struct stat st1, st2;
// check the input filepath
_realpath(fpath, buf);
if (stat(buf, &st1) < 0)
return NULL;
// check dest file & orginal file, compare there mtime
#ifdef WIN32
{
char *tmp_ptr;
GetTempPath(sizeof(tmpfile) - 20, tmpfile);
tmp_ptr = tmpfile + strlen(tmpfile);
if (tmp_ptr[-1] == '\\') tmp_ptr--;
sprintf(tmp_ptr, "\\scws-%08x.xdb", scws_crc32(buf));
}
#else
sprintf(tmpfile, "/tmp/scws-%08x.xdb", scws_crc32(buf));
#endif
if (!stat(tmpfile, &st2) && st2.st_mtime > st1.st_mtime)
{
xdb_t x;
if ((x = xdb_open(tmpfile, 'r')) != NULL)
{
xd = (xdict_t) malloc(sizeof(xdict_st));
memset(xd, 0, sizeof(xdict_st));
xd->ref = 1;
if (mode & SCWS_XDICT_MEM)
{
/* convert the xdb(disk) -> xtree(memory) */
if ((xt = xdb_to_xtree(x, NULL)) != NULL)
{
xdb_close(x);
xd->xdict = (void *) xt;
xd->xmode = SCWS_XDICT_MEM;
return xd;
}
}
xd->xmode = SCWS_XDICT_XDB;
xd->xdict = (void *) x;
return xd;
}
}
// create xtree
if ((xt = xtree_new(0, 0)) == NULL)
return NULL;
else
{
int cl, kl;
FILE *fp;
word_st word, *w;
char *key, *part, *last, *delim = " \t\r\n";
// re-build the xdb file from text file
if ((fp = fopen(buf, "r")) == NULL)
return NULL;
// parse every line
word.attr[2] = '\0';
while (fgets(buf, sizeof(buf) - 1, fp) != NULL)
{
// <word>[\t<tf>[\t<idf>[\t<attr>]]]
if (buf[0] == ';' || buf[0] == '#') continue;
key = _strtok_r(buf, delim, &last);
if (key == NULL) continue;
kl = strlen(key);
// init the word
do
{
word.tf = word.idf = 1.0;
word.flag = SCWS_WORD_FULL;
word.attr[0] = '@';
word.attr[1] = '\0';
if (!(part = _strtok_r(NULL, delim, &last))) break;
word.tf = (float) atof(part);
if (!(part = _strtok_r(NULL, delim, &last))) break;
word.idf = (float) atof(part);
if (part = _strtok_r(NULL, delim, &last))
{
word.attr[0] = part[0];
if (part[1]) word.attr[1] = part[1];
}
}
while (0);
// save into xtree
if ((w = xtree_nget(xt, key, kl, NULL)) == NULL)
{
w = (word_st *) pmalloc(xt->p, sizeof(word_st));
memcpy(w, &word, sizeof(word));
xtree_nput(xt, w, sizeof(word), key, kl);
}
else
{
w->tf = word.tf;
w->idf = word.idf;
w->flag |= word.flag;
strcpy(w->attr, word.attr);
}
// parse the part
cl = ml[(unsigned char) (key[0])];
while (1)
{
cl += ml[(unsigned char) (key[cl])];
if (cl >= kl) break;
if ((w = xtree_nget(xt, key, cl, NULL)) != NULL)
w->flag |= SCWS_WORD_PART;
else
{
w = (word_st *) pmalloc_z(xt->p, sizeof(word_st));
w->flag = SCWS_WORD_PART;
xtree_nput(xt, w, sizeof(word), key, cl);
}
}
}
fclose(fp);
// optimize the xtree & save to xdb
xtree_optimize(xt);
unlink(tmpfile);
xtree_to_xdb(xt, tmpfile);
chmod(tmpfile, 0777);
// return xtree
xd = (xdict_t) malloc(sizeof(xdict_st));
memset(xd, 0, sizeof(xdict_st));
xd->ref = 1;
xd->xdict = (void *) xt;
xd->xmode = SCWS_XDICT_MEM;
return xd;
}
}
/* setup & open the dict */
xdict_t xdict_open(const char *fpath, int mode)
{
xdict_t xd;
xdb_t x;
if (!(x = xdb_open(fpath, 'r')))
return NULL;
xd = (xdict_t) malloc(sizeof(xdict_st));
memset(xd, 0, sizeof(xdict_st));
xd->ref = 1;
if (mode & SCWS_XDICT_MEM)
{
xtree_t xt;
/* convert the xdb(disk) -> xtree(memory) */
if ((xt = xdb_to_xtree(x, NULL)) != NULL)
{
xdb_close(x);
xd->xdict = (void *) xt;
xd->xmode = SCWS_XDICT_MEM;
return xd;
}
}
xd->xmode = SCWS_XDICT_XDB;
xd->xdict = (void *) x;
return xd;
}
/* add a dict */
xdict_t xdict_add(xdict_t xd, const char *fpath, int mode, unsigned char *ml)
{
xdict_t xx;
xx = (mode & SCWS_XDICT_TXT ? _xdict_open_txt(fpath, mode, ml) : xdict_open(fpath, mode));
if (xx != NULL)
{
xx->next = xd;
return xx;
}
return xd;
}
/* fork the dict */
xdict_t xdict_fork(xdict_t xd)
{
xdict_t xx;
for (xx = xd; xx != NULL; xx = xx->next)
{
xx->ref++;
}
return xd;
}
/* close the dict */
void xdict_close(xdict_t xd)
{
xdict_t xx;
while ((xx = xd) != NULL)
{
xd = xx->next;
xx->ref--;
if (xx->ref == 0)
{
if (xx->xmode == SCWS_XDICT_MEM)
xtree_free((xtree_t) xx->xdict);
else
{
xdb_close((xdb_t) xx->xdict);
}
free(xx);
}
}
}
/* query the word */
#define _FLAG_BOTH(x) (((x)->flag & (SCWS_WORD_PART|SCWS_WORD_FULL)) == (SCWS_WORD_PART|SCWS_WORD_FULL))
#define _FLAG_FULL(x) ((x)->flag & SCWS_WORD_FULL)
#define _FLAG_PART(x) ((x)->flag & SCWS_WORD_PART)
#define _FLAG_MALLOC(x) ((x)->flag & SCWS_WORD_MALLOCED)
word_t xdict_query(xdict_t xd, const char *key, int len)
{
word_t value, value2;
value = value2 = NULL;
while (xd != NULL)
{
if (xd->xmode == SCWS_XDICT_MEM)
{
/* this is ThreadSafe, recommend. */
value = (word_t) xtree_nget((xtree_t) xd->xdict, key, len, NULL);
}
else
{
/* the value malloced in lib-XDB. free required */
value = (word_t) xdb_nget((xdb_t) xd->xdict, key, len, NULL);
if (value != NULL) value->flag |= SCWS_WORD_MALLOCED;
}
xd = xd->next;
// check value2
if (value != NULL)
{
if (value2 == NULL)
{
if (_FLAG_BOTH(value))
return value;
value2 = value;
}
else
{
if (_FLAG_FULL(value2) && _FLAG_PART(value))
{
value2->flag |= SCWS_WORD_PART;
if (_FLAG_MALLOC(value))
free(value);
return value2;
}
if (_FLAG_FULL(value) && _FLAG_PART(value2))
{
value->flag |= SCWS_WORD_PART;
if (_FLAG_MALLOC(value2))
free(value2);
return value;
}
if (_FLAG_MALLOC(value))
free(value);
}
}
}
return value2;
}