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