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