2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.20 1997-10-27 14:33:03 adam
8 * Moved towards generic character mapping depending on "structure"
9 * field in abstract syntax file. Fixed a few memory leaks. Fixed
10 * bug with negative integers when doing searches with relational
13 * Revision 1.19 1997/09/18 08:59:18 adam
14 * Extra generic handle for the character mapping routines.
16 * Revision 1.18 1997/09/05 15:29:58 adam
17 * Changed prototype for chr_map_input - added const.
18 * Added support for C++, headers uses extern "C" for public definitions.
20 * Revision 1.17 1996/06/04 10:20:06 adam
21 * Added support for character mapping.
23 * Revision 1.16 1996/05/24 14:46:04 adam
24 * Added dict_grep_cmap function to define user-mapping in grep lookups.
26 * Revision 1.15 1996/03/20 09:35:18 adam
27 * Function dict_lookup_grep got extra parameter, init_pos, which marks
28 * from which position in pattern approximate pattern matching should occur.
30 * Revision 1.14 1996/02/02 13:43:51 adam
31 * The public functions simply use char instead of Dict_char to represent
32 * search strings. Dict_char is used internally only.
34 * Revision 1.13 1996/01/08 09:09:30 adam
35 * Function dfa_parse got 'const' string argument.
37 * Revision 1.12 1995/12/11 09:04:48 adam
38 * Bug fix: the lookup/scan/lookgrep didn't handle empty dictionary.
40 * Revision 1.11 1995/12/06 14:43:02 adam
41 * New function: dict_delete.
43 * Revision 1.10 1995/11/16 17:00:44 adam
46 * Revision 1.9 1995/10/27 13:58:09 adam
47 * Makes 'Database unavailable' diagnostic.
49 * Revision 1.8 1995/10/19 14:57:21 adam
50 * New feature: grep lookup saves length of longest prefix match.
52 * Revision 1.7 1995/10/17 18:01:22 adam
53 * Userfunc may return non-zero in which case the the grepping stops
56 * Revision 1.6 1995/10/09 16:18:32 adam
57 * Function dict_lookup_grep got extra client data parameter.
59 * Revision 1.5 1995/09/14 11:52:59 adam
60 * Grep handle function parameter info is const now.
62 * Revision 1.4 1995/01/24 16:01:02 adam
63 * Added -ansi to CFLAGS.
64 * Use new API of dfa module.
66 * Revision 1.3 1994/10/05 12:16:50 adam
67 * Pagesize is a resource now.
69 * Revision 1.2 1994/10/04 12:08:07 adam
70 * Some bug fixes and some optimizations.
72 * Revision 1.1 1994/10/03 17:23:04 adam
73 * First version of dictionary lookup with regular expressions and errors.
85 typedef unsigned MatchWord;
87 #define MAX_LENGTH 1024
90 int n; /* no of MatchWord needed */
91 int range; /* max no. of errors */
92 int fact; /* (range+1)*n */
93 MatchWord *match_mask; /* match_mask */
98 static INLINE void set_bit (MatchContext *mc, MatchWord *m, int ch, int state)
100 int off = state & (WORD_BITS-1);
101 int wno = state / WORD_BITS;
103 m[mc->n * ch + wno] |= 1<<off;
106 static INLINE MatchWord get_bit (MatchContext *mc, MatchWord *m, int ch,
109 int off = state & (WORD_BITS-1);
110 int wno = state / WORD_BITS;
112 return m[mc->n * ch + wno] & (1<<off);
115 static MatchContext *mk_MatchContext (struct DFA *dfa, int range)
117 MatchContext *mc = xmalloc (sizeof(*mc));
120 mc->n = (dfa->no_states+WORD_BITS) / WORD_BITS;
122 mc->fact = (range+1)*mc->n;
123 mc->match_mask = xcalloc (mc->n, sizeof(*mc->match_mask));
125 for (s = 0; s<dfa->no_states; s++)
126 if (dfa->states[s]->rule_no)
127 set_bit (mc, mc->match_mask, 0, s);
131 static void rm_MatchContext (MatchContext **mc)
133 xfree ((*mc)->match_mask);
138 static void mask_shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
139 struct DFA *dfa, int ch)
142 MatchWord *Rsrc_p = Rsrc, mask;
144 for (j = 0; j<mc->n; j++)
149 for (j = 0; j<WORD_BITS/4; j++)
155 struct DFA_state *state = dfa->states[s];
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+1];
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+2];
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 struct DFA_state *state = dfa->states[s+3];
183 int i = state->tran_no;
185 if (ch >= state->trans[i].ch[0] &&
186 ch <= state->trans[i].ch[1])
187 set_bit (mc, Rdst, 0, state->trans[i].to);
191 if (s >= dfa->no_states)
198 static void shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
202 MatchWord *Rsrc_p = Rsrc, mask;
203 for (j = 0; j<mc->n; j++)
208 for (j = 0; j<WORD_BITS/4; j++)
214 struct DFA_state *state = dfa->states[s];
215 int i = state->tran_no;
217 set_bit (mc, Rdst, 0, state->trans[i].to);
221 struct DFA_state *state = dfa->states[s+1];
222 int i = state->tran_no;
224 set_bit (mc, Rdst, 0, state->trans[i].to);
228 struct DFA_state *state = dfa->states[s+2];
229 int i = state->tran_no;
231 set_bit (mc, Rdst, 0, state->trans[i].to);
235 struct DFA_state *state = dfa->states[s+3];
236 int i = state->tran_no;
238 set_bit (mc, Rdst, 0, state->trans[i].to);
242 if (s >= dfa->no_states)
249 static void or (MatchContext *mc, MatchWord *Rdst,
250 MatchWord *Rsrc1, MatchWord *Rsrc2)
253 for (i = 0; i<mc->n; i++)
254 Rdst[i] = Rsrc1[i] | Rsrc2[i];
257 static INLINE int move (MatchContext *mc, MatchWord *Rj1, MatchWord *Rj,
258 Dict_char ch, struct DFA *dfa, MatchWord *Rtmp,
262 MatchWord *Rtmp_2 = Rtmp + mc->n;
264 mask_shift (mc, Rj1, Rj, dfa, ch);
265 for (d = 1; d <= mc->range; d++)
267 or (mc, Rtmp, Rj, Rj1); /* 2,3 */
269 shift (mc, Rtmp_2, Rtmp, dfa);
271 mask_shift (mc, Rtmp, Rj+mc->n, dfa, ch); /* 1 */
273 or (mc, Rtmp, Rtmp_2, Rtmp); /* 1,2,3*/
277 or (mc, Rj1, Rtmp, Rj); /* 1,2,3,4 */
286 static int dict_grep (Dict dict, Dict_ptr ptr, MatchContext *mc,
287 MatchWord *Rj, int pos, void *client,
288 int (*userfunc)(char *, const char *, void *),
289 Dict_char *prefix, struct DFA *dfa,
290 int *max_pos, int init_pos)
297 dict_bf_readp (dict->dbf, ptr, &p);
299 hi = DICT_nodir(p)-1;
300 indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
306 /* string (Dict_char *) DICT_EOS terminated */
307 /* unsigned char length of information */
308 /* char * information */
311 info = (char*)p + indxp[-lo];
315 MatchWord *Rj0 = Rj + j *mc->fact;
316 MatchWord *Rj1 = Rj + (j+1)*mc->fact;
317 MatchWord *Rj_tmp = Rj + (j+2)*mc->fact;
320 memcpy (&ch, info+j*sizeof(Dict_char), sizeof(Dict_char));
322 if (pos+j > *max_pos)
327 if ((*userfunc)((char*) prefix,
328 info+(j+1)*sizeof(Dict_char), client))
332 if (pos+j >= init_pos)
336 move (mc, Rj1, Rj0, ch, dfa, Rj_tmp, range);
337 for (d = mc->n; --d >= 0; )
338 if (Rj1[range*mc->n + d])
343 for (d = mc->n; --d >= 0; )
344 if (Rj1[range*mc->n + d] & mc->match_mask[d])
353 MatchWord *Rj1 = Rj+ mc->fact;
354 MatchWord *Rj_tmp = Rj+2*mc->fact;
358 /* Dict_ptr subptr */
359 /* Dict_char sub char */
360 /* unsigned char length of information */
361 /* char * information */
362 info = (char*)p - indxp[-lo];
363 memcpy (&ch, info+sizeof(Dict_ptr), sizeof(Dict_char));
372 move (mc, Rj1, Rj, ch, dfa, Rj_tmp, range);
373 for (d = mc->n; --d >= 0; )
374 if (Rj1[range*mc->n + d])
379 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
381 for (d = mc->n; --d >= 0; )
382 if (Rj1[range*mc->n + d] & mc->match_mask[d])
384 prefix[pos+1] = DICT_EOS;
385 if ((*userfunc)((char*) prefix,
386 info+sizeof(Dict_ptr)+
387 sizeof(Dict_char), client))
392 memcpy (&subptr, info, sizeof(Dict_ptr));
395 if (dict_grep (dict, subptr, mc, Rj1, pos+1,
396 client, userfunc, prefix, dfa, max_pos,
399 dict_bf_readp (dict->dbf, ptr, &p);
400 indxp = (short*) ((char*) p+DICT_pagesize(dict)
410 int dict_lookup_grep (Dict dict, const char *pattern, int range, void *client,
411 int *max_pos, int init_pos,
412 int (*userfunc)(char *name, const char *info,
416 Dict_char prefix[MAX_LENGTH+1];
417 const char *this_pattern = pattern;
419 struct DFA *dfa = dfa_init();
422 logf (LOG_DEBUG, "dict_lookup_grep range=%d", range);
423 for (i = 0; pattern[i]; i++)
425 logf (LOG_DEBUG, " %3d %c", pattern[i],
426 (pattern[i] > ' ' && pattern[i] < 127) ? pattern[i] : '?');
429 dfa_set_cmap (dfa, dict->grep_cmap_data, dict->grep_cmap);
431 i = dfa_parse (dfa, &this_pattern);
432 if (i || *this_pattern)
439 mc = mk_MatchContext (dfa, range);
441 Rj = xcalloc ((MAX_LENGTH+1) * mc->n, sizeof(*Rj));
443 set_bit (mc, Rj, 0, 0);
444 for (d = 1; d<=mc->range; d++)
447 memcpy (Rj + mc->n * d, Rj + mc->n * (d-1), mc->n * sizeof(*Rj));
448 for (s = 0; s<dfa->no_states; s++)
450 if (get_bit (mc, Rj, d-1, s))
452 struct DFA_state *state = dfa->states[s];
453 int i = state->tran_no;
455 set_bit (mc, Rj, d, state->trans[i].to);
460 if (dict->head.last > 1)
461 i = dict_grep (dict, 1, mc, Rj, 0, client, userfunc, prefix,
462 dfa, max_pos, init_pos);
465 logf (LOG_DEBUG, "max_pos = %d", *max_pos);
468 rm_MatchContext (&mc);
472 void dict_grep_cmap (Dict dict, void *vp,
473 const char **(*cmap)(void *vp, const char **from, int len))
475 dict->grep_cmap = cmap;
476 dict->grep_cmap_data = vp;