Some bug fixes and some optimizations.
[idzebra-moved-to-github.git] / dict / dicttest.c
1 /*
2  * Copyright (C) 1994, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: dicttest.c,v $
7  * Revision 1.13  1994-10-04 12:08:05  adam
8  * Some bug fixes and some optimizations.
9  *
10  * Revision 1.12  1994/10/03  17:23:03  adam
11  * First version of dictionary lookup with regular expressions and errors.
12  *
13  * Revision 1.11  1994/09/28  13:07:09  adam
14  * Use log_mask_str now.
15  *
16  * Revision 1.10  1994/09/26  10:17:24  adam
17  * Minor changes.
18  *
19  * Revision 1.9  1994/09/22  14:43:56  adam
20  * First functional version of lookup with error correction. A 'range'
21  * specified the maximum number of insertions+deletions+substitutions.
22  *
23  * Revision 1.8  1994/09/22  10:43:44  adam
24  * Two versions of depend. Type 1 is the tail-type compatible with
25  * all make programs. Type 2 is the GNU make with include facility.
26  * Type 2 is default. depend rule chooses current rule.
27  *
28  * Revision 1.7  1994/09/19  16:34:26  adam
29  * Depend rule change. Minor changes in dicttest.c
30  *
31  * Revision 1.6  1994/09/16  15:39:12  adam
32  * Initial code of lookup - not tested yet.
33  *
34  * Revision 1.5  1994/09/06  13:05:14  adam
35  * Further development of insertion. Some special cases are
36  * not properly handled yet! assert(0) are put here. The
37  * binary search in each page definitely reduce usr CPU.
38  *
39  * Revision 1.4  1994/09/01  17:49:37  adam
40  * Removed stupid line. Work on insertion in dictionary. Not finished yet.
41  *
42  * Revision 1.3  1994/09/01  17:44:06  adam
43  * depend include change.
44  *
45  * Revision 1.2  1994/08/18  12:40:54  adam
46  * Some development of dictionary. Not finished at all!
47  *
48  * Revision 1.1  1994/08/16  16:26:47  adam
49  * Added dict.
50  *
51  */
52
53 #include <stdlib.h>
54 #include <string.h>
55 #include <stdio.h>
56 #include <ctype.h>
57
58 #include <dict.h>
59
60 char *prog;
61 static Dict dict;
62
63 static int look_hits;
64
65 static int grep_handle (Dict_char *name, char *info)
66 {
67     look_hits++;
68     printf ("%s\n", name);
69     return 0;
70 }
71
72 int main (int argc, char **argv)
73 {
74     const char *name = NULL;
75     const char *inputfile = NULL;
76     const char *base = NULL;
77     int range = -1;
78     int rw = 0;
79     int infosize = 4;
80     int cache = 10;
81     int ret;
82     int unique = 0;
83     char *grep_pattern = NULL;
84     char *arg;
85     int no_of_iterations = 0;
86     int no_of_new = 0, no_of_same = 0, no_of_change = 0;
87     int no_of_hits = 0, no_of_misses = 0;
88
89     
90     prog = argv[0];
91     if (argc < 2)
92     {
93         fprintf (stderr, "usage:\n "
94                  " %s [-r n] [-u] [-g pat] [-s n] [-v n] [-i f] [-w] [-c n]"
95                  " base file\n",
96                  prog);
97         exit (1);
98     }
99     while ((ret = options ("r:ug:s:v:i:wc:", argv, argc, &arg)) != -2)
100     {
101         if (ret == 0)
102         {
103             if (!base)
104                 base = arg;
105             else if (!name)
106                 name = arg;
107             else
108             {
109                 log (LOG_FATAL, "too many files specified\n");
110                 exit (1);
111             }
112         }
113         else if (ret == 'g')
114         {
115             grep_pattern = arg;
116         }
117         else if (ret == 'r')
118         {
119             range = atoi (arg);
120         }
121         else if (ret == 'u')
122         {
123             unique = 1;
124         }
125         else if (ret == 'c')
126         {
127             cache = atoi(arg);
128             if (cache<2)
129                 cache = 2;
130         }
131         else if (ret == 'w')
132             rw = 1;
133         else if (ret == 'i')
134             inputfile = arg;
135         else if (ret == 's')
136         {
137             infosize = atoi(arg);
138         }
139         else if (ret == 'v')
140         {
141             log_init (log_mask_str(arg), prog, NULL);
142         }
143         else
144         {
145             log (LOG_FATAL, "unknown option");
146             exit (1);
147         }
148     }
149     if (!base || !name)
150     {
151         log (LOG_FATAL, "no base and/or dictionary specified");
152         exit (1);
153     }
154     common_resource = res_open (base);
155     if (!common_resource)
156     {
157         log (LOG_FATAL, "cannot open resource `%s'", base);
158         exit (1);
159     }
160     dict = dict_open (name, cache, rw);
161     if (!dict)
162     {
163         log (LOG_FATAL, "dict_open fail of `%s'", name);
164         exit (1);
165     }
166     if (inputfile)
167     {
168         FILE *ipf;
169         char ipf_buf[1024];
170         int line = 1;
171         char infobytes[120];
172         memset (infobytes, 0, 120);
173
174         if (!(ipf = fopen(inputfile, "r")))
175         {
176             log (LOG_FATAL|LOG_ERRNO, "cannot open %s", inputfile);
177             exit (1);
178         }
179         
180         while (fgets (ipf_buf, 1023, ipf))
181         {
182             char *ipf_ptr = ipf_buf;
183             sprintf (infobytes, "%d", line);
184             for (;*ipf_ptr && *ipf_ptr != '\n';ipf_ptr++)
185             {
186                 if (isalpha(*ipf_ptr) || *ipf_ptr == '_')
187                 {
188                     int i = 1;
189                     while (ipf_ptr[i] && (isalnum(ipf_ptr[i]) ||
190                                           ipf_ptr[i] == '_'))
191                         i++;
192                     if (ipf_ptr[i])
193                         ipf_ptr[i++] = '\0';
194                     if (rw)
195                     {
196                         switch(dict_insert (dict, ipf_ptr,
197                                             infosize, infobytes))
198                         {
199                         case 0:
200                             no_of_new++;
201                             break;
202                         case 1:
203                             no_of_change++;
204                         if (unique)
205                             log (LOG_LOG, "%s change\n", ipf_ptr);
206                             break;
207                         case 2:
208                             if (unique)
209                                 log (LOG_LOG, "%s duplicate\n", ipf_ptr);
210                             no_of_same++;
211                             break;
212                         }
213                     }
214                     else if(range < 0)
215                     {
216                         char *cp;
217
218                         cp = dict_lookup (dict, ipf_ptr);
219                         if (cp && *cp)
220                             no_of_hits++;
221                         else
222                             no_of_misses++;
223                     }
224                     else
225                     {
226                         look_hits = 0;
227                         dict_lookup_grep (dict, ipf_ptr, range, grep_handle);
228                         if (look_hits)
229                             no_of_hits++;
230                         else
231                             no_of_misses++;
232                     }
233                     ++no_of_iterations;
234                     ipf_ptr += (i-1);
235                 }
236             }
237             ++line;
238         }
239         fclose (ipf);
240     }
241     if (grep_pattern)
242     {
243         if (range < 0)
244             range = 0;
245         log (LOG_LOG, "Grepping '%s'", grep_pattern);
246         dict_lookup_grep (dict, grep_pattern, range, grep_handle);
247     }
248     if (rw)
249     {
250         log (LOG_LOG, "Insertions.... %d", no_of_iterations);
251         log (LOG_LOG, "No of new..... %d", no_of_new);
252         log (LOG_LOG, "No of change.. %d", no_of_change);
253         log (LOG_LOG, "No of same.... %d", no_of_same);
254     }
255     else
256     {
257         log (LOG_LOG, "Lookups....... %d", no_of_iterations);
258         log (LOG_LOG, "No of hits.... %d", no_of_hits);
259         log (LOG_LOG, "No of misses.. %d", no_of_misses);
260     }
261     dict_close (dict);
262     res_close (common_resource);
263     return 0;
264 }