diff options
author | David Walter Seikel | 2013-01-13 17:29:19 +1000 |
---|---|---|
committer | David Walter Seikel | 2013-01-13 17:29:19 +1000 |
commit | 07274513e984f0b5544586c74508ccd16e7dcafa (patch) | |
tree | b32ff2a9136fbc1a4a6a0ed1e4d79cde0f5f16d9 /libraries/eina/src/examples/eina_hash_06.c | |
parent | Added Irrlicht 1.8, but without all the Windows binaries. (diff) | |
download | SledjHamr-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_06.c')
-rw-r--r-- | libraries/eina/src/examples/eina_hash_06.c | 201 |
1 files changed, 0 insertions, 201 deletions
diff --git a/libraries/eina/src/examples/eina_hash_06.c b/libraries/eina/src/examples/eina_hash_06.c deleted file mode 100644 index 6e3c451..0000000 --- a/libraries/eina/src/examples/eina_hash_06.c +++ /dev/null | |||
@@ -1,201 +0,0 @@ | |||
1 | //Compile with: | ||
2 | //gcc -g eina_hash_06.c -o eina_hash_06 `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 | |||
18 | struct _Phone_Entry { | ||
19 | int64_t id; // Full name. | ||
20 | const char *number; // Phone number. | ||
21 | }; | ||
22 | |||
23 | typedef struct _Phone_Entry Phone_Entry; | ||
24 | |||
25 | static Phone_Entry _start_entries[] = { | ||
26 | { 1, "+01 23 456-78910" }, | ||
27 | { 2, "+12 34 567-89101" }, | ||
28 | { 3, "+23 45 678-91012" }, | ||
29 | { 4, "+34 56 789-10123" }, | ||
30 | { -1, NULL } | ||
31 | }; // _start_entries | ||
32 | |||
33 | static void | ||
34 | _phone_entry_free_cb(void *data) | ||
35 | { | ||
36 | free(data); | ||
37 | } | ||
38 | |||
39 | static Eina_Bool | ||
40 | _phone_book_foreach_cb(const Eina_Hash *phone_book, const void *key, | ||
41 | void *data, void *fdata) | ||
42 | { | ||
43 | const int64_t *id = key; | ||
44 | const char *number = data; | ||
45 | printf("%ld: %s\n", *id, number); | ||
46 | |||
47 | // Return EINA_FALSE to stop this callback from being called | ||
48 | return EINA_TRUE; | ||
49 | } | ||
50 | |||
51 | int | ||
52 | main(int argc, const char *argv[]) | ||
53 | { | ||
54 | Eina_Hash *phone_book = NULL; | ||
55 | int i; | ||
56 | int64_t entry_id = 4; | ||
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_int64_new(_phone_entry_free_cb); | ||
65 | |||
66 | // Add initial entries to our hash | ||
67 | for (i = 0; _start_entries[i].id != -1; i++) | ||
68 | { | ||
69 | eina_hash_add(phone_book, &_start_entries[i].id, | ||
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_id); | ||
75 | if (phone) | ||
76 | { | ||
77 | printf("Printing entry.\n"); | ||
78 | printf("Id: %ld\n", entry_id); | ||
79 | printf("Number: %s\n\n", phone); | ||
80 | } | ||
81 | |||
82 | // Delete this entry | ||
83 | r = eina_hash_del(phone_book, &entry_id, 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 | int64_t id3 = 3; | ||
88 | phone = eina_hash_modify(phone_book, &id3, | ||
89 | strdup("+23 45 111-11111")); | ||
90 | free(phone); | ||
91 | |||
92 | // Modify or add an entry to the hash with eina_hash_set | ||
93 | // Let's first add a new entry | ||
94 | int64_t id5 = 5; | ||
95 | eina_error_set(0); | ||
96 | phone = eina_hash_set(phone_book, &id5, | ||
97 | strdup("+55 01 234-56789")); | ||
98 | if (!phone) | ||
99 | { | ||
100 | Eina_Error err = eina_error_get(); | ||
101 | if (!err) | ||
102 | { | ||
103 | printf("No previous phone found for id5. "); | ||
104 | printf("Creating new entry.\n"); | ||
105 | } | ||
106 | else | ||
107 | printf("Error when setting phone for Raul Seixas\n"); | ||
108 | } | ||
109 | else | ||
110 | { | ||
111 | printf("Old phone for id5 was %s\n", phone); | ||
112 | free(phone); | ||
113 | } | ||
114 | |||
115 | printf("\n"); | ||
116 | |||
117 | // Now change the phone number | ||
118 | eina_error_set(0); | ||
119 | phone = eina_hash_set(phone_book, &id5, | ||
120 | strdup("+55 02 222-22222")); | ||
121 | if (phone) | ||
122 | { | ||
123 | printf("Changing phone for id5 to +55 02 222-22222. "); | ||
124 | printf("Old phone was %s\n", phone); | ||
125 | free(phone); | ||
126 | } | ||
127 | else | ||
128 | { | ||
129 | Eina_Error err = eina_error_get(); | ||
130 | if (err) | ||
131 | printf("Error when changing phone for id5\n"); | ||
132 | else | ||
133 | { | ||
134 | printf("No previous phone found for id5. "); | ||
135 | printf("Creating new entry.\n"); | ||
136 | } | ||
137 | } | ||
138 | |||
139 | // There are many ways to iterate over our Phone book. | ||
140 | // First, iterate showing the names and associated numbers. | ||
141 | printf("List of phones:\n"); | ||
142 | eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL); | ||
143 | printf("\n"); | ||
144 | |||
145 | // Now iterate using an iterator | ||
146 | printf("List of phones:\n"); | ||
147 | it = eina_hash_iterator_tuple_new(phone_book); | ||
148 | while (eina_iterator_next(it, &data)) | ||
149 | { | ||
150 | Eina_Hash_Tuple *t = data; | ||
151 | const int64_t *id = t->key; | ||
152 | const char *number = t->data; | ||
153 | printf("%ld: %s\n", *id, number); | ||
154 | } | ||
155 | eina_iterator_free(it); // Always free the iterator after its use | ||
156 | printf("\n"); | ||
157 | |||
158 | // Just iterate over the keys (names) | ||
159 | printf("List of ids in the phone book:\n"); | ||
160 | it = eina_hash_iterator_key_new(phone_book); | ||
161 | while (eina_iterator_next(it, &data)) | ||
162 | { | ||
163 | const int64_t *id = data; | ||
164 | printf("%ld\n", *id); | ||
165 | } | ||
166 | eina_iterator_free(it); | ||
167 | printf("\n"); | ||
168 | |||
169 | // Just iterate over the data (numbers) | ||
170 | printf("List of numbers in the phone book:\n"); | ||
171 | it = eina_hash_iterator_data_new(phone_book); | ||
172 | while (eina_iterator_next(it, &data)) | ||
173 | { | ||
174 | const char *number = data; | ||
175 | printf("%s\n", number); | ||
176 | } | ||
177 | eina_iterator_free(it); | ||
178 | printf("\n"); | ||
179 | |||
180 | // Check how many items are in the phone book | ||
181 | printf("There are %d items in the hash.\n\n", | ||
182 | eina_hash_population(phone_book)); | ||
183 | |||
184 | // Change the name (key) on an entry | ||
185 | int64_t id6 = 6; | ||
186 | eina_hash_move(phone_book, &id5, &id6); | ||
187 | printf("List of phones after change:\n"); | ||
188 | eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL); | ||
189 | printf("\n"); | ||
190 | |||
191 | // Empty the phone book, but don't destroy it | ||
192 | eina_hash_free_buckets(phone_book); | ||
193 | printf("There are %d items in the hash.\n\n", | ||
194 | eina_hash_population(phone_book)); | ||
195 | |||
196 | // Phone book could still be used, but we are freeing it since we are | ||
197 | // done for now | ||
198 | eina_hash_free(phone_book); | ||
199 | |||
200 | eina_shutdown(); | ||
201 | } | ||