diff options
author | Jari Vetoniemi <jari.vetoniemi@indooratlas.com> | 2020-03-16 18:49:26 +0900 |
---|---|---|
committer | Jari Vetoniemi <jari.vetoniemi@indooratlas.com> | 2020-03-30 00:39:06 +0900 |
commit | fcbf63e62c627deae76c1b8cb8c0876c536ed811 (patch) | |
tree | 64cb17de3f41a2b6fef2368028fbd00349946994 /jni/ruby/template/encdb.h.tmpl |
Fresh start
Diffstat (limited to 'jni/ruby/template/encdb.h.tmpl')
-rw-r--r-- | jni/ruby/template/encdb.h.tmpl | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/jni/ruby/template/encdb.h.tmpl b/jni/ruby/template/encdb.h.tmpl new file mode 100644 index 0000000..9cbb1f0 --- /dev/null +++ b/jni/ruby/template/encdb.h.tmpl @@ -0,0 +1,91 @@ +<% +# +# OnigEncodingDefine(foo, Foo) = { +# .. +# "Shift_JIS", /* Canonical Name */ +# .. +# }; +# ENC_ALIAS("SJIS", "Shift_JIS") +# ENC_REPLICATE("Windows-31J", "Shift_JIS") +# ENC_ALIAS("CP932", "Windows-31J") +# + +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] + end +end + +lines = [] +BUILTIN_ENCODINGS = { + 'ASCII-8BIT' => 0, + 'UTF-8' => 1, + 'US-ASCII' => 2, +} +encodings = %w[ASCII-8BIT UTF-8 US-ASCII] # BUILTIN_ENCODINGS.keys is not available on cross compiling and used ruby 1.8 +count = encodings.size +defs = {} +encdirs = ARGV.dup +encdirs << 'enc' if encdirs.empty? +files = {} +encdirs.each do |encdir| + next unless File.directory?(encdir) + 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| + next if files[fn] + files[fn] = true + open(File.join(encdir,fn)) do |f| + name = nil + f.each_line do |line| + if (/^OnigEncodingDefine/ =~ line)..(/"(.*?)"/ =~ line) + if $1 + if name + lines << %[ENC_SET_BASE("#$1", "#{name}");] + else + name = $1 + end + check_duplication(defs, $1, fn, $.) + next if BUILTIN_ENCODINGS[name] + encodings << $1 + count += 1 + end + else + case line + when /^\s*rb_enc_register\(\s*"([^"]+)"/ + count += 1 + line = nil + encodings << $1 + when /^ENC_REPLICATE\(\s*"([^"]+)"\s*,\s*"([^"]+)"/ + raise ArgumentError, + '%s:%d: ENC_REPLICATE: %s is not defined yet. (replica %s)' % + [fn, $., $2, $1] unless defs[$2.upcase] + count += 1 + when /^ENC_ALIAS\(\s*"([^"]+)"\s*,\s*"([^"]+)"/ + raise ArgumentError, + '%s:%d: ENC_ALIAS: %s is not defined yet. (alias %s)' % + [fn, $., $2, $1] unless defs[$2.upcase] + when /^ENC_DUMMY\w*\(\s*"([^"]+)"/ + count += 1 + else + next + end + check_duplication(defs, $1, fn, $.) + lines << line.sub(/;.*/m, "").chomp + ";" if line + end + end + end + end +end +encodings.each_with_index do |e, i| +%>ENC_DEFINE("<%=e%>"); +% end +% encidx = encodings.size - 1 +% lines.each do |line| +<%=line%> +% end + +#define ENCODING_COUNT <%=count%> |