sync with sylpheed 0.6.1cvs2
[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 #include "utils.h"
25
26 /* alfons - hashed string table (I wasn't content with GStringChunk; 
27  * can't recall why :-) */
28
29 #define XXX_DEBUG \
30         debug_print  
31  
32 typedef struct StringEntry_ {
33         gint    ref_count;
34         gchar  *string;
35 } StringEntry;
36
37 static StringEntry *string_entry_new(const gchar *str)
38 {
39         StringEntry *entry;
40
41         entry = g_new0(StringEntry, 1);
42         entry->ref_count = 1;
43         entry->string = g_strdup(str);
44         return entry;
45 }
46
47 static void string_entry_free(StringEntry *entry)
48 {
49         g_return_if_fail(entry != NULL);
50         g_return_if_fail(entry->string);
51
52         g_free(entry->string);
53         g_free(entry);
54 }
55
56 StringTable *string_table_new(void)
57 {
58         StringTable *strtable;
59
60         strtable = g_new0(StringTable, 1);
61         g_return_val_if_fail(strtable != NULL, NULL);
62         strtable->hash_table = g_hash_table_new(g_str_hash, g_str_equal);
63         g_return_val_if_fail(strtable->hash_table, NULL);
64         return strtable;
65 }
66
67 gchar *string_table_lookup_string(StringTable *table, gchar *str)
68 {
69         StringEntry *entry = g_hash_table_lookup(table->hash_table,
70                                                  (gconstpointer)str);
71         if (!entry) {
72                 return NULL;
73         } else {
74                 return entry->string;
75         }
76 }
77
78 gchar *string_table_insert_string(StringTable *table, gchar *str)
79 {
80         gchar *key = NULL;
81         StringEntry *entry = NULL;
82
83         if (g_hash_table_lookup_extended
84                 (table->hash_table, str, (gpointer *)&key, (gpointer *)&entry)) {
85                 entry->ref_count++;
86                 XXX_DEBUG ("ref++ for %s (%d)\n", entry->string, entry->ref_count);
87         } else {
88                 entry = string_entry_new(str);
89                 XXX_DEBUG ("inserting %s\n", str);
90                 /* insert entry->string instead of str, since it can be
91                  * invalid pointer after this. */
92                 g_hash_table_insert(table->hash_table, entry->string, entry);
93         }
94
95         return entry->string;
96 }
97
98 void string_table_free_string(StringTable *table, gchar *str)
99 {
100         StringEntry *entry;
101
102         entry = g_hash_table_lookup(table->hash_table, str);
103
104         if (entry) {
105                 entry->ref_count--;
106                 if (entry->ref_count <= 0) {
107                         XXX_DEBUG ("refcount of string %s dropped to zero\n", entry->string);
108                         g_hash_table_remove(table->hash_table, str);
109                         g_free(entry->string);
110                         g_free(entry);
111                 } else {
112                         XXX_DEBUG ("ref-- for %s (%d)\n", entry->string, entry->ref_count); 
113                 }
114         }
115 }
116
117 static gboolean string_table_remove_for_each_fn(gchar *key, StringEntry *entry,
118                                                 gpointer user_data)
119 {
120         g_return_val_if_fail(key != NULL, TRUE);
121         g_return_val_if_fail(entry != NULL, TRUE);
122
123         g_free(entry->string);
124         g_free(entry);
125
126         return TRUE;
127 }
128
129 void string_table_free(StringTable *table)
130 {
131         g_return_if_fail(table != NULL);
132         g_return_if_fail(table->hash_table != NULL);
133
134         g_hash_table_foreach_remove(table->hash_table,
135                                     (GHRFunc)string_table_remove_for_each_fn,
136                                     NULL);
137         g_hash_table_destroy(table->hash_table);
138         g_free(table);
139 }
140
141 static void string_table_stats_for_each_fn(gchar *key, StringEntry *entry,
142                                            guint *totals)
143 {
144         if (entry->ref_count > 1) {
145                 *totals += strlen(key) * (entry->ref_count - 1);
146         }
147 }
148
149 void string_table_get_stats(StringTable *table)
150 {
151         guint totals = 0;
152
153         g_hash_table_foreach(table->hash_table,
154                              (GHFunc)string_table_stats_for_each_fn, &totals);
155         XXX_DEBUG ("TOTAL UNSPILLED %d (%dK)\n", totals, totals / 1024);
156 }