Updated WIN32 code specific sections. Changed header.
[idzebra-moved-to-github.git] / dict / lookgrep.c
1 /*
2  * Copyright (C) 1994-1999, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: lookgrep.c,v $
7  * Revision 1.22  1999-02-02 14:50:23  adam
8  * Updated WIN32 code specific sections. Changed header.
9  *
10  * Revision 1.21  1998/06/24 12:16:12  adam
11  * Support for relations on text operands. Open range support in
12  * DFA module (i.e. [-j], [g-]).
13  *
14  * Revision 1.20  1997/10/27 14:33:03  adam
15  * Moved towards generic character mapping depending on "structure"
16  * field in abstract syntax file. Fixed a few memory leaks. Fixed
17  * bug with negative integers when doing searches with relational
18  * operators.
19  *
20  * Revision 1.19  1997/09/18 08:59:18  adam
21  * Extra generic handle for the character mapping routines.
22  *
23  * Revision 1.18  1997/09/05 15:29:58  adam
24  * Changed prototype for chr_map_input - added const.
25  * Added support for C++, headers uses extern "C" for public definitions.
26  *
27  * Revision 1.17  1996/06/04 10:20:06  adam
28  * Added support for character mapping.
29  *
30  * Revision 1.16  1996/05/24  14:46:04  adam
31  * Added dict_grep_cmap function to define user-mapping in grep lookups.
32  *
33  * Revision 1.15  1996/03/20  09:35:18  adam
34  * Function dict_lookup_grep got extra parameter, init_pos, which marks
35  * from which position in pattern approximate pattern matching should occur.
36  *
37  * Revision 1.14  1996/02/02  13:43:51  adam
38  * The public functions simply use char instead of Dict_char to represent
39  * search strings. Dict_char is used internally only.
40  *
41  * Revision 1.13  1996/01/08  09:09:30  adam
42  * Function dfa_parse got 'const' string argument.
43  *
44  * Revision 1.12  1995/12/11  09:04:48  adam
45  * Bug fix: the lookup/scan/lookgrep didn't handle empty dictionary.
46  *
47  * Revision 1.11  1995/12/06  14:43:02  adam
48  * New function: dict_delete.
49  *
50  * Revision 1.10  1995/11/16  17:00:44  adam
51  * Changed stupid log.
52  *
53  * Revision 1.9  1995/10/27  13:58:09  adam
54  * Makes 'Database unavailable' diagnostic.
55  *
56  * Revision 1.8  1995/10/19  14:57:21  adam
57  * New feature: grep lookup saves length of longest prefix match.
58  *
59  * Revision 1.7  1995/10/17  18:01:22  adam
60  * Userfunc may return non-zero in which case the the grepping stops
61  * immediately.
62  *
63  * Revision 1.6  1995/10/09  16:18:32  adam
64  * Function dict_lookup_grep got extra client data parameter.
65  *
66  * Revision 1.5  1995/09/14  11:52:59  adam
67  * Grep handle function parameter info is const now.
68  *
69  * Revision 1.4  1995/01/24  16:01:02  adam
70  * Added -ansi to CFLAGS.
71  * Use new API of dfa module.
72  *
73  * Revision 1.3  1994/10/05  12:16:50  adam
74  * Pagesize is a resource now.
75  *
76  * Revision 1.2  1994/10/04  12:08:07  adam
77  * Some bug fixes and some optimizations.
78  *
79  * Revision 1.1  1994/10/03  17:23:04  adam
80  * First version of dictionary lookup with regular expressions and errors.
81  *
82  */
83
84 #include <stdlib.h>
85 #include <string.h>
86 #include <stdio.h>
87 #include <assert.h>
88
89 #include <dfa.h>
90 #include <dict.h>
91
92 typedef unsigned MatchWord;
93 #define WORD_BITS 32
94 #define MAX_LENGTH 1024
95
96 typedef struct {
97   int n;                 /* no of MatchWord needed */
98   int range;             /* max no. of errors */
99   int fact;              /* (range+1)*n */
100   MatchWord *match_mask; /* match_mask */
101 } MatchContext;
102
103 #define INLINE 
104
105 static INLINE void set_bit (MatchContext *mc, MatchWord *m, int ch, int state)
106 {
107     int off = state & (WORD_BITS-1);
108     int wno = state / WORD_BITS;
109   
110     m[mc->n * ch + wno] |= 1<<off;
111 }
112
113 static INLINE MatchWord get_bit (MatchContext *mc, MatchWord *m, int ch,
114                                  int state)
115 {
116     int off = state & (WORD_BITS-1);
117     int wno = state / WORD_BITS;
118
119     return m[mc->n * ch + wno] & (1<<off);
120 }
121
122 static MatchContext *mk_MatchContext (struct DFA *dfa, int range)
123 {
124     MatchContext *mc = xmalloc (sizeof(*mc));
125     int s;
126
127     mc->n = (dfa->no_states+WORD_BITS) / WORD_BITS;
128     mc->range = range;
129     mc->fact = (range+1)*mc->n;
130     mc->match_mask = xcalloc (mc->n, sizeof(*mc->match_mask));
131
132     for (s = 0; s<dfa->no_states; s++)
133         if (dfa->states[s]->rule_no)
134             set_bit (mc, mc->match_mask, 0, s);
135     return mc;
136 }
137
138 static void rm_MatchContext (MatchContext **mc)
139 {
140     xfree ((*mc)->match_mask);
141     xfree (*mc);
142     *mc = NULL;
143 }
144
145 static void mask_shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
146                    struct DFA *dfa, int ch)
147 {
148     int j, s = 0;
149     MatchWord *Rsrc_p = Rsrc, mask;
150
151     for (j = 0; j<mc->n; j++)
152         Rdst[j] = 0;
153     while (1)
154     {
155         mask = *Rsrc_p++;
156         for (j = 0; j<WORD_BITS/4; j++)
157         {
158             if (mask & 15)
159             {
160                 if (mask & 1)
161                 {
162                     struct DFA_state *state = dfa->states[s];
163                     int i = state->tran_no;
164                     while (--i >= 0)
165                         if (ch >= state->trans[i].ch[0] &&
166                             ch <= state->trans[i].ch[1])
167                             set_bit (mc, Rdst, 0, state->trans[i].to);
168                 }
169                 if (mask & 2)
170                 {
171                     struct DFA_state *state = dfa->states[s+1];
172                     int i = state->tran_no;
173                     while (--i >= 0)
174                         if (ch >= state->trans[i].ch[0] &&
175                             ch <= state->trans[i].ch[1])
176                             set_bit (mc, Rdst, 0, state->trans[i].to);
177                 }
178                 if (mask & 4)
179                 {
180                     struct DFA_state *state = dfa->states[s+2];
181                     int i = state->tran_no;
182                     while (--i >= 0)
183                         if (ch >= state->trans[i].ch[0] &&
184                             ch <= state->trans[i].ch[1])
185                             set_bit (mc, Rdst, 0, state->trans[i].to);
186                 }
187                 if (mask & 8)
188                 {
189                     struct DFA_state *state = dfa->states[s+3];
190                     int i = state->tran_no;
191                     while (--i >= 0)
192                         if (ch >= state->trans[i].ch[0] &&
193                             ch <= state->trans[i].ch[1])
194                             set_bit (mc, Rdst, 0, state->trans[i].to);
195                 }
196             }
197             s += 4;
198             if (s >= dfa->no_states)
199                 return;
200             mask >>= 4;
201         }
202     }
203 }
204
205 static void shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
206                    struct DFA *dfa)
207 {
208     int j, s = 0;
209     MatchWord *Rsrc_p = Rsrc, mask;
210     for (j = 0; j<mc->n; j++)
211         Rdst[j] = 0;
212     while (1)
213     {
214         mask = *Rsrc_p++;
215         for (j = 0; j<WORD_BITS/4; j++)
216         {
217             if (mask & 15)
218             {
219                 if (mask & 1)
220                 {
221                     struct DFA_state *state = dfa->states[s];
222                     int i = state->tran_no;
223                     while (--i >= 0)
224                         set_bit (mc, Rdst, 0, state->trans[i].to);
225                 }
226                 if (mask & 2)
227                 {
228                     struct DFA_state *state = dfa->states[s+1];
229                     int i = state->tran_no;
230                     while (--i >= 0)
231                         set_bit (mc, Rdst, 0, state->trans[i].to);
232                 }
233                 if (mask & 4)
234                 {
235                     struct DFA_state *state = dfa->states[s+2];
236                     int i = state->tran_no;
237                     while (--i >= 0)
238                         set_bit (mc, Rdst, 0, state->trans[i].to);
239                 }
240                 if (mask & 8)
241                 {
242                     struct DFA_state *state = dfa->states[s+3];
243                     int i = state->tran_no;
244                     while (--i >= 0)
245                         set_bit (mc, Rdst, 0, state->trans[i].to);
246                 }
247             }
248             s += 4;
249             if (s >= dfa->no_states)
250                 return;
251             mask >>= 4;
252         }
253     }
254 }
255
256 static void or (MatchContext *mc, MatchWord *Rdst,
257                 MatchWord *Rsrc1, MatchWord *Rsrc2)
258 {
259     int i;
260     for (i = 0; i<mc->n; i++)
261         Rdst[i] = Rsrc1[i] | Rsrc2[i];
262 }
263
264 static INLINE int move (MatchContext *mc, MatchWord *Rj1, MatchWord *Rj,
265                         Dict_char ch, struct DFA *dfa, MatchWord *Rtmp,
266                         int range)
267 {
268     int d;
269     MatchWord *Rtmp_2 = Rtmp + mc->n;
270
271     mask_shift (mc, Rj1, Rj, dfa, ch);
272     for (d = 1; d <= mc->range; d++)
273     {
274         or (mc, Rtmp, Rj, Rj1);                         /* 2,3 */
275         
276         shift (mc, Rtmp_2, Rtmp, dfa);
277
278         mask_shift (mc, Rtmp, Rj+mc->n, dfa, ch);      /* 1 */
279                 
280         or (mc, Rtmp, Rtmp_2, Rtmp);                    /* 1,2,3*/
281
282         Rj1 += mc->n;
283         
284         or (mc, Rj1, Rtmp, Rj);                         /* 1,2,3,4 */
285
286         Rj += mc->n;
287     }
288     return 1;
289
290 }
291
292
293 static int dict_grep (Dict dict, Dict_ptr ptr, MatchContext *mc,
294                       MatchWord *Rj, int pos, void *client,
295                       int (*userfunc)(char *, const char *, void *),
296                       Dict_char *prefix, struct DFA *dfa,
297                       int *max_pos, int init_pos)
298 {
299     int lo, hi, d;
300     void *p;
301     short *indxp;
302     char *info;
303
304     dict_bf_readp (dict->dbf, ptr, &p);
305     lo = 0;
306     hi = DICT_nodir(p)-1;
307     indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));    
308
309     while (lo <= hi)
310     {
311         if (indxp[-lo] > 0)
312         {
313             /* string (Dict_char *) DICT_EOS terminated */
314             /* unsigned char        length of information */
315             /* char *               information */
316             int j;
317             int was_match = 0;
318             info = (char*)p + indxp[-lo];
319             for (j=0; ; j++)
320             {
321                 Dict_char ch;
322                 MatchWord *Rj0 =    Rj + j    *mc->fact;
323                 MatchWord *Rj1 =    Rj + (j+1)*mc->fact;
324                 MatchWord *Rj_tmp = Rj + (j+2)*mc->fact;
325                 int range;
326
327                 memcpy (&ch, info+j*sizeof(Dict_char), sizeof(Dict_char));
328                 prefix[pos+j] = ch;
329                 if (pos+j > *max_pos)
330                     *max_pos = pos+j;
331                 if (ch == DICT_EOS)
332                 {
333                     if (was_match)
334                         if ((*userfunc)((char*) prefix,
335                                         info+(j+1)*sizeof(Dict_char), client))
336                             return 1;
337                     break;
338                 }
339                 if (pos+j >= init_pos)
340                     range = mc->range;
341                 else
342                     range = 0;
343                 move (mc, Rj1, Rj0, ch, dfa, Rj_tmp, range);
344                 for (d = mc->n; --d >= 0; )
345                     if (Rj1[range*mc->n + d])
346                         break;
347                 if (d < 0)
348                     break;
349                 was_match = 0;
350                 for (d = mc->n; --d >= 0; )
351                     if (Rj1[range*mc->n + d] & mc->match_mask[d])
352                     {
353                         was_match = 1;
354                         break;
355                     }
356             }
357         }
358         else
359         {
360             MatchWord *Rj1 =    Rj+  mc->fact;
361             MatchWord *Rj_tmp = Rj+2*mc->fact;
362             Dict_char ch;
363             int range;
364
365             /* Dict_ptr             subptr */
366             /* Dict_char            sub char */
367             /* unsigned char        length of information */
368             /* char *               information */
369             info = (char*)p - indxp[-lo];
370             memcpy (&ch, info+sizeof(Dict_ptr), sizeof(Dict_char));
371             prefix[pos] = ch;
372             
373             if (pos > *max_pos)
374                 *max_pos = pos;
375             if (pos >= init_pos)
376                 range = mc->range;
377             else
378                 range = 0;
379             move (mc, Rj1, Rj, ch, dfa, Rj_tmp, range);
380             for (d = mc->n; --d >= 0; )
381                 if (Rj1[range*mc->n + d])
382                     break;
383             if (d >= 0)
384             {
385                 Dict_ptr subptr;
386                 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
387                 {
388                     for (d = mc->n; --d >= 0; )
389                         if (Rj1[range*mc->n + d] & mc->match_mask[d])
390                         {
391                             prefix[pos+1] = DICT_EOS;
392                             if ((*userfunc)((char*) prefix,
393                                             info+sizeof(Dict_ptr)+
394                                             sizeof(Dict_char), client))
395                                 return 1;
396                             break;
397                         }
398                 }
399                 memcpy (&subptr, info, sizeof(Dict_ptr));
400                 if (subptr)
401                 {
402                     if (dict_grep (dict, subptr, mc, Rj1, pos+1,
403                                    client, userfunc, prefix, dfa, max_pos,
404                                    init_pos))
405                         return 1;
406                     dict_bf_readp (dict->dbf, ptr, &p);
407                     indxp = (short*) ((char*) p+DICT_pagesize(dict)
408                                       -sizeof(short));
409                 }
410             }
411         }
412         lo++;
413     }
414     return 0;
415 }
416
417 int dict_lookup_grep (Dict dict, const char *pattern, int range, void *client,
418                       int *max_pos, int init_pos,
419                       int (*userfunc)(char *name, const char *info,
420                                       void *client))
421 {
422     MatchWord *Rj;
423     Dict_char prefix[MAX_LENGTH+1];
424     const char *this_pattern = pattern;
425     MatchContext *mc;
426     struct DFA *dfa = dfa_init();
427     int i, d;
428
429 #if 0
430     debug_dfa_trav = 1;
431     debug_dfa_tran = 1;
432     debug_dfa_followpos = 1;
433     dfa_verbose = 1;
434 #endif
435
436     logf (LOG_DEBUG, "dict_lookup_grep range=%d", range);
437     for (i = 0; pattern[i]; i++)
438     {
439         logf (LOG_DEBUG, " %3d  %c", pattern[i],
440               (pattern[i] > ' ' && pattern[i] < 127) ? pattern[i] : '?');
441     }
442    
443     dfa_set_cmap (dfa, dict->grep_cmap_data, dict->grep_cmap);
444
445     i = dfa_parse (dfa, &this_pattern);
446     if (i || *this_pattern)
447     {
448         dfa_delete (&dfa);
449         return -1;
450     }
451     dfa_mkstate (dfa);
452
453     mc = mk_MatchContext (dfa, range);
454
455     Rj = xcalloc ((MAX_LENGTH+1) * mc->n, sizeof(*Rj));
456
457     set_bit (mc, Rj, 0, 0);
458     for (d = 1; d<=mc->range; d++)
459     {
460         int s;
461         memcpy (Rj + mc->n * d, Rj + mc->n * (d-1), mc->n * sizeof(*Rj));
462         for (s = 0; s<dfa->no_states; s++)
463         {
464             if (get_bit (mc, Rj, d-1, s))
465             {
466                 struct DFA_state *state = dfa->states[s];
467                 int i = state->tran_no;
468                 while (--i >= 0)
469                     set_bit (mc, Rj, d, state->trans[i].to);
470             }
471         }
472     }
473     *max_pos = 0;
474     if (dict->head.last > 1)
475         i = dict_grep (dict, 1, mc, Rj, 0, client, userfunc, prefix,
476                        dfa, max_pos, init_pos);
477     else
478         i = 0;
479     logf (LOG_DEBUG, "max_pos = %d", *max_pos);
480     dfa_delete (&dfa);
481     xfree (Rj);
482     rm_MatchContext (&mc);
483     return i;
484 }
485
486 void dict_grep_cmap (Dict dict, void *vp,
487                      const char **(*cmap)(void *vp, const char **from, int len))
488 {
489     dict->grep_cmap = cmap;
490     dict->grep_cmap_data = vp;
491 }