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
);
21 * LWES::TypeDB.new("events.esf") -> LWES::TypeDB
23 * Initializes a new TypeDB object based on the path to a given ESF file.
25 static VALUE
tdb_init(VALUE self
, VALUE path
)
27 struct lwes_event_type_db
*db
= DATA_PTR(self
);
29 char *cpath
= StringValueCStr(path
);
32 rb_raise(rb_eRuntimeError
, "ESF already initialized");
34 db
= lwes_event_type_db_create(cpath
);
36 if (--gc_retry
== 0) {
40 rb_raise(rb_eRuntimeError
,
41 "failed to create type DB for LWES file %s", cpath
);
48 static VALUE
attr_sym(struct lwes_event_attribute
*attr
)
51 case LWES_TYPE_U_INT_16
:
52 case LWES_TYPE_INT_16
:
53 case LWES_TYPE_U_INT_32
:
54 case LWES_TYPE_INT_32
:
55 case LWES_TYPE_U_INT_64
:
56 case LWES_TYPE_INT_64
:
57 case LWES_TYPE_BOOLEAN
:
58 case LWES_TYPE_IP_ADDR
:
59 case LWES_TYPE_STRING
:
60 return INT2NUM(attr
->type
);
61 case LWES_TYPE_UNDEFINED
:
63 rb_raise(rb_eRuntimeError
,
64 "unknown LWES attribute type: 0x%02x", attr
->type
);
70 static VALUE
event_def_ary(struct lwes_hash
*hash
)
72 struct lwes_hash_enumeration e
;
73 VALUE rv
= rb_ary_new();
75 if (!lwes_hash_keys(hash
, &e
))
76 rb_raise(rb_eRuntimeError
, "couldn't get event attributes");
77 while (lwes_hash_enumeration_has_more_elements(&e
)) {
78 LWES_SHORT_STRING key
= lwes_hash_enumeration_next_element(&e
);
79 struct lwes_event_attribute
*attr
;
83 rb_raise(rb_eRuntimeError
,
84 "LWES event type iteration key fail");
86 attr
= (struct lwes_event_attribute
*)lwes_hash_get(hash
, key
);
88 rb_raise(rb_eRuntimeError
,
89 "LWES event type iteration value fail");
91 pair
= rb_ary_new3(2, ID2SYM(rb_intern(key
)), attr_sym(attr
));
92 rb_ary_push(rv
, pair
);
98 struct lwes_event_type_db
* lwesrb_get_type_db(VALUE self
)
100 struct lwes_event_type_db
*db
= DATA_PTR(self
);
103 volatile VALUE raise_inspect
;
104 rb_raise(rb_eRuntimeError
,
105 "couldn't get lwes_type_db from %s",
106 RAISE_INSPECT(self
));
112 static VALUE
tdb_to_hash(VALUE self
)
114 struct lwes_event_type_db
*db
= DATA_PTR(self
);
115 VALUE rv
= rb_hash_new();
116 struct lwes_hash
*events
;
117 struct lwes_hash_enumeration e
;
119 assert(db
&& db
->events
&& "tdb not initialized");
122 if (!lwes_hash_keys(events
, &e
))
123 rb_raise(rb_eRuntimeError
, "couldn't get type_db events");
125 while (lwes_hash_enumeration_has_more_elements(&e
)) {
126 struct lwes_hash
*hash
;
128 LWES_SHORT_STRING key
= lwes_hash_enumeration_next_element(&e
);
131 rb_raise(rb_eRuntimeError
,
132 "LWES type DB rv iteration key fail");
134 hash
= (struct lwes_hash
*)lwes_hash_get(events
, key
);
136 rb_raise(rb_eRuntimeError
,
137 "LWES type DB rv iteration value fail");
139 event_key
= ID2SYM(rb_intern(key
));
140 rb_hash_aset(rv
, event_key
, event_def_ary(hash
));
146 void lwesrb_init_type_db(void)
148 VALUE mLWES
= rb_define_module("LWES");
149 cLWES_TypeDB
= rb_define_class_under(mLWES
, "TypeDB", rb_cObject
);
150 rb_define_private_method(cLWES_TypeDB
, "initialize", tdb_init
, 1);
151 rb_define_method(cLWES_TypeDB
, "to_hash", tdb_to_hash
, 0);
152 rb_define_alloc_func(cLWES_TypeDB
, tdb_alloc
);