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