diff options
Diffstat (limited to 'libraries/eina/src/examples/eina_hash_07.c')
-rw-r--r-- | libraries/eina/src/examples/eina_hash_07.c | 219 |
1 files changed, 219 insertions, 0 deletions
diff --git a/libraries/eina/src/examples/eina_hash_07.c b/libraries/eina/src/examples/eina_hash_07.c new file mode 100644 index 0000000..b3e486a --- /dev/null +++ b/libraries/eina/src/examples/eina_hash_07.c | |||
@@ -0,0 +1,219 @@ | |||
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 | |||
15 | struct _Phone_Entry { | ||
16 | const char *name; // Full name. | ||
17 | const char *number; // Phone number. | ||
18 | }; | ||
19 | |||
20 | typedef struct _Phone_Entry Phone_Entry; | ||
21 | |||
22 | static 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 | |||
30 | static const char *_nicknames[] = { | ||
31 | "mozzart", | ||
32 | "betho", | ||
33 | "george", | ||
34 | "hector", | ||
35 | NULL | ||
36 | }; | ||
37 | |||
38 | static void | ||
39 | _phone_entry_free_cb(void *data) | ||
40 | { | ||
41 | free(data); | ||
42 | } | ||
43 | |||
44 | static Eina_Bool | ||
45 | _phone_book_foreach_cb(const Eina_Hash *phone_book, const void *key, | ||
46 | void *data, void *fdata) | ||
47 | { | ||
48 | Phone_Entry **pe = (Phone_Entry **)key; | ||
49 | const char *nick = data; | ||
50 | printf("%s: %s, nick=%s\n", (*pe)->name, (*pe)->number, nick); | ||
51 | |||
52 | // Return EINA_FALSE to stop this callback from being called | ||
53 | return EINA_TRUE; | ||
54 | } | ||
55 | |||
56 | int | ||
57 | main(int argc, const char *argv[]) | ||
58 | { | ||
59 | Eina_Hash *phone_book = NULL; | ||
60 | int i; | ||
61 | Phone_Entry *entry_vl = &_start_entries[3]; | ||
62 | Phone_Entry *p = NULL; | ||
63 | char *nick = NULL; | ||
64 | Eina_Bool r; | ||
65 | Eina_Iterator *it; | ||
66 | void *data; | ||
67 | |||
68 | eina_init(); | ||
69 | |||
70 | phone_book = eina_hash_pointer_new(_phone_entry_free_cb); | ||
71 | |||
72 | // Add initial entries to our hash | ||
73 | for (i = 0; _start_entries[i].name != NULL; i++) | ||
74 | { | ||
75 | p = &_start_entries[i]; | ||
76 | eina_hash_add(phone_book, &p, | ||
77 | strdup(_nicknames[i])); | ||
78 | } | ||
79 | printf("Phonebook:\n"); | ||
80 | eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL); | ||
81 | printf("\n"); | ||
82 | |||
83 | // Look for a specific entry and get its nickname | ||
84 | nick = eina_hash_find(phone_book, &entry_vl); | ||
85 | if (nick) | ||
86 | { | ||
87 | printf("Printing entry.\n"); | ||
88 | printf("Name: %s\n", entry_vl->name); | ||
89 | printf("Number: %s\n", entry_vl->number); | ||
90 | printf("Nick: %s\n\n", nick); | ||
91 | } | ||
92 | |||
93 | // Delete this entry | ||
94 | r = eina_hash_del(phone_book, &entry_vl, NULL); | ||
95 | printf("Hash entry successfully deleted? %d\n\n", r); | ||
96 | |||
97 | // Modify the pointer data of an entry and free the old one | ||
98 | p = &_start_entries[2]; | ||
99 | nick = eina_hash_modify(phone_book, &p, | ||
100 | strdup("el jorge")); | ||
101 | free(nick); | ||
102 | |||
103 | // Modify or add an entry to the hash with eina_hash_set | ||
104 | // Let's first add a new entry | ||
105 | eina_error_set(0); | ||
106 | Phone_Entry *p1 = malloc(sizeof(*p1)); | ||
107 | p1->name = "Raul Seixas"; | ||
108 | p1->number = "+55 01 234-56789"; | ||
109 | nick = eina_hash_set(phone_book, &p1, | ||
110 | strdup("raulzito")); | ||
111 | if (!nick) | ||
112 | { | ||
113 | Eina_Error err = eina_error_get(); | ||
114 | if (!err) | ||
115 | { | ||
116 | printf("No previous nick found for Raul Seixas. "); | ||
117 | printf("Creating new entry.\n"); | ||
118 | } | ||
119 | else | ||
120 | printf("Error when setting nick for Raul Seixas\n"); | ||
121 | } | ||
122 | else | ||
123 | { | ||
124 | printf("Old nick for Raul Seixas was %s\n", nick); | ||
125 | free(nick); | ||
126 | } | ||
127 | |||
128 | printf("\n"); | ||
129 | |||
130 | // Now change the nick | ||
131 | eina_error_set(0); | ||
132 | nick = eina_hash_set(phone_book, &p1, | ||
133 | strdup("raulzao")); | ||
134 | if (nick) | ||
135 | { | ||
136 | printf("Changing nick for Raul Seixas to raulzao. "); | ||
137 | printf("Old nick was %s\n", nick); | ||
138 | free(nick); | ||
139 | } | ||
140 | else | ||
141 | { | ||
142 | Eina_Error err = eina_error_get(); | ||
143 | if (err) | ||
144 | printf("Error when changing nick for Raul Seixas\n"); | ||
145 | else | ||
146 | { | ||
147 | printf("No previous nick found for Raul Seixas. "); | ||
148 | printf("Creating new entry.\n"); | ||
149 | } | ||
150 | } | ||
151 | |||
152 | // There are many ways to iterate over our Phone book. | ||
153 | // First, iterate showing the names, phones and associated nicks. | ||
154 | printf("Phonebook:\n"); | ||
155 | eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL); | ||
156 | printf("\n"); | ||
157 | |||
158 | // Now iterate using an iterator | ||
159 | printf("Phonebook:\n"); | ||
160 | it = eina_hash_iterator_tuple_new(phone_book); | ||
161 | while (eina_iterator_next(it, &data)) | ||
162 | { | ||
163 | Eina_Hash_Tuple *t = data; | ||
164 | Phone_Entry **pe = (Phone_Entry **)t->key; | ||
165 | nick = t->data; | ||
166 | printf("%s: %s, nick=%s\n", (*pe)->name, (*pe)->number, nick); | ||
167 | } | ||
168 | eina_iterator_free(it); // Always free the iterator after its use | ||
169 | printf("\n"); | ||
170 | |||
171 | // Just iterate over the keys (names) | ||
172 | printf("List of names/numbers in the phone book:\n"); | ||
173 | it = eina_hash_iterator_key_new(phone_book); | ||
174 | while (eina_iterator_next(it, &data)) | ||
175 | { | ||
176 | Phone_Entry **pe = (Phone_Entry **)data; | ||
177 | printf("%s: %s\n", (*pe)->name, (*pe)->number); | ||
178 | } | ||
179 | eina_iterator_free(it); | ||
180 | printf("\n"); | ||
181 | |||
182 | // Just iterate over the data (nicks) | ||
183 | printf("List of nicks in the phone book:\n"); | ||
184 | it = eina_hash_iterator_data_new(phone_book); | ||
185 | while (eina_iterator_next(it, &data)) | ||
186 | { | ||
187 | nick = data; | ||
188 | printf("%s\n", nick); | ||
189 | } | ||
190 | eina_iterator_free(it); | ||
191 | printf("\n"); | ||
192 | |||
193 | // Check how many items are in the phone book | ||
194 | printf("There are %d items in the hash.\n\n", | ||
195 | eina_hash_population(phone_book)); | ||
196 | |||
197 | // Change the name (key) on an entry | ||
198 | Phone_Entry *p2 = malloc(sizeof(*p2)); | ||
199 | p2->name = "Alceu Valenca"; | ||
200 | p2->number = "000000000000"; | ||
201 | eina_hash_move(phone_book, p1, p2); | ||
202 | printf("List of phones after change:\n"); | ||
203 | eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL); | ||
204 | printf("\n"); | ||
205 | |||
206 | // Empty the phone book, but don't destroy it | ||
207 | eina_hash_free_buckets(phone_book); | ||
208 | printf("There are %d items in the hash.\n\n", | ||
209 | eina_hash_population(phone_book)); | ||
210 | |||
211 | // Phone book could still be used, but we are freeing it since we are | ||
212 | // done for now | ||
213 | eina_hash_free(phone_book); | ||
214 | |||
215 | free(p1); | ||
216 | free(p2); | ||
217 | |||
218 | eina_shutdown(); | ||
219 | } | ||