1 # Copyright (C) 2002-2004 Kouichirou Eto, All rights reserved.
2 # libchise extension compatible library.
11 module ChiseValue; end
12 module TableAccessModule; end
14 module EachEntryModule
15 def each_entry(subdir)
16 #dir = @location + subdir
17 dir = DataSource::DB_DIR.path + subdir
19 next if f.to_s == "." || f.to_s == ".."
20 next if f.to_s =~ /\.txt\Z/
21 yield(f.unescape_win_filename.unescape.to_s)
29 DB_DIR = "/cygdrive/c/chise/chise-db"
31 def initialize(type=Berkeley_DB, loc=DB_DIR, subtype=0, modemask=0755)
33 #loc = Config.instance.db_dir if loc.nil?
40 attr_reader :type, :location, :subtype, :modemask
45 @fdb[f] = Feature.new(self, f) if @fdb[f].nil?
50 @cdb[ccs] = CCS.new(self, ccs) if @cdb[ccs].nil?
55 each_entry("character/feature") {|f| yield f }
58 def load_feature(cid, name)
59 feature = get_feature(name)
60 return nil if feature.nil?
61 feature.get_value(cid)
64 def decode_char(ccs, code_point)
66 return nil if ccst.nil?
67 ccst.decode(code_point)
71 include EachEntryModule
75 def initialize(dir, cat, keytype, name, amask, mmask)
79 dbdir = dir + cat + keytype
80 path = dbdir + name.path.escape.escape_win_filename
82 #TODO: should make dir.
84 if amask == BDB::RDONLY
85 raise unless FileTest.exist?(path.to_s)
88 @db = BDB::Hash.open(path.to_s, nil, amask)
104 def get(k) @db.get(k); end
105 def put(k, v) @db.put(k, v); end
107 def each() @db.each {|k, v| yield(k, v) } end
110 module TableAccessModule
123 def setup_db(writable=nil)
125 sync if @access & BDB::CREATE == 0
134 db_dir = @ds.location
135 modemask = @ds.modemask
137 db_dir = CHISE::DataSource::DB_DIR.path
141 #qp db_dir, @category, @keyvalue, @name, @access, modemask
143 @db = AttributeTable.new(db_dir, @category, @keyvalue,
144 @name, access, modemask)
145 return false if @db.nil?
158 include TableAccessModule
160 def initialize(ds, name)
161 @ds, @name = ds, name
162 @category, @keyvalue = "character", "feature"
168 return nil if @db.nil?
169 @db.get(format_char_id(cid))
172 def set_value(cid, value)
174 raise "@db is nil." if @db.nil?
175 @db.put(format_char_id(cid), value)
181 raise "@db is nil." if @db.nil?
183 yield(parse_c_string(k), v)
190 include TableAccessModule
192 def initialize(ds, name)
193 @ds, @name = ds, name
194 @category, @keyvalue = "character", "by_feature"
198 def set(code_point, cid)
200 raise "@db is nil." if @db.nil?
201 parse_c_string(@db.get(code_point.to_s))
202 @db.put(code_point.to_s, format_char_id(cid))
206 def decode(code_point)
208 return nil if @db.nil?
209 parse_c_string(@db.get(code_point.to_s))
212 def set_decoded_char(code_point, cid)
214 raise "@db is nil." if @db.nil?
215 @db.put(code_point.to_s, format_char_id(cid))
221 #raise "@db is nil."+@name
222 p "@db is nil."+@name
225 @db.each {|code_point, cid|
226 yield(code_point, parse_c_string(cid))
232 def parse_c_string(str)
233 return nil if str.nil?
240 raise unless 2 <= len && c == ?\?
256 return c & (0x80 | 0x1F)
282 if (counter + 2 <= len)
283 (0...counter).each {|j|
284 cid = (cid << 6) | (str[j + i] & 0x3F)
292 def format_char_id(cid)
294 when ?\t then return "?\t"
295 when ?\n then return "?\n"
296 when ?\r then return "?\r"
297 when 0x1C then return "?\^\\"
301 return "?\\^"+(?@+cid).chr
302 elsif (cid == ?\s) || (cid == ?\") ||
303 (cid == ?\#) || (cid == ?\') ||
304 (cid == ?\() || (cid == ?\)) ||
305 (cid == ?\,) || (cid == ?\.) ||
306 (cid == ?\;) || (cid == ?\?) ||
307 (cid == ?\[) || (cid == ?\\) ||
308 (cid == ?\]) || (cid == ?\`)
316 dest += (((cid + ?@) >> 6) | 0xC0).chr
317 dest += (((cid + ?@) & 0x3F) | 0x80).chr
321 dest[1] = (cid >> 6) | 0xC0
322 dest[2] = (cid & 0x3F) | 0x80
324 elsif (cid <= 0xFFFF)
326 dest[1] = (cid >> 12) | 0xE0
327 dest[2] = ((cid >> 6) & 0x3F) | 0x80
328 dest[3] = (cid & 0x3F) | 0x80
330 elsif (cid <= 0x1FFFFF)
332 dest[1] = (cid >> 18) | 0xF0
333 dest[2] = ((cid >> 12) & 0x3F) | 0x80
334 dest[3] = ((cid >> 6) & 0x3F) | 0x80
335 dest[4] = (cid & 0x3F) | 0x80
337 elsif (cid <= 0x3FFFFFF)
339 dest[1] = (cid >> 24) | 0xF8
340 dest[2] = ((cid >> 18) & 0x3F) | 0x80
341 dest[3] = ((cid >> 12) & 0x3F) | 0x80
342 dest[4] = ((cid >> 6) & 0x3F) | 0x80
343 dest[5] = (cid & 0x3F) | 0x80
347 dest[1] = (cid >> 30) | 0xFC
348 dest[2] = ((cid >> 24) & 0x3F) | 0x80
349 dest[3] = ((cid >> 18) & 0x3F) | 0x80
350 dest[4] = ((cid >> 12) & 0x3F) | 0x80
351 dest[5] = ((cid >> 6) & 0x3F) | 0x80
352 dest[6] = (cid & 0x3F) | 0x80