Debian Codegraph @

libutil/kazlib/drivers/dict-main.c

raw symbols SCIP
1 #include <stdio.h>
2 #include <string.h>
3 #include <ctype.h>
4 #include <stdarg.h>
5
6 typedef char input_t[256];
7
8 static int tokenize(char *string, ...)
9 {
10 char **tokptr;
11 va_list arglist;
12 int tokcount = 0;
13
14 va_start(arglist, string);
15 tokptr = va_arg(arglist, char **);
16 while (tokptr) {
17 while (*string && isspace((unsigned char) *string))
18 string++;
19 if (!*string)
20 break;
21 *tokptr = string;
22 while (*string && !isspace((unsigned char) *string))
23 string++;
24 tokptr = va_arg(arglist, char **);
25 tokcount++;
26 if (!*string)
27 break;
28 *string++ = 0;
29 }
30 va_end(arglist);
31
32 return tokcount;
33 }
34
35 static int comparef(const void *key1, const void *key2)
36 {
37 return strcmp(key1, key2);
38 }
39
40 static char *dupstring(char *str)
41 {
42 int sz = strlen(str) + 1;
43 char *new = malloc(sz);
44 if (new)
45 memcpy(new, str, sz);
46 return new;
47 }
48
49 static dnode_t *new_node(void *c)
50 {
51 static dnode_t few[5];
52 static int count;
53
54 if (count < 5)
55 return few + count++;
56
57 return NULL;
58 }
59
60 static void del_node(dnode_t *n, void *c)
61 {
62 }
63
64 static int prompt = 0;
65
66 static void construct(dict_t *d)
67 {
68 input_t in;
69 int done = 0;
70 dict_load_t dl;
71 dnode_t *dn;
72 char *tok1, *tok2, *val;
73 const char *key;
74 char *help =
75 "p turn prompt on\n"
76 "q finish construction\n"
77 "a <key> <val> add new entry\n";
78
79 if (!dict_isempty(d))
80 puts("warning: dictionary not empty!");
81
82 dict_load_begin(&dl, d);
83
84 while (!done) {
85 if (prompt)
86 putchar('>');
87 fflush(stdout);
88
89 if (!fgets(in, sizeof(input_t), stdin))
90 break;
91
92 switch (in[0]) {
93 case '?':
94 puts(help);
95 break;
96 case 'p':
97 prompt = 1;
98 break;
99 case 'q':
100 done = 1;
101 break;
102 case 'a':
103 if (tokenize(in+1, &tok1, &tok2, (char **) 0) != 2) {
104 puts("what?");
105 break;
106 }
107 key = dupstring(tok1);
108 val = dupstring(tok2);
109 dn = dnode_create(val);
110
111 if (!key || !val || !dn) {
112 puts("out of memory");
113 free((void *) key);
114 free(val);
115 if (dn)
116 dnode_destroy(dn);
117 }
118
119 dict_load_next(&dl, dn, key);
120 break;
121 default:
122 putchar('?');
123 putchar('\n');
124 break;
125 }
126 }
127
128 dict_load_end(&dl);
129 }
130
131 int main(void)
132 {
133 input_t in;
134 dict_t darray[10];
135 dict_t *d = &darray[0];
136 dnode_t *dn;
137 int i;
138 char *tok1, *tok2, *val;
139 const char *key;
140
141 char *help =
142 "a <key> <val> add value to dictionary\n"
143 "d <key> delete value from dictionary\n"
144 "l <key> lookup value in dictionary\n"
145 "( <key> lookup lower bound\n"
146 ") <key> lookup upper bound\n"
147 "# <num> switch to alternate dictionary (0-9)\n"
148 "j <num> <num> merge two dictionaries\n"
149 "f free the whole dictionary\n"
150 "k allow duplicate keys\n"
151 "c show number of entries\n"
152 "t dump whole dictionary in sort order\n"
153 "m make dictionary out of sorted items\n"
154 "p turn prompt on\n"
155 "s switch to non-functioning allocator\n"
156 "q quit";
157
158 for (i = 0; i < sizeof darray / sizeof *darray; i++)
159 dict_init(&darray[i], DICTCOUNT_T_MAX, comparef);
160
161 for (;;) {
162 if (prompt)
163 putchar('>');
164 fflush(stdout);
165
166 if (!fgets(in, sizeof(input_t), stdin))
167 break;
168
169 switch(in[0]) {
170 case '?':
171 puts(help);
172 break;
173 case 'a':
174 if (tokenize(in+1, &tok1, &tok2, (char **) 0) != 2) {
175 puts("what?");
176 break;
177 }
178 key = dupstring(tok1);
179 val = dupstring(tok2);
180
181 if (!key || !val) {
182 puts("out of memory");
183 free((void *) key);
184 free(val);
185 }
186
187 if (!dict_alloc_insert(d, key, val)) {
188 puts("dict_alloc_insert failed");
189 free((void *) key);
190 free(val);
191 break;
192 }
193 break;
194 case 'd':
195 if (tokenize(in+1, &tok1, (char **) 0) != 1) {
196 puts("what?");
197 break;
198 }
199 dn = dict_lookup(d, tok1);
200 if (!dn) {
201 puts("dict_lookup failed");
202 break;
203 }
204 val = dnode_get(dn);
205 key = dnode_getkey(dn);
206 dict_delete_free(d, dn);
207
208 free(val);
209 free((void *) key);
210 break;
211 case 'f':
212 dict_free(d);
213 break;
214 case 'l':
215 case '(':
216 case ')':
217 if (tokenize(in+1, &tok1, (char **) 0) != 1) {
218 puts("what?");
219 break;
220 }
221 dn = 0;
222 switch (in[0]) {
223 case 'l':
224 dn = dict_lookup(d, tok1);
225 break;
226 case '(':
227 dn = dict_lower_bound(d, tok1);
228 break;
229 case ')':
230 dn = dict_upper_bound(d, tok1);
231 break;
232 }
233 if (!dn) {
234 puts("lookup failed");
235 break;
236 }
237 val = dnode_get(dn);
238 puts(val);
239 break;
240 case 'm':
241 construct(d);
242 break;
243 case 'k':
244 dict_allow_dupes(d);
245 break;
246 case 'c':
247 printf("%lu\n", (unsigned long) dict_count(d));
248 break;
249 case 't':
250 for (dn = dict_first(d); dn; dn = dict_next(d, dn)) {
251 printf("%s\t%s\n", (char *) dnode_getkey(dn),
252 (char *) dnode_get(dn));
253 }
254 break;
255 case 'q':
256 exit(0);
257 break;
258 case '\0':
259 break;
260 case 'p':
261 prompt = 1;
262 break;
263 case 's':
264 dict_set_allocator(d, new_node, del_node, NULL);
265 break;
266 case '#':
267 if (tokenize(in+1, &tok1, (char **) 0) != 1) {
268 puts("what?");
269 break;
270 } else {
271 int dictnum = atoi(tok1);
272 if (dictnum < 0 || dictnum > 9) {
273 puts("invalid number");
274 break;
275 }
276 d = &darray[dictnum];
277 }
278 break;
279 case 'j':
280 if (tokenize(in+1, &tok1, &tok2, (char **) 0) != 2) {
281 puts("what?");
282 break;
283 } else {
284 int dict1 = atoi(tok1), dict2 = atoi(tok2);
285 if (dict1 < 0 || dict1 > 9 || dict2 < 0 || dict2 > 9) {
286 puts("invalid number");
287 break;
288 }
289 dict_merge(&darray[dict1], &darray[dict2]);
290 }
291 break;
292 default:
293 putchar('?');
294 putchar('\n');
295 break;
296 }
297 }
298
299 return 0;
300 }