From: eto Date: Mon, 5 Jul 2004 12:34:57 +0000 (+0000) Subject: update. X-Git-Url: http://git.chise.org/gitweb/?a=commitdiff_plain;h=5dac29593fc01dc74c6ddbb2f45830e417484101;p=chise%2Fruby.git update. --- diff --git a/chise/chisedb.rb b/chise/chisedb.rb index d2dfa7d..b7124e6 100755 --- a/chise/chisedb.rb +++ b/chise/chisedb.rb @@ -8,17 +8,24 @@ module CHISE include Singleton def initialize - @ds = DataSource.new + db_dir = CHISE::DataSource::DB_DIR + @ds = DataSource.new(CHISE::DataSource::Berkeley_DB, db_dir, 0, 0755) @byids_db = {} end def location() @ds.location; end + def get_feature(f) @ds.get_feature(f) end + def get_ccs(c) @ds.get_ccs(c) end + def decode_char(n, cid) @ds.decode_char(n, cid) end + def load_feature(n, cid) @ds.load_feature(n, cid) end - def each_feature() @ds.each_feature {|f| yield f } end - def each_ccs() @ds.each_ccs {|c| yield c } end + + def each_feature_name() @ds.each_feature_name {|f| yield f } end + + #def each_ccs() @ds.each_ccs {|c| yield c } end def get_by_ids_db(n) @byids_db[n] = ByIDS_DB.new(@ds, n) if @byids_db[n].nil? @@ -26,6 +33,17 @@ module CHISE end end + class FeatureDB + def initialize(ds, name) + @ds, @name = ds, name + end + + end + + class CCS_DB + end + + class ByIDS_DB include ChiseValue include TableAccessModule diff --git a/chise/libchise.rb b/chise/libchise.rb index 937194d..0cb975c 100755 --- a/chise/libchise.rb +++ b/chise/libchise.rb @@ -1,3 +1,18 @@ # Copyright (C) 2002-2004 Kouichirou Eto, All rights reserved. require "chise/libchise_r" +begin + require "libchise_c.so" + #raise LoadError + module CHISE + DataSource = DataSource_C + Feature = Feature_C + CCS = CCS_C + end +rescue LoadError + module CHISE + DataSource = DataSource_R + Feature = Feature_R + CCS = CCS_R + end +end diff --git a/chise/libchise_r.rb b/chise/libchise_r.rb index afe3510..dbc421d 100755 --- a/chise/libchise_r.rb +++ b/chise/libchise_r.rb @@ -11,11 +11,12 @@ module CHISE module ChiseValue; end module TableAccessModule; end - class DataSource + class DataSource_R NONE = 0 Berkeley_DB = 1 + DB_DIR = "/cygdrive/c/chise/chise-db" - def initialize(type=Berkeley_DB, loc=nil, subtype=0, modemask=0755) + def initialize(type=Berkeley_DB, loc=DB_DIR, subtype=0, modemask=0755) @type = type loc = Config.instance.db_dir if loc.nil? @location = loc.path @@ -24,24 +25,22 @@ module CHISE @fdb = {} @cdb = {} end - attr_reader :type, :location, :subtype, :modemask + attr_reader :type, :subtype, :modemask + + def close() end def get_feature(f) - @fdb[f] = FeatureTable.new(self, f) if @fdb[f].nil? + @fdb[f] = Feature.new(self, f) if @fdb[f].nil? @fdb[f] end def get_ccs(ccs) - @cdb[ccs] = CCSTable.new(self, ccs) if @cdb[ccs].nil? + @cdb[ccs] = CCS.new(self, ccs) if @cdb[ccs].nil? @cdb[ccs] end - def each_feature - each_entry("character/feature") {|f| yield(f) } - end - - def each_ccs - each_entry("character/by_feature") {|f| yield(f) } + def each_feature_name + each_entry("character/feature") {|f| yield f } end def load_feature(name, cid) @@ -72,23 +71,11 @@ module CHISE @name = name dbdir = dir + cat + keytype - #qp dbdir.to_s - #FileUtils.mkdir_p(dbdir.to_s) unless dbdir.directory? - #qp dbdir.to_s, "2" path = dbdir + name.path.escape.escape_win_filename - #qp path.basename.to_s, amask, mmask -# if /test/ =~ path.to_s -# qp path.to_s, amask -# end if amask == BDB::RDONLY - #raise unless path.exist? - #raise unless FileTest.exist?(path.to_s) - v = FileTest.exist?(path.to_s) - #return unless v - raise unless v + raise unless FileTest.exist?(path.to_s) end -# @db = BDB::Hash.open(path.to_s, nil, amask, mmask) @db = BDB::Hash.open(path.to_s, nil, amask) at_exit { close @@ -100,9 +87,8 @@ module CHISE begin @db.sync @db.close - #p ["AttributeTable: close", @name] rescue => e - #p e + p e end end @@ -140,14 +126,12 @@ module CHISE @db = AttributeTable.new(@ds.location, @category, @keyvalue, @name, @access, @ds.modemask) rescue => e - #qp e @db = nil end - #raise if @db.nil? end end - class FeatureTable + class Feature_R include ChiseValue include TableAccessModule @@ -160,7 +144,7 @@ module CHISE def get_value(cid) setup_db return nil if @db.nil? - parse_value(@db.get(format_char_id(cid))) + @db.get(format_char_id(cid)) end def set_value(cid, value) @@ -169,7 +153,7 @@ module CHISE @db.put(format_char_id(cid), value) end - def each + def each_char setup_db raise "@db is nil." if @db.nil? @db.each {|k, v| @@ -178,7 +162,7 @@ module CHISE end end - class CCSTable + class CCS_R include ChiseValue include TableAccessModule @@ -204,22 +188,12 @@ module CHISE setup_db raise "@db is nil." if @db.nil? @db.each {|k, v| - yield(parse_value(k), parse_c_string(v)) + yield(k, parse_c_string(v)) } end end module ChiseValue - def parse_value(v) - return v if v.nil? - #return v if v.kind_of?(Integer) - return v.to_i if /\A\d+\Z/ =~ v # number? - return $1 if /\A"(.+)"\Z/ =~ v # remove surrounding " - #return v.sub(/\A\?/, "") if v =~ /\A\?/ # remove ? in the head - #return parse_sexp(v) if v =~ /\A\(.+\)\Z/ # parse sexp # not yet - v - end - def parse_c_string(str) return nil if str.nil? diff --git a/ext/libchise_c.c b/ext/libchise_c.c index fd26e62..6f81fb4 100755 --- a/ext/libchise_c.c +++ b/ext/libchise_c.c @@ -19,7 +19,7 @@ #include "ruby.h" #include "chise.h" -static VALUE mCHISE, cDS, cCCS, cFEATURE, cVALUE; +static VALUE mCHISE, cDS, cCCS, cFEATURE; typedef struct { CHISE_DS *ds; @@ -33,17 +33,11 @@ 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; - } + rds->ds = CHISE_DS_open((CHISE_DS_Type)NUM2INT(type), STR2CSTR(location), NUM2INT(dbtype), NUM2INT(mode)); + if (rds->ds == NULL) return Qnil; return tdata; } @@ -54,72 +48,72 @@ static VALUE fds_close(VALUE obj){ 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 int name_map_func(CHISE_DS *ds, unsigned char *name){ + rb_yield(rb_str_new2(name)); + return 0; // important } -static VALUE fds_get_ccs(VALUE obj, VALUE ccs){ +static VALUE fds_each_feature_name(VALUE obj){ 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; + chise_ds_foreach_char_feature_name(rds->ds, &name_map_func); + return Qnil; } -static VALUE fds_decode_char(VALUE obj, VALUE ccs, VALUE code_point){ +static VALUE fds_get_feature(VALUE obj, VALUE name){ RB_CHISE_DS *rds; - CHISE_Char_ID char_id; + RB_CHISE_FEATURE *rfeature; 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); + VALUE vfeature = Data_Make_Struct(cFEATURE, RB_CHISE_FEATURE, 0, free, rfeature); + rfeature->feature = chise_ds_get_feature(rds->ds, STR2CSTR(name)); + return vfeature; } -static int name_map_func(CHISE_DS *ds, unsigned char *name){ - rb_yield(rb_str_new2(name)); - return 0; // important +static VALUE ffeature_setup_db(VALUE obj, VALUE writable){ + RB_CHISE_FEATURE *rfeature; + Data_Get_Struct(obj, RB_CHISE_FEATURE, rfeature); + int status = chise_feature_setup_db(rfeature->feature, NUM2INT(writable)); + if (status) return Qnil; + return Qtrue; } -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_sync(VALUE obj){ + RB_CHISE_FEATURE *rfeature; + Data_Get_Struct(obj, RB_CHISE_FEATURE, rfeature); + int status = chise_feature_sync(rfeature->feature); + if (status) return Qnil; + return Qtrue; +} + +static VALUE ffeature_set_value(VALUE obj, VALUE cid, VALUE value){ + RB_CHISE_FEATURE *rfeature; + Data_Get_Struct(obj, RB_CHISE_FEATURE, rfeature); + int status = chise_char_set_feature_value((CHISE_Char_ID)NUM2INT(cid), rfeature->feature, STR2CSTR(value)); + if (status) return Qnil; + return Qtrue; } -static VALUE ffeature_get_value(VALUE obj, VALUE char_id){ +static VALUE ffeature_get_value(VALUE obj, VALUE cid){ RB_CHISE_FEATURE *rfeature; - RB_CHISE_VALUE *rvalue; - VALUE vvalue; - int status; + CHISE_Value valdatum; 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; + int status = chise_char_load_feature_value((CHISE_Char_ID)NUM2INT(cid), rfeature->feature, &valdatum); + if (status) return Qnil; + return rb_str_new(valdatum.data, valdatum.size); +} + +static VALUE fds_load_feature(VALUE obj, VALUE cid, VALUE name){ + RB_CHISE_DS *rds; + CHISE_Value valdatum; + Data_Get_Struct(obj, RB_CHISE_DS, rds); + int status = chise_ds_load_char_feature_value(rds->ds, NUM2INT(cid), STR2CSTR(name), &valdatum); + if (status) return Qnil; + return rb_str_new(valdatum.data, valdatum.size); } 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); + VALUE vstr = rb_str_new(valdatum->data, valdatum->size); + rb_yield(rb_assoc_new(INT2NUM(cid), vstr)); return 0; } @@ -130,42 +124,79 @@ static VALUE ffeature_each_char(VALUE obj){ return Qnil; } -static VALUE fccs_decode(VALUE obj, VALUE code_point){ +static VALUE fds_get_ccs(VALUE obj, VALUE ccs){ + RB_CHISE_DS *rds; + RB_CHISE_CCS *rccs; + Data_Get_Struct(obj, RB_CHISE_DS, rds); + VALUE vccs = Data_Make_Struct(cCCS, RB_CHISE_CCS, 0, free, rccs); + rccs->ccs = chise_ds_get_ccs(rds->ds, STR2CSTR(ccs)); + if (rccs->ccs == NULL) return Qnil; + return vccs; +} + +static VALUE fds_decode_char(VALUE obj, VALUE ccs, VALUE code_point){ + RB_CHISE_DS *rds; + Data_Get_Struct(obj, RB_CHISE_DS, rds); + CHISE_Char_ID cid = chise_ds_decode_char(rds->ds, STR2CSTR(ccs), NUM2INT(code_point)); + return INT2NUM(cid); +} + +static VALUE fccs_setup_db(VALUE obj, VALUE writable){ 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); + int status = chise_ccs_setup_db(rccs->ccs, NUM2INT(writable)); + if (status) return Qnil; + return Qtrue; } -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)); +static VALUE fccs_sync(VALUE obj){ + RB_CHISE_CCS *rccs; + Data_Get_Struct(obj, RB_CHISE_CCS, rccs); + int status = chise_ccs_sync(rccs->ccs); + if (status) return Qnil; + return Qtrue; +} + +static VALUE fccs_set(VALUE obj, VALUE code_point, VALUE cid){ + RB_CHISE_CCS *rccs; + Data_Get_Struct(obj, RB_CHISE_CCS, rccs); + int status = chise_ccs_set_decoded_char(rccs->ccs, NUM2INT(code_point), (CHISE_Char_ID)NUM2INT(cid)); + if (status) return Qnil; + return Qtrue; +} + +static VALUE fccs_decode(VALUE obj, VALUE code_point){ + RB_CHISE_CCS *rccs; + Data_Get_Struct(obj, RB_CHISE_CCS, rccs); + CHISE_Char_ID cid = chise_ccs_decode(rccs->ccs, NUM2INT(code_point)); + return INT2NUM(cid); } 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); + cDS = rb_define_class_under(mCHISE, "DataSource_C", rb_cObject); rb_define_const(cDS, "NONE", INT2FIX(CHISE_DS_NONE)); rb_define_const(cDS, "Berkeley_DB", INT2FIX(CHISE_DS_Berkeley_DB)); - + rb_define_const(cDS, "DB_DIR", rb_str_new2(chise_system_db_dir)); + rb_define_singleton_method(cDS, "new", fds_new, 4); + rb_define_method(cDS, "close", fds_close, 0); + rb_define_method(cDS, "each_feature_name", fds_each_feature_name, 0); rb_define_method(cDS, "get_feature", fds_get_feature, 1); + rb_define_method(cDS, "load_feature", fds_load_feature, 2); 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); + cFEATURE = rb_define_class_under(mCHISE, "Feature_C", rb_cObject); + rb_define_method(cFEATURE, "setup_db", ffeature_setup_db, 1); + rb_define_method(cFEATURE, "sync", ffeature_sync, 0); + rb_define_method(cFEATURE, "set_value", ffeature_set_value, 2); 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); + cCCS = rb_define_class_under(mCHISE, "CCS_C", rb_cObject); + rb_define_method(cCCS, "setup_db", fccs_setup_db, 1); + rb_define_method(cCCS, "sync", fccs_sync, 0); + rb_define_method(cCCS, "set", fccs_set, 2); 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); } diff --git a/ext/sample.rb b/ext/sample.rb index 9702556..dd5bf40 100755 --- a/ext/sample.rb +++ b/ext/sample.rb @@ -1,7 +1,7 @@ #!/usr/bin/env ruby # Copyright (C) 2002-2004 Kouichirou Eto, All rights reserved. -require "libchise_c" +require "libchise" $LOAD_PATH.unshift("..") require "chise/qp" @@ -12,7 +12,7 @@ def die(msg) end def main - db_dir = CHISE::DB_DIR + db_dir = CHISE::DataSource::DB_DIR @ds = CHISE::DataSource.new(CHISE::DataSource::Berkeley_DB, db_dir, 0, 0755) die "Can't open data source" if @ds.nil? @@ -28,10 +28,10 @@ def main ft = @ds.get_feature("ideographic-structure") value = ft.get_value(char_id) - printf("#x%X => %s\n", char_id, value.to_s) + printf("#x%X => %s\n", char_id, value) @ds.each_feature_name {|name| - #puts "rb_feature : "+name + puts "feature : "+name } ft = @ds.get_feature("numeric-value") @@ -40,18 +40,18 @@ def main ucs = @ds.get_feature("=ucs").get_value(cid) if ucs - printf("[U-%08X]", ucs.to_s.to_i) + printf("[U-%08X]", ucs.to_i) else ucs = @ds.get_feature("=>ucs").get_value(cid) if ucs - printf("(U-%08X)", ucs.to_s.to_i) + printf("(U-%08X)", ucs.to_i) else printf(" ") end end printf(" %s", @ds.get_feature("name").get_value(cid)) - printf(" %s\n", valdatum.to_s) + printf(" %s\n", valdatum) } @ds.close