Fix a test.
[obnox/wireshark/wip.git] / conditions.c
1 /* conditions.c
2  * Implementation for condition handler.
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #include <string.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include "conditions.h"
29
30 /* container for condition classes */
31 static GHashTable* classes = NULL;
32
33 /* condition data structure declaration */
34 struct condition{
35   char* class_id;
36   void* user_data;
37   _cnd_eval eval_func;
38   _cnd_reset reset_func;
39 };
40
41 /* structure used to store class functions in GHashTable */
42 typedef struct _cnd_class{
43   _cnd_constr constr_func;
44   _cnd_destr destr_func;
45   _cnd_eval eval_func;
46   _cnd_reset reset_func;
47 } _cnd_class;
48
49 /* helper function prototypes */
50 static void _cnd_init(void);
51 static void _cnd_find_hash_key_for_class_id(gpointer, gpointer, gpointer);
52
53 condition* cnd_new(const char* class_id, ...){
54   va_list ap;
55   condition *cnd = NULL, *cnd_ref = NULL;
56   _cnd_class *cls = NULL;
57   char* id = NULL;
58
59   /* check if hash table is already initialized */
60   _cnd_init();
61
62   /* get class structure for this id */
63   if((cls = (_cnd_class*)g_hash_table_lookup(classes, class_id)) == NULL) {
64     g_warning("cnd_new: Couldn't find class ID \"%s\"", class_id);
65     return NULL;
66   }
67
68   /* initialize the basic structure */
69   if((cnd_ref = (condition*)g_malloc(sizeof(condition))) == NULL) return NULL;
70   cnd_ref->user_data = NULL;
71   cnd_ref->eval_func = cls->eval_func;
72   cnd_ref->reset_func = cls->reset_func;
73
74   cnd_ref->class_id = g_strdup(class_id);
75
76   /* perform class specific initialization */
77   va_start(ap, class_id);
78   cnd = (cls->constr_func)(cnd_ref, ap);
79   va_end(ap);
80
81   /* check for successful construction */
82   if(cnd == NULL){
83     g_free(cnd_ref);
84     g_free(id);
85   }
86   return cnd;
87 } /* END cnd_new() */
88
89 void cnd_delete(condition *cnd){
90   _cnd_class *cls = NULL;
91   const char* class_id;
92   /* check for valid pointer */
93   if(cnd == NULL) return;
94
95   class_id = cnd->class_id;
96   /* check if hash table is already initialized */
97   _cnd_init();
98   /* get the condition class */
99   cls = (_cnd_class*)g_hash_table_lookup(classes, class_id);
100   /* call class specific destructor */
101   if(cls != NULL) (cls->destr_func)(cnd);
102   /* free memory */
103   g_free(cnd->class_id);
104   /* free basic structure */
105   g_free(cnd);
106 } /* END cnd_delete() */
107
108 gboolean cnd_eval(condition *cnd, ...){
109   va_list ap;
110   gboolean ret_val = FALSE;
111   /* validate cnd */
112   if(cnd == NULL) return FALSE;
113   /* call specific handler */
114   va_start(ap, cnd);
115   ret_val = (cnd->eval_func)(cnd, ap);
116   va_end(ap);
117   return ret_val;
118 } /*  END cnd_eval() */
119
120 void cnd_reset(condition *cnd){
121   if(cnd != NULL) (cnd->reset_func)(cnd);
122 } /* END cnd_reset() */
123
124 void* cnd_get_user_data(condition *cnd){
125   return cnd->user_data;
126 } /* END cnd_get_user_data() */
127
128 void cnd_set_user_data(condition *cnd, void* user_data){
129   cnd->user_data = user_data;
130 } /* END cnd_set_user_data() */
131
132 gboolean cnd_register_class(const char* class_id,
133                             _cnd_constr constr_func,
134                             _cnd_destr destr_func,
135                             _cnd_eval eval_func,
136                             _cnd_reset reset_func){
137   char* key = NULL;
138   _cnd_class *cls = NULL;
139   /* check for valid parameters */
140   if((constr_func == NULL) || (destr_func == NULL) ||
141      (eval_func == NULL) || (reset_func == NULL) || (class_id == NULL))
142     return FALSE;
143   /* check if hash table is already initialized */
144   _cnd_init();
145   /* check for unique class id */
146   if(g_hash_table_lookup(classes, class_id) != NULL) {
147     g_warning("cnd_register_class: Duplicate class ID \"%s\"", class_id);
148     return FALSE;
149   }
150   /* GHashTable keys need to be persistent for the lifetime of the hash
151      table. Allocate memory and copy the class id which we use as key. */
152   key = g_strdup(class_id);
153   /* initialize class structure */
154   if((cls = (_cnd_class*)g_malloc(sizeof(_cnd_class))) == NULL){
155     g_free(key);
156     return FALSE;
157   }
158   cls->constr_func = constr_func;
159   cls->destr_func = destr_func;
160   cls->eval_func = eval_func;
161   cls->reset_func = reset_func;
162   /* insert new class */
163   g_hash_table_insert(classes, key, cls);
164   return TRUE;
165 } /* END cnd_register_class() */
166
167 static char* pkey = NULL;
168 void cnd_unregister_class(const char* class_id){
169   const char *key = (const char*)class_id;
170   _cnd_class *cls = NULL;
171   /* check if hash table is already initialized */
172   _cnd_init();
173   /* find the key for this class id and store it in 'pkey' */
174   g_hash_table_foreach(classes,
175                        _cnd_find_hash_key_for_class_id,
176                        (gpointer)key);
177   /* find the class structure for this class id */
178   cls = (_cnd_class*)g_hash_table_lookup(classes, class_id);
179   /* remove constructor from hash table */
180   g_hash_table_remove(classes, class_id);
181   /* free the key */
182   g_free(pkey);
183   pkey = NULL;
184   /* free the value */
185   g_free(cls);
186 } /* END cnd_unregister_class() */
187
188 /*
189  * Initialize hash table.
190  */
191 static void _cnd_init(void){
192   if(classes != NULL) return;
193   /* create hash table, we use strings as keys */
194   classes = g_hash_table_new(g_str_hash, g_str_equal);
195 } /* END _cnd_init() */
196
197 /*
198  * Callback for function 'g_hash_table_foreach()'.
199  * We don't keep references to hash table keys. Keys have memory allocated
200  * which must be freed when they are not used anymore. This function finds
201  * the reference to a key corresponding to a particular class id. The reference
202  * to the key is stored in a global variable.
203  */
204 void _cnd_find_hash_key_for_class_id(gpointer key,
205                                      gpointer value _U_,
206                                      gpointer user_data){
207   char* class_id = (char*)user_data;
208   char* key_value = (char*)key;
209   if(strcmp(class_id, key_value) == 0) pkey = key_value;
210 } /* END _cnd_find_hash_key_for_class_id() */
211
212 /*
213  * Editor modelines
214  *
215  * Local Variables:
216  * c-basic-offset: 2
217  * tab-width: 2
218  * indent-tabs-mode: nil
219  * End:
220  *
221  * ex: set shiftwidth=2 tabstop=2 expandtab
222  * :indentSize=2:tabSize=2:noTabs=true:
223  */