diff options
author | David Walter Seikel | 2012-01-04 18:41:13 +1000 |
---|---|---|
committer | David Walter Seikel | 2012-01-04 18:41:13 +1000 |
commit | dd7595a3475407a7fa96a97393bae8c5220e8762 (patch) | |
tree | e341e911d7eb911a51684a7412ef7f7c7605d28e /libraries/eina/src/examples/eina_hash_05.c | |
parent | Add the skeleton. (diff) | |
download | SledjHamr-dd7595a3475407a7fa96a97393bae8c5220e8762.zip SledjHamr-dd7595a3475407a7fa96a97393bae8c5220e8762.tar.gz SledjHamr-dd7595a3475407a7fa96a97393bae8c5220e8762.tar.bz2 SledjHamr-dd7595a3475407a7fa96a97393bae8c5220e8762.tar.xz |
Add the base Enlightenment Foundation Libraries - eina, eet, evas, ecore, embryo, and edje.
Note that embryo wont be used, but I'm not sure yet if you can build edje without it.
Diffstat (limited to '')
-rw-r--r-- | libraries/eina/src/examples/eina_hash_05.c | 198 |
1 files changed, 198 insertions, 0 deletions
diff --git a/libraries/eina/src/examples/eina_hash_05.c b/libraries/eina/src/examples/eina_hash_05.c new file mode 100644 index 0000000..f8dbf78 --- /dev/null +++ b/libraries/eina/src/examples/eina_hash_05.c | |||
@@ -0,0 +1,198 @@ | |||
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 | int32_t id; // 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 | { 1, "+01 23 456-78910" }, | ||
24 | { 2, "+12 34 567-89101" }, | ||
25 | { 3, "+23 45 678-91012" }, | ||
26 | { 4, "+34 56 789-10123" }, | ||
27 | { -1, NULL } | ||
28 | }; // _start_entries | ||
29 | |||
30 | static void | ||
31 | _phone_entry_free_cb(void *data) | ||
32 | { | ||
33 | free(data); | ||
34 | } | ||
35 | |||
36 | static Eina_Bool | ||
37 | _phone_book_foreach_cb(const Eina_Hash *phone_book, const void *key, | ||
38 | void *data, void *fdata) | ||
39 | { | ||
40 | const int32_t *id = key; | ||
41 | const char *number = data; | ||
42 | printf("%d: %s\n", *id, number); | ||
43 | |||
44 | // Return EINA_FALSE to stop this callback from being called | ||
45 | return EINA_TRUE; | ||
46 | } | ||
47 | |||
48 | int | ||
49 | main(int argc, const char *argv[]) | ||
50 | { | ||
51 | Eina_Hash *phone_book = NULL; | ||
52 | int i; | ||
53 | int32_t entry_id = 4; | ||
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_int32_new(_phone_entry_free_cb); | ||
62 | |||
63 | // Add initial entries to our hash | ||
64 | for (i = 0; _start_entries[i].id != -1; i++) | ||
65 | { | ||
66 | eina_hash_add(phone_book, &_start_entries[i].id, | ||
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_id); | ||
72 | if (phone) | ||
73 | { | ||
74 | printf("Printing entry.\n"); | ||
75 | printf("Id: %d\n", entry_id); | ||
76 | printf("Number: %s\n\n", phone); | ||
77 | } | ||
78 | |||
79 | // Delete this entry | ||
80 | r = eina_hash_del(phone_book, &entry_id, 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 | int32_t id3 = 3; | ||
85 | phone = eina_hash_modify(phone_book, &id3, | ||
86 | strdup("+23 45 111-11111")); | ||
87 | free(phone); | ||
88 | |||
89 | // Modify or add an entry to the hash with eina_hash_set | ||
90 | // Let's first add a new entry | ||
91 | int32_t id5 = 5; | ||
92 | eina_error_set(0); | ||
93 | phone = eina_hash_set(phone_book, &id5, | ||
94 | strdup("+55 01 234-56789")); | ||
95 | if (!phone) | ||
96 | { | ||
97 | Eina_Error err = eina_error_get(); | ||
98 | if (!err) | ||
99 | { | ||
100 | printf("No previous phone found for id5. "); | ||
101 | printf("Creating new entry.\n"); | ||
102 | } | ||
103 | else | ||
104 | printf("Error when setting phone for Raul Seixas\n"); | ||
105 | } | ||
106 | else | ||
107 | { | ||
108 | printf("Old phone for id5 was %s\n", phone); | ||
109 | free(phone); | ||
110 | } | ||
111 | |||
112 | printf("\n"); | ||
113 | |||
114 | // Now change the phone number | ||
115 | eina_error_set(0); | ||
116 | phone = eina_hash_set(phone_book, &id5, | ||
117 | strdup("+55 02 222-22222")); | ||
118 | if (phone) | ||
119 | { | ||
120 | printf("Changing phone for id5 to +55 02 222-22222. "); | ||
121 | printf("Old phone was %s\n", phone); | ||
122 | free(phone); | ||
123 | } | ||
124 | else | ||
125 | { | ||
126 | Eina_Error err = eina_error_get(); | ||
127 | if (err) | ||
128 | printf("Error when changing phone for id5\n"); | ||
129 | else | ||
130 | { | ||
131 | printf("No previous phone found for id5. "); | ||
132 | printf("Creating new entry.\n"); | ||
133 | } | ||
134 | } | ||
135 | |||
136 | // There are many ways to iterate over our Phone book. | ||
137 | // First, iterate showing the names and associated numbers. | ||
138 | printf("List of phones:\n"); | ||
139 | eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL); | ||
140 | printf("\n"); | ||
141 | |||
142 | // Now iterate using an iterator | ||
143 | printf("List of phones:\n"); | ||
144 | it = eina_hash_iterator_tuple_new(phone_book); | ||
145 | while (eina_iterator_next(it, &data)) | ||
146 | { | ||
147 | Eina_Hash_Tuple *t = data; | ||
148 | const int32_t *id = t->key; | ||
149 | const char *number = t->data; | ||
150 | printf("%d: %s\n", *id, number); | ||
151 | } | ||
152 | eina_iterator_free(it); // Always free the iterator after its use | ||
153 | printf("\n"); | ||
154 | |||
155 | // Just iterate over the keys (names) | ||
156 | printf("List of ids in the phone book:\n"); | ||
157 | it = eina_hash_iterator_key_new(phone_book); | ||
158 | while (eina_iterator_next(it, &data)) | ||
159 | { | ||
160 | const int32_t *id = data; | ||
161 | printf("%d\n", *id); | ||
162 | } | ||
163 | eina_iterator_free(it); | ||
164 | printf("\n"); | ||
165 | |||
166 | // Just iterate over the data (numbers) | ||
167 | printf("List of numbers in the phone book:\n"); | ||
168 | it = eina_hash_iterator_data_new(phone_book); | ||
169 | while (eina_iterator_next(it, &data)) | ||
170 | { | ||
171 | const char *number = data; | ||
172 | printf("%s\n", number); | ||
173 | } | ||
174 | eina_iterator_free(it); | ||
175 | printf("\n"); | ||
176 | |||
177 | // Check how many items are in the phone book | ||
178 | printf("There are %d items in the hash.\n\n", | ||
179 | eina_hash_population(phone_book)); | ||
180 | |||
181 | // Change the name (key) on an entry | ||
182 | int32_t id6 = 6; | ||
183 | eina_hash_move(phone_book, &id5, &id6); | ||
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 | } | ||