+++ /dev/null
-/* Copyright (C) 2002-2004 Kouichirou Eto, All rights reserved.
- This file is part of the Ruby/CHISE Extension.
-
- This software is free software; you can redistribute it and/or
- modify it under the terms of the GNU Lesser General Public
- License as published by the Free Software Foundation; either
- version 2.1 of the License, or (at your option) any later version.
-
- This software is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public
- License along with the CHISE Library; if not, write to the Free
- Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
- 02111-1307 USA. */
-
-#include "ruby.h"
-#include "chise.h"
-
-static VALUE mCHISE, cDS, cCCS, cFEATURE, cVALUE;
-
-typedef struct {
- CHISE_DS *ds;
-} RB_CHISE_DS;
-
-typedef struct {
- CHISE_Feature_Table *feature;
-} RB_CHISE_FEATURE;
-
-typedef struct {
- CHISE_CCS_Table *ccs;
-} RB_CHISE_CCS;
-
-typedef struct {
- CHISE_Value value;
-} RB_CHISE_VALUE;
-
-static VALUE fds_new(VALUE klass, VALUE type, VALUE location, VALUE dbtype, VALUE mode){
- RB_CHISE_DS *rds;
- VALUE tdata = Data_Make_Struct(klass, RB_CHISE_DS, 0, free, rds);
- rds->ds = CHISE_DS_open((CHISE_DS_Type)NUM2INT(type), RSTRING(rb_str_to_str(location))->ptr, NUM2INT(dbtype), NUM2INT(mode));
- if (rds->ds == NULL){
- return Qnil;
- }
- return tdata;
-}
-
-static VALUE fds_close(VALUE obj){
- RB_CHISE_DS *rds;
- Data_Get_Struct(obj, RB_CHISE_DS, rds);
- CHISE_DS_close(rds->ds);
- return Qnil;
-}
-
-static VALUE fds_get_feature(VALUE obj, VALUE feature){
- RB_CHISE_DS *rds;
- RB_CHISE_FEATURE *rfeature;
- VALUE vfeature;
- int status;
- Data_Get_Struct(obj, RB_CHISE_DS, rds);
- vfeature = Data_Make_Struct(cFEATURE, RB_CHISE_FEATURE, 0, free, rfeature);
- rfeature->feature = chise_ds_get_feature(rds->ds, RSTRING(rb_str_to_str(feature))->ptr);
- return vfeature;
-}
-
-static VALUE fds_get_ccs(VALUE obj, VALUE ccs){
- RB_CHISE_DS *rds;
- RB_CHISE_CCS *rccs;
- VALUE vccs;
- int status;
- Data_Get_Struct(obj, RB_CHISE_DS, rds);
- vccs = Data_Make_Struct(cCCS, RB_CHISE_CCS, 0, free, rccs);
- rccs->ccs = chise_ds_get_ccs(rds->ds, RSTRING(rb_str_to_str(ccs))->ptr);
- if (rccs->ccs == NULL){
- return Qnil;
- }
- return vccs;
-}
-
-static VALUE fds_decode_char(VALUE obj, VALUE ccs, VALUE code_point){
- RB_CHISE_DS *rds;
- CHISE_Char_ID char_id;
- Data_Get_Struct(obj, RB_CHISE_DS, rds);
- char_id = chise_ds_decode_char(rds->ds, RSTRING(rb_str_to_str(ccs))->ptr, NUM2INT(code_point));
- return INT2NUM(char_id);
-}
-
-static int name_map_func(CHISE_DS *ds, unsigned char *name){
- rb_yield(rb_str_new2(name));
- return 0; // important
-}
-
-static VALUE fds_each_feature_name(VALUE obj){
- RB_CHISE_DS *rds;
- Data_Get_Struct(obj, RB_CHISE_DS, rds);
- chise_ds_foreach_char_feature_name(rds->ds, &name_map_func);
- return Qnil;
-}
-
-static VALUE ffeature_get_value(VALUE obj, VALUE char_id){
- RB_CHISE_FEATURE *rfeature;
- RB_CHISE_VALUE *rvalue;
- VALUE vvalue;
- int status;
- Data_Get_Struct(obj, RB_CHISE_FEATURE, rfeature);
- vvalue = Data_Make_Struct(cVALUE, RB_CHISE_VALUE, 0, free, rvalue);
- status = chise_char_load_feature_value((CHISE_Char_ID)NUM2INT(char_id), rfeature->feature, &rvalue->value);
- if (status)
- return Qnil;
- return vvalue;
-}
-
-static int feature_map_func(CHISE_Char_ID cid, CHISE_Feature_Table *db, CHISE_Value *valdatum){
- RB_CHISE_VALUE *rvalue;
- VALUE vvalue;
- VALUE var;
- vvalue = Data_Make_Struct(cVALUE, RB_CHISE_VALUE, 0, free, rvalue);
- memcpy(&rvalue->value, valdatum, sizeof(CHISE_Value));
- var = rb_ary_new3(2, INT2NUM(cid), vvalue);
- rb_yield(var);
- return 0;
-}
-
-static VALUE ffeature_each_char(VALUE obj){
- RB_CHISE_FEATURE *rfeature;
- Data_Get_Struct(obj, RB_CHISE_FEATURE, rfeature);
- chise_feature_foreach_char_with_value(rfeature->feature, &feature_map_func);
- return Qnil;
-}
-
-static VALUE fccs_decode(VALUE obj, VALUE code_point){
- RB_CHISE_CCS *rccs;
- Data_Get_Struct(obj, RB_CHISE_CCS, rccs);
- CHISE_Char_ID char_id;
- char_id = chise_ccs_decode(rccs->ccs, NUM2INT(code_point));
- return INT2NUM(char_id);
-}
-
-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_libchise_c(){
- mCHISE = rb_define_module("CHISE");
- rb_define_const(mCHISE, "DB_DIR", rb_str_new2(chise_system_db_dir));
-
- cDS = rb_define_class_under(mCHISE, "DataSource", rb_cObject);
- rb_define_singleton_method(cDS, "new", fds_new, 4);
- 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, "get_feature", fds_get_feature, 1);
- rb_define_method(cDS, "get_ccs", fds_get_ccs, 1);
- rb_define_method(cDS, "decode_char", fds_decode_char, 2);
- rb_define_method(cDS, "each_feature_name", fds_each_feature_name, 0);
-
- cFEATURE = rb_define_class_under(mCHISE, "Feature", rb_cObject);
- rb_define_method(cFEATURE, "get_value", ffeature_get_value, 1);
- rb_define_method(cFEATURE, "each_char", ffeature_each_char, 0);
-
- cCCS = rb_define_class_under(mCHISE, "CCS", rb_cObject);
- rb_define_method(cCCS, "decode", fccs_decode, 1);
-
- cVALUE = rb_define_class_under(mCHISE, "Value", rb_cObject);
- rb_define_method(cVALUE, "to_s", fvalue_to_s, 0);
-}
+++ /dev/null
-#!/usr/bin/env ruby
-# Copyright (C) 2002-2004 Kouichirou Eto, All rights reserved.
-
-require "libchise_c"
-$LOAD_PATH.unshift("..")
-require "chise/qp"
-
-def die(msg)
- puts msg
- @ds.close unless @ds.nil?
- exit 1
-end
-
-def main
- db_dir = CHISE::DB_DIR
- @ds = CHISE::DataSource.new(CHISE::DataSource::Berkeley_DB, db_dir, 0, 0755)
- die "Can't open data source" if @ds.nil?
-
- # get a character by Daikanwa number 364.
- if true
- char_id = @ds.decode_char("=daikanwa", 364)
- else
- ccs = @ds.get_ccs("=daikanwa")
- die "Can't open CCS =daikanwa" if ccs.nil?
- char_id = ccs.decode(364)
- end
- puts char_id
-
- ft = @ds.get_feature("ideographic-structure")
- value = ft.get_value(char_id)
- printf("#x%X => %s\n", char_id, value.to_s)
-
- @ds.each_feature_name {|name|
- #puts "rb_feature : "+name
- }
-
- ft = @ds.get_feature("numeric-value")
- ft.each_char {|cid, valdatum|
- printf("#x%08X ", cid)
-
- ucs = @ds.get_feature("=ucs").get_value(cid)
- if ucs
- printf("[U-%08X]", ucs.to_s.to_i)
- else
- ucs = @ds.get_feature("=>ucs").get_value(cid)
- if ucs
- printf("(U-%08X)", ucs.to_s.to_i)
- else
- printf(" ")
- end
- end
-
- printf(" %s", @ds.get_feature("name").get_value(cid))
- printf(" %s\n", valdatum.to_s)
- }
-
- @ds.close
-end
-main
+++ /dev/null
-#!/usr/bin/env ruby
-# Copyright (C) 2002-2004 Kouichirou Eto, All rights reserved.
-
-$VERBOSE = true
-$LOAD_PATH.unshift("..")
-require "test/unit"
-require "libchise_c"
-require "chise/qp"
-
-class TestLibChise < Test::Unit::TestCase
- def test_libchise
- db_dir = CHISE::DB_DIR
- assert_equal("/cygdrive/c/chise/chise-db", db_dir)
-
- @ds = CHISE::DataSource.new(CHISE::DataSource::Berkeley_DB, db_dir, 0, 0755)
- assert_instance_of(CHISE::DataSource, @ds)
-
- char_id = @ds.decode_char("=daikanwa", 364)
- assert_equal(0x4ECF, char_id)
-
- ccs = @ds.get_ccs("=daikanwa")
- assert_instance_of(CHISE::CCS, ccs)
- char_id = ccs.decode(364)
- assert_equal(0x4ECF, char_id)
-
- feature = @ds.get_feature("ideographic-structure")
- assert_instance_of(CHISE::Feature, feature)
- value = feature.get_value(char_id)
- assert_instance_of(CHISE::Value, value)
- assert_equal("(?\342\277\260 ?\344\272\273 ?\345\216\266)", value.to_s)
-
- @ds.each_feature_name {|name|
- assert_instance_of(String, name)
- }
-
- feature = @ds.get_feature("numeric-value")
- feature.each_char {|cid, valdatum|
- assert_kind_of(Numeric, cid)
- assert_instance_of(CHISE::Value, valdatum)
-
- ucs = @ds.get_feature("=ucs").get_value(cid)
- if ucs
- assert_instance_of(CHISE::Value, ucs)
- else
- ucs = @ds.get_feature("=>ucs").get_value(cid)
- if ucs
- assert_instance_of(CHISE::Value, ucs)
- end
- end
-
- name = @ds.get_feature("name").get_value(cid)
- if name
- assert_instance_of(CHISE::Value, name)
- end
- }
-
- @ds.close
- end
-end