2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.18 1997-09-05 15:29:58 adam
8 * Changed prototype for chr_map_input - added const.
9 * Added support for C++, headers uses extern "C" for public definitions.
11 * Revision 1.17 1996/06/04 10:20:06 adam
12 * Added support for character mapping.
14 * Revision 1.16 1996/05/24 14:46:04 adam
15 * Added dict_grep_cmap function to define user-mapping in grep lookups.
17 * Revision 1.15 1996/03/20 09:35:18 adam
18 * Function dict_lookup_grep got extra parameter, init_pos, which marks
19 * from which position in pattern approximate pattern matching should occur.
21 * Revision 1.14 1996/02/02 13:43:51 adam
22 * The public functions simply use char instead of Dict_char to represent
23 * search strings. Dict_char is used internally only.
25 * Revision 1.13 1996/01/08 09:09:30 adam
26 * Function dfa_parse got 'const' string argument.
28 * Revision 1.12 1995/12/11 09:04:48 adam
29 * Bug fix: the lookup/scan/lookgrep didn't handle empty dictionary.
31 * Revision 1.11 1995/12/06 14:43:02 adam
32 * New function: dict_delete.
34 * Revision 1.10 1995/11/16 17:00:44 adam
37 * Revision 1.9 1995/10/27 13:58:09 adam
38 * Makes 'Database unavailable' diagnostic.
40 * Revision 1.8 1995/10/19 14:57:21 adam
41 * New feature: grep lookup saves length of longest prefix match.
43 * Revision 1.7 1995/10/17 18:01:22 adam
44 * Userfunc may return non-zero in which case the the grepping stops
47 * Revision 1.6 1995/10/09 16:18:32 adam
48 * Function dict_lookup_grep got extra client data parameter.
50 * Revision 1.5 1995/09/14 11:52:59 adam
51 * Grep handle function parameter info is const now.
53 * Revision 1.4 1995/01/24 16:01:02 adam
54 * Added -ansi to CFLAGS.
55 * Use new API of dfa module.
57 * Revision 1.3 1994/10/05 12:16:50 adam
58 * Pagesize is a resource now.
60 * Revision 1.2 1994/10/04 12:08:07 adam
61 * Some bug fixes and some optimizations.
63 * Revision 1.1 1994/10/03 17:23:04 adam
64 * First version of dictionary lookup with regular expressions and errors.
76 typedef unsigned MatchWord;
78 #define MAX_LENGTH 1024
81 int n; /* no of MatchWord needed */
82 int range; /* max no. of errors */
83 int fact; /* (range+1)*n */
84 MatchWord *match_mask; /* match_mask */
89 static INLINE void set_bit (MatchContext *mc, MatchWord *m, int ch, int state)
91 int off = state & (WORD_BITS-1);
92 int wno = state / WORD_BITS;
94 m[mc->n * ch + wno] |= 1<<off;
97 static INLINE MatchWord get_bit (MatchContext *mc, MatchWord *m, int ch,
100 int off = state & (WORD_BITS-1);
101 int wno = state / WORD_BITS;
103 return m[mc->n * ch + wno] & (1<<off);
106 static MatchContext *mk_MatchContext (struct DFA *dfa, int range)
108 MatchContext *mc = xmalloc (sizeof(*mc));
111 mc->n = (dfa->no_states+WORD_BITS) / WORD_BITS;
113 mc->fact = (range+1)*mc->n;
114 mc->match_mask = xcalloc (mc->n, sizeof(*mc->match_mask));
116 for (s = 0; s<dfa->no_states; s++)
117 if (dfa->states[s]->rule_no)
118 set_bit (mc, mc->match_mask, 0, s);
122 static void rm_MatchContext (MatchContext **mc)
124 xfree ((*mc)->match_mask);
129 static void mask_shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
130 struct DFA *dfa, int ch)
133 MatchWord *Rsrc_p = Rsrc, mask;
135 for (j = 0; j<mc->n; j++)
140 for (j = 0; j<WORD_BITS/4; j++)
146 struct DFA_state *state = dfa->states[s];
147 int i = state->tran_no;
149 if (ch >= state->trans[i].ch[0] &&
150 ch <= state->trans[i].ch[1])
151 set_bit (mc, Rdst, 0, state->trans[i].to);
155 struct DFA_state *state = dfa->states[s+1];
156 int i = state->tran_no;
158 if (ch >= state->trans[i].ch[0] &&
159 ch <= state->trans[i].ch[1])
160 set_bit (mc, Rdst, 0, state->trans[i].to);
164 struct DFA_state *state = dfa->states[s+2];
165 int i = state->tran_no;
167 if (ch >= state->trans[i].ch[0] &&
168 ch <= state->trans[i].ch[1])
169 set_bit (mc, Rdst, 0, state->trans[i].to);
173 struct DFA_state *state = dfa->states[s+3];
174 int i = state->tran_no;
176 if (ch >= state->trans[i].ch[0] &&
177 ch <= state->trans[i].ch[1])
178 set_bit (mc, Rdst, 0, state->trans[i].to);
182 if (s >= dfa->no_states)
189 static void shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
193 MatchWord *Rsrc_p = Rsrc, mask;
194 for (j = 0; j<mc->n; j++)
199 for (j = 0; j<WORD_BITS/4; j++)
205 struct DFA_state *state = dfa->states[s];
206 int i = state->tran_no;
208 set_bit (mc, Rdst, 0, state->trans[i].to);
212 struct DFA_state *state = dfa->states[s+1];
213 int i = state->tran_no;
215 set_bit (mc, Rdst, 0, state->trans[i].to);
219 struct DFA_state *state = dfa->states[s+2];
220 int i = state->tran_no;
222 set_bit (mc, Rdst, 0, state->trans[i].to);
226 struct DFA_state *state = dfa->states[s+3];
227 int i = state->tran_no;
229 set_bit (mc, Rdst, 0, state->trans[i].to);
233 if (s >= dfa->no_states)
240 static void or (MatchContext *mc, MatchWord *Rdst,
241 MatchWord *Rsrc1, MatchWord *Rsrc2)
244 for (i = 0; i<mc->n; i++)
245 Rdst[i] = Rsrc1[i] | Rsrc2[i];
248 static INLINE int move (MatchContext *mc, MatchWord *Rj1, MatchWord *Rj,
249 Dict_char ch, struct DFA *dfa, MatchWord *Rtmp,
253 MatchWord *Rtmp_2 = Rtmp + mc->n;
255 mask_shift (mc, Rj1, Rj, dfa, ch);
256 for (d = 1; d <= mc->range; d++)
258 or (mc, Rtmp, Rj, Rj1); /* 2,3 */
260 shift (mc, Rtmp_2, Rtmp, dfa);
262 mask_shift (mc, Rtmp, Rj+mc->n, dfa, ch); /* 1 */
264 or (mc, Rtmp, Rtmp_2, Rtmp); /* 1,2,3*/
268 or (mc, Rj1, Rtmp, Rj); /* 1,2,3,4 */
277 static int dict_grep (Dict dict, Dict_ptr ptr, MatchContext *mc,
278 MatchWord *Rj, int pos, void *client,
279 int (*userfunc)(char *, const char *, void *),
280 Dict_char *prefix, struct DFA *dfa,
281 int *max_pos, int init_pos)
288 dict_bf_readp (dict->dbf, ptr, &p);
290 hi = DICT_nodir(p)-1;
291 indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
297 /* string (Dict_char *) DICT_EOS terminated */
298 /* unsigned char length of information */
299 /* char * information */
302 info = (char*)p + indxp[-lo];
306 MatchWord *Rj0 = Rj + j *mc->fact;
307 MatchWord *Rj1 = Rj + (j+1)*mc->fact;
308 MatchWord *Rj_tmp = Rj + (j+2)*mc->fact;
311 memcpy (&ch, info+j*sizeof(Dict_char), sizeof(Dict_char));
313 if (pos+j > *max_pos)
318 if ((*userfunc)((char*) prefix,
319 info+(j+1)*sizeof(Dict_char), client))
323 if (pos+j >= init_pos)
327 move (mc, Rj1, Rj0, ch, dfa, Rj_tmp, range);
328 for (d = mc->n; --d >= 0; )
329 if (Rj1[range*mc->n + d])
334 for (d = mc->n; --d >= 0; )
335 if (Rj1[range*mc->n + d] & mc->match_mask[d])
344 MatchWord *Rj1 = Rj+ mc->fact;
345 MatchWord *Rj_tmp = Rj+2*mc->fact;
349 /* Dict_ptr subptr */
350 /* Dict_char sub char */
351 /* unsigned char length of information */
352 /* char * information */
353 info = (char*)p - indxp[-lo];
354 memcpy (&ch, info+sizeof(Dict_ptr), sizeof(Dict_char));
363 move (mc, Rj1, Rj, ch, dfa, Rj_tmp, range);
364 for (d = mc->n; --d >= 0; )
365 if (Rj1[range*mc->n + d])
370 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
372 for (d = mc->n; --d >= 0; )
373 if (Rj1[range*mc->n + d] & mc->match_mask[d])
375 prefix[pos+1] = DICT_EOS;
376 if ((*userfunc)((char*) prefix,
377 info+sizeof(Dict_ptr)+
378 sizeof(Dict_char), client))
383 memcpy (&subptr, info, sizeof(Dict_ptr));
386 if (dict_grep (dict, subptr, mc, Rj1, pos+1,
387 client, userfunc, prefix, dfa, max_pos,
390 dict_bf_readp (dict->dbf, ptr, &p);
391 indxp = (short*) ((char*) p+DICT_pagesize(dict)
401 int dict_lookup_grep (Dict dict, const char *pattern, int range, void *client,
402 int *max_pos, int init_pos,
403 int (*userfunc)(char *name, const char *info,
407 Dict_char prefix[MAX_LENGTH+1];
408 const char *this_pattern = pattern;
410 struct DFA *dfa = dfa_init();
413 logf (LOG_DEBUG, "dict_lookup_grep '%s' range=%d", pattern, range);
415 dfa_set_cmap (dfa, dict->grep_cmap);
417 i = dfa_parse (dfa, &this_pattern);
418 if (i || *this_pattern)
425 mc = mk_MatchContext (dfa, range);
427 Rj = xcalloc ((MAX_LENGTH+1) * mc->n, sizeof(*Rj));
429 set_bit (mc, Rj, 0, 0);
430 for (d = 1; d<=mc->range; d++)
433 memcpy (Rj + mc->n * d, Rj + mc->n * (d-1), mc->n * sizeof(*Rj));
434 for (s = 0; s<dfa->no_states; s++)
436 if (get_bit (mc, Rj, d-1, s))
438 struct DFA_state *state = dfa->states[s];
439 int i = state->tran_no;
441 set_bit (mc, Rj, d, state->trans[i].to);
446 if (dict->head.last > 1)
447 i = dict_grep (dict, 1, mc, Rj, 0, client, userfunc, prefix,
448 dfa, max_pos, init_pos);
451 logf (LOG_DEBUG, "max_pos = %d", *max_pos);
454 rm_MatchContext (&mc);
458 void dict_grep_cmap (Dict dict,
459 const char **(*cmap)(const char **from, int len))
461 dict->grep_cmap = cmap;