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