add license to distribution file
[lwes.git] / src / lwes_event_type_db.c
blobfb77342e1df0a426544b9cf31e861a5e56abc3fc
1 /*======================================================================*
2 * Copyright (c) 2008, Yahoo! Inc. All rights reserved. *
3 * *
4 * Licensed under the New BSD License (the "License"); you may not use *
5 * this file except in compliance with the License. Unless required *
6 * by applicable law or agreed to in writing, software distributed *
7 * under the License is distributed on an "AS IS" BASIS, WITHOUT *
8 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
9 * See the License for the specific language governing permissions and *
10 * limitations under the License. See accompanying LICENSE file. *
11 *======================================================================*/
13 #include "lwes_event_type_db.h"
14 #include "lwes_esf_parser.h"
15 #include "lwes_hash.h"
17 struct lwes_event_type_db *
18 lwes_event_type_db_create
19 (const char *filename)
21 struct lwes_event_type_db *db =
22 (struct lwes_event_type_db *)
23 malloc (sizeof (struct lwes_event_type_db));
24 if (db != NULL)
26 db->esf_filename[0] = '\0';
27 strcat (db->esf_filename, filename);
29 db->events = lwes_hash_create ();
30 if (db->events != NULL)
32 if (lwes_parse_esf (db, db->esf_filename) != 0)
34 free (db);
35 db = NULL;
38 else
40 free (db);
41 db = NULL;
45 return db;
49 int
50 lwes_event_type_db_destroy
51 (struct lwes_event_type_db *db)
53 struct lwes_hash_enumeration e;
54 struct lwes_hash_enumeration e2;
55 LWES_SHORT_STRING eventName = NULL;
56 struct lwes_hash *attrHash = NULL;
57 LWES_SHORT_STRING attrName = NULL;
58 LWES_BYTE *attrType = NULL;
59 /* clear out the hash */
60 if (lwes_hash_keys (db->events, &e))
62 while (lwes_hash_enumeration_has_more_elements (&e))
64 eventName = lwes_hash_enumeration_next_element (&e);
65 attrHash =
66 (struct lwes_hash *)lwes_hash_remove (db->events, eventName);
67 if (lwes_hash_keys (attrHash, &e2))
69 while (lwes_hash_enumeration_has_more_elements (&e2))
71 attrName = lwes_hash_enumeration_next_element (&e2);
72 attrType = (LWES_BYTE *)lwes_hash_remove (attrHash, attrName);
74 /* free the type and the name */
75 if (attrName != NULL)
76 free (attrName);
77 if (attrType != NULL)
78 free (attrType);
81 lwes_hash_destroy (attrHash);
83 if (eventName != NULL)
84 free (eventName);
87 lwes_hash_destroy (db->events);
88 if (db != NULL)
89 free (db);
91 lwes_parse_esf_destroy ();
93 return 0;
96 int
97 lwes_event_type_db_add_event
98 (struct lwes_event_type_db *db,
99 LWES_SHORT_STRING event_name)
101 int ret = 0;
102 struct lwes_hash *eventHash = NULL;
103 /* try and allocate the key */
104 LWES_SHORT_STRING eventHashKey =
105 (LWES_SHORT_STRING)malloc (sizeof (LWES_CHAR)*(strlen (event_name)+1));
106 if (eventHashKey == NULL)
108 return -3;
110 strcpy (eventHashKey, event_name);
111 /* try and allocate the value */
112 eventHash = lwes_hash_create ();
113 if (eventHash == NULL)
115 free (eventHashKey);
116 return -3;
119 /* try and place the key and value into the hash, if we fail free both */
120 ret = lwes_hash_put (db->events, eventHashKey, eventHash);
121 if ( ret < 0 )
123 free (eventHashKey);
124 lwes_hash_destroy (eventHash);
127 return ret;
131 lwes_event_type_db_add_attribute
132 (struct lwes_event_type_db *db,
133 LWES_SHORT_STRING event_name,
134 LWES_SHORT_STRING attr_name,
135 LWES_SHORT_STRING type)
137 int ret;
138 struct lwes_hash *eventHash =
139 (struct lwes_hash *)lwes_hash_get (db->events, event_name);
140 LWES_SHORT_STRING tmpAttrName = NULL;
141 LWES_BYTE *tmpAttrType = NULL;
143 tmpAttrName =
144 (LWES_SHORT_STRING)malloc (sizeof (LWES_CHAR)*(strlen (attr_name)+1));
145 if (tmpAttrName == NULL)
147 return -3;
149 strcpy (tmpAttrName, attr_name);
151 tmpAttrType = (LWES_BYTE *)malloc (sizeof (LWES_BYTE));
152 if (tmpAttrType == NULL)
154 free (tmpAttrName);
155 return -3;
158 if (strcmp (type, LWES_U_INT_16_STRING) == 0)
160 *tmpAttrType = LWES_U_INT_16_TOKEN;
162 else if (strcmp (type, LWES_INT_16_STRING) == 0)
164 *tmpAttrType = LWES_INT_16_TOKEN;
166 else if (strcmp (type, LWES_U_INT_32_STRING) == 0)
168 *tmpAttrType = LWES_U_INT_32_TOKEN;
170 else if (strcmp (type, LWES_INT_32_STRING) == 0)
172 *tmpAttrType = LWES_INT_32_TOKEN;
174 else if (strcmp (type, LWES_U_INT_64_STRING) == 0)
176 *tmpAttrType = LWES_U_INT_64_TOKEN;
178 else if (strcmp (type, LWES_INT_64_STRING) == 0)
180 *tmpAttrType = LWES_INT_64_TOKEN;
182 else if (strcmp (type, LWES_BOOLEAN_STRING) == 0)
184 *tmpAttrType = LWES_BOOLEAN_TOKEN;
186 else if (strcmp (type, LWES_IP_ADDR_STRING) == 0)
188 *tmpAttrType = LWES_IP_ADDR_TOKEN;
190 else if (strcmp (type, LWES_STRING_STRING) == 0)
192 *tmpAttrType = LWES_STRING_TOKEN;
195 ret = lwes_hash_put (eventHash, tmpAttrName, tmpAttrType);
197 /* if inserting into the hash fails we should free up our memory */
198 if (ret < 0)
200 free (tmpAttrName);
201 free (tmpAttrType);
204 return ret;
208 lwes_event_type_db_check_for_event
209 (struct lwes_event_type_db *db,
210 LWES_SHORT_STRING event_name)
212 return lwes_hash_contains_key (db->events, event_name);
216 lwes_event_type_db_check_for_attribute
217 (struct lwes_event_type_db *db,
218 LWES_CONST_SHORT_STRING attr_name,
219 LWES_CONST_SHORT_STRING event_name)
221 struct lwes_hash *event =
222 (struct lwes_hash *)lwes_hash_get (db->events, event_name);
223 struct lwes_hash *meta_event =
224 (struct lwes_hash *)lwes_hash_get (db->events, LWES_META_INFO_STRING);
225 int metaContainsAttribute = 0;
226 int eventContainsAttribute = 0;
227 if (event != NULL)
229 eventContainsAttribute = lwes_hash_contains_key (event, attr_name);
231 if (meta_event != NULL)
233 metaContainsAttribute = lwes_hash_contains_key (meta_event, attr_name);
235 return (metaContainsAttribute || eventContainsAttribute);
239 lwes_event_type_db_check_for_type
240 (struct lwes_event_type_db *db,
241 LWES_BYTE type_value,
242 LWES_CONST_SHORT_STRING attr_name,
243 LWES_CONST_SHORT_STRING event_name)
245 struct lwes_hash *event =
246 (struct lwes_hash *)lwes_hash_get (db->events, event_name);
247 struct lwes_hash *meta_event =
248 (struct lwes_hash *)lwes_hash_get (db->events, LWES_META_INFO_STRING);
249 LWES_BYTE *tmp_type = NULL;
251 if (event != NULL)
253 tmp_type = (LWES_BYTE *)lwes_hash_get (event, attr_name);
255 if (tmp_type == NULL && meta_event != NULL)
257 tmp_type = (LWES_BYTE *)lwes_hash_get (meta_event, attr_name);
259 if (tmp_type == NULL)
261 return 0;
264 return ((*tmp_type)==type_value);