2 * Copyright (c) 2015 Mohamed Aslan <maslan@sce.carleton.ca>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 main(int argc
, char **argv
)
30 size_t key_size
, val_size
;
32 char *keys
[] = {"hi", "hello", "world", "hello world", "open", "source", "kernel", "hack"};
33 char *values
[] = {"1", "2", "3", "4", "5", "6", "7", "8"};
35 if (!hashtab_init(&ht
, 4, NULL
))
36 errx(1, "hashtab_init");
37 for (i
= 0; i
< 8; i
++) {
38 hashtab_put(&ht
, keys
[i
], strlen(keys
[i
]), values
[i
], strlen(values
[i
]) + 1);
39 printf("%s -> %s\n", keys
[i
], values
[i
]);
42 for (i
= 0; i
< 8; i
++) {
43 hashtab_get(&ht
, keys
[i
], strlen(keys
[i
]), (void **) &val
, &val_size
);
44 printf("%s -> %s\n", keys
[i
], val
);
48 if (hashtab_first(&ht
, &iter
)) {
50 hashtab_at(&ht
, iter
, (void *) &key
, &key_size
, (void **) &val
, &val_size
);
51 printf("%s -> %s\n", key
, val
);
52 } while (hashtab_next(&ht
, &iter
));
57 HASHTAB_FOREACH(ht
, iter
, key
, key_size
, val
, val_size
) {
58 hashtab_at(&ht
, iter
, (void *) &key
, &key_size
, (void **) &val
, &val_size
);
59 printf("%s -> %s\n", key
, val
);
65 hashtab_put(&ht
, "world", strlen("world"), "10", strlen("10") + 1);
66 hashtab_get(&ht
, "world", strlen("world"), (void **) &val
, &val_size
);
67 printf("world -> %s\n", val
);
69 hashtab_del(&ht
, "world", strlen("world"));
70 if (hashtab_get(&ht
, "world", strlen("world"), (void **) &val
, &val_size
))
71 printf("world -> %s\n", val
);
73 printf("world not found\n");