Line data Source code
1 : /*
2 : Authors:
3 : John Dennis <jdennis.redhat.com>
4 :
5 : Copyright (C) 2009 Red Hat
6 :
7 : This program is free software; you can redistribute it and/or modify
8 : it under the terms of the GNU Lesser General Public License as published by
9 : the Free Software Foundation; either version 3 of the License, or
10 : (at your option) any later version.
11 :
12 : This program is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : GNU Lesser General Public License for more details.
16 :
17 : You should have received a copy of the GNU Lesser General Public License
18 : along with this program. If not, see <http://www.gnu.org/licenses/>.
19 : */
20 :
21 : #include <stdio.h>
22 : #include <string.h>
23 : #include <stdlib.h>
24 : #include <assert.h>
25 : #include "dhash.h"
26 :
27 : struct my_data_t {
28 : int foo;
29 : char bar[128];
30 : };
31 :
32 1 : static void delete_callback(hash_entry_t *entry,
33 : hash_destroy_enum type,
34 : void *pvt)
35 : {
36 1 : if (entry->value.type == HASH_VALUE_PTR) free(entry->value.ptr);
37 1 : }
38 :
39 1 : static bool visit_callback(hash_entry_t *entry,
40 : void *user_data)
41 : {
42 1 : unsigned long *count = (unsigned long *)user_data;
43 1 : struct my_data_t *my_data = (struct my_data_t *) entry->value.ptr;
44 :
45 1 : (*count)++;
46 :
47 1 : printf("%s = [foo=%d bar=%s]\n", entry->key.str, my_data->foo, my_data->bar);
48 1 : return true;
49 : }
50 :
51 1 : static struct my_data_t *new_data(int foo, const char *bar)
52 : {
53 1 : struct my_data_t *my_data = malloc(sizeof(struct my_data_t));
54 1 : my_data->foo = foo;
55 1 : strncpy(my_data->bar, bar, sizeof(my_data->bar));
56 1 : return my_data;
57 : }
58 :
59 1 : int main(int argc, char **argv)
60 : {
61 : static hash_table_t *table = NULL;
62 : hash_key_t key, *keys;
63 : hash_value_t value;
64 : struct hash_iter_context_t *iter;
65 : hash_entry_t *entry;
66 : unsigned long i, n_entries;
67 : int error;
68 1 : struct my_data_t *my_data = new_data(1024, "Hello World!");
69 : unsigned long count;
70 :
71 : /* Create a hash table */
72 1 : error = hash_create(10, &table, delete_callback, NULL);
73 1 : if (error != HASH_SUCCESS) {
74 0 : fprintf(stderr, "cannot create hash table (%s)\n", hash_error_string(error));
75 0 : return error;
76 : }
77 :
78 : /* Enter a key named "My Data" and specify it's value as a pointer to my_data */
79 1 : key.type = HASH_KEY_STRING;
80 1 : key.str = strdup("My Data");
81 1 : value.type = HASH_VALUE_PTR;
82 1 : value.ptr = my_data;
83 :
84 1 : if ((error = hash_enter(table, &key, &value)) != HASH_SUCCESS) {
85 0 : fprintf(stderr, "cannot add to table \"%s\" (%s)\n", key.str, hash_error_string(error));
86 0 : return error;
87 : }
88 1 : free(key.str);
89 :
90 : /* Get a list of keys and print them out, free the list when we're done */
91 1 : if ((error = hash_keys(table, &count, &keys)) != HASH_SUCCESS) {
92 0 : fprintf(stderr, "cannot get key list (%s)\n", hash_error_string(error));
93 0 : return error;
94 : }
95 :
96 2 : for (i = 0; i < count; i++)
97 1 : printf("key: %s\n", keys[i].str);
98 1 : free(keys);
99 :
100 : /* Lookup the key named "My Data" */
101 1 : key.type = HASH_KEY_STRING;
102 1 : key.str = strdup("My Data");
103 1 : if ((error = hash_lookup(table, &key, &value)) != HASH_SUCCESS) {
104 0 : fprintf(stderr, "cannot find key \"%s\" (%s)\n", key.str, hash_error_string(error));
105 : }
106 1 : free(key.str);
107 :
108 : /* Visit each entry in the table, callback will increment count on each visit */
109 1 : printf("Iterate using callback\n");
110 1 : count = 0;
111 1 : hash_iterate(table, visit_callback, &count);
112 :
113 : /* Assure number of visits equal the table size */
114 1 : assert(count == hash_count(table));
115 :
116 : /* Visit each entry using iterator object */
117 1 : printf("Iterate using iterator\n");
118 1 : n_entries = 0;
119 1 : iter = new_hash_iter_context(table);
120 3 : while ((entry = iter->next(iter)) != NULL) {
121 1 : struct my_data_t *data = (struct my_data_t *) entry->value.ptr;
122 :
123 1 : printf("%s = [foo=%d bar=%s]\n", entry->key.str, data->foo, data->bar);
124 1 : n_entries++;
125 : }
126 1 : free(iter);
127 : /* Assure number of visits equal the table size */
128 1 : assert(n_entries == hash_count(table));
129 :
130 : /* Remove the entry, deletion callback will be invoked */
131 1 : key.type = HASH_KEY_STRING;
132 1 : key.str = strdup("My Data");
133 1 : if ((error = hash_delete(table, &key)) != HASH_SUCCESS) {
134 0 : fprintf(stderr, "cannot delete from table (%s)\n", hash_error_string(error));
135 : }
136 :
137 : /* Assure key is no longer in table */
138 1 : assert (!hash_has_key(table, &key));
139 1 : free(key.str);
140 :
141 : /* Free the table */
142 1 : hash_destroy(table);
143 :
144 1 : return 0;
145 : }
|