--- /dev/null
+/* Ruby/CHISE ext by eto 2003-0308 */
+
+#include "ruby.h"
+#include "chise.h"
+
+static VALUE mCHISE, cDS, cDT, cFT, cVALUE;
+
+typedef struct {
+ CHISE_DS ds;
+} RB_CHISE_DS;
+
+typedef struct {
+ CHISE_Decoding_Table *dt;
+} RB_CHISE_DT;
+
+typedef struct {
+ CHISE_Feature_Table *ft;
+} RB_CHISE_FT;
+
+typedef struct {
+ CHISE_Value value;
+} RB_CHISE_VALUE;
+
+static VALUE fds_new(VALUE klass, VALUE type, VALUE location){
+ RB_CHISE_DS *rds;
+ VALUE tdata = Data_Make_Struct(klass, RB_CHISE_DS, 0, free, rds);
+ chise_open_data_source (&rds->ds, (CHISE_DS_Type)NUM2INT(type), RSTRING(rb_str_to_str(location))->ptr);
+ return tdata;
+}
+
+static VALUE fds_close(VALUE obj){
+ RB_CHISE_DS *rds;
+ Data_Get_Struct(obj, RB_CHISE_DS, rds);
+ chise_close_data_source (&rds->ds);
+ return Qnil;
+}
+
+static VALUE fds_open_dt(VALUE obj, VALUE ccs){
+ RB_CHISE_DS *rds;
+ RB_CHISE_DT *rdt;
+ VALUE vdt;
+ int status;
+ Data_Get_Struct(obj, RB_CHISE_DS, rds);
+ vdt = Data_Make_Struct(cDT, RB_CHISE_DT, 0, free, rdt);
+ status = chise_open_decoding_table (&rdt->dt, &rds->ds, RSTRING(rb_str_to_str(ccs))->ptr, DB_HASH, DB_RDONLY, 0755);
+ if (status){
+ chise_close_decoding_table (rdt->dt);
+ chise_close_data_source (&rds->ds);
+ return Qnil;
+ }
+ return vdt;
+}
+
+static VALUE fds_open_ft(VALUE obj, VALUE feature){
+ RB_CHISE_DS *rds;
+ RB_CHISE_FT *rft;
+ VALUE vft;
+ int status;
+ Data_Get_Struct(obj, RB_CHISE_DS, rds);
+ vft = Data_Make_Struct(cFT, RB_CHISE_FT, 0, free, rft);
+ status = chise_open_feature_table (&rft->ft, &rds->ds, RSTRING(rb_str_to_str(feature))->ptr, DB_HASH, DB_RDONLY, 0755);
+ if (status){
+ chise_close_feature_table (rft->ft);
+ chise_close_data_source (&rds->ds);
+ return Qnil;
+ }
+ return vft;
+}
+
+static VALUE fdt_get_char(VALUE obj, VALUE code_point){
+ RB_CHISE_DT *rdt;
+ Data_Get_Struct(obj, RB_CHISE_DT, rdt);
+ CHISE_Char_ID char_id;
+ char_id = chise_dt_get_char (rdt->dt, NUM2INT(code_point));
+ return INT2NUM(char_id);
+}
+
+static VALUE fdt_close(VALUE obj){
+ RB_CHISE_DT *rdt;
+ Data_Get_Struct(obj, RB_CHISE_DT, rdt);
+ chise_close_decoding_table (rdt->dt);
+ return Qnil;
+}
+
+static VALUE fft_get_value(VALUE obj, VALUE char_id){
+ RB_CHISE_FT *rft;
+ RB_CHISE_VALUE *rvalue;
+ VALUE vvalue;
+ int status;
+ Data_Get_Struct(obj, RB_CHISE_FT, rft);
+ vvalue = Data_Make_Struct(cVALUE, RB_CHISE_VALUE, 0, free, rvalue);
+ status = chise_ft_get_value (rft->ft, (CHISE_Char_ID)NUM2INT(char_id), &rvalue->value);
+ return vvalue;
+}
+
+static VALUE fft_close(VALUE obj){
+ RB_CHISE_FT *rft;
+ Data_Get_Struct(obj, RB_CHISE_FT, rft);
+ chise_close_feature_table (rft->ft);
+ return Qnil;
+}
+
+static VALUE fvalue_to_s(VALUE obj){
+ RB_CHISE_VALUE *rvalue;
+ Data_Get_Struct(obj, RB_CHISE_VALUE, rvalue);
+ return rb_str_new(chise_value_to_c_string(&rvalue->value), chise_value_size(&rvalue->value));
+}
+
+void Init_chise(){
+ mCHISE = rb_define_module("CHISE");
+
+ cDS = rb_define_class_under(mCHISE, "DataSource", rb_cObject);
+ rb_define_singleton_method(cDS, "new", fds_new, 2);
+ rb_define_method(cDS, "close", fds_close, 0);
+ rb_define_const(cDS, "NONE", INT2FIX(CHISE_DS_NONE));
+ rb_define_const(cDS, "Berkeley_DB", INT2FIX(CHISE_DS_Berkeley_DB));
+ rb_define_method(cDS, "open_decoding_table", fds_open_dt, 1);
+ rb_define_method(cDS, "open_feature_table", fds_open_ft, 1);
+
+ cDT = rb_define_class_under(mCHISE, "DecodingTable", rb_cObject);
+ rb_define_method(cDT, "get_char", fdt_get_char, 1);
+ rb_define_method(cDT, "close", fdt_close, 0);
+
+ cFT = rb_define_class_under(mCHISE, "FeatureTable", rb_cObject);
+ rb_define_method(cFT, "get_value", fft_get_value, 1);
+ rb_define_method(cFT, "close", fft_close, 0);
+
+ cVALUE = rb_define_class_under(mCHISE, "Value", rb_cObject);
+ rb_define_method(cVALUE, "to_s", fvalue_to_s, 0);
+}