3 static VALUE cLWES_TypeDB
;
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 Data_Get_Struct(self
, struct _tdb
, tdb
);
31 rb_raise(rb_eRuntimeError
, "ESF already initialized");
33 tdb
->db
= lwes_event_type_db_create(RSTRING_PTR(path
));
35 rb_raise(rb_eRuntimeError
,
36 "failed to create type DB for LWES file %s",
42 static VALUE
attr_sym(struct lwes_event_attribute
*attr
)
45 case LWES_TYPE_U_INT_16
:
46 case LWES_TYPE_INT_16
:
47 case LWES_TYPE_U_INT_32
:
48 case LWES_TYPE_INT_32
:
49 case LWES_TYPE_U_INT_64
:
50 case LWES_TYPE_INT_64
:
51 case LWES_TYPE_BOOLEAN
:
52 case LWES_TYPE_IP_ADDR
:
53 case LWES_TYPE_STRING
:
54 return INT2NUM(attr
->type
);
55 case LWES_TYPE_UNDEFINED
:
57 rb_raise(rb_eRuntimeError
,
58 "unknown LWES attribute type: 0x%02x", attr
->type
);
64 static VALUE
event_def_hash(struct lwes_hash
*hash
)
66 struct lwes_hash_enumeration e
;
67 VALUE rv
= rb_hash_new();
69 if (!lwes_hash_keys(hash
, &e
))
70 rb_raise(rb_eRuntimeError
, "couldn't get event attributes");
71 while (lwes_hash_enumeration_has_more_elements(&e
)) {
72 LWES_SHORT_STRING key
= lwes_hash_enumeration_next_element(&e
);
73 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 rb_hash_aset(rv
, ID2SYM(rb_intern(key
)), attr_sym(attr
));
90 static VALUE
tdb_to_hash(VALUE self
)
93 VALUE rv
= rb_hash_new();
94 struct lwes_hash
*events
;
95 struct lwes_hash_enumeration e
;
97 Data_Get_Struct(self
, struct _tdb
, tdb
);
98 assert(tdb
->db
&& tdb
->db
->events
&& "tdb not initialized");
99 events
= tdb
->db
->events
;
101 if (!lwes_hash_keys(events
, &e
))
102 rb_raise(rb_eRuntimeError
, "couldn't get type_db events");
104 while (lwes_hash_enumeration_has_more_elements(&e
)) {
105 struct lwes_hash
*hash
;
107 LWES_SHORT_STRING key
= lwes_hash_enumeration_next_element(&e
);
110 rb_raise(rb_eRuntimeError
,
111 "LWES type DB rv iteration key fail");
113 hash
= (struct lwes_hash
*)lwes_hash_get(events
, key
);
115 rb_raise(rb_eRuntimeError
,
116 "LWES type DB rv iteration value fail");
118 event_key
= ID2SYM(rb_intern(key
));
119 rb_hash_aset(rv
, event_key
, event_def_hash(hash
));
125 void init_type_db(void)
127 VALUE mLWES
= rb_define_module("LWES");
128 cLWES_TypeDB
= rb_define_class_under(mLWES
, "TypeDB", rb_cObject
);
129 rb_define_method(cLWES_TypeDB
, "initialize", tdb_init
, 1);
130 rb_define_method(cLWES_TypeDB
, "to_hash", tdb_to_hash
, 0);
131 rb_define_alloc_func(cLWES_TypeDB
, tdb_alloc
);