Changed stupid log.
[idzebra-moved-to-github.git] / dict / lookgrep.c
1 /*
2  * Copyright (C) 1994, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: lookgrep.c,v $
7  * Revision 1.10  1995-11-16 17:00:44  adam
8  * Changed stupid log.
9  *
10  * Revision 1.9  1995/10/27  13:58:09  adam
11  * Makes 'Database unavailable' diagnostic.
12  *
13  * Revision 1.8  1995/10/19  14:57:21  adam
14  * New feature: grep lookup saves length of longest prefix match.
15  *
16  * Revision 1.7  1995/10/17  18:01:22  adam
17  * Userfunc may return non-zero in which case the the grepping stops
18  * immediately.
19  *
20  * Revision 1.6  1995/10/09  16:18:32  adam
21  * Function dict_lookup_grep got extra client data parameter.
22  *
23  * Revision 1.5  1995/09/14  11:52:59  adam
24  * Grep handle function parameter info is const now.
25  *
26  * Revision 1.4  1995/01/24  16:01:02  adam
27  * Added -ansi to CFLAGS.
28  * Use new API of dfa module.
29  *
30  * Revision 1.3  1994/10/05  12:16:50  adam
31  * Pagesize is a resource now.
32  *
33  * Revision 1.2  1994/10/04  12:08:07  adam
34  * Some bug fixes and some optimizations.
35  *
36  * Revision 1.1  1994/10/03  17:23:04  adam
37  * First version of dictionary lookup with regular expressions and errors.
38  *
39  */
40
41 #include <stdlib.h>
42 #include <string.h>
43 #include <stdio.h>
44 #include <assert.h>
45
46 #include <dfa.h>
47 #include <dict.h>
48
49 typedef unsigned MatchWord;
50 #define WORD_BITS 32
51 #define MAX_LENGTH 1024
52
53 typedef struct {
54   int n;                 /* no of MatchWord needed */
55   int range;             /* max no. of errors */
56   int fact;              /* (range+1)*n */
57   MatchWord *match_mask; /* match_mask */
58 } MatchContext;
59
60 #define INLINE 
61
62 static INLINE void set_bit (MatchContext *mc, MatchWord *m, int ch, int state)
63 {
64     int off = state & (WORD_BITS-1);
65     int wno = state / WORD_BITS;
66   
67     m[mc->n * ch + wno] |= 1<<off;
68 }
69
70 static INLINE MatchWord get_bit (MatchContext *mc, MatchWord *m, int ch,
71                                  int state)
72 {
73     int off = state & (WORD_BITS-1);
74     int wno = state / WORD_BITS;
75
76     return m[mc->n * ch + wno] & (1<<off);
77 }
78
79 static MatchContext *mk_MatchContext (struct DFA *dfa, int range)
80 {
81     MatchContext *mc = xmalloc (sizeof(*mc));
82     int s;
83
84     mc->n = (dfa->no_states+WORD_BITS) / WORD_BITS;
85     mc->range = range;
86     mc->fact = (range+1)*mc->n;
87     mc->match_mask = xcalloc (mc->n, sizeof(*mc->match_mask));
88
89     for (s = 0; s<dfa->no_states; s++)
90         if (dfa->states[s]->rule_no)
91             set_bit (mc, mc->match_mask, 0, s);
92     return mc;
93 }
94
95 static void rm_MatchContext (MatchContext **mc)
96 {
97     xfree ((*mc)->match_mask);
98     xfree (*mc);
99     *mc = NULL;
100 }
101
102 static void mask_shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
103                    struct DFA *dfa, int ch)
104 {
105     int j, s = 0;
106     MatchWord *Rsrc_p = Rsrc, mask;
107
108     for (j = 0; j<mc->n; j++)
109         Rdst[j] = 0;
110     while (1)
111     {
112         mask = *Rsrc_p++;
113         for (j = 0; j<WORD_BITS/4; j++)
114         {
115             if (mask & 15)
116             {
117                 if (mask & 1)
118                 {
119                     struct DFA_state *state = dfa->states[s];
120                     int i = state->tran_no;
121                     while (--i >= 0)
122                         if (ch >= state->trans[i].ch[0] &&
123                             ch <= state->trans[i].ch[1])
124                             set_bit (mc, Rdst, 0, state->trans[i].to);
125                 }
126                 if (mask & 2)
127                 {
128                     struct DFA_state *state = dfa->states[s+1];
129                     int i = state->tran_no;
130                     while (--i >= 0)
131                         if (ch >= state->trans[i].ch[0] &&
132                             ch <= state->trans[i].ch[1])
133                             set_bit (mc, Rdst, 0, state->trans[i].to);
134                 }
135                 if (mask & 4)
136                 {
137                     struct DFA_state *state = dfa->states[s+2];
138                     int i = state->tran_no;
139                     while (--i >= 0)
140                         if (ch >= state->trans[i].ch[0] &&
141                             ch <= state->trans[i].ch[1])
142                             set_bit (mc, Rdst, 0, state->trans[i].to);
143                 }
144                 if (mask & 8)
145                 {
146                     struct DFA_state *state = dfa->states[s+3];
147                     int i = state->tran_no;
148                     while (--i >= 0)
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);
152                 }
153             }
154             s += 4;
155             if (s >= dfa->no_states)
156                 return;
157             mask >>= 4;
158         }
159     }
160 }
161
162 static void shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
163                    struct DFA *dfa)
164 {
165     int j, s = 0;
166     MatchWord *Rsrc_p = Rsrc, mask;
167     for (j = 0; j<mc->n; j++)
168         Rdst[j] = 0;
169     while (1)
170     {
171         mask = *Rsrc_p++;
172         for (j = 0; j<WORD_BITS/4; j++)
173         {
174             if (mask & 15)
175             {
176                 if (mask & 1)
177                 {
178                     struct DFA_state *state = dfa->states[s];
179                     int i = state->tran_no;
180                     while (--i >= 0)
181                         set_bit (mc, Rdst, 0, state->trans[i].to);
182                 }
183                 if (mask & 2)
184                 {
185                     struct DFA_state *state = dfa->states[s+1];
186                     int i = state->tran_no;
187                     while (--i >= 0)
188                         set_bit (mc, Rdst, 0, state->trans[i].to);
189                 }
190                 if (mask & 4)
191                 {
192                     struct DFA_state *state = dfa->states[s+2];
193                     int i = state->tran_no;
194                     while (--i >= 0)
195                         set_bit (mc, Rdst, 0, state->trans[i].to);
196                 }
197                 if (mask & 8)
198                 {
199                     struct DFA_state *state = dfa->states[s+3];
200                     int i = state->tran_no;
201                     while (--i >= 0)
202                         set_bit (mc, Rdst, 0, state->trans[i].to);
203                 }
204             }
205             s += 4;
206             if (s >= dfa->no_states)
207                 return;
208             mask >>= 4;
209         }
210     }
211 }
212
213 static void or (MatchContext *mc, MatchWord *Rdst,
214                 MatchWord *Rsrc1, MatchWord *Rsrc2)
215 {
216     int i;
217     for (i = 0; i<mc->n; i++)
218         Rdst[i] = Rsrc1[i] | Rsrc2[i];
219 }
220
221 static INLINE int move (MatchContext *mc, MatchWord *Rj1, MatchWord *Rj,
222                  Dict_char ch, struct DFA *dfa, MatchWord *Rtmp)
223 {
224     int d;
225     MatchWord *Rtmp_2 = Rtmp + mc->n;
226
227     mask_shift (mc, Rj1, Rj, dfa, ch);
228     for (d = 1; d <= mc->range; d++)
229     {
230         or (mc, Rtmp, Rj, Rj1);                         /* 2,3 */
231         
232         shift (mc, Rtmp_2, Rtmp, dfa);
233
234         mask_shift (mc, Rtmp, Rj+mc->n, dfa, ch);      /* 1 */
235                 
236         or (mc, Rtmp, Rtmp_2, Rtmp);                    /* 1,2,3*/
237
238         Rj1 += mc->n;
239         
240         or (mc, Rj1, Rtmp, Rj);                         /* 1,2,3,4 */
241
242         Rj += mc->n;
243     }
244     return 1;
245
246 }
247
248
249 static int dict_grep (Dict dict, Dict_ptr ptr, MatchContext *mc,
250                       MatchWord *Rj, int pos, void *client,
251                       int (*userfunc)(Dict_char *, const char *, void *),
252                       Dict_char *prefix, struct DFA *dfa,
253                       int *max_pos)
254 {
255     int lo, hi, d;
256     void *p;
257     short *indxp;
258     char *info;
259
260     dict_bf_readp (dict->dbf, ptr, &p);
261     lo = 0;
262     hi = DICT_nodir(p)-1;
263     indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));    
264
265     while (lo <= hi)
266     {
267         if (indxp[-lo] > 0)
268         {
269             /* string (Dict_char *) DICT_EOS terminated */
270             /* unsigned char        length of information */
271             /* char *               information */
272             int j;
273             int was_match = 0;
274             info = (char*)p + indxp[-lo];
275             for (j=0; ; j++)
276             {
277                 Dict_char ch;
278                 MatchWord *Rj0 =    Rj + j    *mc->fact;
279                 MatchWord *Rj1 =    Rj + (j+1)*mc->fact;
280                 MatchWord *Rj_tmp = Rj + (j+2)*mc->fact;
281
282                 memcpy (&ch, info+j*sizeof(Dict_char), sizeof(Dict_char));
283                 prefix[pos+j] = ch;
284                 if (pos+j > *max_pos)
285                     *max_pos = pos+j;
286                 if (ch == DICT_EOS)
287                 {
288                     if (was_match)
289                         if ((*userfunc)(prefix, info+(j+1)*sizeof(Dict_char),
290                                         client))
291                             return 1;
292                     break;
293                 }
294                 move (mc, Rj1, Rj0, ch, dfa, Rj_tmp);
295                 for (d = mc->n; --d >= 0; )
296                     if (Rj1[mc->range*mc->n + d])
297                         break;
298                 if (d < 0)
299                     break;
300                 was_match = 0;
301                 for (d = mc->n; --d >= 0; )
302                     if (Rj1[mc->range*mc->n + d] & mc->match_mask[d])
303                     {
304                         was_match = 1;
305                         break;
306                     }
307             }
308         }
309         else
310         {
311             MatchWord *Rj1 =    Rj+  mc->fact;
312             MatchWord *Rj_tmp = Rj+2*mc->fact;
313             Dict_char ch;
314
315             /* Dict_ptr             subptr */
316             /* Dict_char            sub char */
317             /* unsigned char        length of information */
318             /* char *               information */
319             info = (char*)p - indxp[-lo];
320             memcpy (&ch, info+sizeof(Dict_ptr), sizeof(Dict_char));
321             prefix[pos] = ch;
322             
323             if (pos > *max_pos)
324                 *max_pos = pos;
325             move (mc, Rj1, Rj, ch, dfa, Rj_tmp);
326             for (d = mc->n; --d >= 0; )
327                 if (Rj1[mc->range*mc->n + d])
328                     break;
329             if (d >= 0)
330             {
331                 Dict_ptr subptr;
332                 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
333                 {
334                     for (d = mc->n; --d >= 0; )
335                         if (Rj1[mc->range*mc->n + d] & mc->match_mask[d])
336                         {
337                             prefix[pos+1] = DICT_EOS;
338                             if ((*userfunc)(prefix, info+sizeof(Dict_ptr)+
339                                             sizeof(Dict_char), client))
340                                 return 1;
341                             break;
342                         }
343                 }
344                 memcpy (&subptr, info, sizeof(Dict_ptr));
345                 if (subptr)
346                 {
347                     if (dict_grep (dict, subptr, mc, Rj1, pos+1,
348                                    client, userfunc, prefix, dfa, max_pos))
349                         return 1;
350                     dict_bf_readp (dict->dbf, ptr, &p);
351                     indxp = (short*) ((char*) p+DICT_pagesize(dict)
352                                       -sizeof(short));
353                 }
354             }
355         }
356         lo++;
357     }
358     return 0;
359 }
360
361 int dict_lookup_grep (Dict dict, Dict_char *pattern, int range, void *client,
362                       int *max_pos,
363                       int (*userfunc)(Dict_char *name, const char *info,
364                                       void *client))
365 {
366     MatchWord *Rj;
367     Dict_char prefix[MAX_LENGTH+1];
368     char *this_pattern = pattern;
369     MatchContext *mc;
370     struct DFA *dfa = dfa_init();
371     int i, d;
372
373     i = dfa_parse (dfa, &this_pattern);
374     if (i || *this_pattern)
375     {
376         dfa_delete (&dfa);
377         return -1;
378     }
379     dfa_mkstate (dfa);
380
381     mc = mk_MatchContext (dfa, range);
382
383     Rj = xcalloc ((MAX_LENGTH+1) * mc->n, sizeof(*Rj));
384
385     set_bit (mc, Rj, 0, 0);
386     for (d = 1; d<=mc->range; d++)
387     {
388         int s;
389         memcpy (Rj + mc->n * d, Rj + mc->n * (d-1), mc->n * sizeof(*Rj));
390         for (s = 0; s<dfa->no_states; s++)
391         {
392             if (get_bit (mc, Rj, d-1, s))
393             {
394                 struct DFA_state *state = dfa->states[s];
395                 int i = state->tran_no;
396                 while (--i >= 0)
397                     set_bit (mc, Rj, d, state->trans[i].to);
398             }
399         }
400     }
401     *max_pos = 0;
402     i = dict_grep (dict, 1, mc, Rj, 0, client, userfunc, prefix, dfa,
403                    max_pos);
404     logf (LOG_DEBUG, "max_pos = %d", *max_pos);
405     dfa_delete (&dfa);
406     xfree (Rj);
407     rm_MatchContext (&mc);
408     return i;
409 }