2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.19 1997-09-18 08:59:18 adam
8 * Extra generic handle for the character mapping routines.
10 * Revision 1.18 1997/09/05 15:29:58 adam
11 * Changed prototype for chr_map_input - added const.
12 * Added support for C++, headers uses extern "C" for public definitions.
14 * Revision 1.17 1996/06/04 10:20:06 adam
15 * Added support for character mapping.
17 * Revision 1.16 1996/05/24 14:46:04 adam
18 * Added dict_grep_cmap function to define user-mapping in grep lookups.
20 * Revision 1.15 1996/03/20 09:35:18 adam
21 * Function dict_lookup_grep got extra parameter, init_pos, which marks
22 * from which position in pattern approximate pattern matching should occur.
24 * Revision 1.14 1996/02/02 13:43:51 adam
25 * The public functions simply use char instead of Dict_char to represent
26 * search strings. Dict_char is used internally only.
28 * Revision 1.13 1996/01/08 09:09:30 adam
29 * Function dfa_parse got 'const' string argument.
31 * Revision 1.12 1995/12/11 09:04:48 adam
32 * Bug fix: the lookup/scan/lookgrep didn't handle empty dictionary.
34 * Revision 1.11 1995/12/06 14:43:02 adam
35 * New function: dict_delete.
37 * Revision 1.10 1995/11/16 17:00:44 adam
40 * Revision 1.9 1995/10/27 13:58:09 adam
41 * Makes 'Database unavailable' diagnostic.
43 * Revision 1.8 1995/10/19 14:57:21 adam
44 * New feature: grep lookup saves length of longest prefix match.
46 * Revision 1.7 1995/10/17 18:01:22 adam
47 * Userfunc may return non-zero in which case the the grepping stops
50 * Revision 1.6 1995/10/09 16:18:32 adam
51 * Function dict_lookup_grep got extra client data parameter.
53 * Revision 1.5 1995/09/14 11:52:59 adam
54 * Grep handle function parameter info is const now.
56 * Revision 1.4 1995/01/24 16:01:02 adam
57 * Added -ansi to CFLAGS.
58 * Use new API of dfa module.
60 * Revision 1.3 1994/10/05 12:16:50 adam
61 * Pagesize is a resource now.
63 * Revision 1.2 1994/10/04 12:08:07 adam
64 * Some bug fixes and some optimizations.
66 * Revision 1.1 1994/10/03 17:23:04 adam
67 * First version of dictionary lookup with regular expressions and errors.
79 typedef unsigned MatchWord;
81 #define MAX_LENGTH 1024
84 int n; /* no of MatchWord needed */
85 int range; /* max no. of errors */
86 int fact; /* (range+1)*n */
87 MatchWord *match_mask; /* match_mask */
92 static INLINE void set_bit (MatchContext *mc, MatchWord *m, int ch, int state)
94 int off = state & (WORD_BITS-1);
95 int wno = state / WORD_BITS;
97 m[mc->n * ch + wno] |= 1<<off;
100 static INLINE MatchWord get_bit (MatchContext *mc, MatchWord *m, int ch,
103 int off = state & (WORD_BITS-1);
104 int wno = state / WORD_BITS;
106 return m[mc->n * ch + wno] & (1<<off);
109 static MatchContext *mk_MatchContext (struct DFA *dfa, int range)
111 MatchContext *mc = xmalloc (sizeof(*mc));
114 mc->n = (dfa->no_states+WORD_BITS) / WORD_BITS;
116 mc->fact = (range+1)*mc->n;
117 mc->match_mask = xcalloc (mc->n, sizeof(*mc->match_mask));
119 for (s = 0; s<dfa->no_states; s++)
120 if (dfa->states[s]->rule_no)
121 set_bit (mc, mc->match_mask, 0, s);
125 static void rm_MatchContext (MatchContext **mc)
127 xfree ((*mc)->match_mask);
132 static void mask_shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
133 struct DFA *dfa, int ch)
136 MatchWord *Rsrc_p = Rsrc, mask;
138 for (j = 0; j<mc->n; j++)
143 for (j = 0; j<WORD_BITS/4; j++)
149 struct DFA_state *state = dfa->states[s];
150 int i = state->tran_no;
152 if (ch >= state->trans[i].ch[0] &&
153 ch <= state->trans[i].ch[1])
154 set_bit (mc, Rdst, 0, state->trans[i].to);
158 struct DFA_state *state = dfa->states[s+1];
159 int i = state->tran_no;
161 if (ch >= state->trans[i].ch[0] &&
162 ch <= state->trans[i].ch[1])
163 set_bit (mc, Rdst, 0, state->trans[i].to);
167 struct DFA_state *state = dfa->states[s+2];
168 int i = state->tran_no;
170 if (ch >= state->trans[i].ch[0] &&
171 ch <= state->trans[i].ch[1])
172 set_bit (mc, Rdst, 0, state->trans[i].to);
176 struct DFA_state *state = dfa->states[s+3];
177 int i = state->tran_no;
179 if (ch >= state->trans[i].ch[0] &&
180 ch <= state->trans[i].ch[1])
181 set_bit (mc, Rdst, 0, state->trans[i].to);
185 if (s >= dfa->no_states)
192 static void shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
196 MatchWord *Rsrc_p = Rsrc, mask;
197 for (j = 0; j<mc->n; j++)
202 for (j = 0; j<WORD_BITS/4; j++)
208 struct DFA_state *state = dfa->states[s];
209 int i = state->tran_no;
211 set_bit (mc, Rdst, 0, state->trans[i].to);
215 struct DFA_state *state = dfa->states[s+1];
216 int i = state->tran_no;
218 set_bit (mc, Rdst, 0, state->trans[i].to);
222 struct DFA_state *state = dfa->states[s+2];
223 int i = state->tran_no;
225 set_bit (mc, Rdst, 0, state->trans[i].to);
229 struct DFA_state *state = dfa->states[s+3];
230 int i = state->tran_no;
232 set_bit (mc, Rdst, 0, state->trans[i].to);
236 if (s >= dfa->no_states)
243 static void or (MatchContext *mc, MatchWord *Rdst,
244 MatchWord *Rsrc1, MatchWord *Rsrc2)
247 for (i = 0; i<mc->n; i++)
248 Rdst[i] = Rsrc1[i] | Rsrc2[i];
251 static INLINE int move (MatchContext *mc, MatchWord *Rj1, MatchWord *Rj,
252 Dict_char ch, struct DFA *dfa, MatchWord *Rtmp,
256 MatchWord *Rtmp_2 = Rtmp + mc->n;
258 mask_shift (mc, Rj1, Rj, dfa, ch);
259 for (d = 1; d <= mc->range; d++)
261 or (mc, Rtmp, Rj, Rj1); /* 2,3 */
263 shift (mc, Rtmp_2, Rtmp, dfa);
265 mask_shift (mc, Rtmp, Rj+mc->n, dfa, ch); /* 1 */
267 or (mc, Rtmp, Rtmp_2, Rtmp); /* 1,2,3*/
271 or (mc, Rj1, Rtmp, Rj); /* 1,2,3,4 */
280 static int dict_grep (Dict dict, Dict_ptr ptr, MatchContext *mc,
281 MatchWord *Rj, int pos, void *client,
282 int (*userfunc)(char *, const char *, void *),
283 Dict_char *prefix, struct DFA *dfa,
284 int *max_pos, int init_pos)
291 dict_bf_readp (dict->dbf, ptr, &p);
293 hi = DICT_nodir(p)-1;
294 indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
300 /* string (Dict_char *) DICT_EOS terminated */
301 /* unsigned char length of information */
302 /* char * information */
305 info = (char*)p + indxp[-lo];
309 MatchWord *Rj0 = Rj + j *mc->fact;
310 MatchWord *Rj1 = Rj + (j+1)*mc->fact;
311 MatchWord *Rj_tmp = Rj + (j+2)*mc->fact;
314 memcpy (&ch, info+j*sizeof(Dict_char), sizeof(Dict_char));
316 if (pos+j > *max_pos)
321 if ((*userfunc)((char*) prefix,
322 info+(j+1)*sizeof(Dict_char), client))
326 if (pos+j >= init_pos)
330 move (mc, Rj1, Rj0, ch, dfa, Rj_tmp, range);
331 for (d = mc->n; --d >= 0; )
332 if (Rj1[range*mc->n + d])
337 for (d = mc->n; --d >= 0; )
338 if (Rj1[range*mc->n + d] & mc->match_mask[d])
347 MatchWord *Rj1 = Rj+ mc->fact;
348 MatchWord *Rj_tmp = Rj+2*mc->fact;
352 /* Dict_ptr subptr */
353 /* Dict_char sub char */
354 /* unsigned char length of information */
355 /* char * information */
356 info = (char*)p - indxp[-lo];
357 memcpy (&ch, info+sizeof(Dict_ptr), sizeof(Dict_char));
366 move (mc, Rj1, Rj, ch, dfa, Rj_tmp, range);
367 for (d = mc->n; --d >= 0; )
368 if (Rj1[range*mc->n + d])
373 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
375 for (d = mc->n; --d >= 0; )
376 if (Rj1[range*mc->n + d] & mc->match_mask[d])
378 prefix[pos+1] = DICT_EOS;
379 if ((*userfunc)((char*) prefix,
380 info+sizeof(Dict_ptr)+
381 sizeof(Dict_char), client))
386 memcpy (&subptr, info, sizeof(Dict_ptr));
389 if (dict_grep (dict, subptr, mc, Rj1, pos+1,
390 client, userfunc, prefix, dfa, max_pos,
393 dict_bf_readp (dict->dbf, ptr, &p);
394 indxp = (short*) ((char*) p+DICT_pagesize(dict)
404 int dict_lookup_grep (Dict dict, const char *pattern, int range, void *client,
405 int *max_pos, int init_pos,
406 int (*userfunc)(char *name, const char *info,
410 Dict_char prefix[MAX_LENGTH+1];
411 const char *this_pattern = pattern;
413 struct DFA *dfa = dfa_init();
416 logf (LOG_DEBUG, "dict_lookup_grep '%s' range=%d", pattern, range);
418 dfa_set_cmap (dfa, dict->grep_cmap_data, dict->grep_cmap);
420 i = dfa_parse (dfa, &this_pattern);
421 if (i || *this_pattern)
428 mc = mk_MatchContext (dfa, range);
430 Rj = xcalloc ((MAX_LENGTH+1) * mc->n, sizeof(*Rj));
432 set_bit (mc, Rj, 0, 0);
433 for (d = 1; d<=mc->range; d++)
436 memcpy (Rj + mc->n * d, Rj + mc->n * (d-1), mc->n * sizeof(*Rj));
437 for (s = 0; s<dfa->no_states; s++)
439 if (get_bit (mc, Rj, d-1, s))
441 struct DFA_state *state = dfa->states[s];
442 int i = state->tran_no;
444 set_bit (mc, Rj, d, state->trans[i].to);
449 if (dict->head.last > 1)
450 i = dict_grep (dict, 1, mc, Rj, 0, client, userfunc, prefix,
451 dfa, max_pos, init_pos);
454 logf (LOG_DEBUG, "max_pos = %d", *max_pos);
457 rm_MatchContext (&mc);
461 void dict_grep_cmap (Dict dict, void *vp,
462 const char **(*cmap)(void *vp, const char **from, int len))
464 dict->grep_cmap = cmap;
465 dict->grep_cmap_data = vp;