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