1 # Copyright (C) 2002-2004 Kouichirou Eto, All rights reserved.
2 # "rbchise.so" ext compatible library by eto 2003-0317
10 module ChiseValue; end
11 module TableAccessModule; end
17 def initialize(type=Berkeley_DB, loc=nil, subtype=0, modemask=0755)
19 loc = Config.instance.db_dir if loc.nil?
26 attr_reader :type, :location, :subtype, :modemask
29 @fdb[f] = FeatureTable.new(self, f) if @fdb[f].nil?
34 @cdb[ccs] = CCSTable.new(self, ccs) if @cdb[ccs].nil?
39 each_entry("character/feature") {|f| yield(f) }
43 each_entry("character/by_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(ds, name)
85 def setup_db(writable=nil)
86 setup_db_exec(writable, @category, @keyvalue)
89 def setup_db_exec(writable, cat, key)
91 sync if @access & BDB::CREATE == 0
100 @db = AttributeTable.new(@ds.location, cat, key,
101 @name, @access, @ds.modemask)
109 class FeatureTable < TableAccess
112 def initialize(ds, name)
114 @category, @keyvalue = "character", "feature"
119 return nil if @db.nil?
120 parse_value(@db.get(format_char_id(cid)))
123 def set_value(cid, value)
125 return nil if @db.nil?
126 @db.put(format_char_id(cid), value)
131 return nil if @db.nil?
133 yield(parse_c_string(k), v)
138 class CCSTable < TableAccess
141 def initialize(ds, name)
143 @category, @keyvalue = "character", "by_feature"
146 def decode(code_point)
148 return nil if @db.nil?
149 parse_c_string(@db.get(code_point.to_s))
152 def set_decoded_char(code_point, cid)
154 return nil if @db.nil?
155 @db.put(code_point.to_s, format_char_id(cid))
160 return nil if @db.nil?
162 yield(parse_value(k), parse_c_string(v))
168 def initialize(dir, cat, keytype, name, amask, mmask)
169 dbdir = dir + cat + keytype
170 #FileUtils.mkdir_p(dbdir.to_s) unless dbdir.directory?
171 path = dbdir + name.path.escape.escape_win_filename
172 # qp path, amask, mmask
173 raise unless path.exist?
174 # @db = BDB::Hash.open(path.to_s, amask, mmask)
175 @db = BDB::Hash.open(path.to_s)
190 def get(k) @db.get(k); end
191 def put(k, v) @db.put(k, v); end
192 def each() @db.each {|k, v| yield(k, v) } end
198 #return v if v.kind_of?(Integer)
199 return v.to_i if /\A\d+\Z/ =~ v # number?
200 return $1 if /\A"(.+)"\Z/ =~ v # remove surrounding "
201 #return v.sub(/\A\?/, "") if v =~ /\A\?/ # remove ? in the head
202 #return parse_sexp(v) if v =~ /\A\(.+\)\Z/ # parse sexp # not yet
206 def parse_c_string(str)
207 return nil if str.nil?
214 raise unless 2 <= len && c == ?\?
230 return c & (0x80 | 0x1F)
256 if (counter + 2 <= len)
257 (0...counter).each {|j|
258 cid = (cid << 6) | (str[j + i] & 0x3F)
266 def format_char_id(cid)
268 when ?\t then return "?\t"
269 when ?\n then return "?\n"
270 when ?\r then return "?\r"
271 when 0x1C then return "?\^\\"
275 return "?\\^"+(?@+cid).chr
276 elsif (cid == ?\s) || (cid == ?\") ||
277 (cid == ?\#) || (cid == ?\') ||
278 (cid == ?\() || (cid == ?\)) ||
279 (cid == ?\,) || (cid == ?\.) ||
280 (cid == ?\;) || (cid == ?\?) ||
281 (cid == ?\[) || (cid == ?\\) ||
282 (cid == ?\]) || (cid == ?\`)
290 dest += (((cid + ?@) >> 6) | 0xC0).chr
291 dest += (((cid + ?@) & 0x3F) | 0x80).chr
295 dest[1] = (cid >> 6) | 0xC0
296 dest[2] = (cid & 0x3F) | 0x80
298 elsif (cid <= 0xFFFF)
300 dest[1] = (cid >> 12) | 0xE0
301 dest[2] = ((cid >> 6) & 0x3F) | 0x80
302 dest[3] = (cid & 0x3F) | 0x80
304 elsif (cid <= 0x1FFFFF)
306 dest[1] = (cid >> 18) | 0xF0
307 dest[2] = ((cid >> 12) & 0x3F) | 0x80
308 dest[3] = ((cid >> 6) & 0x3F) | 0x80
309 dest[4] = (cid & 0x3F) | 0x80
311 elsif (cid <= 0x3FFFFFF)
313 dest[1] = (cid >> 24) | 0xF8
314 dest[2] = ((cid >> 18) & 0x3F) | 0x80
315 dest[3] = ((cid >> 12) & 0x3F) | 0x80
316 dest[4] = ((cid >> 6) & 0x3F) | 0x80
317 dest[5] = (cid & 0x3F) | 0x80
321 dest[1] = (cid >> 30) | 0xFC
322 dest[2] = ((cid >> 24) & 0x3F) | 0x80
323 dest[3] = ((cid >> 18) & 0x3F) | 0x80
324 dest[4] = ((cid >> 12) & 0x3F) | 0x80
325 dest[5] = ((cid >> 6) & 0x3F) | 0x80
326 dest[6] = (cid & 0x3F) | 0x80