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