1 # Copyright (C) 2002-2004 Kouichirou Eto, All rights reserved.
2 # libchise extension compatible library.
11 module ChiseValue; end
12 module TableAccessModule; end
17 DB_DIR = "/cygdrive/c/chise/chise-db"
19 def initialize(type=Berkeley_DB, loc=DB_DIR, subtype=0, modemask=0755)
21 loc = Config.instance.db_dir if loc.nil?
28 attr_reader :type, :subtype, :modemask
33 @fdb[f] = Feature.new(self, f) if @fdb[f].nil?
38 @cdb[ccs] = CCS.new(self, ccs) if @cdb[ccs].nil?
43 each_entry("character/feature") {|f| yield f }
46 def load_feature(name, cid)
47 ft = get_feature(name)
52 def decode_char(ccs, code_point)
59 def each_entry(subdir)
60 dir = @location + subdir
62 next if f.to_s == "." || f.to_s == ".."
63 next if f.to_s =~ /\.txt\Z/
64 yield(f.unescape_win_filename.unescape.to_s)
70 def initialize(dir, cat, keytype, name, amask, mmask)
73 dbdir = dir + cat + keytype
74 path = dbdir + name.path.escape.escape_win_filename
76 if amask == BDB::RDONLY
77 raise unless FileTest.exist?(path.to_s)
79 @db = BDB::Hash.open(path.to_s, nil, amask)
95 def get(k) @db.get(k); end
96 def put(k, v) @db.put(k, v); end
98 def each() @db.each {|k, v| yield(k, v) } end
101 module TableAccessModule
115 def setup_db(writable=nil)
117 sync if @access & BDB::CREATE == 0
118 @access = BDB::CREATE
120 @access = BDB::RDONLY
126 @db = AttributeTable.new(@ds.location, @category, @keyvalue,
127 @name, @access, @ds.modemask)
136 include TableAccessModule
138 def initialize(ds, name)
139 @ds, @name = ds, name
140 @category, @keyvalue = "character", "feature"
146 return nil if @db.nil?
147 @db.get(format_char_id(cid))
150 def set_value(cid, value)
152 raise "@db is nil." if @db.nil?
153 @db.put(format_char_id(cid), value)
158 raise "@db is nil." if @db.nil?
160 yield(parse_c_string(k), v)
167 include TableAccessModule
169 def initialize(ds, name)
170 @ds, @name = ds, name
171 @category, @keyvalue = "character", "by_feature"
175 def decode(code_point)
177 return nil if @db.nil?
178 parse_c_string(@db.get(code_point.to_s))
181 def set_decoded_char(code_point, cid)
183 raise "@db is nil." if @db.nil?
184 @db.put(code_point.to_s, format_char_id(cid))
189 raise "@db is nil." if @db.nil?
191 yield(k, parse_c_string(v))
197 def parse_c_string(str)
198 return nil if str.nil?
205 raise unless 2 <= len && c == ?\?
221 return c & (0x80 | 0x1F)
247 if (counter + 2 <= len)
248 (0...counter).each {|j|
249 cid = (cid << 6) | (str[j + i] & 0x3F)
257 def format_char_id(cid)
259 when ?\t then return "?\t"
260 when ?\n then return "?\n"
261 when ?\r then return "?\r"
262 when 0x1C then return "?\^\\"
266 return "?\\^"+(?@+cid).chr
267 elsif (cid == ?\s) || (cid == ?\") ||
268 (cid == ?\#) || (cid == ?\') ||
269 (cid == ?\() || (cid == ?\)) ||
270 (cid == ?\,) || (cid == ?\.) ||
271 (cid == ?\;) || (cid == ?\?) ||
272 (cid == ?\[) || (cid == ?\\) ||
273 (cid == ?\]) || (cid == ?\`)
281 dest += (((cid + ?@) >> 6) | 0xC0).chr
282 dest += (((cid + ?@) & 0x3F) | 0x80).chr
286 dest[1] = (cid >> 6) | 0xC0
287 dest[2] = (cid & 0x3F) | 0x80
289 elsif (cid <= 0xFFFF)
291 dest[1] = (cid >> 12) | 0xE0
292 dest[2] = ((cid >> 6) & 0x3F) | 0x80
293 dest[3] = (cid & 0x3F) | 0x80
295 elsif (cid <= 0x1FFFFF)
297 dest[1] = (cid >> 18) | 0xF0
298 dest[2] = ((cid >> 12) & 0x3F) | 0x80
299 dest[3] = ((cid >> 6) & 0x3F) | 0x80
300 dest[4] = (cid & 0x3F) | 0x80
302 elsif (cid <= 0x3FFFFFF)
304 dest[1] = (cid >> 24) | 0xF8
305 dest[2] = ((cid >> 18) & 0x3F) | 0x80
306 dest[3] = ((cid >> 12) & 0x3F) | 0x80
307 dest[4] = ((cid >> 6) & 0x3F) | 0x80
308 dest[5] = (cid & 0x3F) | 0x80
312 dest[1] = (cid >> 30) | 0xFC
313 dest[2] = ((cid >> 24) & 0x3F) | 0x80
314 dest[3] = ((cid >> 18) & 0x3F) | 0x80
315 dest[4] = ((cid >> 12) & 0x3F) | 0x80
316 dest[5] = ((cid >> 6) & 0x3F) | 0x80
317 dest[6] = (cid & 0x3F) | 0x80