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