1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
/*
modules.c : irssi
Copyright (C) 1999-2000 Timo Sirainen
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "module.h"
#include "modules.h"
static GHashTable *uniqids, *uniqstrids;
static GHashTable *idlookup, *stridlookup;
static int next_uniq_id;
/* return unique number across all modules for `id' */
int module_get_uniq_id(const char *module, int id)
{
GHashTable *ids;
gpointer origkey, uniqid;
int ret;
g_return_val_if_fail(module != NULL, -1);
ids = g_hash_table_lookup(idlookup, module);
if (ids == NULL) {
/* new module */
ids = g_hash_table_new((GHashFunc) g_direct_hash, (GCompareFunc) g_direct_equal);
g_hash_table_insert(idlookup, g_strdup(module), ids);
}
if (!g_hash_table_lookup_extended(ids, GINT_TO_POINTER(id), &origkey, &uniqid)) {
/* not found */
ret = next_uniq_id++;
g_hash_table_insert(ids, GINT_TO_POINTER(id), GINT_TO_POINTER(ret));
g_hash_table_insert(uniqids, GINT_TO_POINTER(ret), GINT_TO_POINTER(id));
} else {
ret = GPOINTER_TO_INT(uniqid);
}
return ret;
}
/* return unique number across all modules for `id' */
int module_get_uniq_id_str(const char *module, const char *id)
{
GHashTable *ids;
gpointer origkey, uniqid;
int ret;
g_return_val_if_fail(module != NULL, -1);
ids = g_hash_table_lookup(stridlookup, module);
if (ids == NULL) {
/* new module */
ids = g_hash_table_new((GHashFunc) g_str_hash, (GCompareFunc) g_str_equal);
g_hash_table_insert(stridlookup, g_strdup(module), ids);
}
if (!g_hash_table_lookup_extended(ids, id, &origkey, &uniqid)) {
/* not found */
char *saveid;
saveid = g_strdup(id);
ret = next_uniq_id++;
g_hash_table_insert(ids, saveid, GINT_TO_POINTER(ret));
g_hash_table_insert(uniqstrids, GINT_TO_POINTER(ret), saveid);
} else {
ret = GPOINTER_TO_INT(uniqid);
}
return ret;
}
/* returns the original module specific id, -1 = not found */
int module_find_id(const char *module, int uniqid)
{
GHashTable *ids;
gpointer origkey, id;
int ret;
g_return_val_if_fail(module != NULL, -1);
ret = g_hash_table_lookup_extended(uniqids, GINT_TO_POINTER(uniqid), &origkey, &id) ?
GPOINTER_TO_INT(id) : -1;
if (ret != -1) {
/* check that module matches */
ids = g_hash_table_lookup(idlookup, module);
if (ids == NULL || !g_hash_table_lookup_extended(ids, GINT_TO_POINTER(ret), &origkey, &id))
ret = -1;
}
return ret;
}
/* returns the original module specific id, NULL = not found */
const char *module_find_id_str(const char *module, int uniqid)
{
GHashTable *ids;
gpointer origkey, id;
const char *ret;
g_return_val_if_fail(module != NULL, NULL);
ret = g_hash_table_lookup_extended(uniqstrids, GINT_TO_POINTER(uniqid),
&origkey, &id) ? id : NULL;
if (ret != NULL) {
/* check that module matches */
ids = g_hash_table_lookup(stridlookup, module);
if (ids == NULL || !g_hash_table_lookup_extended(ids, GINT_TO_POINTER(ret), &origkey, &id))
ret = NULL;
}
return ret;
}
static void gh_uniq_destroy(gpointer key, gpointer value)
{
g_hash_table_remove(uniqids, value);
}
static void gh_uniq_destroy_str(gpointer key, gpointer value)
{
g_hash_table_remove(uniqstrids, value);
g_free(key);
}
/* Destroy unique IDs from `module'. This function is automatically called
when module is destroyed with module's name as the parameter. */
void module_uniq_destroy(const char *module)
{
GHashTable *ids;
gpointer key;
if (g_hash_table_lookup_extended(idlookup, module, &key, (gpointer *) &ids)) {
g_hash_table_remove(idlookup, key);
g_free(key);
g_hash_table_foreach(ids, (GHFunc) gh_uniq_destroy, NULL);
g_hash_table_destroy(ids);
}
if (g_hash_table_lookup_extended(stridlookup, module, &key, (gpointer *) &ids)) {
g_hash_table_remove(stridlookup, key);
g_free(key);
g_hash_table_foreach(ids, (GHFunc) gh_uniq_destroy_str, NULL);
g_hash_table_destroy(ids);
}
}
void modules_init(void)
{
idlookup = g_hash_table_new((GHashFunc) g_str_hash, (GCompareFunc) g_str_equal);
uniqids = g_hash_table_new((GHashFunc) g_direct_hash, (GCompareFunc) g_direct_equal);
stridlookup = g_hash_table_new((GHashFunc) g_str_hash, (GCompareFunc) g_str_equal);
uniqstrids = g_hash_table_new((GHashFunc) g_direct_hash, (GCompareFunc) g_direct_equal);
next_uniq_id = 0;
}
void modules_deinit(void)
{
g_hash_table_foreach(idlookup, (GHFunc) module_uniq_destroy, NULL);
g_hash_table_destroy(idlookup);
g_hash_table_destroy(uniqids);
g_hash_table_foreach(stridlookup, (GHFunc) module_uniq_destroy, NULL);
g_hash_table_destroy(stridlookup);
g_hash_table_destroy(uniqstrids);
}
|