aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/eina/src/examples/eina_hash_04.c
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/eina/src/examples/eina_hash_04.c')
-rw-r--r--libraries/eina/src/examples/eina_hash_04.c198
1 files changed, 0 insertions, 198 deletions
diff --git a/libraries/eina/src/examples/eina_hash_04.c b/libraries/eina/src/examples/eina_hash_04.c
deleted file mode 100644
index da73e04..0000000
--- a/libraries/eina/src/examples/eina_hash_04.c
+++ /dev/null
@@ -1,198 +0,0 @@
1//Compile with:
2//gcc -g eina_hash_04.c -o eina_hash_04 `pkg-config --cflags --libs eina`
3
4#include <stdio.h>
5#include <string.h>
6#include <Eina.h>
7
8/*
9 * Eina Hash - phonebook
10 *
11 * This example demonstrate the use of Eina Hash by implementing a phonebook
12 * that stores its contact data into the hash.
13 *
14 * It indexes the phone numbers by Contact Full Name, so it's a hash with
15 * string keys.
16 */
17
18struct _Phone_Entry {
19 const char *name; // Full name.
20 const char *number; // Phone number.
21};
22
23typedef struct _Phone_Entry Phone_Entry;
24
25static 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" },
30 { NULL, NULL }
31}; // _start_entries
32
33static void
34_phone_entry_free_cb(void *data)
35{
36 free(data);
37}
38
39static Eina_Bool
40_phone_book_foreach_cb(const Eina_Hash *phone_book, const void *key,
41 void *data, void *fdata)
42{
43 const char *name = key;
44 const char *number = data;
45 printf("%s: %s\n", name, number);
46
47 // Return EINA_FALSE to stop this callback from being called
48 return EINA_TRUE;
49}
50
51int
52main(int argc, const char *argv[])
53{
54 Eina_Hash *phone_book = NULL;
55 int i;
56 const char *entry_name = "Heitor Villa-Lobos";
57 char *phone = NULL;
58 Eina_Bool r;
59 Eina_Iterator *it;
60 void *data;
61
62 eina_init();
63
64 phone_book = eina_hash_string_djb2_new(_phone_entry_free_cb);
65
66 // Add initial entries to our hash
67 for (i = 0; _start_entries[i].name != NULL; i++)
68 {
69 eina_hash_add(phone_book, _start_entries[i].name,
70 strdup(_start_entries[i].number));
71 }
72
73 // Look for a specific entry and get its phone number
74 phone = eina_hash_find(phone_book, entry_name);
75 if (phone)
76 {
77 printf("Printing entry.\n");
78 printf("Name: %s\n", entry_name);
79 printf("Number: %s\n\n", phone);
80 }
81
82 // Delete this entry
83 r = eina_hash_del(phone_book, entry_name, NULL);
84 printf("Hash entry successfully deleted? %d\n\n", r);
85
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"));
89 free(phone);
90
91 // Modify or add an entry to the hash with eina_hash_set
92 // Let's first add a new entry
93 eina_error_set(0);
94 phone = eina_hash_set(phone_book, "Raul Seixas",
95 strdup("+55 01 234-56789"));
96 if (!phone)
97 {
98 Eina_Error err = eina_error_get();
99 if (!err)
100 {
101 printf("No previous phone found for Raul Seixas. ");
102 printf("Creating new entry.\n");
103 }
104 else
105 printf("Error when setting phone for Raul Seixas\n");
106 }
107 else
108 {
109 printf("Old phone for Raul Seixas was %s\n", phone);
110 free(phone);
111 }
112
113 printf("\n");
114
115 // Now change the phone number
116 eina_error_set(0);
117 phone = eina_hash_set(phone_book, "Raul Seixas",
118 strdup("+55 02 222-22222"));
119 if (phone)
120 {
121 printf("Changing phone for Raul Seixas to +55 02 222-22222. ");
122 printf("Old phone was %s\n", phone);
123 free(phone);
124 }
125 else
126 {
127 Eina_Error err = eina_error_get();
128 if (err)
129 printf("Error when changing phone for Raul Seixas\n");
130 else
131 {
132 printf("No previous phone found for Raul Seixas. ");
133 printf("Creating new entry.\n");
134 }
135 }
136
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);
141 printf("\n");
142
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))
147 {
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);
152 }
153 eina_iterator_free(it); // Always free the iterator after its use
154 printf("\n");
155
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))
160 {
161 const char *name = data;
162 printf("%s\n", name);
163 }
164 eina_iterator_free(it);
165 printf("\n");
166
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))
171 {
172 const char *number = data;
173 printf("%s\n", number);
174 }
175 eina_iterator_free(it);
176 printf("\n");
177
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));
181
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);
186 printf("\n");
187
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));
192
193 // Phone book could still be used, but we are freeing it since we are
194 // done for now
195 eina_hash_free(phone_book);
196
197 eina_shutdown();
198}