cleanups
[lwes-ruby.git] / ext / lwes_ext / type_db.c
blobdf95f85760598f0e6ce655a7e9e7edec27ff4fca
1 #include "lwes_ruby.h"
3 VALUE cLWES_TypeDB;
5 static void tdb_free(void *ptr)
7 struct lwes_event_type_db *db = ptr;
9 if (db)
10 lwes_event_type_db_destroy(db);
13 static VALUE tdb_alloc(VALUE klass)
15 return Data_Wrap_Struct(klass, NULL, tdb_free, NULL);
18 static VALUE tdb_init(VALUE self, VALUE path)
20 struct lwes_event_type_db *db = DATA_PTR(self);
21 int gc_retry = 1;
22 char *cpath = StringValueCStr(path);
24 if (db)
25 rb_raise(rb_eRuntimeError, "ESF already initialized");
26 retry:
27 db = lwes_event_type_db_create(cpath);
28 if (!db) {
29 if (--gc_retry == 0) {
30 rb_gc();
31 goto retry;
33 rb_raise(rb_eRuntimeError,
34 "failed to create type DB for LWES file %s", cpath);
36 DATA_PTR(self) = db;
38 return Qnil;
41 static VALUE attr_sym(struct lwes_event_attribute *attr)
43 switch (attr->type) {
44 case LWES_TYPE_U_INT_16:
45 case LWES_TYPE_INT_16:
46 case LWES_TYPE_U_INT_32:
47 case LWES_TYPE_INT_32:
48 case LWES_TYPE_U_INT_64:
49 case LWES_TYPE_INT_64:
50 case LWES_TYPE_BOOLEAN:
51 case LWES_TYPE_IP_ADDR:
52 case LWES_TYPE_STRING:
53 return INT2NUM(attr->type);
54 case LWES_TYPE_UNDEFINED:
55 default:
56 rb_raise(rb_eRuntimeError,
57 "unknown LWES attribute type: 0x%02x", attr->type);
60 return Qfalse;
63 static VALUE event_def_ary(struct lwes_hash *hash)
65 struct lwes_hash_enumeration e;
66 VALUE rv = rb_ary_new();
68 if (!lwes_hash_keys(hash, &e))
69 rb_raise(rb_eRuntimeError, "couldn't get event attributes");
70 while (lwes_hash_enumeration_has_more_elements(&e)) {
71 LWES_SHORT_STRING key = lwes_hash_enumeration_next_element(&e);
72 struct lwes_event_attribute *attr;
73 VALUE pair;
75 if (!key)
76 rb_raise(rb_eRuntimeError,
77 "LWES event type iteration key fail");
79 attr = (struct lwes_event_attribute *)lwes_hash_get(hash, key);
80 if (!attr)
81 rb_raise(rb_eRuntimeError,
82 "LWES event type iteration value fail");
84 pair = rb_ary_new3(2, ID2SYM(rb_intern(key)), attr_sym(attr));
85 rb_ary_push(rv, pair);
88 return rv;
91 struct lwes_event_type_db * lwesrb_get_type_db(VALUE self)
93 struct lwes_event_type_db *db = DATA_PTR(self);
95 if (!db) {
96 volatile VALUE raise_inspect;
97 rb_raise(rb_eRuntimeError,
98 "couldn't get lwes_type_db from %s",
99 RAISE_INSPECT(self));
101 return db;
104 static VALUE tdb_to_hash(VALUE self)
106 struct lwes_event_type_db *db = DATA_PTR(self);
107 VALUE rv = rb_hash_new();
108 struct lwes_hash *events;
109 struct lwes_hash_enumeration e;
111 assert(db && db->events && "tdb not initialized");
112 events = db->events;
114 if (!lwes_hash_keys(events, &e))
115 rb_raise(rb_eRuntimeError, "couldn't get type_db events");
117 while (lwes_hash_enumeration_has_more_elements(&e)) {
118 struct lwes_hash *hash;
119 ID event_key;
120 LWES_SHORT_STRING key = lwes_hash_enumeration_next_element(&e);
122 if (!key)
123 rb_raise(rb_eRuntimeError,
124 "LWES type DB rv iteration key fail");
126 hash = (struct lwes_hash*)lwes_hash_get(events, key);
127 if (!hash)
128 rb_raise(rb_eRuntimeError,
129 "LWES type DB rv iteration value fail");
131 event_key = ID2SYM(rb_intern(key));
132 rb_hash_aset(rv, event_key, event_def_ary(hash));
135 return rv;
138 void lwesrb_init_type_db(void)
140 VALUE mLWES = rb_define_module("LWES");
141 cLWES_TypeDB = rb_define_class_under(mLWES, "TypeDB", rb_cObject);
142 rb_define_private_method(cLWES_TypeDB, "initialize", tdb_init, 1);
143 rb_define_method(cLWES_TypeDB, "to_hash", tdb_to_hash, 0);
144 rb_define_alloc_func(cLWES_TypeDB, tdb_alloc);