· 8 years ago · Apr 27, 2018, 04:22 AM
1/*!
2 * \file use_hash_table.c
3 * \brief Example code of using a hash table for key-value pairs.
4 */
5
6#include <glib.h>
7
8/*!
9 * \brief Create a hash table from file.
10 */
11int
12create_hash_table_from_file (gchar *filename)
13{
14 FILE *fp
15 char buf[1024];
16
17 /* Intialise the hash table. */
18 table = g_hash_table_new (g_str_hash, g_str_equal);
19 /* Just a standard way to open files. */
20 fp = fopen (filename, "r");
21 /* If file does not exist, exit. */
22 if (!fp)
23 {
24 exit (1);
25 }
26 /* Read file line by line. */
27 while (fgets (buf, sizeof (buf), fp))
28 {
29 char *key;
30 char *value;
31 /* Get the first and the second field. */
32 key = strtok (buf, "\t");
33 if (!key) continue;
34 value = strtok (NULL, "\t");
35 if (!value) continue;
36 /* Look up the table for an existing key.
37 * If it exists replace it by inserting the new ke-value pair and
38 * freeing the old key-value pair */
39 char *old_key;
40 char *old_value;
41 /* Try looking up this key. */
42 if (g_hash_table_lookup_extended (table, key, &old_key, &old_value))
43 {
44 /* Insert the new value */
45 g_hash_table_insert (table, g_strdup (key), g_strdup (value));
46 /* Just free the key and value */
47 g_free (old_key);
48 g_free (old_value);
49 }
50 else
51 {
52 /* Insert into our hash table it is not a duplicate. */
53 g_hash_table_insert (table, g_strdup (key), g_strdup (value));
54 }
55 }
56 /* Close the file when done. */
57 fclose (fp);
58 return (EXIT_SUCCESS);
59}
60
61/*!
62 * \brief Dispose of the hash table.
63 */
64int
65destroy_hash_table ()
66{
67 g_hash_table_destroy (table);
68 return (EXIT_SUCCESS);
69}
70
71
72/*!
73 * \brief Remove the entry with the passed key from the hash table.
74 */
75int
76remove_entry (char *key)
77{
78 int result = EXIT_FAILURE;
79 char *old_key;
80 char *old_value;
81 /* Try looking up this key */
82 if (g_hash_table_lookup_extended (table, key, &old_key, &old_value))
83 {
84 /* Remove the entry in the hash table. */
85 g_hash_table_remove (table, key);
86 /* Just free the key and value. */
87 g_free( old_key);
88 g_free (old_value);
89 result = EXIT_SUCCESS;
90 }
91 return (result);
92}
93
94
95/*!
96 * \brief Free a key-value pair inside the hash table.
97 */
98static void
99free_a_hash_table_entry (gpointer key, gpointer value, gpointer user_data)
100{
101 g_free (key);
102 g_free (value);
103}
104
105
106/*!
107 * \brief Free all key-value entries in the hash table.
108 */
109int
110free_all_key_value_entries ()
111{
112 g_hash_table_foreach (table, free_a_hash_table_entry, NULL);
113}
114
115
116static gboolean
117remove_keys_with_A (gpointer key, gpointer value, gpointer user_data)
118{
119 char *char_value = (char *) value;
120
121 if (char_value[0] == 'A')
122 {
123 g_free (key);
124 g_free (value);
125 return TRUE;
126 }
127 else
128 {
129 return FALSE;
130 }
131}
132
133
134/*!
135 * \brief Let's have a main function.
136 */
137int
138main ()
139{
140 GHashTable *table;
141 int deleted;
142 gchar file_name = test.txt";
143
144 create_hash_table_from_file (file_name);
145 /* Do something with the hash table here. */
146 deleted = g_hash_table_foreach_remove (table, remove_keys_with_A, NULL);
147 printf ("Deleted %d items!\n", deleted);
148 free_all_key_value_entries ();
149 destroy_hash_table ();
150}
151
152
153/* EOF */