diff options
Diffstat (limited to 'libraries/eina/src/examples/eina_hash_08.c')
-rw-r--r-- | libraries/eina/src/examples/eina_hash_08.c | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/libraries/eina/src/examples/eina_hash_08.c b/libraries/eina/src/examples/eina_hash_08.c new file mode 100644 index 0000000..368a2fb --- /dev/null +++ b/libraries/eina/src/examples/eina_hash_08.c | |||
@@ -0,0 +1,125 @@ | |||
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 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 char *name = key; | ||
41 | const char *number = data; | ||
42 | printf("%s: %s\n", name, 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 | const char *entry_name = "Heitor Villa-Lobos"; | ||
54 | int entry_size; | ||
55 | const char *saved_entry_name = "Alceu Valenca"; | ||
56 | int saved_entry_size = sizeof("Alceu Valenca"); | ||
57 | const 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_superfast_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 | // Delete entries | ||
74 | r = eina_hash_del(phone_book, entry_name, NULL); | ||
75 | printf("Hash entry successfully deleted? %d\n\n", r); | ||
76 | |||
77 | int hash = eina_hash_superfast("Ludwig van Beethoven", | ||
78 | sizeof("Ludwig van Beethoven")); | ||
79 | |||
80 | r = eina_hash_del_by_key_hash(phone_book, "Ludwig van Beethoven", | ||
81 | sizeof("Ludwig van Beethoven"), hash); | ||
82 | printf("Hash entry successfully deleted? %d\n\n", r); | ||
83 | |||
84 | r = eina_hash_del_by_key(phone_book, "Richard Georg Strauss"); | ||
85 | printf("Hash entry successfully deleted? %d\n\n", r); | ||
86 | |||
87 | // add entry by hash | ||
88 | entry_name = "Raul_Seixas"; | ||
89 | entry_size = sizeof("Raul Seixas"); | ||
90 | phone = strdup("+33 33 333-33333"); | ||
91 | hash = eina_hash_superfast(entry_name, entry_size); | ||
92 | eina_hash_add_by_hash(phone_book, entry_name, entry_size, hash, phone); | ||
93 | |||
94 | // don't need to free 'phone' after the next del: | ||
95 | r = eina_hash_del_by_data(phone_book, phone); | ||
96 | printf("Hash entry successfully deleted? %d\n\n", r); | ||
97 | |||
98 | // add entry by hash directly - no copy of the key will be done | ||
99 | hash = eina_hash_superfast(saved_entry_name, saved_entry_size); | ||
100 | phone = strdup("+44 44 444-44444"); | ||
101 | eina_hash_direct_add_by_hash(phone_book, saved_entry_name, | ||
102 | saved_entry_size, hash, phone); | ||
103 | |||
104 | // find the added entry by its hash: | ||
105 | phone = eina_hash_find_by_hash(phone_book, saved_entry_name, | ||
106 | saved_entry_size, hash); | ||
107 | if (phone) | ||
108 | { | ||
109 | char *newphone = strdup("+55 55 555-55555"); | ||
110 | phone = eina_hash_modify_by_hash(phone_book, saved_entry_name, | ||
111 | saved_entry_size, hash, newphone); | ||
112 | if (phone) | ||
113 | printf("changing phone to %s, old one was %s\n", newphone, phone); | ||
114 | else | ||
115 | printf("couldn't modify entry identified by %d\n", hash); | ||
116 | } | ||
117 | else | ||
118 | { | ||
119 | printf("couldn't find entry identified by %d\n", hash); | ||
120 | } | ||
121 | |||
122 | eina_hash_free(phone_book); | ||
123 | |||
124 | eina_shutdown(); | ||
125 | } | ||