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