2008-01-13 03:10:00 +03:00
|
|
|
#
|
|
|
|
# OnigEncodingDefine(foo, Foo) = {
|
|
|
|
# ..
|
|
|
|
# "Shift_JIS", /* Canonical Name */
|
|
|
|
# ..
|
|
|
|
# };
|
|
|
|
# ENC_ALIAS("SJIS", "Shift_JIS")
|
|
|
|
# ENC_REPLICATE("Windows-31J", "Shift_JIS")
|
|
|
|
# ENC_ALIAS("CP932", "Windows-31J")
|
|
|
|
#
|
|
|
|
|
2008-01-19 23:15:13 +03:00
|
|
|
def check_duplication(defs, name, fn, line)
|
|
|
|
if defs[name]
|
|
|
|
raise ArgumentError, "%s:%d: encoding %s is already registered(%s:%d)" %
|
|
|
|
[fn, line, name, *defs[name]]
|
|
|
|
else
|
|
|
|
defs[name.upcase] = [fn,line]
|
2008-01-13 23:46:00 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-01-17 17:56:22 +03:00
|
|
|
count = 0
|
|
|
|
lines = []
|
2008-01-13 03:10:00 +03:00
|
|
|
encodings = []
|
2008-01-19 23:15:13 +03:00
|
|
|
defs = {}
|
2008-01-13 03:10:00 +03:00
|
|
|
encdir = ARGV[0]
|
2008-01-17 17:56:22 +03:00
|
|
|
outhdr = ARGV[1] || 'encdb.h'
|
2008-02-22 03:07:23 +03:00
|
|
|
Dir.open(encdir) {|d| d.grep(/.+\.[ch]\z/)}.sort_by {|e|
|
|
|
|
e.scan(/(\d+)|(\D+)/).map {|n,a| a||[n.size,n.to_i]}.flatten
|
|
|
|
}.each do |fn|
|
2008-01-13 03:10:00 +03:00
|
|
|
open(File.join(encdir,fn)) do |f|
|
|
|
|
orig = nil
|
|
|
|
name = nil
|
|
|
|
f.each_line do |line|
|
2008-01-17 17:56:22 +03:00
|
|
|
if (/^OnigEncodingDefine/ =~ line)..(/"(.*?)"/ =~ line)
|
|
|
|
if $1
|
2008-01-19 23:15:13 +03:00
|
|
|
check_duplication(defs, $1, fn, $.)
|
2008-01-17 17:56:22 +03:00
|
|
|
encodings << $1
|
|
|
|
count += 1
|
|
|
|
end
|
|
|
|
else
|
|
|
|
case line
|
|
|
|
when /^\s*rb_enc_register\(\s*"([^"]+)"/
|
|
|
|
count += 1
|
|
|
|
line = nil
|
|
|
|
when /^ENC_REPLICATE\(\s*"([^"]+)"\s*,\s*"([^"]+)"/
|
|
|
|
raise ArgumentError,
|
|
|
|
'%s:%d: ENC_REPLICATE: %s is not defined yet. (replica %s)' %
|
2008-01-19 23:15:13 +03:00
|
|
|
[fn, $., $2, $1] unless defs[$2.upcase]
|
2008-01-17 17:56:22 +03:00
|
|
|
count += 1
|
|
|
|
when /^ENC_ALIAS\(\s*"([^"]+)"\s*,\s*"([^"]+)"/
|
|
|
|
raise ArgumentError,
|
|
|
|
'%s:%d: ENC_ALIAS: %s is not defined yet. (alias %s)' %
|
2008-01-19 23:15:13 +03:00
|
|
|
[fn, $., $2, $1] unless defs[$2.upcase]
|
2008-01-17 17:56:22 +03:00
|
|
|
when /^ENC_DUMMY\(\s*"([^"]+)"/
|
|
|
|
count += 1
|
|
|
|
else
|
|
|
|
next
|
|
|
|
end
|
2008-01-19 23:15:13 +03:00
|
|
|
check_duplication(defs, $1, fn, $.)
|
2008-01-17 17:56:22 +03:00
|
|
|
lines << line.sub(/;.*/m, ";\n") if line
|
2008-01-13 03:10:00 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2008-01-13 17:29:12 +03:00
|
|
|
|
2008-01-17 17:56:22 +03:00
|
|
|
result = encodings.map {|e| %[ENC_DEFINE("#{e}");\n]}.join + lines.join +
|
|
|
|
"\n#define ENCODING_COUNT #{count}\n"
|
|
|
|
mode = IO::RDWR|IO::CREAT
|
|
|
|
mode |= IO::BINARY if defined?(IO::BINARY)
|
|
|
|
open(outhdr, mode) do |f|
|
|
|
|
unless f.read == result
|
|
|
|
f.rewind
|
|
|
|
f.truncate(0)
|
|
|
|
f.print result
|
|
|
|
end
|
2008-01-13 03:10:00 +03:00
|
|
|
end
|