depend include change.
[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.3  1994-09-01 17:44:06  adam
8  * depend include change.
9  * CVS ----------------------------------------------------------------------
10  *
11  * Revision 1.2  1994/08/18  12:40:54  adam
12  * Some development of dictionary. Not finished at all!
13  *
14  * Revision 1.1  1994/08/16  16:26:47  adam
15  * Added dict.
16  *
17  */
18
19 #include <stdlib.h>
20 #include <string.h>
21 #include <stdio.h>
22
23 #include <dict.h>
24
25 char *prog;
26 Dict dict;
27
28 int main (int argc, char **argv)
29 {
30     const char *name = NULL;
31     const char *inputfile = NULL;
32     const char *base = NULL;
33     int rw = 0;
34     int infosize = 2;
35     int cache = 10;
36     int ret;
37     char *arg;
38     
39     prog = argv[0];
40     if (argc < 2)
41     {
42         fprintf (stderr, "usage:\n"
43                  "  %s [-s n] [-v n] [-i f] [-w] [-c n] base file\n",
44                  prog);
45         exit (1);
46     }
47     while ((ret = options ("s:v:i:wc:", argv, argc, &arg)) != -2)
48     {
49         if (ret == 0)
50         {
51             if (!base)
52                 base = arg;
53             else if (!name)
54                 name = arg;
55             else
56             {
57                 log (LOG_FATAL, "too many files specified\n");
58                 exit (1);
59             }
60         }
61         else if (ret == 'c')
62         {
63             cache = atoi(arg);
64             if (cache<2)
65                 cache = 2;
66         }
67         else if (ret == 'w')
68             rw = 1;
69         else if (ret == 'i')
70         {
71             inputfile = arg;
72             rw = 1;
73         }
74         else if (ret == 's')
75         {
76             infosize = atoi(arg);
77         }
78         else if (ret == 'v')
79         {
80             log_init (atoi(arg), prog, NULL);
81         }
82         else
83         {
84             log (LOG_FATAL, "unknown option");
85             exit (1);
86         }
87     }
88     if (!base || !name)
89     {
90         log (LOG_FATAL, "no base and/or dictionary specified");
91         exit (1);
92     }
93     common_resource = res_open (base);
94     if (!common_resource)
95     {
96         log (LOG_FATAL, "cannot open resource `%s'", base);
97         exit (1);
98     }
99     dict = dict_open (name, cache, rw);
100     if (!dict)
101     {
102         log (LOG_FATAL, "dict_open fail of `%s'", name);
103         exit (1);
104     }
105     if (inputfile)
106     {
107         FILE *ipf;
108         char ipf_buf[256];
109         char word[256];
110         int line = 1;
111         char infobytes[120];
112         memset (infobytes, 0, 120);
113
114         if (!(ipf = fopen(inputfile, "r")))
115         {
116             log (LOG_FATAL|LOG_ERRNO, "cannot open %s", inputfile);
117             exit (1);
118         }
119         
120         while (fgets (ipf_buf, 255, ipf))
121         {
122             if (sscanf (ipf_buf, "%s", word) == 1)
123             {
124                 sprintf (infobytes, "%d", line);
125                 dict_insert (dict, word, infosize, infobytes);
126             }
127             ++line;
128         }
129         fclose (ipf);
130     }
131     dict_close (dict);
132     res_close (common_resource);
133     return 0;
134 }