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"
30 DB_DIR = CHISE::DB_DIR
32 def initialize(type=Berkeley_DB, loc=DB_DIR, subtype=0, modemask=0755)
34 #loc = Config.instance.db_dir if loc.nil?
41 attr_reader :type, :location, :subtype, :modemask
46 @fdb[f] = Feature.new(self, f) if @fdb[f].nil?
51 @cdb[ccs] = CCS.new(self, ccs) if @cdb[ccs].nil?
56 each_entry("character/feature") {|f| yield f }
59 def load_feature(cid, name)
60 feature = get_feature(name)
61 return nil if feature.nil?
62 feature.get_value(cid)
65 def decode_char(ccs, code_point)
67 return nil if ccst.nil?
68 ccst.decode(code_point)
72 include EachEntryModule
76 def initialize(dir, cat, keytype, name, amask, mmask)
77 @dir, @cat, @keytype, @name, @amask, @mmask = dir, cat, keytype, name, amask, mmask
96 def get(k) setup_db; @db.get(k); end
97 def put(k, v) setup_db; @db.put(k, v); end
99 def each() setup_db; @db.each {|k, v| yield(k, v) } end
104 dbdir = @dir + @cat + @keytype
105 path = dbdir + @name.path.escape.escape_win_filename
106 #TODO: should make dir.
107 if @amask == BDB::RDONLY
108 raise unless FileTest.exist?(path.to_s)
111 @db = BDB::Hash.open(path.to_s, nil, @amask)
115 module TableAccessModule
128 def setup_db(writable=nil)
130 sync if @access & BDB::CREATE == 0
139 db_dir = @ds.location
140 modemask = @ds.modemask
142 db_dir = CHISE::DataSource::DB_DIR.path
146 #qp db_dir, @category, @keyvalue, @name, @access, modemask
148 @db = AttributeTable.new(db_dir, @category, @keyvalue,
149 @name, access, modemask)
150 return false if @db.nil?
163 include TableAccessModule
165 def initialize(ds, name)
166 @ds, @name = ds, name
167 @category, @keyvalue = "character", "feature"
173 return nil if @db.nil?
174 @db.get(format_char_id(cid))
177 def set_value(cid, value)
179 raise "@db is nil." if @db.nil?
180 @db.put(format_char_id(cid), value)
186 raise "@db is nil." if @db.nil?
188 yield(parse_c_string(k), v)
195 include TableAccessModule
197 def initialize(ds, name)
198 @ds, @name = ds, name
199 @category, @keyvalue = "character", "by_feature"
203 def set(code_point, cid)
205 raise "@db is nil." if @db.nil?
206 parse_c_string(@db.get(code_point.to_s))
207 @db.put(code_point.to_s, format_char_id(cid))
211 def decode(code_point)
213 return nil if @db.nil?
214 parse_c_string(@db.get(code_point.to_s))
217 def set_decoded_char(code_point, cid)
219 raise "@db is nil." if @db.nil?
220 @db.put(code_point.to_s, format_char_id(cid))
226 #raise "@db is nil."+@name
227 p "@db is nil."+@name
230 @db.each {|code_point, cid|
231 yield(code_point, parse_c_string(cid))
237 def parse_c_string(str)
238 return nil if str.nil?
245 raise unless 2 <= len && c == ?\?
261 return c & (0x80 | 0x1F)
287 if (counter + 2 <= len)
288 (0...counter).each {|j|
289 cid = (cid << 6) | (str[j + i] & 0x3F)
297 def format_char_id(cid)
299 when ?\t then return "?\t"
300 when ?\n then return "?\n"
301 when ?\r then return "?\r"
302 when 0x1C then return "?\^\\"
306 return "?\\^"+(?@+cid).chr
307 elsif (cid == ?\s) || (cid == ?\") ||
308 (cid == ?\#) || (cid == ?\') ||
309 (cid == ?\() || (cid == ?\)) ||
310 (cid == ?\,) || (cid == ?\.) ||
311 (cid == ?\;) || (cid == ?\?) ||
312 (cid == ?\[) || (cid == ?\\) ||
313 (cid == ?\]) || (cid == ?\`)
321 dest += (((cid + ?@) >> 6) | 0xC0).chr
322 dest += (((cid + ?@) & 0x3F) | 0x80).chr
326 dest[1] = (cid >> 6) | 0xC0
327 dest[2] = (cid & 0x3F) | 0x80
329 elsif (cid <= 0xFFFF)
331 dest[1] = (cid >> 12) | 0xE0
332 dest[2] = ((cid >> 6) & 0x3F) | 0x80
333 dest[3] = (cid & 0x3F) | 0x80
335 elsif (cid <= 0x1FFFFF)
337 dest[1] = (cid >> 18) | 0xF0
338 dest[2] = ((cid >> 12) & 0x3F) | 0x80
339 dest[3] = ((cid >> 6) & 0x3F) | 0x80
340 dest[4] = (cid & 0x3F) | 0x80
342 elsif (cid <= 0x3FFFFFF)
344 dest[1] = (cid >> 24) | 0xF8
345 dest[2] = ((cid >> 18) & 0x3F) | 0x80
346 dest[3] = ((cid >> 12) & 0x3F) | 0x80
347 dest[4] = ((cid >> 6) & 0x3F) | 0x80
348 dest[5] = (cid & 0x3F) | 0x80
352 dest[1] = (cid >> 30) | 0xFC
353 dest[2] = ((cid >> 24) & 0x3F) | 0x80
354 dest[3] = ((cid >> 18) & 0x3F) | 0x80
355 dest[4] = ((cid >> 12) & 0x3F) | 0x80
356 dest[5] = ((cid >> 6) & 0x3F) | 0x80
357 dest[6] = (cid & 0x3F) | 0x80