aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/eina/src/examples/eina_hash_08.c
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/eina/src/examples/eina_hash_08.c')
-rw-r--r--libraries/eina/src/examples/eina_hash_08.c128
1 files changed, 0 insertions, 128 deletions
diff --git a/libraries/eina/src/examples/eina_hash_08.c b/libraries/eina/src/examples/eina_hash_08.c
deleted file mode 100644
index ed45f7f..0000000
--- a/libraries/eina/src/examples/eina_hash_08.c
+++ /dev/null
@@ -1,128 +0,0 @@
1//Compile with:
2//gcc -g eina_hash_08.c -o eina_hash_08 `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 void
34_phone_entry_free_cb(void *data)
35{
36 free(data);
37}
38
39static Eina_Bool
40_phone_book_foreach_cb(const Eina_Hash *phone_book, const void *key,
41 void *data, void *fdata)
42{
43 const char *name = key;
44 const char *number = data;
45 printf("%s: %s\n", name, number);
46
47 // Return EINA_FALSE to stop this callback from being called
48 return EINA_TRUE;
49}
50
51int
52main(int argc, const char *argv[])
53{
54 Eina_Hash *phone_book = NULL;
55 int i;
56 const char *entry_name = "Heitor Villa-Lobos";
57 int entry_size;
58 const char *saved_entry_name = "Alceu Valenca";
59 int saved_entry_size = sizeof("Alceu Valenca");
60 const char *phone = NULL;
61 Eina_Bool r;
62 Eina_Iterator *it;
63 void *data;
64
65 eina_init();
66
67 phone_book = eina_hash_string_superfast_new(_phone_entry_free_cb);
68
69 // Add initial entries to our hash
70 for (i = 0; _start_entries[i].name != NULL; i++)
71 {
72 eina_hash_add(phone_book, _start_entries[i].name,
73 strdup(_start_entries[i].number));
74 }
75
76 // Delete entries
77 r = eina_hash_del(phone_book, entry_name, NULL);
78 printf("Hash entry successfully deleted? %d\n\n", r);
79
80 int hash = eina_hash_superfast("Ludwig van Beethoven",
81 sizeof("Ludwig van Beethoven"));
82
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);
86
87 r = eina_hash_del_by_key(phone_book, "Richard Georg Strauss");
88 printf("Hash entry successfully deleted? %d\n\n", r);
89
90 // add entry by hash
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);
96
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);
100
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);
106
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);
110 if (phone)
111 {
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);
115 if (phone)
116 printf("changing phone to %s, old one was %s\n", newphone, phone);
117 else
118 printf("couldn't modify entry identified by %d\n", hash);
119 }
120 else
121 {
122 printf("couldn't find entry identified by %d\n", hash);
123 }
124
125 eina_hash_free(phone_book);
126
127 eina_shutdown();
128}