add libchise extension.
authoreto <eto>
Tue, 11 Mar 2003 10:55:43 +0000 (10:55 +0000)
committereto <eto>
Tue, 11 Mar 2003 10:55:43 +0000 (10:55 +0000)
ext/README [new file with mode: 0755]
ext/extconf.rb [new file with mode: 0755]
ext/getlibchise [new file with mode: 0755]
ext/rbchise.c [new file with mode: 0755]
ext/test.rb [new file with mode: 0755]

diff --git a/ext/README b/ext/README
new file mode 100755 (executable)
index 0000000..43b1ff9
--- /dev/null
@@ -0,0 +1,24 @@
+----------------------------------------------------------------------
+\81¡Ruby/CHISE ext version (pre-alpha)
+libchise\82ðextension\82Æ\82µ\82Ä\8eg\97p\82·\82é\82±\82Æ\82ð\8e\8e\82Ý\82½\82à\82Ì\82Å\82·\81B
+\8c»\8dÝ\82Í\82Ü\82¾chise.rb\82Æ\82Í\8a®\91S\82É\95ª\97£\82³\82ê\82Ä\82¢\82Ü\82·\81B
+
+\81¡make\95û\96@
+libchise\82ª\95K\97v\82Å\82·\81B
+% ./getlibchise
+\82Æ\82µ\82Ä\83R\83s\81[\82µ\82Ü\82·\81B\8c»\8dÝ\82Í\8aÈ\97ª\89»\82Ì\82½\82ß\82±\82Ì\82æ\82¤\82É\82µ\82Ä\82¢\82Ü\82·\82ª\81A
+\8f«\97\88\93I\82É\82Íshared object\82ð\8eg\97p\82·\82é\97\\92è\82Å\82·\81B
+% ruby extconf.rb
+% make
+% make install
+\82Æ\82µ\82Äinstall\82µ\82Ü\82·\81B
+BDB\82ª\8eg\82¦\82é\8fó\91Ô\82Å\82 \82é\82±\82Æ\82ª\95K\97v\82Å\82·\81B
+\8c»\8dÝ\82ÍCygwin\82¾\82¯\82Åinstall\82ð\8am\94F\82µ\82Ä\82 \82è\82Ü\82·\81B
+
+\81¡test
+% ruby test.rb
+\83e\83X\83g\82µ\82Ü\82·\81B
+
+\81¡homepage, http://eto.com/2003/ruby/
+\81¡contact, Kouichirou Eto <2003 at eto.com>
+----------------------------------------------------------------------
diff --git a/ext/extconf.rb b/ext/extconf.rb
new file mode 100755 (executable)
index 0000000..759d7a1
--- /dev/null
@@ -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 (executable)
index 0000000..75c16b5
--- /dev/null
@@ -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 (executable)
index 0000000..f30069a
--- /dev/null
@@ -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 (executable)
index 0000000..a43aaff
--- /dev/null
@@ -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