hashtbtest.c

Go to the documentation of this file.
00001 /**
00002  * @file hashtbtest.c
00003  * Try out some hash table calls (ccn/hashtb).
00004  *
00005  * A CCNx program.
00006  *
00007  * Copyright (C) 2008, 2009 Palo Alto Research Center, Inc.
00008  *
00009  * This work is free software; you can redistribute it and/or modify it under
00010  * the terms of the GNU General Public License version 2 as published by the
00011  * Free Software Foundation.
00012  * This work is distributed in the hope that it will be useful, but WITHOUT ANY
00013  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
00015  * for more details. You should have received a copy of the GNU General Public
00016  * License along with this program; if not, write to the
00017  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
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 }

Generated on Thu Feb 16 00:44:00 2012 for Content-Centric Networking in C by  doxygen 1.5.6