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 dir = @location + "character/feature"
41 next if f.to_s == "." || f.to_s == ".."
42 f = f.unescape_win_filename
48 def load_feature(name, cid)
49 ft = get_feature(name)
54 def decode_char(ccs, code_point)
63 include TableAccessModule
65 def set_value(cid, value)
67 return nil if @db.nil?
68 @db.put(format_char_id(cid), value)
73 return nil if @db.nil?
74 @db.get(format_char_id(cid))
79 return nil if @db.nil?
81 yield(parse_c_string(k), v)
86 def setup_db(writable=nil)
87 setup_db_exec(writable, "character", "feature")
93 include TableAccessModule
95 def decode(code_point)
97 return nil if @db.nil?
98 v = @db.get(code_point.to_s)
103 def set_decoded_char(code_point, cid)
105 return nil if @db.nil?
106 @db.put(code_point.to_s, format_char_id(cid))
110 def setup_db(writable=nil)
111 setup_db_exec(writable, "character", "by_feature")
115 module TableAccessModule
116 def initialize(ds, name)
117 @ds, @name = ds, name
129 def setup_db_exec(writable, cat, key)
131 sync if @access & BDB::CREATE == 0
132 @access = BDB::CREATE
134 @access = BDB::RDONLY
140 @db = AttributeTable.new(@ds.location, cat, key,
141 @name, @access, @ds.modemask)
150 def initialize(dir, cat, keytype, name, amask, mmask)
151 dbdir = dir + cat + keytype
152 #FileUtils.mkdir_p(dbdir.to_s) unless dbdir.directory?
153 path = dbdir + name.path.escape.escape_win_filename
154 # qp path, amask, mmask
155 raise unless path.exist?
156 # @db = BDB::Hash.open(path.to_s, amask, mmask)
157 @db = BDB::Hash.open(path.to_s)
172 def get(k) @db.get(k); end
173 def put(k, v) @db.put(k, v); end
174 def each() @db.each {|k, v| yield(k, v) } end
178 def parse_c_string(str)
184 raise unless 2 <= len && c == ?\?
200 return c & (0x80 | 0x1F)
226 if (counter + 2 <= len)
227 (0...counter).each {|j|
228 cid = (cid << 6) | (str[j + i] & 0x3F)
236 def format_char_id(cid)
238 when ?\t then return "?\t"
239 when ?\n then return "?\n"
240 when ?\r then return "?\r"
241 when 0x1C then return "?\^\\"
245 return "?\\^"+(?@+cid).chr
246 elsif (cid == ?\s) || (cid == ?\") ||
247 (cid == ?\#) || (cid == ?\') ||
248 (cid == ?\() || (cid == ?\)) ||
249 (cid == ?\,) || (cid == ?\.) ||
250 (cid == ?\;) || (cid == ?\?) ||
251 (cid == ?\[) || (cid == ?\\) ||
252 (cid == ?\]) || (cid == ?\`)
260 dest += (((cid + ?@) >> 6) | 0xC0).chr
261 dest += (((cid + ?@) & 0x3F) | 0x80).chr
265 dest[1] = (cid >> 6) | 0xC0
266 dest[2] = (cid & 0x3F) | 0x80
268 elsif (cid <= 0xFFFF)
270 dest[1] = (cid >> 12) | 0xE0
271 dest[2] = ((cid >> 6) & 0x3F) | 0x80
272 dest[3] = (cid & 0x3F) | 0x80
274 elsif (cid <= 0x1FFFFF)
276 dest[1] = (cid >> 18) | 0xF0
277 dest[2] = ((cid >> 12) & 0x3F) | 0x80
278 dest[3] = ((cid >> 6) & 0x3F) | 0x80
279 dest[4] = (cid & 0x3F) | 0x80
281 elsif (cid <= 0x3FFFFFF)
283 dest[1] = (cid >> 24) | 0xF8
284 dest[2] = ((cid >> 18) & 0x3F) | 0x80
285 dest[3] = ((cid >> 12) & 0x3F) | 0x80
286 dest[4] = ((cid >> 6) & 0x3F) | 0x80
287 dest[5] = (cid & 0x3F) | 0x80
291 dest[1] = (cid >> 30) | 0xFC
292 dest[2] = ((cid >> 24) & 0x3F) | 0x80
293 dest[3] = ((cid >> 18) & 0x3F) | 0x80
294 dest[4] = ((cid >> 12) & 0x3F) | 0x80
295 dest[5] = ((cid >> 6) & 0x3F) | 0x80
296 dest[6] = (cid & 0x3F) | 0x80