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