put back includes, cleaner ssl certs popups
[claws.git] / src / stringtable.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include <glib.h>
21 #include <string.h>
22
23 #include "stringtable.h"
24
25 /* alfons - hashed string table (I wasn't content with GStringChunk; 
26  * can't recall why :-) */
27
28 #if 0
29 #define XXX_DEBUG \
30         debug_print
31 #else
32 #define XXX_DEBUG \
33         if (0) debug_print
34 #endif
35
36 typedef struct StringEntry_ {
37         gint    ref_count;
38         gchar  *string;
39 } StringEntry;
40
41 static StringEntry *string_entry_new(const gchar *str)
42 {
43         StringEntry *entry;
44
45         entry = g_new0(StringEntry, 1);
46         entry->ref_count = 1;
47         entry->string = g_strdup(str);
48         return entry;
49 }
50
51 StringTable *string_table_new(void)
52 {
53         StringTable *strtable;
54
55         strtable = g_new0(StringTable, 1);
56         g_return_val_if_fail(strtable != NULL, NULL);
57         strtable->hash_table = g_hash_table_new(g_str_hash, g_str_equal);
58         g_return_val_if_fail(strtable->hash_table, NULL);
59         return strtable;
60 }
61
62 gchar *string_table_lookup_string(StringTable *table, gchar *str)
63 {
64         StringEntry *entry = g_hash_table_lookup(table->hash_table,
65                                                  (gconstpointer)str);
66         if (!entry) {
67                 return NULL;
68         } else {
69                 return entry->string;
70         }
71 }
72
73 gchar *string_table_insert_string(StringTable *table, gchar *str)
74 {
75         gchar *key = NULL;
76         StringEntry *entry = NULL;
77
78         if (g_hash_table_lookup_extended
79                 (table->hash_table, str, (gpointer *)&key, (gpointer *)&entry)) {
80                 entry->ref_count++;
81                 XXX_DEBUG ("ref++ for %s (%d)\n", entry->string, entry->ref_count);
82         } else {
83                 entry = string_entry_new(str);
84                 XXX_DEBUG ("inserting %s\n", str);
85                 /* insert entry->string instead of str, since it can be
86                  * invalid pointer after this. */
87                 g_hash_table_insert(table->hash_table, entry->string, entry);
88         }
89
90         return entry->string;
91 }
92
93 void string_table_free_string(StringTable *table, gchar *str)
94 {
95         StringEntry *entry;
96
97         entry = g_hash_table_lookup(table->hash_table, str);
98
99         if (entry) {
100                 entry->ref_count--;
101                 if (entry->ref_count <= 0) {
102                         XXX_DEBUG ("refcount of string %s dropped to zero\n", entry->string);
103                         g_hash_table_remove(table->hash_table, str);
104                         g_free(entry->string);
105                         g_free(entry);
106                 } else {
107                         XXX_DEBUG ("ref-- for %s (%d)\n", entry->string, entry->ref_count); 
108                 }
109         }
110 }
111
112 static gboolean string_table_remove_for_each_fn(gchar *key, StringEntry *entry,
113                                                 gpointer user_data)
114 {
115         g_return_val_if_fail(key != NULL, TRUE);
116         g_return_val_if_fail(entry != NULL, TRUE);
117
118         g_free(entry->string);
119         g_free(entry);
120
121         return TRUE;
122 }
123
124 void string_table_free(StringTable *table)
125 {
126         g_return_if_fail(table != NULL);
127         g_return_if_fail(table->hash_table != NULL);
128
129         g_hash_table_foreach_remove(table->hash_table,
130                                     (GHRFunc)string_table_remove_for_each_fn,
131                                     NULL);
132         g_hash_table_destroy(table->hash_table);
133         g_free(table);
134 }
135
136 static void string_table_stats_for_each_fn(gchar *key, StringEntry *entry,
137                                            guint *totals)
138 {
139         if (entry->ref_count > 1) {
140                 *totals += strlen(key) * (entry->ref_count - 1);
141         }
142 }
143
144 void string_table_get_stats(StringTable *table)
145 {
146         guint totals = 0;
147
148         g_hash_table_foreach(table->hash_table,
149                              (GHFunc)string_table_stats_for_each_fn, &totals);
150         XXX_DEBUG ("TOTAL UNSPILLED %d (%dK)\n", totals, totals / 1024);
151 }