From Chris Wilson:
[obnox/wireshark/wip.git] / conditions.c
1 /* conditions.c
2  * Implementation for condition handler.
3  *
4  * $Id: conditions.c,v 1.6 2004/01/18 16:20:09 jmayer Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
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   /* check if hash table is already initialized */
59   _cnd_init();
60   /* get class structure for this id */
61   if((cls = (_cnd_class*)g_hash_table_lookup(classes, class_id)) == NULL)
62     return NULL;
63   /* initialize the basic structure */
64   if((cnd_ref = (condition*)malloc(sizeof(condition))) == NULL) return NULL;
65   cnd_ref->user_data = NULL;
66   cnd_ref->eval_func = cls->eval_func;
67   cnd_ref->reset_func = cls->reset_func;
68   /* copy the class id */
69   if((id = (char*)malloc(strlen(class_id)+1)) == NULL){
70     free(cnd_ref);
71     return NULL;
72   }
73   strcpy(id, class_id);
74   cnd_ref->class_id = id;
75   /* perform class specific initialization */
76   va_start(ap, class_id);
77   cnd = (cls->constr_func)(cnd_ref, ap);
78   va_end(ap);
79   /* check for successful construction */
80   if(cnd == NULL){
81     free(cnd_ref);
82     free(id);
83   }
84   return cnd;
85 } /* END cnd_new() */
86
87 void cnd_delete(condition *cnd){
88   _cnd_class *cls = NULL;
89   const char* class_id = cnd->class_id;
90   /* check for valid pointer */
91   if(cnd == NULL) return;
92   /* check if hash table is already initialized */
93   _cnd_init();
94   /* get the condition class */
95   cls = (_cnd_class*)g_hash_table_lookup(classes, class_id);
96   /* call class specific destructor */
97   if(cls != NULL) (cls->destr_func)(cnd);
98   /* free memory */
99   free(cnd->class_id);
100   /* free basic structure */
101   free(cnd);
102 } /* END cnd_delete() */
103
104 gboolean cnd_eval(condition *cnd, ...){
105   va_list ap;
106   gboolean ret_val = FALSE;
107   /* validate cnd */
108   if(cnd == NULL) return FALSE;
109   /* call specific handler */
110   va_start(ap, cnd);
111   ret_val = (cnd->eval_func)(cnd, ap);
112   va_end(ap);
113   return ret_val;
114 } /*  END cnd_eval() */
115
116 void cnd_reset(condition *cnd){
117   if(cnd != NULL) (cnd->reset_func)(cnd);
118 } /* END cnd_reset() */
119
120 void* cnd_get_user_data(condition *cnd){
121   return cnd->user_data;
122 } /* END cnd_get_user_data() */
123
124 void cnd_set_user_data(condition *cnd, void* user_data){
125   cnd->user_data = user_data;
126 } /* END cnd_set_user_data() */
127
128 gboolean cnd_register_class(const char* class_id,
129                             _cnd_constr constr_func,
130                             _cnd_destr destr_func,
131                             _cnd_eval eval_func,
132                             _cnd_reset reset_func){
133   char* key = NULL;
134   _cnd_class *cls = NULL;
135   /* check for valid parameters */
136   if((constr_func == NULL) || (destr_func == NULL) ||
137      (eval_func == NULL) || (reset_func == NULL) || (class_id == NULL))
138     return FALSE;
139   /* check if hash table is already initialized */
140   _cnd_init();
141   /* check for unique class id */
142   if((cls = (_cnd_class*)g_hash_table_lookup(classes, class_id)) != NULL)
143     return FALSE;
144   /* GHashTable keys need to be persistent for the lifetime of the hash
145      table. Allocate memory and copy the class id which we use as key. */
146   if((key = (char*)malloc(strlen(class_id)+1)) == NULL) return FALSE;
147   strcpy(key, class_id);
148   /* initialize class structure */
149   if((cls = (_cnd_class*)malloc(sizeof(_cnd_class))) == NULL){
150     free(key);
151     return FALSE;
152   }
153   cls->constr_func = constr_func;
154   cls->destr_func = destr_func;
155   cls->eval_func = eval_func;
156   cls->reset_func = reset_func;
157   /* insert new class */
158   g_hash_table_insert(classes, key, cls);
159   return TRUE;
160 } /* END cnd_register_class() */
161
162 static char* pkey = NULL;
163 void cnd_unregister_class(const char* class_id){
164   const char *key = (const char*)class_id;
165   _cnd_class *cls = NULL;
166   /* check if hash table is already initialized */
167   _cnd_init();
168   /* find the key for this class id and store it in 'pkey' */
169   g_hash_table_foreach(classes,
170                        _cnd_find_hash_key_for_class_id,
171                        (gpointer)key);
172   /* find the class structure for this class id */
173   cls = (_cnd_class*)g_hash_table_lookup(classes, class_id);
174   /* remove constructor from hash table */
175   g_hash_table_remove(classes, class_id);
176   /* free the key */
177   free(pkey);
178   pkey = NULL;
179   /* free the value */
180   free(cls);
181 } /* END cnd_unregister_class() */
182
183 /*
184  * Initialize hash table.
185  */
186 static void _cnd_init(void){
187   if(classes != NULL) return;
188   /* create hash table, we use strings as keys */
189   classes = g_hash_table_new(g_str_hash, g_str_equal);
190 } /* END _cnd_init() */
191
192 /*
193  * Callback for function 'g_hash_table_foreach()'.
194  * We don't keep references to hash table keys. Keys have memory allocated
195  * which must be freed when they are not used anymore. This function finds
196  * the reference to a key corresponding to a particular class id. The reference
197  * to the key is stored in a global variable.
198  */
199 void _cnd_find_hash_key_for_class_id(gpointer key,
200                                      gpointer value _U_,
201                                      gpointer user_data){
202   char* class_id = (char*)user_data;
203   char* key_value = (char*)key;
204   if(strcmp(class_id, key_value) == 0) pkey = key;
205 } /* END _cnd_find_hash_key_for_class_id() */