Map LWES_TYPE_* constants to Ruby constants
[lwes-ruby.git] / ext / lwes / type_db.c
blob2ab957dcc639054215300d2d43141e9b907b16e6
1 #include "lwes_ruby.h"
3 static 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 tdb->db = NULL;
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;
29 Data_Get_Struct(self, struct _tdb, tdb);
30 if (tdb->db)
31 rb_raise(rb_eRuntimeError, "ESF already initialized");
33 tdb->db = lwes_event_type_db_create(RSTRING_PTR(path));
34 if (!tdb->db)
35 rb_raise(rb_eRuntimeError,
36 "failed to create type DB for LWES file %s",
37 RSTRING_PTR(path));
39 return Qnil;
42 static VALUE attr_sym(struct lwes_event_attribute *attr)
44 switch (attr->type) {
45 case LWES_TYPE_U_INT_16:
46 case LWES_TYPE_INT_16:
47 case LWES_TYPE_U_INT_32:
48 case LWES_TYPE_INT_32:
49 case LWES_TYPE_U_INT_64:
50 case LWES_TYPE_INT_64:
51 case LWES_TYPE_BOOLEAN:
52 case LWES_TYPE_IP_ADDR:
53 case LWES_TYPE_STRING:
54 return INT2NUM(attr->type);
55 case LWES_TYPE_UNDEFINED:
56 default:
57 rb_raise(rb_eRuntimeError,
58 "unknown LWES attribute type: 0x%02x", attr->type);
61 return Qfalse;
64 static VALUE event_def_hash(struct lwes_hash *hash)
66 struct lwes_hash_enumeration e;
67 VALUE rv = rb_hash_new();
69 if (!lwes_hash_keys(hash, &e))
70 rb_raise(rb_eRuntimeError, "couldn't get event attributes");
71 while (lwes_hash_enumeration_has_more_elements(&e)) {
72 LWES_SHORT_STRING key = lwes_hash_enumeration_next_element(&e);
73 struct lwes_event_attribute *attr;
75 if (!key)
76 rb_raise(rb_eRuntimeError,
77 "LWES event type iteration key fail");
79 attr = (struct lwes_event_attribute *)lwes_hash_get(hash, key);
80 if (!attr)
81 rb_raise(rb_eRuntimeError,
82 "LWES event type iteration value fail");
84 rb_hash_aset(rv, ID2SYM(rb_intern(key)), attr_sym(attr));
87 return rv;
90 static VALUE tdb_to_hash(VALUE self)
92 struct _tdb *tdb;
93 VALUE rv = rb_hash_new();
94 struct lwes_hash *events;
95 struct lwes_hash_enumeration e;
97 Data_Get_Struct(self, struct _tdb, tdb);
98 assert(tdb->db && tdb->db->events && "tdb not initialized");
99 events = tdb->db->events;
101 if (!lwes_hash_keys(events, &e))
102 rb_raise(rb_eRuntimeError, "couldn't get type_db events");
104 while (lwes_hash_enumeration_has_more_elements(&e)) {
105 struct lwes_hash *hash;
106 ID event_key;
107 LWES_SHORT_STRING key = lwes_hash_enumeration_next_element(&e);
109 if (!key)
110 rb_raise(rb_eRuntimeError,
111 "LWES type DB rv iteration key fail");
113 hash = (struct lwes_hash*)lwes_hash_get(events, key);
114 if (!hash)
115 rb_raise(rb_eRuntimeError,
116 "LWES type DB rv iteration value fail");
118 event_key = ID2SYM(rb_intern(key));
119 rb_hash_aset(rv, event_key, event_def_hash(hash));
122 return rv;
125 void init_type_db(void)
127 VALUE mLWES = rb_define_module("LWES");
128 cLWES_TypeDB = rb_define_class_under(mLWES, "TypeDB", rb_cObject);
129 rb_define_method(cLWES_TypeDB, "initialize", tdb_init, 1);
130 rb_define_method(cLWES_TypeDB, "to_hash", tdb_to_hash, 0);
131 rb_define_alloc_func(cLWES_TypeDB, tdb_alloc);