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"
31 def initialize(type=Berkeley_DB, loc=DB_DIR, subtype=0, modemask=0755)
33 #loc = Config.instance.db_dir if loc.nil?
40 attr_reader :type, :location, :subtype, :modemask
45 @fdb[f] = Feature.new(self, f) if @fdb[f].nil?
50 @cdb[ccs] = CCS.new(self, ccs) if @cdb[ccs].nil?
55 each_entry("character/feature") {|f| yield f }
58 def load_feature(cid, name)
59 feature = get_feature(name)
60 return nil if feature.nil?
61 feature.get_value(cid)
64 def decode_char(ccs, code_point)
66 return nil if ccst.nil?
67 ccst.decode(code_point)
71 include EachEntryModule
75 def initialize(dir, cat, keytype, name, amask, mmask)
76 @dir, @cat, @keytype, @name, @amask, @mmask = dir, cat, keytype, name, amask, mmask
95 def get(k) setup_db; @db.get(k); end
96 def put(k, v) setup_db; @db.put(k, v); end
98 def each() setup_db; @db.each {|k, v| yield(k, v) } end
103 dbdir = @dir + @cat + @keytype
104 path = dbdir + @name.path.escape.escape_win_filename
105 #TODO: should make dir.
106 if @amask == BDB::RDONLY
107 raise unless FileTest.exist?(path.to_s)
110 @db = BDB::Hash.open(path.to_s, nil, @amask)
114 module TableAccessModule
127 def setup_db(writable=nil)
129 sync if @access & BDB::CREATE == 0
138 db_dir = @ds.location
139 modemask = @ds.modemask
141 db_dir = CHISE::DataSource::DB_DIR.path
145 #qp db_dir, @category, @keyvalue, @name, @access, modemask
147 @db = AttributeTable.new(db_dir, @category, @keyvalue,
148 @name, access, modemask)
149 return false if @db.nil?
162 include TableAccessModule
164 def initialize(ds, name)
165 @ds, @name = ds, name
166 @category, @keyvalue = "character", "feature"
172 return nil if @db.nil?
173 @db.get(format_char_id(cid))
176 def set_value(cid, value)
178 raise "@db is nil." if @db.nil?
179 @db.put(format_char_id(cid), value)
185 raise "@db is nil." if @db.nil?
187 yield(parse_c_string(k), v)
194 include TableAccessModule
196 def initialize(ds, name)
197 @ds, @name = ds, name
198 @category, @keyvalue = "character", "by_feature"
202 def set(code_point, cid)
204 raise "@db is nil." if @db.nil?
205 parse_c_string(@db.get(code_point.to_s))
206 @db.put(code_point.to_s, format_char_id(cid))
210 def decode(code_point)
212 return nil if @db.nil?
213 parse_c_string(@db.get(code_point.to_s))
216 def set_decoded_char(code_point, cid)
218 raise "@db is nil." if @db.nil?
219 @db.put(code_point.to_s, format_char_id(cid))
225 #raise "@db is nil."+@name
226 p "@db is nil."+@name
229 @db.each {|code_point, cid|
230 yield(code_point, parse_c_string(cid))
236 def parse_c_string(str)
237 return nil if str.nil?
244 raise unless 2 <= len && c == ?\?
260 return c & (0x80 | 0x1F)
286 if (counter + 2 <= len)
287 (0...counter).each {|j|
288 cid = (cid << 6) | (str[j + i] & 0x3F)
296 def format_char_id(cid)
298 when ?\t then return "?\t"
299 when ?\n then return "?\n"
300 when ?\r then return "?\r"
301 when 0x1C then return "?\^\\"
305 return "?\\^"+(?@+cid).chr
306 elsif (cid == ?\s) || (cid == ?\") ||
307 (cid == ?\#) || (cid == ?\') ||
308 (cid == ?\() || (cid == ?\)) ||
309 (cid == ?\,) || (cid == ?\.) ||
310 (cid == ?\;) || (cid == ?\?) ||
311 (cid == ?\[) || (cid == ?\\) ||
312 (cid == ?\]) || (cid == ?\`)
320 dest += (((cid + ?@) >> 6) | 0xC0).chr
321 dest += (((cid + ?@) & 0x3F) | 0x80).chr
325 dest[1] = (cid >> 6) | 0xC0
326 dest[2] = (cid & 0x3F) | 0x80
328 elsif (cid <= 0xFFFF)
330 dest[1] = (cid >> 12) | 0xE0
331 dest[2] = ((cid >> 6) & 0x3F) | 0x80
332 dest[3] = (cid & 0x3F) | 0x80
334 elsif (cid <= 0x1FFFFF)
336 dest[1] = (cid >> 18) | 0xF0
337 dest[2] = ((cid >> 12) & 0x3F) | 0x80
338 dest[3] = ((cid >> 6) & 0x3F) | 0x80
339 dest[4] = (cid & 0x3F) | 0x80
341 elsif (cid <= 0x3FFFFFF)
343 dest[1] = (cid >> 24) | 0xF8
344 dest[2] = ((cid >> 18) & 0x3F) | 0x80
345 dest[3] = ((cid >> 12) & 0x3F) | 0x80
346 dest[4] = ((cid >> 6) & 0x3F) | 0x80
347 dest[5] = (cid & 0x3F) | 0x80
351 dest[1] = (cid >> 30) | 0xFC
352 dest[2] = ((cid >> 24) & 0x3F) | 0x80
353 dest[3] = ((cid >> 18) & 0x3F) | 0x80
354 dest[4] = ((cid >> 12) & 0x3F) | 0x80
355 dest[5] = ((cid >> 6) & 0x3F) | 0x80
356 dest[6] = (cid & 0x3F) | 0x80