extra argument checking because users can be careless
[lwes-ruby.git] / ext / lwes / type_db.c
blob1c348629280cb0090fe95a5d4d1639c6018adf9d
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 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 if (TYPE(path) != T_STRING)
30 rb_raise(rb_eArgError, "path must be a string");
32 Data_Get_Struct(self, struct _tdb, tdb);
33 if (tdb->db)
34 rb_raise(rb_eRuntimeError, "ESF already initialized");
36 tdb->db = lwes_event_type_db_create(RSTRING_PTR(path));
37 if (!tdb->db)
38 rb_raise(rb_eRuntimeError,
39 "failed to create type DB for LWES file %s",
40 RSTRING_PTR(path));
42 return Qnil;
45 static VALUE attr_sym(struct lwes_event_attribute *attr)
47 switch (attr->type) {
48 case LWES_TYPE_U_INT_16:
49 case LWES_TYPE_INT_16:
50 case LWES_TYPE_U_INT_32:
51 case LWES_TYPE_INT_32:
52 case LWES_TYPE_U_INT_64:
53 case LWES_TYPE_INT_64:
54 case LWES_TYPE_BOOLEAN:
55 case LWES_TYPE_IP_ADDR:
56 case LWES_TYPE_STRING:
57 return INT2NUM(attr->type);
58 case LWES_TYPE_UNDEFINED:
59 default:
60 rb_raise(rb_eRuntimeError,
61 "unknown LWES attribute type: 0x%02x", attr->type);
64 return Qfalse;
67 static VALUE event_def_ary(struct lwes_hash *hash)
69 struct lwes_hash_enumeration e;
70 VALUE rv = rb_ary_new();
72 if (!lwes_hash_keys(hash, &e))
73 rb_raise(rb_eRuntimeError, "couldn't get event attributes");
74 while (lwes_hash_enumeration_has_more_elements(&e)) {
75 LWES_SHORT_STRING key = lwes_hash_enumeration_next_element(&e);
76 struct lwes_event_attribute *attr;
77 VALUE pair;
79 if (!key)
80 rb_raise(rb_eRuntimeError,
81 "LWES event type iteration key fail");
83 attr = (struct lwes_event_attribute *)lwes_hash_get(hash, key);
84 if (!attr)
85 rb_raise(rb_eRuntimeError,
86 "LWES event type iteration value fail");
88 pair = rb_ary_new3(2, ID2SYM(rb_intern(key)), attr_sym(attr));
89 rb_ary_push(rv, pair);
92 return rv;
95 struct lwes_event_type_db * lwesrb_get_type_db(VALUE self)
97 struct _tdb *tdb;
99 Data_Get_Struct(self, struct _tdb, tdb);
100 if (!tdb->db)
101 rb_raise(rb_eRuntimeError,
102 "couldn't get lwes_type_db from %s",
103 RSTRING_PTR(rb_inspect(self)));
105 return tdb->db;
108 static VALUE tdb_to_hash(VALUE self)
110 struct _tdb *tdb;
111 VALUE rv = rb_hash_new();
112 struct lwes_hash *events;
113 struct lwes_hash_enumeration e;
115 Data_Get_Struct(self, struct _tdb, tdb);
116 assert(tdb->db && tdb->db->events && "tdb not initialized");
117 events = tdb->db->events;
119 if (!lwes_hash_keys(events, &e))
120 rb_raise(rb_eRuntimeError, "couldn't get type_db events");
122 while (lwes_hash_enumeration_has_more_elements(&e)) {
123 struct lwes_hash *hash;
124 ID event_key;
125 LWES_SHORT_STRING key = lwes_hash_enumeration_next_element(&e);
127 if (!key)
128 rb_raise(rb_eRuntimeError,
129 "LWES type DB rv iteration key fail");
131 hash = (struct lwes_hash*)lwes_hash_get(events, key);
132 if (!hash)
133 rb_raise(rb_eRuntimeError,
134 "LWES type DB rv iteration value fail");
136 event_key = ID2SYM(rb_intern(key));
137 rb_hash_aset(rv, event_key, event_def_ary(hash));
140 return rv;
143 void lwesrb_init_type_db(void)
145 VALUE mLWES = rb_define_module("LWES");
146 cLWES_TypeDB = rb_define_class_under(mLWES, "TypeDB", rb_cObject);
147 rb_define_method(cLWES_TypeDB, "initialize", tdb_init, 1);
148 rb_define_method(cLWES_TypeDB, "to_hash", tdb_to_hash, 0);
149 rb_define_alloc_func(cLWES_TypeDB, tdb_alloc);