1 /* $Id: lookgrep.c,v 1.26.2.2 2006-12-05 21:14:40 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include <yaz/xmalloc.h>
32 typedef unsigned MatchWord;
34 #define MAX_LENGTH 1024
37 int n; /* no of MatchWord needed */
38 int range; /* max no. of errors */
39 int fact; /* (range+1)*n */
40 MatchWord *match_mask; /* match_mask */
45 static INLINE void set_bit (MatchContext *mc, MatchWord *m, int ch, int state)
47 int off = state & (WORD_BITS-1);
48 int wno = state / WORD_BITS;
50 m[mc->n * ch + wno] |= 1<<off;
53 static INLINE MatchWord get_bit (MatchContext *mc, MatchWord *m, int ch,
56 int off = state & (WORD_BITS-1);
57 int wno = state / WORD_BITS;
59 return m[mc->n * ch + wno] & (1<<off);
62 static MatchContext *mk_MatchContext (struct DFA *dfa, int range)
64 MatchContext *mc = (MatchContext *) xmalloc (sizeof(*mc));
67 mc->n = (dfa->no_states+WORD_BITS) / WORD_BITS;
69 mc->fact = (range+1)*mc->n;
70 mc->match_mask = (MatchWord *) xcalloc (mc->n, sizeof(*mc->match_mask));
72 for (s = 0; s<dfa->no_states; s++)
73 if (dfa->states[s]->rule_no)
74 set_bit (mc, mc->match_mask, 0, s);
78 static void rm_MatchContext (MatchContext **mc)
80 xfree ((*mc)->match_mask);
85 static void mask_shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
86 struct DFA *dfa, int ch)
89 MatchWord *Rsrc_p = Rsrc, mask;
91 for (j = 0; j<mc->n; j++)
96 for (j = 0; j<WORD_BITS/4; j++)
102 struct DFA_state *state = dfa->states[s];
103 int i = state->tran_no;
105 if (ch >= state->trans[i].ch[0] &&
106 ch <= state->trans[i].ch[1])
107 set_bit (mc, Rdst, 0, state->trans[i].to);
111 struct DFA_state *state = dfa->states[s+1];
112 int i = state->tran_no;
114 if (ch >= state->trans[i].ch[0] &&
115 ch <= state->trans[i].ch[1])
116 set_bit (mc, Rdst, 0, state->trans[i].to);
120 struct DFA_state *state = dfa->states[s+2];
121 int i = state->tran_no;
123 if (ch >= state->trans[i].ch[0] &&
124 ch <= state->trans[i].ch[1])
125 set_bit (mc, Rdst, 0, state->trans[i].to);
129 struct DFA_state *state = dfa->states[s+3];
130 int i = state->tran_no;
132 if (ch >= state->trans[i].ch[0] &&
133 ch <= state->trans[i].ch[1])
134 set_bit (mc, Rdst, 0, state->trans[i].to);
138 if (s >= dfa->no_states)
145 static void shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
149 MatchWord *Rsrc_p = Rsrc, mask;
150 for (j = 0; j<mc->n; j++)
155 for (j = 0; j<WORD_BITS/4; j++)
161 struct DFA_state *state = dfa->states[s];
162 int i = state->tran_no;
164 set_bit (mc, Rdst, 0, state->trans[i].to);
168 struct DFA_state *state = dfa->states[s+1];
169 int i = state->tran_no;
171 set_bit (mc, Rdst, 0, state->trans[i].to);
175 struct DFA_state *state = dfa->states[s+2];
176 int i = state->tran_no;
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 set_bit (mc, Rdst, 0, state->trans[i].to);
189 if (s >= dfa->no_states)
196 static void or (MatchContext *mc, MatchWord *Rdst,
197 MatchWord *Rsrc1, MatchWord *Rsrc2)
200 for (i = 0; i<mc->n; i++)
201 Rdst[i] = Rsrc1[i] | Rsrc2[i];
204 static INLINE int move (MatchContext *mc, MatchWord *Rj1, MatchWord *Rj,
205 Dict_char ch, struct DFA *dfa, MatchWord *Rtmp,
209 MatchWord *Rtmp_2 = Rtmp + mc->n;
211 mask_shift (mc, Rj1, Rj, dfa, ch);
212 for (d = 1; d <= mc->range; d++)
214 or (mc, Rtmp, Rj, Rj1); /* 2,3 */
216 shift (mc, Rtmp_2, Rtmp, dfa);
218 mask_shift (mc, Rtmp, Rj+mc->n, dfa, ch); /* 1 */
220 or (mc, Rtmp, Rtmp_2, Rtmp); /* 1,2,3*/
224 or (mc, Rj1, Rtmp, Rj); /* 1,2,3,4 */
233 static int dict_grep (Dict dict, Dict_ptr ptr, MatchContext *mc,
234 MatchWord *Rj, int pos, void *client,
235 int (*userfunc)(char *, const char *, void *),
236 Dict_char *prefix, struct DFA *dfa,
237 int *max_pos, int init_pos)
244 dict_bf_readp (dict->dbf, ptr, &p);
246 hi = DICT_nodir(p)-1;
247 indxp = (short*) ((char*) p+DICT_bsize(p)-sizeof(short));
253 /* string (Dict_char *) DICT_EOS terminated */
254 /* unsigned char length of information */
255 /* char * information */
258 info = (char*)p + indxp[-lo];
262 MatchWord *Rj0 = Rj + j *mc->fact;
263 MatchWord *Rj1 = Rj + (j+1)*mc->fact;
264 MatchWord *Rj_tmp = Rj + (j+2)*mc->fact;
267 memcpy (&ch, info+j*sizeof(Dict_char), sizeof(Dict_char));
269 if (pos+j > *max_pos)
274 if ((*userfunc)((char*) prefix,
275 info+(j+1)*sizeof(Dict_char), client))
279 if (pos+j >= init_pos)
283 move (mc, Rj1, Rj0, ch, dfa, Rj_tmp, range);
284 for (d = mc->n; --d >= 0; )
285 if (Rj1[range*mc->n + d])
290 for (d = mc->n; --d >= 0; )
291 if (Rj1[range*mc->n + d] & mc->match_mask[d])
300 MatchWord *Rj1 = Rj+ mc->fact;
301 MatchWord *Rj_tmp = Rj+2*mc->fact;
305 /* Dict_ptr subptr */
306 /* Dict_char sub char */
307 /* unsigned char length of information */
308 /* char * information */
309 info = (char*)p - indxp[-lo];
310 memcpy (&ch, info+sizeof(Dict_ptr), sizeof(Dict_char));
319 move (mc, Rj1, Rj, ch, dfa, Rj_tmp, range);
320 for (d = mc->n; --d >= 0; )
321 if (Rj1[range*mc->n + d])
326 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
328 for (d = mc->n; --d >= 0; )
329 if (Rj1[range*mc->n + d] & mc->match_mask[d])
331 prefix[pos+1] = DICT_EOS;
332 if ((*userfunc)((char*) prefix,
333 info+sizeof(Dict_ptr)+
334 sizeof(Dict_char), client))
339 memcpy (&subptr, info, sizeof(Dict_ptr));
342 if (dict_grep (dict, subptr, mc, Rj1, pos+1,
343 client, userfunc, prefix, dfa, max_pos,
346 dict_bf_readp (dict->dbf, ptr, &p);
347 indxp = (short*) ((char*) p+DICT_bsize(p)-sizeof(short));
356 int dict_lookup_grep (Dict dict, const char *pattern, int range, void *client,
357 int *max_pos, int init_pos,
358 int (*userfunc)(char *name, const char *info,
362 Dict_char prefix[MAX_LENGTH+1];
363 const char *this_pattern = pattern;
365 struct DFA *dfa = dfa_init();
371 debug_dfa_followpos = 1;
375 yaz_log(YLOG_DEBUG, "dict_lookup_grep range=%d", range);
376 for (i = 0; pattern[i]; i++)
378 yaz_log(YLOG_DEBUG, " %3d %c", pattern[i],
379 (pattern[i] > ' ' && pattern[i] < 127) ? pattern[i] : '?');
382 dfa_set_cmap (dfa, dict->grep_cmap_data, dict->grep_cmap);
384 i = dfa_parse (dfa, &this_pattern);
385 if (i || *this_pattern)
387 yaz_log(YLOG_WARN, "dfa_parse fail=%d", i);
393 mc = mk_MatchContext (dfa, range);
395 Rj = (MatchWord *) xcalloc ((MAX_LENGTH+1) * mc->n, sizeof(*Rj));
397 set_bit (mc, Rj, 0, 0);
398 for (d = 1; d<=mc->range; d++)
401 memcpy (Rj + mc->n * d, Rj + mc->n * (d-1), mc->n * sizeof(*Rj));
402 for (s = 0; s<dfa->no_states; s++)
404 if (get_bit (mc, Rj, d-1, s))
406 struct DFA_state *state = dfa->states[s];
407 int i = state->tran_no;
409 set_bit (mc, Rj, d, state->trans[i].to);
415 i = dict_grep (dict, dict->head.root, mc, Rj, 0, client,
417 dfa, max_pos, init_pos);
420 yaz_log(YLOG_DEBUG, "max_pos = %d", *max_pos);
423 rm_MatchContext (&mc);
427 void dict_grep_cmap (Dict dict, void *vp,
428 const char **(*cmap)(void *vp, const char **from, int len))
430 dict->grep_cmap = cmap;
431 dict->grep_cmap_data = vp;