save emitter attributes in our emitter struct
[lwes-ruby.git] / ext / lwes / type_db.c
blobc7433a28185bc9bdc1c6042d9a8a6634e5ad0220
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 rb_raise(rb_eRuntimeError,
108 "couldn't get lwes_type_db from %s",
109 RSTRING_PTR(rb_inspect(self)));
111 return tdb->db;
114 static VALUE tdb_to_hash(VALUE self)
116 struct _tdb *tdb;
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;
130 ID event_key;
131 LWES_SHORT_STRING key = lwes_hash_enumeration_next_element(&e);
133 if (!key)
134 rb_raise(rb_eRuntimeError,
135 "LWES type DB rv iteration key fail");
137 hash = (struct lwes_hash*)lwes_hash_get(events, key);
138 if (!hash)
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));
146 return rv;
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);