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