2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.24 1999-05-26 07:49:12 adam
10 * Revision 1.23 1999/05/15 14:36:37 adam
11 * Updated dictionary. Implemented "compression" of dictionary.
13 * Revision 1.22 1999/02/02 14:50:23 adam
14 * Updated WIN32 code specific sections. Changed header.
16 * Revision 1.21 1998/06/24 12:16:12 adam
17 * Support for relations on text operands. Open range support in
18 * DFA module (i.e. [-j], [g-]).
20 * Revision 1.20 1997/10/27 14:33:03 adam
21 * Moved towards generic character mapping depending on "structure"
22 * field in abstract syntax file. Fixed a few memory leaks. Fixed
23 * bug with negative integers when doing searches with relational
26 * Revision 1.19 1997/09/18 08:59:18 adam
27 * Extra generic handle for the character mapping routines.
29 * Revision 1.18 1997/09/05 15:29:58 adam
30 * Changed prototype for chr_map_input - added const.
31 * Added support for C++, headers uses extern "C" for public definitions.
33 * Revision 1.17 1996/06/04 10:20:06 adam
34 * Added support for character mapping.
36 * Revision 1.16 1996/05/24 14:46:04 adam
37 * Added dict_grep_cmap function to define user-mapping in grep lookups.
39 * Revision 1.15 1996/03/20 09:35:18 adam
40 * Function dict_lookup_grep got extra parameter, init_pos, which marks
41 * from which position in pattern approximate pattern matching should occur.
43 * Revision 1.14 1996/02/02 13:43:51 adam
44 * The public functions simply use char instead of Dict_char to represent
45 * search strings. Dict_char is used internally only.
47 * Revision 1.13 1996/01/08 09:09:30 adam
48 * Function dfa_parse got 'const' string argument.
50 * Revision 1.12 1995/12/11 09:04:48 adam
51 * Bug fix: the lookup/scan/lookgrep didn't handle empty dictionary.
53 * Revision 1.11 1995/12/06 14:43:02 adam
54 * New function: dict_delete.
56 * Revision 1.10 1995/11/16 17:00:44 adam
59 * Revision 1.9 1995/10/27 13:58:09 adam
60 * Makes 'Database unavailable' diagnostic.
62 * Revision 1.8 1995/10/19 14:57:21 adam
63 * New feature: grep lookup saves length of longest prefix match.
65 * Revision 1.7 1995/10/17 18:01:22 adam
66 * Userfunc may return non-zero in which case the the grepping stops
69 * Revision 1.6 1995/10/09 16:18:32 adam
70 * Function dict_lookup_grep got extra client data parameter.
72 * Revision 1.5 1995/09/14 11:52:59 adam
73 * Grep handle function parameter info is const now.
75 * Revision 1.4 1995/01/24 16:01:02 adam
76 * Added -ansi to CFLAGS.
77 * Use new API of dfa module.
79 * Revision 1.3 1994/10/05 12:16:50 adam
80 * Pagesize is a resource now.
82 * Revision 1.2 1994/10/04 12:08:07 adam
83 * Some bug fixes and some optimizations.
85 * Revision 1.1 1994/10/03 17:23:04 adam
86 * First version of dictionary lookup with regular expressions and errors.
98 typedef unsigned MatchWord;
100 #define MAX_LENGTH 1024
103 int n; /* no of MatchWord needed */
104 int range; /* max no. of errors */
105 int fact; /* (range+1)*n */
106 MatchWord *match_mask; /* match_mask */
111 static INLINE void set_bit (MatchContext *mc, MatchWord *m, int ch, int state)
113 int off = state & (WORD_BITS-1);
114 int wno = state / WORD_BITS;
116 m[mc->n * ch + wno] |= 1<<off;
119 static INLINE MatchWord get_bit (MatchContext *mc, MatchWord *m, int ch,
122 int off = state & (WORD_BITS-1);
123 int wno = state / WORD_BITS;
125 return m[mc->n * ch + wno] & (1<<off);
128 static MatchContext *mk_MatchContext (struct DFA *dfa, int range)
130 MatchContext *mc = (MatchContext *) xmalloc (sizeof(*mc));
133 mc->n = (dfa->no_states+WORD_BITS) / WORD_BITS;
135 mc->fact = (range+1)*mc->n;
136 mc->match_mask = (MatchWord *) xcalloc (mc->n, sizeof(*mc->match_mask));
138 for (s = 0; s<dfa->no_states; s++)
139 if (dfa->states[s]->rule_no)
140 set_bit (mc, mc->match_mask, 0, s);
144 static void rm_MatchContext (MatchContext **mc)
146 xfree ((*mc)->match_mask);
151 static void mask_shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
152 struct DFA *dfa, int ch)
155 MatchWord *Rsrc_p = Rsrc, mask;
157 for (j = 0; j<mc->n; j++)
162 for (j = 0; j<WORD_BITS/4; j++)
168 struct DFA_state *state = dfa->states[s];
169 int i = state->tran_no;
171 if (ch >= state->trans[i].ch[0] &&
172 ch <= state->trans[i].ch[1])
173 set_bit (mc, Rdst, 0, state->trans[i].to);
177 struct DFA_state *state = dfa->states[s+1];
178 int i = state->tran_no;
180 if (ch >= state->trans[i].ch[0] &&
181 ch <= state->trans[i].ch[1])
182 set_bit (mc, Rdst, 0, state->trans[i].to);
186 struct DFA_state *state = dfa->states[s+2];
187 int i = state->tran_no;
189 if (ch >= state->trans[i].ch[0] &&
190 ch <= state->trans[i].ch[1])
191 set_bit (mc, Rdst, 0, state->trans[i].to);
195 struct DFA_state *state = dfa->states[s+3];
196 int i = state->tran_no;
198 if (ch >= state->trans[i].ch[0] &&
199 ch <= state->trans[i].ch[1])
200 set_bit (mc, Rdst, 0, state->trans[i].to);
204 if (s >= dfa->no_states)
211 static void shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
215 MatchWord *Rsrc_p = Rsrc, mask;
216 for (j = 0; j<mc->n; j++)
221 for (j = 0; j<WORD_BITS/4; j++)
227 struct DFA_state *state = dfa->states[s];
228 int i = state->tran_no;
230 set_bit (mc, Rdst, 0, state->trans[i].to);
234 struct DFA_state *state = dfa->states[s+1];
235 int i = state->tran_no;
237 set_bit (mc, Rdst, 0, state->trans[i].to);
241 struct DFA_state *state = dfa->states[s+2];
242 int i = state->tran_no;
244 set_bit (mc, Rdst, 0, state->trans[i].to);
248 struct DFA_state *state = dfa->states[s+3];
249 int i = state->tran_no;
251 set_bit (mc, Rdst, 0, state->trans[i].to);
255 if (s >= dfa->no_states)
262 static void or (MatchContext *mc, MatchWord *Rdst,
263 MatchWord *Rsrc1, MatchWord *Rsrc2)
266 for (i = 0; i<mc->n; i++)
267 Rdst[i] = Rsrc1[i] | Rsrc2[i];
270 static INLINE int move (MatchContext *mc, MatchWord *Rj1, MatchWord *Rj,
271 Dict_char ch, struct DFA *dfa, MatchWord *Rtmp,
275 MatchWord *Rtmp_2 = Rtmp + mc->n;
277 mask_shift (mc, Rj1, Rj, dfa, ch);
278 for (d = 1; d <= mc->range; d++)
280 or (mc, Rtmp, Rj, Rj1); /* 2,3 */
282 shift (mc, Rtmp_2, Rtmp, dfa);
284 mask_shift (mc, Rtmp, Rj+mc->n, dfa, ch); /* 1 */
286 or (mc, Rtmp, Rtmp_2, Rtmp); /* 1,2,3*/
290 or (mc, Rj1, Rtmp, Rj); /* 1,2,3,4 */
299 static int dict_grep (Dict dict, Dict_ptr ptr, MatchContext *mc,
300 MatchWord *Rj, int pos, void *client,
301 int (*userfunc)(char *, const char *, void *),
302 Dict_char *prefix, struct DFA *dfa,
303 int *max_pos, int init_pos)
310 dict_bf_readp (dict->dbf, ptr, &p);
312 hi = DICT_nodir(p)-1;
313 indxp = (short*) ((char*) p+DICT_bsize(p)-sizeof(short));
319 /* string (Dict_char *) DICT_EOS terminated */
320 /* unsigned char length of information */
321 /* char * information */
324 info = (char*)p + indxp[-lo];
328 MatchWord *Rj0 = Rj + j *mc->fact;
329 MatchWord *Rj1 = Rj + (j+1)*mc->fact;
330 MatchWord *Rj_tmp = Rj + (j+2)*mc->fact;
333 memcpy (&ch, info+j*sizeof(Dict_char), sizeof(Dict_char));
335 if (pos+j > *max_pos)
340 if ((*userfunc)((char*) prefix,
341 info+(j+1)*sizeof(Dict_char), client))
345 if (pos+j >= init_pos)
349 move (mc, Rj1, Rj0, ch, dfa, Rj_tmp, range);
350 for (d = mc->n; --d >= 0; )
351 if (Rj1[range*mc->n + d])
356 for (d = mc->n; --d >= 0; )
357 if (Rj1[range*mc->n + d] & mc->match_mask[d])
366 MatchWord *Rj1 = Rj+ mc->fact;
367 MatchWord *Rj_tmp = Rj+2*mc->fact;
371 /* Dict_ptr subptr */
372 /* Dict_char sub char */
373 /* unsigned char length of information */
374 /* char * information */
375 info = (char*)p - indxp[-lo];
376 memcpy (&ch, info+sizeof(Dict_ptr), sizeof(Dict_char));
385 move (mc, Rj1, Rj, ch, dfa, Rj_tmp, range);
386 for (d = mc->n; --d >= 0; )
387 if (Rj1[range*mc->n + d])
392 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
394 for (d = mc->n; --d >= 0; )
395 if (Rj1[range*mc->n + d] & mc->match_mask[d])
397 prefix[pos+1] = DICT_EOS;
398 if ((*userfunc)((char*) prefix,
399 info+sizeof(Dict_ptr)+
400 sizeof(Dict_char), client))
405 memcpy (&subptr, info, sizeof(Dict_ptr));
408 if (dict_grep (dict, subptr, mc, Rj1, pos+1,
409 client, userfunc, prefix, dfa, max_pos,
412 dict_bf_readp (dict->dbf, ptr, &p);
413 indxp = (short*) ((char*) p+DICT_bsize(p)-sizeof(short));
422 int dict_lookup_grep (Dict dict, const char *pattern, int range, void *client,
423 int *max_pos, int init_pos,
424 int (*userfunc)(char *name, const char *info,
428 Dict_char prefix[MAX_LENGTH+1];
429 const char *this_pattern = pattern;
431 struct DFA *dfa = dfa_init();
437 debug_dfa_followpos = 1;
441 logf (LOG_DEBUG, "dict_lookup_grep range=%d", range);
442 for (i = 0; pattern[i]; i++)
444 logf (LOG_DEBUG, " %3d %c", pattern[i],
445 (pattern[i] > ' ' && pattern[i] < 127) ? pattern[i] : '?');
448 dfa_set_cmap (dfa, dict->grep_cmap_data, dict->grep_cmap);
450 i = dfa_parse (dfa, &this_pattern);
451 if (i || *this_pattern)
458 mc = mk_MatchContext (dfa, range);
460 Rj = (MatchWord *) xcalloc ((MAX_LENGTH+1) * mc->n, sizeof(*Rj));
462 set_bit (mc, Rj, 0, 0);
463 for (d = 1; d<=mc->range; d++)
466 memcpy (Rj + mc->n * d, Rj + mc->n * (d-1), mc->n * sizeof(*Rj));
467 for (s = 0; s<dfa->no_states; s++)
469 if (get_bit (mc, Rj, d-1, s))
471 struct DFA_state *state = dfa->states[s];
472 int i = state->tran_no;
474 set_bit (mc, Rj, d, state->trans[i].to);
480 i = dict_grep (dict, dict->head.root, mc, Rj, 0, client,
482 dfa, max_pos, init_pos);
485 logf (LOG_DEBUG, "max_pos = %d", *max_pos);
488 rm_MatchContext (&mc);
492 void dict_grep_cmap (Dict dict, void *vp,
493 const char **(*cmap)(void *vp, const char **from, int len))
495 dict->grep_cmap = cmap;
496 dict->grep_cmap_data = vp;