1 # Copyright (C) 2002-2004 Kouichirou Eto, All rights reserved.
2 # libchise extension compatible library.
11 module ChiseValue; end
12 module TableAccessModule; end
18 def initialize(type=Berkeley_DB, loc=nil, subtype=0, modemask=0755)
20 loc = Config.instance.db_dir if loc.nil?
27 attr_reader :type, :location, :subtype, :modemask
30 @fdb[f] = FeatureTable.new(self, f) if @fdb[f].nil?
35 @cdb[ccs] = CCSTable.new(self, ccs) if @cdb[ccs].nil?
40 each_entry("character/feature") {|f| yield(f) }
44 each_entry("character/by_feature") {|f| yield(f) }
47 def load_feature(name, cid)
48 ft = get_feature(name)
53 def decode_char(ccs, code_point)
60 def each_entry(subdir)
61 dir = @location + subdir
63 next if f.to_s == "." || f.to_s == ".."
64 next if f.to_s =~ /\.txt\Z/
65 yield(f.unescape_win_filename.unescape.to_s)
71 def initialize(dir, cat, keytype, name, amask, mmask)
74 dbdir = dir + cat + keytype
76 #FileUtils.mkdir_p(dbdir.to_s) unless dbdir.directory?
78 path = dbdir + name.path.escape.escape_win_filename
79 #qp path.basename.to_s, amask, mmask
80 # if /test/ =~ path.to_s
84 if amask == BDB::RDONLY
85 raise unless path.exist?
87 # @db = BDB::Hash.open(path.to_s, nil, amask, mmask)
88 @db = BDB::Hash.open(path.to_s, nil, amask)
99 #p ["AttributeTable: close", @name]
105 def get(k) @db.get(k); end
106 def put(k, v) @db.put(k, v); end
108 def each() @db.each {|k, v| yield(k, v) } end
111 module TableAccessModule
124 def setup_db(writable=nil)
126 sync if @access & BDB::CREATE == 0
127 @access = BDB::CREATE
129 @access = BDB::RDONLY
135 @db = AttributeTable.new(@ds.location, @category, @keyvalue,
136 @name, @access, @ds.modemask)
147 include TableAccessModule
149 def initialize(ds, name)
150 @ds, @name = ds, name
151 @category, @keyvalue = "character", "feature"
157 return nil if @db.nil?
158 parse_value(@db.get(format_char_id(cid)))
161 def set_value(cid, value)
163 raise "@db is nil." if @db.nil?
164 @db.put(format_char_id(cid), value)
169 raise "@db is nil." if @db.nil?
171 yield(parse_c_string(k), v)
178 include TableAccessModule
180 def initialize(ds, name)
181 @ds, @name = ds, name
182 @category, @keyvalue = "character", "by_feature"
186 def decode(code_point)
188 return nil if @db.nil?
189 parse_c_string(@db.get(code_point.to_s))
192 def set_decoded_char(code_point, cid)
194 raise "@db is nil." if @db.nil?
195 @db.put(code_point.to_s, format_char_id(cid))
200 raise "@db is nil." if @db.nil?
202 yield(parse_value(k), parse_c_string(v))
210 #return v if v.kind_of?(Integer)
211 return v.to_i if /\A\d+\Z/ =~ v # number?
212 return $1 if /\A"(.+)"\Z/ =~ v # remove surrounding "
213 #return v.sub(/\A\?/, "") if v =~ /\A\?/ # remove ? in the head
214 #return parse_sexp(v) if v =~ /\A\(.+\)\Z/ # parse sexp # not yet
218 def parse_c_string(str)
219 return nil if str.nil?
226 raise unless 2 <= len && c == ?\?
242 return c & (0x80 | 0x1F)
268 if (counter + 2 <= len)
269 (0...counter).each {|j|
270 cid = (cid << 6) | (str[j + i] & 0x3F)
278 def format_char_id(cid)
280 when ?\t then return "?\t"
281 when ?\n then return "?\n"
282 when ?\r then return "?\r"
283 when 0x1C then return "?\^\\"
287 return "?\\^"+(?@+cid).chr
288 elsif (cid == ?\s) || (cid == ?\") ||
289 (cid == ?\#) || (cid == ?\') ||
290 (cid == ?\() || (cid == ?\)) ||
291 (cid == ?\,) || (cid == ?\.) ||
292 (cid == ?\;) || (cid == ?\?) ||
293 (cid == ?\[) || (cid == ?\\) ||
294 (cid == ?\]) || (cid == ?\`)
302 dest += (((cid + ?@) >> 6) | 0xC0).chr
303 dest += (((cid + ?@) & 0x3F) | 0x80).chr
307 dest[1] = (cid >> 6) | 0xC0
308 dest[2] = (cid & 0x3F) | 0x80
310 elsif (cid <= 0xFFFF)
312 dest[1] = (cid >> 12) | 0xE0
313 dest[2] = ((cid >> 6) & 0x3F) | 0x80
314 dest[3] = (cid & 0x3F) | 0x80
316 elsif (cid <= 0x1FFFFF)
318 dest[1] = (cid >> 18) | 0xF0
319 dest[2] = ((cid >> 12) & 0x3F) | 0x80
320 dest[3] = ((cid >> 6) & 0x3F) | 0x80
321 dest[4] = (cid & 0x3F) | 0x80
323 elsif (cid <= 0x3FFFFFF)
325 dest[1] = (cid >> 24) | 0xF8
326 dest[2] = ((cid >> 18) & 0x3F) | 0x80
327 dest[3] = ((cid >> 12) & 0x3F) | 0x80
328 dest[4] = ((cid >> 6) & 0x3F) | 0x80
329 dest[5] = (cid & 0x3F) | 0x80
333 dest[1] = (cid >> 30) | 0xFC
334 dest[2] = ((cid >> 24) & 0x3F) | 0x80
335 dest[3] = ((cid >> 18) & 0x3F) | 0x80
336 dest[4] = ((cid >> 12) & 0x3F) | 0x80
337 dest[5] = ((cid >> 6) & 0x3F) | 0x80
338 dest[6] = (cid & 0x3F) | 0x80