2 //gcc -g eina_hash_04.c -o eina_hash_04 `pkg-config --cflags --libs eina`
9 * Eina Hash - phonebook
11 * This example demonstrate the use of Eina Hash by implementing a phonebook
12 * that stores its contact data into the hash.
14 * It indexes the phone numbers by Contact Full Name, so it's a hash with
19 const char *name; // Full name.
20 const char *number; // Phone number.
23 typedef struct _Phone_Entry Phone_Entry;
25 static Phone_Entry _start_entries[] = {
26 { "Wolfgang Amadeus Mozart", "+01 23 456-78910" },
27 { "Ludwig van Beethoven", "+12 34 567-89101" },
28 { "Richard Georg Strauss", "+23 45 678-91012" },
29 { "Heitor Villa-Lobos", "+34 56 789-10123" },
34 _phone_entry_free_cb(void *data)
40 _phone_book_foreach_cb(const Eina_Hash *phone_book, const void *key,
41 void *data, void *fdata)
43 const char *name = key;
44 const char *number = data;
45 printf("%s: %s\n", name, number);
47 // Return EINA_FALSE to stop this callback from being called
52 main(int argc, const char *argv[])
54 Eina_Hash *phone_book = NULL;
56 const char *entry_name = "Heitor Villa-Lobos";
64 phone_book = eina_hash_string_djb2_new(_phone_entry_free_cb);
66 // Add initial entries to our hash
67 for (i = 0; _start_entries[i].name != NULL; i++)
69 eina_hash_add(phone_book, _start_entries[i].name,
70 strdup(_start_entries[i].number));
73 // Look for a specific entry and get its phone number
74 phone = eina_hash_find(phone_book, entry_name);
77 printf("Printing entry.\n");
78 printf("Name: %s\n", entry_name);
79 printf("Number: %s\n\n", phone);
83 r = eina_hash_del(phone_book, entry_name, NULL);
84 printf("Hash entry successfully deleted? %d\n\n", r);
86 // Modify the pointer data of an entry and free the old one
87 phone = eina_hash_modify(phone_book, "Richard Georg Strauss",
88 strdup("+23 45 111-11111"));
91 // Modify or add an entry to the hash with eina_hash_set
92 // Let's first add a new entry
94 phone = eina_hash_set(phone_book, "Raul Seixas",
95 strdup("+55 01 234-56789"));
98 Eina_Error err = eina_error_get();
101 printf("No previous phone found for Raul Seixas. ");
102 printf("Creating new entry.\n");
105 printf("Error when setting phone for Raul Seixas\n");
109 printf("Old phone for Raul Seixas was %s\n", phone);
115 // Now change the phone number
117 phone = eina_hash_set(phone_book, "Raul Seixas",
118 strdup("+55 02 222-22222"));
121 printf("Changing phone for Raul Seixas to +55 02 222-22222. ");
122 printf("Old phone was %s\n", phone);
127 Eina_Error err = eina_error_get();
129 printf("Error when changing phone for Raul Seixas\n");
132 printf("No previous phone found for Raul Seixas. ");
133 printf("Creating new entry.\n");
137 // There are many ways to iterate over our Phone book.
138 // First, iterate showing the names and associated numbers.
139 printf("List of phones:\n");
140 eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL);
143 // Now iterate using an iterator
144 printf("List of phones:\n");
145 it = eina_hash_iterator_tuple_new(phone_book);
146 while (eina_iterator_next(it, &data))
148 Eina_Hash_Tuple *t = data;
149 const char *name = t->key;
150 const char *number = t->data;
151 printf("%s: %s\n", name, number);
153 eina_iterator_free(it); // Always free the iterator after its use
156 // Just iterate over the keys (names)
157 printf("List of names in the phone book:\n");
158 it = eina_hash_iterator_key_new(phone_book);
159 while (eina_iterator_next(it, &data))
161 const char *name = data;
162 printf("%s\n", name);
164 eina_iterator_free(it);
167 // Just iterate over the data (numbers)
168 printf("List of numbers in the phone book:\n");
169 it = eina_hash_iterator_data_new(phone_book);
170 while (eina_iterator_next(it, &data))
172 const char *number = data;
173 printf("%s\n", number);
175 eina_iterator_free(it);
178 // Check how many items are in the phone book
179 printf("There are %d items in the hash.\n\n",
180 eina_hash_population(phone_book));
182 // Change the name (key) on an entry
183 eina_hash_move(phone_book, "Raul Seixas", "Alceu Valenca");
184 printf("List of phones after change:\n");
185 eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL);
188 // Empty the phone book, but don't destroy it
189 eina_hash_free_buckets(phone_book);
190 printf("There are %d items in the hash.\n\n",
191 eina_hash_population(phone_book));
193 // Phone book could still be used, but we are freeing it since we are
195 eina_hash_free(phone_book);