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