2 //gcc -g eina_hash_08.c -o eina_hash_08 `pkg-config --cflags --libs eina`
9 * Eina Hash - phonebook
11 * This example demonstrate the use of Eina Hash by implementing a phonebook
12 * that stores its contact data into the hash.
14 * It indexes the phone numbers by Contact Full Name, so it's a hash with
19 const char *name; // Full name.
20 const char *number; // Phone number.
23 typedef struct _Phone_Entry Phone_Entry;
25 static 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" },
34 _phone_entry_free_cb(void *data)
40 _phone_book_foreach_cb(const Eina_Hash *phone_book, const void *key,
41 void *data, void *fdata)
43 const char *name = key;
44 const char *number = data;
45 printf("%s: %s\n", name, number);
47 // Return EINA_FALSE to stop this callback from being called
52 main(int argc, const char *argv[])
54 Eina_Hash *phone_book = NULL;
56 const char *entry_name = "Heitor Villa-Lobos";
58 const char *saved_entry_name = "Alceu Valenca";
59 int saved_entry_size = sizeof("Alceu Valenca");
60 const char *phone = NULL;
67 phone_book = eina_hash_string_superfast_new(_phone_entry_free_cb);
69 // Add initial entries to our hash
70 for (i = 0; _start_entries[i].name != NULL; i++)
72 eina_hash_add(phone_book, _start_entries[i].name,
73 strdup(_start_entries[i].number));
77 r = eina_hash_del(phone_book, entry_name, NULL);
78 printf("Hash entry successfully deleted? %d\n\n", r);
80 int hash = eina_hash_superfast("Ludwig van Beethoven",
81 sizeof("Ludwig van Beethoven"));
83 r = eina_hash_del_by_key_hash(phone_book, "Ludwig van Beethoven",
84 sizeof("Ludwig van Beethoven"), hash);
85 printf("Hash entry successfully deleted? %d\n\n", r);
87 r = eina_hash_del_by_key(phone_book, "Richard Georg Strauss");
88 printf("Hash entry successfully deleted? %d\n\n", r);
91 entry_name = "Raul_Seixas";
92 entry_size = sizeof("Raul Seixas");
93 phone = strdup("+33 33 333-33333");
94 hash = eina_hash_superfast(entry_name, entry_size);
95 eina_hash_add_by_hash(phone_book, entry_name, entry_size, hash, phone);
97 // don't need to free 'phone' after the next del:
98 r = eina_hash_del_by_data(phone_book, phone);
99 printf("Hash entry successfully deleted? %d\n\n", r);
101 // add entry by hash directly - no copy of the key will be done
102 hash = eina_hash_superfast(saved_entry_name, saved_entry_size);
103 phone = strdup("+44 44 444-44444");
104 eina_hash_direct_add_by_hash(phone_book, saved_entry_name,
105 saved_entry_size, hash, phone);
107 // find the added entry by its hash:
108 phone = eina_hash_find_by_hash(phone_book, saved_entry_name,
109 saved_entry_size, hash);
112 char *newphone = strdup("+55 55 555-55555");
113 phone = eina_hash_modify_by_hash(phone_book, saved_entry_name,
114 saved_entry_size, hash, newphone);
116 printf("changing phone to %s, old one was %s\n", newphone, phone);
118 printf("couldn't modify entry identified by %d\n", hash);
122 printf("couldn't find entry identified by %d\n", hash);
125 eina_hash_free(phone_book);