5 static void tdb_free(void *ptr
)
7 struct lwes_event_type_db
*db
= ptr
;
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
);
22 char *cpath
= StringValueCStr(path
);
25 rb_raise(rb_eRuntimeError
, "ESF already initialized");
27 db
= lwes_event_type_db_create(cpath
);
29 if (--gc_retry
== 0) {
33 rb_raise(rb_eRuntimeError
,
34 "failed to create type DB for LWES file %s", cpath
);
41 static VALUE
attr_sym(struct lwes_event_attribute
*attr
)
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
:
56 rb_raise(rb_eRuntimeError
,
57 "unknown LWES attribute type: 0x%02x", attr
->type
);
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
;
76 rb_raise(rb_eRuntimeError
,
77 "LWES event type iteration key fail");
79 attr
= (struct lwes_event_attribute
*)lwes_hash_get(hash
, key
);
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
);
91 struct lwes_event_type_db
* lwesrb_get_type_db(VALUE self
)
93 struct lwes_event_type_db
*db
= DATA_PTR(self
);
96 volatile VALUE raise_inspect
;
97 rb_raise(rb_eRuntimeError
,
98 "couldn't get lwes_type_db from %s",
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");
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
;
120 LWES_SHORT_STRING key
= lwes_hash_enumeration_next_element(&e
);
123 rb_raise(rb_eRuntimeError
,
124 "LWES type DB rv iteration key fail");
126 hash
= (struct lwes_hash
*)lwes_hash_get(events
, key
);
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
));
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
);