From: eto Date: Tue, 11 Mar 2003 10:55:43 +0000 (+0000) Subject: add libchise extension. X-Git-Url: http://git.chise.org/gitweb/?a=commitdiff_plain;h=7826f9983bb5b8a77e5050f6310c400a9d6d0826;p=chise%2Fruby.git add libchise extension. --- diff --git a/ext/README b/ext/README new file mode 100755 index 0000000..43b1ff9 --- /dev/null +++ b/ext/README @@ -0,0 +1,24 @@ +---------------------------------------------------------------------- +¡Ruby/CHISE ext version (pre-alpha) +libchise‚ðextension‚Æ‚µ‚ÄŽg—p‚·‚邱‚Æ‚ðŽŽ‚Ý‚½‚à‚Ì‚Å‚·B +Œ»Ý‚Í‚Ü‚¾chise.rb‚Æ‚ÍŠ®‘S‚É•ª—£‚³‚ê‚Ä‚¢‚Ü‚·B + +¡make•û–@ +libchise‚ª•K—v‚Å‚·B +% ./getlibchise +‚Æ‚µ‚ăRƒs[‚µ‚Ü‚·BŒ»Ý‚ÍŠÈ—ª‰»‚Ì‚½‚ß‚±‚̂悤‚É‚µ‚Ä‚¢‚Ü‚·‚ªA +«—ˆ“I‚É‚Íshared object‚ðŽg—p‚·‚é—\’è‚Å‚·B +% ruby extconf.rb +% make +% make install +‚Æ‚µ‚Äinstall‚µ‚Ü‚·B +BDB‚ªŽg‚¦‚éó‘Ô‚Å‚ ‚邱‚Æ‚ª•K—v‚Å‚·B +Œ»Ý‚ÍCygwin‚¾‚¯‚Åinstall‚ðŠm”F‚µ‚Ä‚ ‚è‚Ü‚·B + +¡test +% ruby test.rb +ƒeƒXƒg‚µ‚Ü‚·B + +¡homepage, http://eto.com/2003/ruby/ +¡contact, Kouichirou Eto <2003 at eto.com> +---------------------------------------------------------------------- diff --git a/ext/extconf.rb b/ext/extconf.rb new file mode 100755 index 0000000..759d7a1 --- /dev/null +++ b/ext/extconf.rb @@ -0,0 +1,5 @@ +require 'mkmf' +if have_library("db", "db_create") and + have_header("db.h") + create_makefile("chise") +end diff --git a/ext/getlibchise b/ext/getlibchise new file mode 100755 index 0000000..75c16b5 --- /dev/null +++ b/ext/getlibchise @@ -0,0 +1,2 @@ +cp ../../libchise/chise.h . +cp ../../libchise/chise.c . diff --git a/ext/rbchise.c b/ext/rbchise.c new file mode 100755 index 0000000..f30069a --- /dev/null +++ b/ext/rbchise.c @@ -0,0 +1,130 @@ +/* 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); +} diff --git a/ext/test.rb b/ext/test.rb new file mode 100755 index 0000000..a43aaff --- /dev/null +++ b/ext/test.rb @@ -0,0 +1,17 @@ +require 'chise' +include CHISE +$KCODE = 'u' + +#DB_DIR = "/usr/local/lib/chise/char-db" +DB_DIR = "/cygdrive/d/work/chise/char-db" +ds = DataSource.new(DataSource::Berkeley_DB , DB_DIR) + +dt = ds.open_decoding_table("ideograph-daikanwa") +char_id = dt.get_char(364) # 大漢和番号364の文字を持ってくる + +ft = ds.open_feature_table("ideographic-structure") +value = ft.get_value(char_id) +printf ("#x%X => %s\n", char_id, value.to_s) +ft.close + +ds.close