2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.22 1999-02-02 14:50:23 adam
8 * Updated WIN32 code specific sections. Changed header.
10 * Revision 1.21 1998/06/24 12:16:12 adam
11 * Support for relations on text operands. Open range support in
12 * DFA module (i.e. [-j], [g-]).
14 * Revision 1.20 1997/10/27 14:33:03 adam
15 * Moved towards generic character mapping depending on "structure"
16 * field in abstract syntax file. Fixed a few memory leaks. Fixed
17 * bug with negative integers when doing searches with relational
20 * Revision 1.19 1997/09/18 08:59:18 adam
21 * Extra generic handle for the character mapping routines.
23 * Revision 1.18 1997/09/05 15:29:58 adam
24 * Changed prototype for chr_map_input - added const.
25 * Added support for C++, headers uses extern "C" for public definitions.
27 * Revision 1.17 1996/06/04 10:20:06 adam
28 * Added support for character mapping.
30 * Revision 1.16 1996/05/24 14:46:04 adam
31 * Added dict_grep_cmap function to define user-mapping in grep lookups.
33 * Revision 1.15 1996/03/20 09:35:18 adam
34 * Function dict_lookup_grep got extra parameter, init_pos, which marks
35 * from which position in pattern approximate pattern matching should occur.
37 * Revision 1.14 1996/02/02 13:43:51 adam
38 * The public functions simply use char instead of Dict_char to represent
39 * search strings. Dict_char is used internally only.
41 * Revision 1.13 1996/01/08 09:09:30 adam
42 * Function dfa_parse got 'const' string argument.
44 * Revision 1.12 1995/12/11 09:04:48 adam
45 * Bug fix: the lookup/scan/lookgrep didn't handle empty dictionary.
47 * Revision 1.11 1995/12/06 14:43:02 adam
48 * New function: dict_delete.
50 * Revision 1.10 1995/11/16 17:00:44 adam
53 * Revision 1.9 1995/10/27 13:58:09 adam
54 * Makes 'Database unavailable' diagnostic.
56 * Revision 1.8 1995/10/19 14:57:21 adam
57 * New feature: grep lookup saves length of longest prefix match.
59 * Revision 1.7 1995/10/17 18:01:22 adam
60 * Userfunc may return non-zero in which case the the grepping stops
63 * Revision 1.6 1995/10/09 16:18:32 adam
64 * Function dict_lookup_grep got extra client data parameter.
66 * Revision 1.5 1995/09/14 11:52:59 adam
67 * Grep handle function parameter info is const now.
69 * Revision 1.4 1995/01/24 16:01:02 adam
70 * Added -ansi to CFLAGS.
71 * Use new API of dfa module.
73 * Revision 1.3 1994/10/05 12:16:50 adam
74 * Pagesize is a resource now.
76 * Revision 1.2 1994/10/04 12:08:07 adam
77 * Some bug fixes and some optimizations.
79 * Revision 1.1 1994/10/03 17:23:04 adam
80 * First version of dictionary lookup with regular expressions and errors.
92 typedef unsigned MatchWord;
94 #define MAX_LENGTH 1024
97 int n; /* no of MatchWord needed */
98 int range; /* max no. of errors */
99 int fact; /* (range+1)*n */
100 MatchWord *match_mask; /* match_mask */
105 static INLINE void set_bit (MatchContext *mc, MatchWord *m, int ch, int state)
107 int off = state & (WORD_BITS-1);
108 int wno = state / WORD_BITS;
110 m[mc->n * ch + wno] |= 1<<off;
113 static INLINE MatchWord get_bit (MatchContext *mc, MatchWord *m, int ch,
116 int off = state & (WORD_BITS-1);
117 int wno = state / WORD_BITS;
119 return m[mc->n * ch + wno] & (1<<off);
122 static MatchContext *mk_MatchContext (struct DFA *dfa, int range)
124 MatchContext *mc = xmalloc (sizeof(*mc));
127 mc->n = (dfa->no_states+WORD_BITS) / WORD_BITS;
129 mc->fact = (range+1)*mc->n;
130 mc->match_mask = xcalloc (mc->n, sizeof(*mc->match_mask));
132 for (s = 0; s<dfa->no_states; s++)
133 if (dfa->states[s]->rule_no)
134 set_bit (mc, mc->match_mask, 0, s);
138 static void rm_MatchContext (MatchContext **mc)
140 xfree ((*mc)->match_mask);
145 static void mask_shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
146 struct DFA *dfa, int ch)
149 MatchWord *Rsrc_p = Rsrc, mask;
151 for (j = 0; j<mc->n; j++)
156 for (j = 0; j<WORD_BITS/4; j++)
162 struct DFA_state *state = dfa->states[s];
163 int i = state->tran_no;
165 if (ch >= state->trans[i].ch[0] &&
166 ch <= state->trans[i].ch[1])
167 set_bit (mc, Rdst, 0, state->trans[i].to);
171 struct DFA_state *state = dfa->states[s+1];
172 int i = state->tran_no;
174 if (ch >= state->trans[i].ch[0] &&
175 ch <= state->trans[i].ch[1])
176 set_bit (mc, Rdst, 0, state->trans[i].to);
180 struct DFA_state *state = dfa->states[s+2];
181 int i = state->tran_no;
183 if (ch >= state->trans[i].ch[0] &&
184 ch <= state->trans[i].ch[1])
185 set_bit (mc, Rdst, 0, state->trans[i].to);
189 struct DFA_state *state = dfa->states[s+3];
190 int i = state->tran_no;
192 if (ch >= state->trans[i].ch[0] &&
193 ch <= state->trans[i].ch[1])
194 set_bit (mc, Rdst, 0, state->trans[i].to);
198 if (s >= dfa->no_states)
205 static void shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
209 MatchWord *Rsrc_p = Rsrc, mask;
210 for (j = 0; j<mc->n; j++)
215 for (j = 0; j<WORD_BITS/4; j++)
221 struct DFA_state *state = dfa->states[s];
222 int i = state->tran_no;
224 set_bit (mc, Rdst, 0, state->trans[i].to);
228 struct DFA_state *state = dfa->states[s+1];
229 int i = state->tran_no;
231 set_bit (mc, Rdst, 0, state->trans[i].to);
235 struct DFA_state *state = dfa->states[s+2];
236 int i = state->tran_no;
238 set_bit (mc, Rdst, 0, state->trans[i].to);
242 struct DFA_state *state = dfa->states[s+3];
243 int i = state->tran_no;
245 set_bit (mc, Rdst, 0, state->trans[i].to);
249 if (s >= dfa->no_states)
256 static void or (MatchContext *mc, MatchWord *Rdst,
257 MatchWord *Rsrc1, MatchWord *Rsrc2)
260 for (i = 0; i<mc->n; i++)
261 Rdst[i] = Rsrc1[i] | Rsrc2[i];
264 static INLINE int move (MatchContext *mc, MatchWord *Rj1, MatchWord *Rj,
265 Dict_char ch, struct DFA *dfa, MatchWord *Rtmp,
269 MatchWord *Rtmp_2 = Rtmp + mc->n;
271 mask_shift (mc, Rj1, Rj, dfa, ch);
272 for (d = 1; d <= mc->range; d++)
274 or (mc, Rtmp, Rj, Rj1); /* 2,3 */
276 shift (mc, Rtmp_2, Rtmp, dfa);
278 mask_shift (mc, Rtmp, Rj+mc->n, dfa, ch); /* 1 */
280 or (mc, Rtmp, Rtmp_2, Rtmp); /* 1,2,3*/
284 or (mc, Rj1, Rtmp, Rj); /* 1,2,3,4 */
293 static int dict_grep (Dict dict, Dict_ptr ptr, MatchContext *mc,
294 MatchWord *Rj, int pos, void *client,
295 int (*userfunc)(char *, const char *, void *),
296 Dict_char *prefix, struct DFA *dfa,
297 int *max_pos, int init_pos)
304 dict_bf_readp (dict->dbf, ptr, &p);
306 hi = DICT_nodir(p)-1;
307 indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
313 /* string (Dict_char *) DICT_EOS terminated */
314 /* unsigned char length of information */
315 /* char * information */
318 info = (char*)p + indxp[-lo];
322 MatchWord *Rj0 = Rj + j *mc->fact;
323 MatchWord *Rj1 = Rj + (j+1)*mc->fact;
324 MatchWord *Rj_tmp = Rj + (j+2)*mc->fact;
327 memcpy (&ch, info+j*sizeof(Dict_char), sizeof(Dict_char));
329 if (pos+j > *max_pos)
334 if ((*userfunc)((char*) prefix,
335 info+(j+1)*sizeof(Dict_char), client))
339 if (pos+j >= init_pos)
343 move (mc, Rj1, Rj0, ch, dfa, Rj_tmp, range);
344 for (d = mc->n; --d >= 0; )
345 if (Rj1[range*mc->n + d])
350 for (d = mc->n; --d >= 0; )
351 if (Rj1[range*mc->n + d] & mc->match_mask[d])
360 MatchWord *Rj1 = Rj+ mc->fact;
361 MatchWord *Rj_tmp = Rj+2*mc->fact;
365 /* Dict_ptr subptr */
366 /* Dict_char sub char */
367 /* unsigned char length of information */
368 /* char * information */
369 info = (char*)p - indxp[-lo];
370 memcpy (&ch, info+sizeof(Dict_ptr), sizeof(Dict_char));
379 move (mc, Rj1, Rj, ch, dfa, Rj_tmp, range);
380 for (d = mc->n; --d >= 0; )
381 if (Rj1[range*mc->n + d])
386 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
388 for (d = mc->n; --d >= 0; )
389 if (Rj1[range*mc->n + d] & mc->match_mask[d])
391 prefix[pos+1] = DICT_EOS;
392 if ((*userfunc)((char*) prefix,
393 info+sizeof(Dict_ptr)+
394 sizeof(Dict_char), client))
399 memcpy (&subptr, info, sizeof(Dict_ptr));
402 if (dict_grep (dict, subptr, mc, Rj1, pos+1,
403 client, userfunc, prefix, dfa, max_pos,
406 dict_bf_readp (dict->dbf, ptr, &p);
407 indxp = (short*) ((char*) p+DICT_pagesize(dict)
417 int dict_lookup_grep (Dict dict, const char *pattern, int range, void *client,
418 int *max_pos, int init_pos,
419 int (*userfunc)(char *name, const char *info,
423 Dict_char prefix[MAX_LENGTH+1];
424 const char *this_pattern = pattern;
426 struct DFA *dfa = dfa_init();
432 debug_dfa_followpos = 1;
436 logf (LOG_DEBUG, "dict_lookup_grep range=%d", range);
437 for (i = 0; pattern[i]; i++)
439 logf (LOG_DEBUG, " %3d %c", pattern[i],
440 (pattern[i] > ' ' && pattern[i] < 127) ? pattern[i] : '?');
443 dfa_set_cmap (dfa, dict->grep_cmap_data, dict->grep_cmap);
445 i = dfa_parse (dfa, &this_pattern);
446 if (i || *this_pattern)
453 mc = mk_MatchContext (dfa, range);
455 Rj = xcalloc ((MAX_LENGTH+1) * mc->n, sizeof(*Rj));
457 set_bit (mc, Rj, 0, 0);
458 for (d = 1; d<=mc->range; d++)
461 memcpy (Rj + mc->n * d, Rj + mc->n * (d-1), mc->n * sizeof(*Rj));
462 for (s = 0; s<dfa->no_states; s++)
464 if (get_bit (mc, Rj, d-1, s))
466 struct DFA_state *state = dfa->states[s];
467 int i = state->tran_no;
469 set_bit (mc, Rj, d, state->trans[i].to);
474 if (dict->head.last > 1)
475 i = dict_grep (dict, 1, mc, Rj, 0, client, userfunc, prefix,
476 dfa, max_pos, init_pos);
479 logf (LOG_DEBUG, "max_pos = %d", *max_pos);
482 rm_MatchContext (&mc);
486 void dict_grep_cmap (Dict dict, void *vp,
487 const char **(*cmap)(void *vp, const char **from, int len))
489 dict->grep_cmap = cmap;
490 dict->grep_cmap_data = vp;