summaryrefslogtreecommitdiff
path: root/template/transdb.h.tmpl
blob: 22b5960cd8879032a9f6914ca35ce204d53ea6fb (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
<% #-*- mode: ruby -*-
#
# static const rb_transcoder
# rb_from_US_ASCII = {
#     "US-ASCII", "UTF-8", &from_US_ASCII, 1, 0,
#     NULL, NULL,
# };
#

count = 0
converters = {}
transdirs = ARGV.dup
transdirs << 'enc/trans' if transdirs.empty?

transdirs = transdirs.sort_by {|td|
  -td.length
}.inject([]) {|tds, td|
  next tds unless File.directory?(td)
  tds << td if tds.all? {|td2| !File.identical?(td2, td) }
  tds
}

files = {}
names_t = []
transdirs.each do |transdir|
  names = Dir.entries(transdir)
  names_t += names.map {|n| n[/.+(?=\.trans\z)/]}.compact
  names_c = names.map {|n| n[/.+(?=\.c\z)/]}.compact
  (names_t & names_c).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
    path = File.join(transdir, "#{fn}.c")
    File.open(path) do |f|
      f.each_line do |line|
        if (/^static const rb_transcoder/ =~ line)
          if (/"(.*?)"\s*,\s*"(.*?)"/ =~ f.gets("\n\};")) # swallow the initializer block
            from_to = [$1.freeze, $2.freeze].freeze
            if converters[from_to]
              raise ArgumentError,
                    '%s:%d: transcode "%s to %s" is already registered at %s:%d' %
                    [path, f.lineno, *from_to, *converters[from_to].values_at(3, 4)]
            else
              converters[from_to] = [fn, path, f.lineno]
            end
          end
        end
      end
    end
  end
end
converters.each do |(from, to), (fn)|
%>rb_declare_transcoder("<%=from%>", "<%=to%>", "<%=fn%>");
% end