update.
[chise/ruby.git] / chise / libchise_r.rb
index dbc421d..f0556f0 100755 (executable)
@@ -11,6 +11,20 @@ module CHISE
   module ChiseValue; end
   module TableAccessModule; end
 
   module ChiseValue; end
   module TableAccessModule; end
 
+  module EachEntryModule
+    def each_entry(subdir)
+      #dir = @location + subdir
+      dir = DataSource::DB_DIR.path + subdir
+      dir.each_entry {|f|
+       #p f
+       next if f.to_s == "." || f.to_s == ".."
+       #next if f.to_s =~ /\.txt\Z/
+       #yield(f.unescape_win_filename.unescape.to_s)
+       yield(f.to_s)
+      }
+    end
+  end
+
   class DataSource_R
     NONE = 0
     Berkeley_DB = 1
   class DataSource_R
     NONE = 0
     Berkeley_DB = 1
@@ -18,14 +32,14 @@ module CHISE
 
     def initialize(type=Berkeley_DB, loc=DB_DIR, subtype=0, modemask=0755)
       @type = type
 
     def initialize(type=Berkeley_DB, loc=DB_DIR, subtype=0, modemask=0755)
       @type = type
-      loc = Config.instance.db_dir if loc.nil?
+      #loc = Config.instance.db_dir if loc.nil?
       @location = loc.path
       @subtype = subtype
       @modemask = modemask
       @fdb = {}
       @cdb = {}
     end
       @location = loc.path
       @subtype = subtype
       @modemask = modemask
       @fdb = {}
       @cdb = {}
     end
-    attr_reader :type, :subtype, :modemask
+    attr_reader :type, :location, :subtype, :modemask
 
     def close() end
 
 
     def close() end
 
@@ -43,39 +57,34 @@ module CHISE
       each_entry("character/feature") {|f| yield f }
     end
 
       each_entry("character/feature") {|f| yield f }
     end
 
-    def load_feature(name, cid)
-      ft = get_feature(name)
-      return nil if ft.nil?
-      ft.get_value(cid)
+    def load_feature(cid, name)
+      feature = get_feature(name)
+      return nil if feature.nil?
+      feature.get_value(cid)
     end
 
     def decode_char(ccs, code_point)
     end
 
     def decode_char(ccs, code_point)
-      ct = get_ccs(ccs)
-      return nil if ct.nil?
-      ct.decode(code_point)
+      ccst = get_ccs(ccs)
+      return nil if ccst.nil?
+      ccst.decode(code_point)
     end
 
     private
     end
 
     private
-    def each_entry(subdir)
-      dir = @location + subdir
-      dir.each_entry {|f|
-       next if f.to_s == "." || f.to_s == ".."
-       next if f.to_s =~ /\.txt\Z/
-       yield(f.unescape_win_filename.unescape.to_s)
-      }
-    end
+    include EachEntryModule
   end
 
   class AttributeTable
     def initialize(dir, cat, keytype, name, amask, mmask)
       @name = name
 
   end
 
   class AttributeTable
     def initialize(dir, cat, keytype, name, amask, mmask)
       @name = name
 
+      #qp name
       dbdir  = dir + cat + keytype
       path = dbdir + name.path.escape.escape_win_filename
 
       if amask == BDB::RDONLY
        raise unless FileTest.exist?(path.to_s)
       end
       dbdir  = dir + cat + keytype
       path = dbdir + name.path.escape.escape_win_filename
 
       if amask == BDB::RDONLY
        raise unless FileTest.exist?(path.to_s)
       end
+      #qp path.to_s
       @db = BDB::Hash.open(path.to_s, nil, amask)
       at_exit {
        close
       @db = BDB::Hash.open(path.to_s, nil, amask)
       at_exit {
        close
@@ -106,28 +115,33 @@ module CHISE
 
     def sync
       @db.close if @db
 
     def sync
       @db.close if @db
-      @db = nil
       reset
       reset
+      true
     end
     alias close sync
 
     end
     alias close sync
 
-    private
     def setup_db(writable=nil)
       if writable
        sync if @access & BDB::CREATE == 0
     def setup_db(writable=nil)
       if writable
        sync if @access & BDB::CREATE == 0
-       @access = BDB::CREATE
+       access = BDB::CREATE
       else
       else
-       @access = BDB::RDONLY
+       access = BDB::RDONLY
       end
 
       end
 
-      return if @db
+      return true if @db
 
 
+      #qp @ds.location, @category, @keyvalue, @name, @access, @ds.modemask
       begin
        @db = AttributeTable.new(@ds.location, @category, @keyvalue,
       begin
        @db = AttributeTable.new(@ds.location, @category, @keyvalue,
-                                @name, @access, @ds.modemask)
+                                @name, access, @ds.modemask)
+       return false if @db.nil?
+       @access = access
       rescue => e
       rescue => e
+       #puts $!, $@
        @db = nil
        @db = nil
+       return false
       end
       end
+      true
     end
   end
 
     end
   end
 
@@ -151,6 +165,7 @@ module CHISE
       setup_db(true)
       raise "@db is nil." if @db.nil?
       @db.put(format_char_id(cid), value)
       setup_db(true)
       raise "@db is nil." if @db.nil?
       @db.put(format_char_id(cid), value)
+      true
     end
 
     def each_char
     end
 
     def each_char
@@ -172,6 +187,14 @@ module CHISE
       reset
     end
 
       reset
     end
 
+    def set(code_point, cid)
+      setup_db(true)
+      raise "@db is nil." if @db.nil?
+      parse_c_string(@db.get(code_point.to_s))
+      @db.put(code_point.to_s, format_char_id(cid))
+      true
+    end
+
     def decode(code_point)
       setup_db
       return nil if @db.nil?
     def decode(code_point)
       setup_db
       return nil if @db.nil?
@@ -184,11 +207,11 @@ module CHISE
       @db.put(code_point.to_s, format_char_id(cid))
     end
 
       @db.put(code_point.to_s, format_char_id(cid))
     end
 
-    def each
+    def each_char
       setup_db
       raise "@db is nil." if @db.nil?
       setup_db
       raise "@db is nil." if @db.nil?
-      @db.each {|k, v|
-       yield(k, parse_c_string(v))
+      @db.each {|code_point, cid|
+       yield(code_point, parse_c_string(cid))
       }
     end
   end
       }
     end
   end