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