summaryrefslogtreecommitdiff
path: root/template/encdb.h.tmpl
blob: fe6af9574782343f06d1d4282938d885da94409a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<% #-*- mode: ruby -*-
#
# 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.glob("*.[ch]", base: encdir).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
    File.open(File.join(encdir, fn)) do |f|
      name = nil
      f.each_line do |line|
        if (/^#ifndef RUBY/ =~ line)..(/^#endif/ =~ line)
        elsif /^OnigEncodingDefine/.match?(line)
          if (n = f.gets("\n\};")[/"(.*?)"/, 1]) # swallow the initializer block
            if name
              lines << %[ENC_SET_BASE("#{n}", "#{name}");]
            else
              name = n
            end
            check_duplication(defs, n, fn, f.lineno)
            next if BUILTIN_ENCODINGS[name]
            encodings << n
            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, f.lineno, $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, f.lineno, $2, $1] unless defs[$2.upcase]
          when /^ENC_DUMMY\w*\(\s*"([^"]+)"/
            count += 1
          else
            next
          end
          check_duplication(defs, $1, fn, f.lineno)
          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%>