htable_free_objects() -> htable_free_all_obj() in main.c
[eleutheria.git] / genstructs / htable / main.c
blob9d4a6082b866aea86f9b983dbe8211d46be025bc
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
5 #include "htable.h"
7 unsigned int djb_hash(const void *key)
9 unsigned int i, hash = 5381;
10 char *str = (char *)key;
12 for (i = 0; i < strlen(str); i++)
13 hash = ((hash << 5) + hash) + str[i];
15 return (hash & 0x7FFFFFFF);
18 int mystrcmp(const void *arg1, const void *arg2)
20 return (strcmp((char *) arg1, (char *) arg2));
23 void myprintf(const void *key, const void *data)
25 printf("%s(%s) ", (char *)key, (char *)data);
28 int main(void)
30 htable_t htable;
31 char *p = malloc(100);
32 char *s = malloc(100);
33 char *t = malloc(100);
34 char *q = malloc(100);
36 strcpy(p, "stathis");
37 strcpy(s, "maria");
38 strcpy(t, "kostas");
39 strcpy(q, "eleni");
41 /* Initialize table */
42 if (htable_init(&htable, 1, 1, djb_hash, mystrcmp, myprintf) == HT_NOMEM) {
43 perror("malloc");
44 exit(EXIT_FAILURE);
47 htable_insert(&htable, "stathis", p);
48 htable_insert(&htable, "maria", s);
49 htable_insert(&htable, "kostas", t);
50 htable_insert(&htable, "eleni", q);
51 htable_print(&htable);
53 /* Free memory */
54 htable_free_all_obj(&htable);
55 htable_free(&htable);
57 return EXIT_SUCCESS;