Commit | Line | Data |
---|---|---|
7cf5c72a | 1 | #include <assert.h> |
ece42f68 | 2 | #include <stdio.h> |
3 | #include <stdlib.h> | |
4 | #include <string.h> | |
5 | ||
6 | #include "assoc.h" | |
7 | #include "atom.h" | |
36188114 | 8 | #include "macros.h" |
ece42f68 | 9 | |
10 | typedef struct word { | |
11 | assoc_base _b; | |
12 | int i; | |
13 | } word; | |
14 | ||
15 | static int cmp(const void *a, const void *b) | |
16 | { | |
17 | const word *const *v = b; | |
18 | const word *const *w = a; | |
90407c44 | 19 | return (strcmp(ATOM_NAME(ASSOC_ATOM(*w)), ATOM_NAME(ASSOC_ATOM(*v)))); |
ece42f68 | 20 | } |
21 | ||
22 | int main(void) | |
23 | { | |
24 | char buf[256]; | |
25 | char *p; | |
26 | atom_table at; | |
27 | assoc_table t; | |
7cf5c72a | 28 | size_t n = 0, j; |
ece42f68 | 29 | |
30 | atom_createtable(&at); | |
31 | assoc_create(&t); | |
32 | ||
33 | while (fgets(buf, sizeof(buf), stdin)) { | |
7cf5c72a | 34 | /* printf("+++ %s", buf); */ |
ece42f68 | 35 | buf[strlen(buf) - 1] = 0; |
ece42f68 | 36 | p = strtok(buf, " "); |
37 | ||
36188114 | 38 | if (STRCMP(p, ==, "set")) { |
ece42f68 | 39 | char *k = strtok(0, " "); |
40 | int i = atoi(strtok(0, " ")); | |
41 | unsigned f; | |
42 | word *w = assoc_find(&t, atom_intern(&at, k), sizeof(word), &f); | |
43 | w->i = i; | |
44 | if (!f) | |
45 | n++; | |
36188114 | 46 | } else if (STRCMP(p, ==, "get")) { |
ece42f68 | 47 | char *k = strtok(0, " "); |
48 | word *w = assoc_find(&t, atom_intern(&at, k), 0, 0); | |
49 | if (w) | |
50 | printf("%i\n", w->i); | |
51 | else | |
52 | puts("*MISSING*"); | |
36188114 | 53 | } else if (STRCMP(p, ==, "del")) { |
ece42f68 | 54 | char *k = strtok(0, " "); |
55 | word *w = assoc_find(&t, atom_intern(&at, k), 0, 0); | |
56 | if (w) { | |
57 | assoc_remove(&t, w); | |
58 | n--; | |
59 | } else | |
60 | puts("*MISSING*"); | |
36188114 | 61 | } else if (STRCMP(p, ==, "count")) { |
53795290 | 62 | printf("%lu\n", (unsigned long)n); |
36188114 | 63 | } else if (STRCMP(p, ==, "show")) { |
ece42f68 | 64 | assoc_iter i; |
65 | word *w; | |
66 | word **v, **vv; | |
67 | ||
68 | if (!n) | |
69 | puts("*EMPTY*"); | |
70 | else { | |
71 | v = malloc(n * sizeof(*v)); | |
72 | if (!v) { | |
73 | puts("*NOMEM*"); | |
74 | continue; | |
75 | } | |
7cf5c72a MW |
76 | for (vv = v, assoc_mkiter(&i, &t), j = 0; |
77 | (w = assoc_next(&i)) != 0; | |
78 | vv++, j++) { | |
79 | assert(j < n); | |
ece42f68 | 80 | *vv = w; |
7cf5c72a MW |
81 | } |
82 | assert(j == n); | |
ece42f68 | 83 | qsort(v, n, sizeof(*v), cmp); |
90407c44 | 84 | printf("%s:%i", ATOM_NAME(ASSOC_ATOM(*v)), (*v)->i); |
7cf5c72a | 85 | for (vv = v + 1; --j; vv++) |
90407c44 | 86 | printf(" %s:%i", ATOM_NAME(ASSOC_ATOM(*vv)), (*vv)->i); |
ece42f68 | 87 | free(v); |
88 | putchar('\n'); | |
89 | } | |
90 | } else | |
91 | puts("*BAD*"); | |
7cf5c72a | 92 | /* printf("--- %d\n", n); */ |
ece42f68 | 93 | } |
94 | ||
95 | assoc_destroy(&t); | |
90407c44 | 96 | atom_destroytable(&at); |
ece42f68 | 97 | return (0); |
98 | } |