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