6 struct lwes_event_type_db
*db
;
9 static void tdb_free(void *ptr
)
11 struct _tdb
*tdb
= ptr
;
14 lwes_event_type_db_destroy(tdb
->db
);
18 static VALUE
tdb_alloc(VALUE klass
)
22 return Data_Make_Struct(klass
, struct _tdb
, NULL
, tdb_free
, tdb
);
25 static VALUE
tdb_init(VALUE self
, VALUE path
)
29 if (TYPE(path
) != T_STRING
)
30 rb_raise(rb_eArgError
, "path must be a string");
32 Data_Get_Struct(self
, struct _tdb
, tdb
);
34 rb_raise(rb_eRuntimeError
, "ESF already initialized");
36 tdb
->db
= lwes_event_type_db_create(RSTRING_PTR(path
));
38 rb_raise(rb_eRuntimeError
,
39 "failed to create type DB for LWES file %s",
45 static VALUE
attr_sym(struct lwes_event_attribute
*attr
)
48 case LWES_TYPE_U_INT_16
:
49 case LWES_TYPE_INT_16
:
50 case LWES_TYPE_U_INT_32
:
51 case LWES_TYPE_INT_32
:
52 case LWES_TYPE_U_INT_64
:
53 case LWES_TYPE_INT_64
:
54 case LWES_TYPE_BOOLEAN
:
55 case LWES_TYPE_IP_ADDR
:
56 case LWES_TYPE_STRING
:
57 return INT2NUM(attr
->type
);
58 case LWES_TYPE_UNDEFINED
:
60 rb_raise(rb_eRuntimeError
,
61 "unknown LWES attribute type: 0x%02x", attr
->type
);
67 static VALUE
event_def_ary(struct lwes_hash
*hash
)
69 struct lwes_hash_enumeration e
;
70 VALUE rv
= rb_ary_new();
72 if (!lwes_hash_keys(hash
, &e
))
73 rb_raise(rb_eRuntimeError
, "couldn't get event attributes");
74 while (lwes_hash_enumeration_has_more_elements(&e
)) {
75 LWES_SHORT_STRING key
= lwes_hash_enumeration_next_element(&e
);
76 struct lwes_event_attribute
*attr
;
80 rb_raise(rb_eRuntimeError
,
81 "LWES event type iteration key fail");
83 attr
= (struct lwes_event_attribute
*)lwes_hash_get(hash
, key
);
85 rb_raise(rb_eRuntimeError
,
86 "LWES event type iteration value fail");
88 pair
= rb_ary_new3(2, ID2SYM(rb_intern(key
)), attr_sym(attr
));
89 rb_ary_push(rv
, pair
);
95 struct lwes_event_type_db
* lwesrb_get_type_db(VALUE self
)
99 Data_Get_Struct(self
, struct _tdb
, tdb
);
101 rb_raise(rb_eRuntimeError
,
102 "couldn't get lwes_type_db from %s",
103 RSTRING_PTR(rb_inspect(self
)));
108 static VALUE
tdb_to_hash(VALUE self
)
111 VALUE rv
= rb_hash_new();
112 struct lwes_hash
*events
;
113 struct lwes_hash_enumeration e
;
115 Data_Get_Struct(self
, struct _tdb
, tdb
);
116 assert(tdb
->db
&& tdb
->db
->events
&& "tdb not initialized");
117 events
= tdb
->db
->events
;
119 if (!lwes_hash_keys(events
, &e
))
120 rb_raise(rb_eRuntimeError
, "couldn't get type_db events");
122 while (lwes_hash_enumeration_has_more_elements(&e
)) {
123 struct lwes_hash
*hash
;
125 LWES_SHORT_STRING key
= lwes_hash_enumeration_next_element(&e
);
128 rb_raise(rb_eRuntimeError
,
129 "LWES type DB rv iteration key fail");
131 hash
= (struct lwes_hash
*)lwes_hash_get(events
, key
);
133 rb_raise(rb_eRuntimeError
,
134 "LWES type DB rv iteration value fail");
136 event_key
= ID2SYM(rb_intern(key
));
137 rb_hash_aset(rv
, event_key
, event_def_ary(hash
));
143 void lwesrb_init_type_db(void)
145 VALUE mLWES
= rb_define_module("LWES");
146 cLWES_TypeDB
= rb_define_class_under(mLWES
, "TypeDB", rb_cObject
);
147 rb_define_method(cLWES_TypeDB
, "initialize", tdb_init
, 1);
148 rb_define_method(cLWES_TypeDB
, "to_hash", tdb_to_hash
, 0);
149 rb_define_alloc_func(cLWES_TypeDB
, tdb_alloc
);