2 * Implementation for condition handler.
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "conditions.h"
30 /* container for condition classes */
31 static GHashTable *classes = NULL;
33 /* condition data structure declaration */
38 _cnd_reset reset_func;
41 /* structure used to store class functions in GHashTable */
42 typedef struct _cnd_class{
43 _cnd_constr constr_func;
44 _cnd_destr destr_func;
46 _cnd_reset reset_func;
49 /* helper function prototypes */
50 static void _cnd_init(void);
51 static void _cnd_find_hash_key_for_class_id(gpointer, gpointer, gpointer);
53 condition *cnd_new(const char *class_id, ...) {
55 condition *cnd = NULL;
56 condition *cnd_ref = NULL;
57 _cnd_class *cls = NULL;
60 /* check if hash table is already initialized */
63 /* get class structure for this id */
64 if ((cls = (_cnd_class *)g_hash_table_lookup(classes, class_id)) == NULL) {
65 g_warning("cnd_new: Couldn't find class ID \"%s\"", class_id);
69 /* initialize the basic structure */
70 if ((cnd_ref = (condition *)g_malloc(sizeof(condition))) == NULL)
72 cnd_ref->user_data = NULL;
73 cnd_ref->eval_func = cls->eval_func;
74 cnd_ref->reset_func = cls->reset_func;
76 cnd_ref->class_id = g_strdup(class_id);
78 /* perform class specific initialization */
79 va_start(ap, class_id);
80 cnd = (cls->constr_func)(cnd_ref, ap);
83 /* check for successful construction */
91 void cnd_delete(condition *cnd) {
92 _cnd_class *cls = NULL;
94 /* check for valid pointer */
98 class_id = cnd->class_id;
99 /* check if hash table is already initialized */
101 /* get the condition class */
102 cls = (_cnd_class *)g_hash_table_lookup(classes, class_id);
103 /* call class specific destructor */
105 (cls->destr_func)(cnd);
107 g_free(cnd->class_id);
108 /* free basic structure */
110 } /* END cnd_delete() */
112 gboolean cnd_eval(condition *cnd, ...) {
114 gboolean ret_val = FALSE;
118 /* call specific handler */
120 ret_val = (cnd->eval_func)(cnd, ap);
123 } /* END cnd_eval() */
125 void cnd_reset(condition *cnd) {
127 (cnd->reset_func)(cnd);
128 } /* END cnd_reset() */
130 void* cnd_get_user_data(condition *cnd) {
131 return cnd->user_data;
132 } /* END cnd_get_user_data() */
134 void cnd_set_user_data(condition *cnd, void *user_data) {
135 cnd->user_data = user_data;
136 } /* END cnd_set_user_data() */
138 gboolean cnd_register_class(const char *class_id,
139 _cnd_constr constr_func,
140 _cnd_destr destr_func,
142 _cnd_reset reset_func) {
144 _cnd_class *cls = NULL;
145 /* check for valid parameters */
146 if ((constr_func == NULL) || (destr_func == NULL) ||
147 (eval_func == NULL) || (reset_func == NULL) || (class_id == NULL))
149 /* check if hash table is already initialized */
151 /* check for unique class id */
152 if (g_hash_table_lookup(classes, class_id) != NULL) {
153 g_warning("cnd_register_class: Duplicate class ID \"%s\"", class_id);
156 /* GHashTable keys need to be persistent for the lifetime of the hash
157 table. Allocate memory and copy the class id which we use as key. */
158 key = g_strdup(class_id);
159 /* initialize class structure */
160 if ((cls = (_cnd_class*)g_malloc(sizeof(_cnd_class))) == NULL) {
164 cls->constr_func = constr_func;
165 cls->destr_func = destr_func;
166 cls->eval_func = eval_func;
167 cls->reset_func = reset_func;
168 /* insert new class */
169 g_hash_table_insert(classes, key, cls);
171 } /* END cnd_register_class() */
173 static char *pkey = NULL;
174 void cnd_unregister_class(const char* class_id) {
175 const char *key = (const char*)class_id;
176 _cnd_class *cls = NULL;
177 /* check if hash table is already initialized */
179 /* find the key for this class id and store it in 'pkey' */
180 g_hash_table_foreach(classes,
181 _cnd_find_hash_key_for_class_id,
183 /* find the class structure for this class id */
184 cls = (_cnd_class*)g_hash_table_lookup(classes, class_id);
185 /* remove constructor from hash table */
186 g_hash_table_remove(classes, class_id);
192 } /* END cnd_unregister_class() */
195 * Initialize hash table.
197 static void _cnd_init(void) {
200 /* create hash table, we use strings as keys */
201 classes = g_hash_table_new(g_str_hash, g_str_equal);
202 } /* END _cnd_init() */
205 * Callback for function 'g_hash_table_foreach()'.
206 * We don't keep references to hash table keys. Keys have memory allocated
207 * which must be freed when they are not used anymore. This function finds
208 * the reference to a key corresponding to a particular class id. The reference
209 * to the key is stored in a global variable.
211 void _cnd_find_hash_key_for_class_id(gpointer key,
213 gpointer user_data) {
214 char *class_id = (char *)user_data;
215 char *key_value = (char *)key;
216 if (strcmp(class_id, key_value) == 0)
218 } /* END _cnd_find_hash_key_for_class_id() */
226 * indent-tabs-mode: nil
229 * ex: set shiftwidth=2 tabstop=8 expandtab:
230 * :indentSize=2:tabSize=8:noTabs=true: