update gemspec
[lwes-ruby.git] / ext / lwes_ext / type_db.c
blob249841a80f5fe536e824945e10a3cff5f881e786
1 #include "lwes_ruby.h"
3 VALUE cLWES_TypeDB;
5 static void tdb_free(void *ptr)
7 struct lwes_event_type_db *db = ptr;
9 if (db)
10 lwes_event_type_db_destroy(db);
13 static VALUE tdb_alloc(VALUE klass)
15 return Data_Wrap_Struct(klass, NULL, tdb_free, NULL);
19 * call-seq:
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);
28 int gc_retry = 1;
29 char *cpath = StringValueCStr(path);
31 if (db)
32 rb_raise(rb_eRuntimeError, "ESF already initialized");
33 retry:
34 db = lwes_event_type_db_create(cpath);
35 if (!db) {
36 if (--gc_retry == 0) {
37 rb_gc();
38 goto retry;
40 rb_raise(rb_eRuntimeError,
41 "failed to create type DB for LWES file %s", cpath);
43 DATA_PTR(self) = db;
45 return Qnil;
48 static VALUE attr_sym(struct lwes_event_attribute *attr)
50 switch (attr->type) {
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:
62 default:
63 rb_raise(rb_eRuntimeError,
64 "unknown LWES attribute type: 0x%02x", attr->type);
67 return Qfalse;
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;
80 VALUE pair;
82 if (!key)
83 rb_raise(rb_eRuntimeError,
84 "LWES event type iteration key fail");
86 attr = (struct lwes_event_attribute *)lwes_hash_get(hash, key);
87 if (!attr)
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);
95 return rv;
98 struct lwes_event_type_db * lwesrb_get_type_db(VALUE self)
100 struct lwes_event_type_db *db = DATA_PTR(self);
102 if (!db) {
103 volatile VALUE raise_inspect;
104 rb_raise(rb_eRuntimeError,
105 "couldn't get lwes_type_db from %s",
106 RAISE_INSPECT(self));
108 return db;
111 /* :nodoc: */
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");
120 events = db->events;
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;
127 ID event_key;
128 LWES_SHORT_STRING key = lwes_hash_enumeration_next_element(&e);
130 if (!key)
131 rb_raise(rb_eRuntimeError,
132 "LWES type DB rv iteration key fail");
134 hash = (struct lwes_hash*)lwes_hash_get(events, key);
135 if (!hash)
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));
143 return rv;
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);