i
[chise/ruby.git] / chise / rbchise.rb
1 # Copyright (C) 2002-2004 Kouichirou Eto, All rights reserved.
2 # "rbchise.so" ext compatible library by eto 2003-0317
3
4 require "bdb"
5 require "chise/config"
6
7 module CHISE
8   class DataSource
9     NONE = 0
10     Berkeley_DB = 1
11
12     def initialize(type = Berkeley_DB, location = nil)
13       @type, @location = type, location
14       @location = Config.instance.db_dir if @location.nil?
15       @dbs = {}
16       at_exit {
17         @dbs.each {|k, db|
18           db.close
19         }
20       }
21     end
22
23     def open_decoding_table(ccs)
24       db = open(ccs, "system-char-id")
25       DecodingTable.new(ccs, db)
26     end
27
28     def open_feature_table(feature)
29       db = open("system-char-id", feature)
30       FeatureTable.new(feature, db)
31     end
32
33     def open(from, to) # real_subtpe, accessmask, modemask
34       name = from+"/"+to
35       return @dbs[name] if @dbs[name]
36       file = @location+"/"+name
37       @dbs[name] = BDB::Hash.open(file, nil, 0)
38     end
39   end
40
41   class AttributeTable # abstract class
42   end
43   
44   class DecodingTable < AttributeTable
45     def initialize(ccs, db)
46       @ccs, @db = ccs, db
47     end
48     def get_char(code_point)
49       @db.get(code_point)
50     end
51     def put_char(code_point, cid)
52       @db.put(code_point, cid)
53     end
54   end
55
56   class FeatureTable < AttributeTable
57     def initialize(feature, db)
58       @feature, @db = feature, db
59     end
60     def get_value(char_id)
61       @db.get(char_id)
62     end
63     def each
64     end
65   end
66 end