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
)
30 if (TYPE(path
) != T_STRING
)
31 rb_raise(rb_eArgError
, "path must be a string");
33 Data_Get_Struct(self
, struct _tdb
, tdb
);
35 rb_raise(rb_eRuntimeError
, "ESF already initialized");
37 tdb
->db
= lwes_event_type_db_create(RSTRING_PTR(path
));
39 if (--gc_retry
== 0) {
43 rb_raise(rb_eRuntimeError
,
44 "failed to create type DB for LWES file %s",
51 static VALUE
attr_sym(struct lwes_event_attribute
*attr
)
54 case LWES_TYPE_U_INT_16
:
55 case LWES_TYPE_INT_16
:
56 case LWES_TYPE_U_INT_32
:
57 case LWES_TYPE_INT_32
:
58 case LWES_TYPE_U_INT_64
:
59 case LWES_TYPE_INT_64
:
60 case LWES_TYPE_BOOLEAN
:
61 case LWES_TYPE_IP_ADDR
:
62 case LWES_TYPE_STRING
:
63 return INT2NUM(attr
->type
);
64 case LWES_TYPE_UNDEFINED
:
66 rb_raise(rb_eRuntimeError
,
67 "unknown LWES attribute type: 0x%02x", attr
->type
);
73 static VALUE
event_def_ary(struct lwes_hash
*hash
)
75 struct lwes_hash_enumeration e
;
76 VALUE rv
= rb_ary_new();
78 if (!lwes_hash_keys(hash
, &e
))
79 rb_raise(rb_eRuntimeError
, "couldn't get event attributes");
80 while (lwes_hash_enumeration_has_more_elements(&e
)) {
81 LWES_SHORT_STRING key
= lwes_hash_enumeration_next_element(&e
);
82 struct lwes_event_attribute
*attr
;
86 rb_raise(rb_eRuntimeError
,
87 "LWES event type iteration key fail");
89 attr
= (struct lwes_event_attribute
*)lwes_hash_get(hash
, key
);
91 rb_raise(rb_eRuntimeError
,
92 "LWES event type iteration value fail");
94 pair
= rb_ary_new3(2, ID2SYM(rb_intern(key
)), attr_sym(attr
));
95 rb_ary_push(rv
, pair
);
101 struct lwes_event_type_db
* lwesrb_get_type_db(VALUE self
)
105 Data_Get_Struct(self
, struct _tdb
, tdb
);
107 volatile VALUE raise_inspect
;
108 rb_raise(rb_eRuntimeError
,
109 "couldn't get lwes_type_db from %s",
110 RAISE_INSPECT(self
));
115 static VALUE
tdb_to_hash(VALUE self
)
118 VALUE rv
= rb_hash_new();
119 struct lwes_hash
*events
;
120 struct lwes_hash_enumeration e
;
122 Data_Get_Struct(self
, struct _tdb
, tdb
);
123 assert(tdb
->db
&& tdb
->db
->events
&& "tdb not initialized");
124 events
= tdb
->db
->events
;
126 if (!lwes_hash_keys(events
, &e
))
127 rb_raise(rb_eRuntimeError
, "couldn't get type_db events");
129 while (lwes_hash_enumeration_has_more_elements(&e
)) {
130 struct lwes_hash
*hash
;
132 LWES_SHORT_STRING key
= lwes_hash_enumeration_next_element(&e
);
135 rb_raise(rb_eRuntimeError
,
136 "LWES type DB rv iteration key fail");
138 hash
= (struct lwes_hash
*)lwes_hash_get(events
, key
);
140 rb_raise(rb_eRuntimeError
,
141 "LWES type DB rv iteration value fail");
143 event_key
= ID2SYM(rb_intern(key
));
144 rb_hash_aset(rv
, event_key
, event_def_ary(hash
));
150 void lwesrb_init_type_db(void)
152 VALUE mLWES
= rb_define_module("LWES");
153 cLWES_TypeDB
= rb_define_class_under(mLWES
, "TypeDB", rb_cObject
);
154 rb_define_method(cLWES_TypeDB
, "initialize", tdb_init
, 1);
155 rb_define_method(cLWES_TypeDB
, "to_hash", tdb_to_hash
, 0);
156 rb_define_alloc_func(cLWES_TypeDB
, tdb_alloc
);