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