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