fix missing raise_inspect variable declaration
[lwes-ruby.git] / ext / lwes / type_db.c
blob95f8af661ac25333731cfec95536e5c831ae0c4f
1 #include "lwes_ruby.h"
3 VALUE cLWES_TypeDB;
5 struct _tdb {
6 struct lwes_event_type_db *db;
7 };
9 static void tdb_free(void *ptr)
11 struct _tdb *tdb = ptr;
13 if (tdb->db)
14 lwes_event_type_db_destroy(tdb->db);
15 xfree(ptr);
18 static VALUE tdb_alloc(VALUE klass)
20 struct _tdb *tdb;
22 return Data_Make_Struct(klass, struct _tdb, NULL, tdb_free, tdb);
25 static VALUE tdb_init(VALUE self, VALUE path)
27 struct _tdb *tdb;
28 int gc_retry = 1;
30 if (TYPE(path) != T_STRING)
31 rb_raise(rb_eArgError, "path must be a string");
33 Data_Get_Struct(self, struct _tdb, tdb);
34 if (tdb->db)
35 rb_raise(rb_eRuntimeError, "ESF already initialized");
36 retry:
37 tdb->db = lwes_event_type_db_create(RSTRING_PTR(path));
38 if (!tdb->db) {
39 if (--gc_retry == 0) {
40 rb_gc();
41 goto retry;
43 rb_raise(rb_eRuntimeError,
44 "failed to create type DB for LWES file %s",
45 RSTRING_PTR(path));
48 return Qnil;
51 static VALUE attr_sym(struct lwes_event_attribute *attr)
53 switch (attr->type) {
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:
65 default:
66 rb_raise(rb_eRuntimeError,
67 "unknown LWES attribute type: 0x%02x", attr->type);
70 return Qfalse;
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;
83 VALUE pair;
85 if (!key)
86 rb_raise(rb_eRuntimeError,
87 "LWES event type iteration key fail");
89 attr = (struct lwes_event_attribute *)lwes_hash_get(hash, key);
90 if (!attr)
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);
98 return rv;
101 struct lwes_event_type_db * lwesrb_get_type_db(VALUE self)
103 struct _tdb *tdb;
105 Data_Get_Struct(self, struct _tdb, tdb);
106 if (! tdb->db) {
107 volatile VALUE raise_inspect;
108 rb_raise(rb_eRuntimeError,
109 "couldn't get lwes_type_db from %s",
110 RAISE_INSPECT(self));
112 return tdb->db;
115 static VALUE tdb_to_hash(VALUE self)
117 struct _tdb *tdb;
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;
131 ID event_key;
132 LWES_SHORT_STRING key = lwes_hash_enumeration_next_element(&e);
134 if (!key)
135 rb_raise(rb_eRuntimeError,
136 "LWES type DB rv iteration key fail");
138 hash = (struct lwes_hash*)lwes_hash_get(events, key);
139 if (!hash)
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));
147 return rv;
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);