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