aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/eina/src/examples/eina_hash_01.c
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/eina/src/examples/eina_hash_01.c')
-rw-r--r--libraries/eina/src/examples/eina_hash_01.c195
1 files changed, 195 insertions, 0 deletions
diff --git a/libraries/eina/src/examples/eina_hash_01.c b/libraries/eina/src/examples/eina_hash_01.c
new file mode 100644
index 0000000..549ca80
--- /dev/null
+++ b/libraries/eina/src/examples/eina_hash_01.c
@@ -0,0 +1,195 @@
1#include <stdio.h>
2#include <string.h>
3#include <Eina.h>
4
5/*
6 * Eina Hash - phonebook
7 *
8 * This example demonstrate the use of Eina Hash by implementing a phonebook
9 * that stores its contact data into the hash.
10 *
11 * It indexes the phone numbers by Contact Full Name, so it's a hash with
12 * string keys.
13 */
14
15struct _Phone_Entry {
16 const char *name; // Full name.
17 const char *number; // Phone number.
18};
19
20typedef struct _Phone_Entry Phone_Entry;
21
22static Phone_Entry _start_entries[] = {
23 { "Wolfgang Amadeus Mozart", "+01 23 456-78910" },
24 { "Ludwig van Beethoven", "+12 34 567-89101" },
25 { "Richard Georg Strauss", "+23 45 678-91012" },
26 { "Heitor Villa-Lobos", "+34 56 789-10123" },
27 { NULL, NULL }
28}; // _start_entries
29
30static void
31_phone_entry_free_cb(void *data)
32{
33 free(data);
34}
35
36static Eina_Bool
37_phone_book_foreach_cb(const Eina_Hash *phone_book, const void *key,
38 void *data, void *fdata)
39{
40 const char *name = key;
41 const char *number = data;
42 printf("%s: %s\n", name, number);
43
44 // Return EINA_FALSE to stop this callback from being called
45 return EINA_TRUE;
46}
47
48int
49main(int argc, const char *argv[])
50{
51 Eina_Hash *phone_book = NULL;
52 int i;
53 const char *entry_name = "Heitor Villa-Lobos";
54 char *phone = NULL;
55 Eina_Bool r;
56 Eina_Iterator *it;
57 void *data;
58
59 eina_init();
60
61 phone_book = eina_hash_string_superfast_new(_phone_entry_free_cb);
62
63 // Add initial entries to our hash
64 for (i = 0; _start_entries[i].name != NULL; i++)
65 {
66 eina_hash_add(phone_book, _start_entries[i].name,
67 strdup(_start_entries[i].number));
68 }
69
70 // Look for a specific entry and get its phone number
71 phone = eina_hash_find(phone_book, entry_name);
72 if (phone)
73 {
74 printf("Printing entry.\n");
75 printf("Name: %s\n", entry_name);
76 printf("Number: %s\n\n", phone);
77 }
78
79 // Delete this entry
80 r = eina_hash_del(phone_book, entry_name, NULL);
81 printf("Hash entry successfully deleted? %d\n\n", r);
82
83 // Modify the pointer data of an entry and free the old one
84 phone = eina_hash_modify(phone_book, "Richard Georg Strauss",
85 strdup("+23 45 111-11111"));
86 free(phone);
87
88 // Modify or add an entry to the hash with eina_hash_set
89 // Let's first add a new entry
90 eina_error_set(0);
91 phone = eina_hash_set(phone_book, "Raul Seixas",
92 strdup("+55 01 234-56789"));
93 if (!phone)
94 {
95 Eina_Error err = eina_error_get();
96 if (!err)
97 {
98 printf("No previous phone found for Raul Seixas. ");
99 printf("Creating new entry.\n");
100 }
101 else
102 printf("Error when setting phone for Raul Seixas\n");
103 }
104 else
105 {
106 printf("Old phone for Raul Seixas was %s\n", phone);
107 free(phone);
108 }
109
110 printf("\n");
111
112 // Now change the phone number
113 eina_error_set(0);
114 phone = eina_hash_set(phone_book, "Raul Seixas",
115 strdup("+55 02 222-22222"));
116 if (phone)
117 {
118 printf("Changing phone for Raul Seixas to +55 02 222-22222. ");
119 printf("Old phone was %s\n", phone);
120 free(phone);
121 }
122 else
123 {
124 Eina_Error err = eina_error_get();
125 if (err)
126 printf("Error when changing phone for Raul Seixas\n");
127 else
128 {
129 printf("No previous phone found for Raul Seixas. ");
130 printf("Creating new entry.\n");
131 }
132 }
133
134 // There are many ways to iterate over our Phone book.
135 // First, iterate showing the names and associated numbers.
136 printf("List of phones:\n");
137 eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL);
138 printf("\n");
139
140 // Now iterate using an iterator
141 printf("List of phones:\n");
142 it = eina_hash_iterator_tuple_new(phone_book);
143 while (eina_iterator_next(it, &data))
144 {
145 Eina_Hash_Tuple *t = data;
146 const char *name = t->key;
147 const char *number = t->data;
148 printf("%s: %s\n", name, number);
149 }
150 eina_iterator_free(it); // Always free the iterator after its use
151 printf("\n");
152
153 // Just iterate over the keys (names)
154 printf("List of names in the phone book:\n");
155 it = eina_hash_iterator_key_new(phone_book);
156 while (eina_iterator_next(it, &data))
157 {
158 const char *name = data;
159 printf("%s\n", name);
160 }
161 eina_iterator_free(it);
162 printf("\n");
163
164 // Just iterate over the data (numbers)
165 printf("List of numbers in the phone book:\n");
166 it = eina_hash_iterator_data_new(phone_book);
167 while (eina_iterator_next(it, &data))
168 {
169 const char *number = data;
170 printf("%s\n", number);
171 }
172 eina_iterator_free(it);
173 printf("\n");
174
175 // Check how many items are in the phone book
176 printf("There are %d items in the hash.\n\n",
177 eina_hash_population(phone_book));
178
179 // Change the name (key) on an entry
180 eina_hash_move(phone_book, "Raul Seixas", "Alceu Valenca");
181 printf("List of phones after change:\n");
182 eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL);
183 printf("\n");
184
185 // Empty the phone book, but don't destroy it
186 eina_hash_free_buckets(phone_book);
187 printf("There are %d items in the hash.\n\n",
188 eina_hash_population(phone_book));
189
190 // Phone book could still be used, but we are freeing it since we are
191 // done for now
192 eina_hash_free(phone_book);
193
194 eina_shutdown();
195}