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 rb_raise(rb_eRuntimeError
,
108 "couldn't get lwes_type_db from %s",
109 RSTRING_PTR(rb_inspect(self
)));
114 static VALUE
tdb_to_hash(VALUE self
)
117 VALUE rv
= rb_hash_new();
118 struct lwes_hash
*events
;
119 struct lwes_hash_enumeration e
;
121 Data_Get_Struct(self
, struct _tdb
, tdb
);
122 assert(tdb
->db
&& tdb
->db
->events
&& "tdb not initialized");
123 events
= tdb
->db
->events
;
125 if (!lwes_hash_keys(events
, &e
))
126 rb_raise(rb_eRuntimeError
, "couldn't get type_db events");
128 while (lwes_hash_enumeration_has_more_elements(&e
)) {
129 struct lwes_hash
*hash
;
131 LWES_SHORT_STRING key
= lwes_hash_enumeration_next_element(&e
);
134 rb_raise(rb_eRuntimeError
,
135 "LWES type DB rv iteration key fail");
137 hash
= (struct lwes_hash
*)lwes_hash_get(events
, key
);
139 rb_raise(rb_eRuntimeError
,
140 "LWES type DB rv iteration value fail");
142 event_key
= ID2SYM(rb_intern(key
));
143 rb_hash_aset(rv
, event_key
, event_def_ary(hash
));
149 void lwesrb_init_type_db(void)
151 VALUE mLWES
= rb_define_module("LWES");
152 cLWES_TypeDB
= rb_define_class_under(mLWES
, "TypeDB", rb_cObject
);
153 rb_define_method(cLWES_TypeDB
, "initialize", tdb_init
, 1);
154 rb_define_method(cLWES_TypeDB
, "to_hash", tdb_to_hash
, 0);
155 rb_define_alloc_func(cLWES_TypeDB
, tdb_alloc
);