added lwes-filter-listener
[lwes.git] / src / lwes_event_type_db.c
blobce3831764a1df70a6a062c97f47517f7380e7c43
1 /*======================================================================*
2 * Copyright (C) 2008 Light Weight Event System *
3 * All rights reserved. *
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., 51 Franklin Street, Fifth Floor, *
18 * Boston, MA 02110-1301 USA. *
19 *======================================================================*/
20 #include "lwes_event_type_db.h"
21 #include "lwes_esf_parser.h"
22 #include "lwes_hash.h"
24 struct lwes_event_type_db *
25 lwes_event_type_db_create
26 (const char *filename)
28 struct lwes_event_type_db *db =
29 (struct lwes_event_type_db *)
30 malloc (sizeof (struct lwes_event_type_db));
31 if (db != NULL)
33 db->esf_filename[0] = '\0';
34 strcat (db->esf_filename, filename);
36 db->events = lwes_hash_create ();
37 if (db->events != NULL)
39 if (lwes_parse_esf (db, db->esf_filename) != 0)
41 free (db);
42 db = NULL;
45 else
47 free (db);
48 db = NULL;
52 return db;
56 int
57 lwes_event_type_db_destroy
58 (struct lwes_event_type_db *db)
60 struct lwes_hash_enumeration e;
61 struct lwes_hash_enumeration e2;
62 LWES_SHORT_STRING eventName = NULL;
63 struct lwes_hash *attrHash = NULL;
64 LWES_SHORT_STRING attrName = NULL;
65 LWES_BYTE *attrType = NULL;
66 /* clear out the hash */
67 if (lwes_hash_keys (db->events, &e))
69 while (lwes_hash_enumeration_has_more_elements (&e))
71 eventName = lwes_hash_enumeration_next_element (&e);
72 attrHash =
73 (struct lwes_hash *)lwes_hash_remove (db->events, eventName);
74 if (lwes_hash_keys (attrHash, &e2))
76 while (lwes_hash_enumeration_has_more_elements (&e2))
78 attrName = lwes_hash_enumeration_next_element (&e2);
79 attrType = (LWES_BYTE *)lwes_hash_remove (attrHash, attrName);
81 /* free the type and the name */
82 if (attrName != NULL)
83 free (attrName);
84 if (attrType != NULL)
85 free (attrType);
88 lwes_hash_destroy (attrHash);
90 if (eventName != NULL)
91 free (eventName);
94 lwes_hash_destroy (db->events);
95 if (db != NULL)
96 free (db);
98 lwes_parse_esf_destroy ();
100 return 0;
104 lwes_event_type_db_add_event
105 (struct lwes_event_type_db *db,
106 LWES_SHORT_STRING event_name)
108 int ret = 0;
109 struct lwes_hash *eventHash = NULL;
110 /* try and allocate the key */
111 LWES_SHORT_STRING eventHashKey =
112 (LWES_SHORT_STRING)malloc (sizeof (LWES_CHAR)*(strlen (event_name)+1));
113 if (eventHashKey == NULL)
115 return -3;
117 strcpy (eventHashKey, event_name);
118 /* try and allocate the value */
119 eventHash = lwes_hash_create ();
120 if (eventHash == NULL)
122 free (eventHashKey);
123 return -3;
126 /* try and place the key and value into the hash, if we fail free both */
127 ret = lwes_hash_put (db->events, eventHashKey, eventHash);
128 if ( ret < 0 )
130 free (eventHashKey);
131 lwes_hash_destroy (eventHash);
134 return ret;
138 lwes_event_type_db_add_attribute
139 (struct lwes_event_type_db *db,
140 LWES_SHORT_STRING event_name,
141 LWES_SHORT_STRING attr_name,
142 LWES_SHORT_STRING type)
144 int ret;
145 struct lwes_hash *eventHash =
146 (struct lwes_hash *)lwes_hash_get (db->events, event_name);
147 LWES_SHORT_STRING tmpAttrName = NULL;
148 LWES_BYTE *tmpAttrType = NULL;
150 tmpAttrName =
151 (LWES_SHORT_STRING)malloc (sizeof (LWES_CHAR)*(strlen (attr_name)+1));
152 if (tmpAttrName == NULL)
154 return -3;
156 strcpy (tmpAttrName, attr_name);
158 tmpAttrType = (LWES_BYTE *)malloc (sizeof (LWES_BYTE));
159 if (tmpAttrType == NULL)
161 free (tmpAttrName);
162 return -3;
165 if (strcmp (type, LWES_U_INT_16_STRING) == 0)
167 *tmpAttrType = LWES_U_INT_16_TOKEN;
169 else if (strcmp (type, LWES_INT_16_STRING) == 0)
171 *tmpAttrType = LWES_INT_16_TOKEN;
173 else if (strcmp (type, LWES_U_INT_32_STRING) == 0)
175 *tmpAttrType = LWES_U_INT_32_TOKEN;
177 else if (strcmp (type, LWES_INT_32_STRING) == 0)
179 *tmpAttrType = LWES_INT_32_TOKEN;
181 else if (strcmp (type, LWES_U_INT_64_STRING) == 0)
183 *tmpAttrType = LWES_U_INT_64_TOKEN;
185 else if (strcmp (type, LWES_INT_64_STRING) == 0)
187 *tmpAttrType = LWES_INT_64_TOKEN;
189 else if (strcmp (type, LWES_BOOLEAN_STRING) == 0)
191 *tmpAttrType = LWES_BOOLEAN_TOKEN;
193 else if (strcmp (type, LWES_IP_ADDR_STRING) == 0)
195 *tmpAttrType = LWES_IP_ADDR_TOKEN;
197 else if (strcmp (type, LWES_STRING_STRING) == 0)
199 *tmpAttrType = LWES_STRING_TOKEN;
202 ret = lwes_hash_put (eventHash, tmpAttrName, tmpAttrType);
204 /* if inserting into the hash fails we should free up our memory */
205 if (ret < 0)
207 free (tmpAttrName);
208 free (tmpAttrType);
211 return ret;
215 lwes_event_type_db_check_for_event
216 (struct lwes_event_type_db *db,
217 LWES_SHORT_STRING event_name)
219 return lwes_hash_contains_key (db->events, event_name);
223 lwes_event_type_db_check_for_attribute
224 (struct lwes_event_type_db *db,
225 LWES_CONST_SHORT_STRING attr_name,
226 LWES_CONST_SHORT_STRING event_name)
228 struct lwes_hash *event =
229 (struct lwes_hash *)lwes_hash_get (db->events, event_name);
230 struct lwes_hash *meta_event =
231 (struct lwes_hash *)lwes_hash_get (db->events, LWES_META_INFO_STRING);
232 int metaContainsAttribute = 0;
233 int eventContainsAttribute = 0;
234 if (event != NULL)
236 eventContainsAttribute = lwes_hash_contains_key (event, attr_name);
238 if (meta_event != NULL)
240 metaContainsAttribute = lwes_hash_contains_key (meta_event, attr_name);
242 return (metaContainsAttribute || eventContainsAttribute);
246 lwes_event_type_db_check_for_type
247 (struct lwes_event_type_db *db,
248 LWES_BYTE type_value,
249 LWES_CONST_SHORT_STRING attr_name,
250 LWES_CONST_SHORT_STRING event_name)
252 struct lwes_hash *event =
253 (struct lwes_hash *)lwes_hash_get (db->events, event_name);
254 struct lwes_hash *meta_event =
255 (struct lwes_hash *)lwes_hash_get (db->events, LWES_META_INFO_STRING);
256 LWES_BYTE *tmp_type = NULL;
258 if (event != NULL)
260 tmp_type = (LWES_BYTE *)lwes_hash_get (event, attr_name);
262 if (tmp_type == NULL && meta_event != NULL)
264 tmp_type = (LWES_BYTE *)lwes_hash_get (meta_event, attr_name);
266 if (tmp_type == NULL)
268 return 0;
271 return ((*tmp_type)==type_value);