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
20 next if f.to_s == "." || f.to_s == ".."
21 #next if f.to_s =~ /\.txt\Z/
22 #yield(f.unescape_win_filename.unescape.to_s)
31 DB_DIR = "/cygdrive/c/chise/chise-db"
33 def initialize(type=Berkeley_DB, loc=DB_DIR, subtype=0, modemask=0755)
35 #loc = Config.instance.db_dir if loc.nil?
42 attr_reader :type, :location, :subtype, :modemask
47 @fdb[f] = Feature.new(self, f) if @fdb[f].nil?
52 @cdb[ccs] = CCS.new(self, ccs) if @cdb[ccs].nil?
57 each_entry("character/feature") {|f| yield f }
60 def load_feature(cid, name)
61 feature = get_feature(name)
62 return nil if feature.nil?
63 feature.get_value(cid)
66 def decode_char(ccs, code_point)
68 return nil if ccst.nil?
69 ccst.decode(code_point)
73 include EachEntryModule
77 def initialize(dir, cat, keytype, name, amask, mmask)
81 dbdir = dir + cat + keytype
82 path = dbdir + name.path.escape.escape_win_filename
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
133 #qp @ds.location, @category, @keyvalue, @name, @access, @ds.modemask
135 @db = AttributeTable.new(@ds.location, @category, @keyvalue,
136 @name, access, @ds.modemask)
137 return false if @db.nil?
150 include TableAccessModule
152 def initialize(ds, name)
153 @ds, @name = ds, name
154 @category, @keyvalue = "character", "feature"
160 return nil if @db.nil?
161 @db.get(format_char_id(cid))
164 def set_value(cid, value)
166 raise "@db is nil." if @db.nil?
167 @db.put(format_char_id(cid), value)
173 raise "@db is nil." if @db.nil?
175 yield(parse_c_string(k), v)
182 include TableAccessModule
184 def initialize(ds, name)
185 @ds, @name = ds, name
186 @category, @keyvalue = "character", "by_feature"
190 def set(code_point, cid)
192 raise "@db is nil." if @db.nil?
193 parse_c_string(@db.get(code_point.to_s))
194 @db.put(code_point.to_s, format_char_id(cid))
198 def decode(code_point)
200 return nil if @db.nil?
201 parse_c_string(@db.get(code_point.to_s))
204 def set_decoded_char(code_point, cid)
206 raise "@db is nil." if @db.nil?
207 @db.put(code_point.to_s, format_char_id(cid))
212 raise "@db is nil." if @db.nil?
213 @db.each {|code_point, cid|
214 yield(code_point, parse_c_string(cid))
220 def parse_c_string(str)
221 return nil if str.nil?
228 raise unless 2 <= len && c == ?\?
244 return c & (0x80 | 0x1F)
270 if (counter + 2 <= len)
271 (0...counter).each {|j|
272 cid = (cid << 6) | (str[j + i] & 0x3F)
280 def format_char_id(cid)
282 when ?\t then return "?\t"
283 when ?\n then return "?\n"
284 when ?\r then return "?\r"
285 when 0x1C then return "?\^\\"
289 return "?\\^"+(?@+cid).chr
290 elsif (cid == ?\s) || (cid == ?\") ||
291 (cid == ?\#) || (cid == ?\') ||
292 (cid == ?\() || (cid == ?\)) ||
293 (cid == ?\,) || (cid == ?\.) ||
294 (cid == ?\;) || (cid == ?\?) ||
295 (cid == ?\[) || (cid == ?\\) ||
296 (cid == ?\]) || (cid == ?\`)
304 dest += (((cid + ?@) >> 6) | 0xC0).chr
305 dest += (((cid + ?@) & 0x3F) | 0x80).chr
309 dest[1] = (cid >> 6) | 0xC0
310 dest[2] = (cid & 0x3F) | 0x80
312 elsif (cid <= 0xFFFF)
314 dest[1] = (cid >> 12) | 0xE0
315 dest[2] = ((cid >> 6) & 0x3F) | 0x80
316 dest[3] = (cid & 0x3F) | 0x80
318 elsif (cid <= 0x1FFFFF)
320 dest[1] = (cid >> 18) | 0xF0
321 dest[2] = ((cid >> 12) & 0x3F) | 0x80
322 dest[3] = ((cid >> 6) & 0x3F) | 0x80
323 dest[4] = (cid & 0x3F) | 0x80
325 elsif (cid <= 0x3FFFFFF)
327 dest[1] = (cid >> 24) | 0xF8
328 dest[2] = ((cid >> 18) & 0x3F) | 0x80
329 dest[3] = ((cid >> 12) & 0x3F) | 0x80
330 dest[4] = ((cid >> 6) & 0x3F) | 0x80
331 dest[5] = (cid & 0x3F) | 0x80
335 dest[1] = (cid >> 30) | 0xFC
336 dest[2] = ((cid >> 24) & 0x3F) | 0x80
337 dest[3] = ((cid >> 18) & 0x3F) | 0x80
338 dest[4] = ((cid >> 12) & 0x3F) | 0x80
339 dest[5] = ((cid >> 6) & 0x3F) | 0x80
340 dest[6] = (cid & 0x3F) | 0x80