1 /*======================================================================*
2 * Copyright (c) 2008, Yahoo! Inc. All rights reserved. *
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
));
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)
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
);
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 */
81 lwes_hash_destroy (attrHash
);
83 if (eventName
!= NULL
)
87 lwes_hash_destroy (db
->events
);
91 lwes_parse_esf_destroy ();
97 lwes_event_type_db_add_event
98 (struct lwes_event_type_db
*db
,
99 LWES_SHORT_STRING event_name
)
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
)
110 strcpy (eventHashKey
, event_name
);
111 /* try and allocate the value */
112 eventHash
= lwes_hash_create ();
113 if (eventHash
== NULL
)
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
);
124 lwes_hash_destroy (eventHash
);
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
)
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
;
144 (LWES_SHORT_STRING
)malloc (sizeof (LWES_CHAR
)*(strlen (attr_name
)+1));
145 if (tmpAttrName
== NULL
)
149 strcpy (tmpAttrName
, attr_name
);
151 tmpAttrType
= (LWES_BYTE
*)malloc (sizeof (LWES_BYTE
));
152 if (tmpAttrType
== NULL
)
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 */
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;
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
;
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
)
264 return ((*tmp_type
)==type_value
);