chiark / gitweb /
infra: Clean up project setup
[mLib] / sym-test.c
... / ...
CommitLineData
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5#include "sym.h"
6
7typedef struct word {
8 sym_base _b;
9 int i;
10} word;
11
12static int cmp(const void *a, const void *b)
13{
14 const word *const *v = b;
15 const word *const *w = a;
16 return (strcmp(SYM_NAME(*w), SYM_NAME(*v)));
17}
18
19int main(void)
20{
21 char buf[256];
22 char *p;
23 sym_table t;
24 size_t n = 0;
25
26 sym_create(&t);
27
28 while (fgets(buf, sizeof(buf), stdin)) {
29 buf[strlen(buf) - 1] = 0;
30/* printf("# %s\n", buf); */
31 p = strtok(buf, " ");
32
33 if (strcmp(p, "set") == 0) {
34 char *k = strtok(0, " ");
35 int i = atoi(strtok(0, " "));
36 unsigned f;
37 word *w = sym_find(&t, k, -1, sizeof(word), &f);
38 w->i = i;
39 if (!f)
40 n++;
41 } else if (strcmp(p, "get") == 0) {
42 char *k = strtok(0, " ");
43 word *w = sym_find(&t, k, -1, 0, 0);
44 if (w)
45 printf("%i\n", w->i);
46 else
47 puts("*MISSING*");
48 } else if (strcmp(p, "del") == 0) {
49 char *k = strtok(0, " ");
50 word *w = sym_find(&t, k, -1, 0, 0);
51 if (w) {
52 sym_remove(&t, w);
53 n--;
54 } else
55 puts("*MISSING*");
56 } else if (strcmp(p, "count") == 0) {
57 printf("%i\n", n);
58 } else if (strcmp(p, "show") == 0) {
59 sym_iter i;
60 word *w;
61 word **v, **vv;
62
63 if (!n)
64 puts("*EMPTY*");
65 else {
66 v = malloc(n * sizeof(*v));
67 if (!v) {
68 puts("*NOMEM*");
69 continue;
70 }
71 for (vv = v, sym_mkiter(&i, &t); (w = sym_next(&i)) != 0; vv++)
72 *vv = w;
73 qsort(v, n, sizeof(*v), cmp);
74 printf("%s:%i", SYM_NAME(*v), (*v)->i);
75 for (vv = v + 1; --n; vv++)
76 printf(" %s:%i", SYM_NAME(*vv), (*vv)->i);
77 free(v);
78 putchar('\n');
79 }
80 } else
81 puts("*BAD*");
82 }
83
84 sym_destroy(&t);
85 return (0);
86}