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