hashtbtest.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <stdlib.h>
00021 #include <stdint.h>
00022 #include <stdio.h>
00023 #include <string.h>
00024 #include <ccn/hashtb.h>
00025
00026 static void
00027 Dump(struct hashtb *h)
00028 {
00029 struct hashtb_enumerator eee;
00030 struct hashtb_enumerator *e = &eee;
00031 printf("------- %d ------\n", hashtb_n(h));
00032 for (hashtb_start(h, e); e->key != NULL; hashtb_next(e)) {
00033 if (e->extsize != 1 || strlen(e->key) != e->keysize)
00034 abort();
00035 printf("%u: %s\n", ((unsigned *)e->data)[0], (const char *)e->key);
00036 }
00037 hashtb_end(e);
00038 }
00039
00040 static void
00041 finally(struct hashtb_enumerator *e)
00042 {
00043 char *who = hashtb_get_param(e->ht, NULL);
00044 fprintf(stderr, "%s deleting %s\n", who, (const char *)e->key);
00045 }
00046
00047 int
00048 main(int argc, char **argv)
00049 {
00050 char buf[1024] = {0};
00051 struct hashtb_param p = { &finally, argv[1]};
00052 struct hashtb *h = hashtb_create(sizeof(unsigned *), p.finalize_data ? &p : NULL);
00053 struct hashtb_enumerator eee;
00054 struct hashtb_enumerator *e = hashtb_start(h, &eee);
00055 struct hashtb_enumerator eee2;
00056 struct hashtb_enumerator *e2 = NULL;
00057 int nest = 0;
00058 while (fgets(buf, sizeof(buf), stdin)) {
00059 int i = strlen(buf);
00060 if (i > 0 && buf[i-1] == '\n')
00061 buf[--i] = 0;
00062 if (buf[0] == '?') {
00063 Dump(h);
00064 }
00065 else if (buf[0] == '-') {
00066 int res;
00067 unsigned *v;
00068 v = hashtb_lookup(h, buf+1, i-1);
00069 if (v != NULL)
00070 printf("(%u)", *v);
00071 res = hashtb_seek(e, buf+1, i-1, 1);
00072 hashtb_delete(e);
00073 printf("%d\n", res == HT_OLD_ENTRY);
00074 }
00075 else if (buf[0] == '.' && buf[1] == '[') {
00076 if ((nest++) == 0)
00077 e2 = hashtb_start(h, &eee2);
00078 }
00079 else if (buf[0] == '.' && buf[1] == ']') {
00080 if ((--nest) == 0 && e2 != NULL)
00081 hashtb_end(e2);
00082 e2 = NULL;
00083 }
00084 else {
00085 hashtb_seek(e, buf, i, 1);
00086 ((unsigned *)(e->data))[0] += 1;
00087 }
00088 }
00089 hashtb_end(e);
00090 hashtb_destroy(&h);
00091 if (h != NULL)
00092 return(1);
00093 return(0);
00094 }