aboutsummaryrefslogtreecommitdiffstats
path: root/enc/make_encdb.rb
blob: 32dacbc740a70c0e8a238b72792a0b09821d5a37 (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
#
# OnigEncodingDefine(foo, Foo) = {
#   ..
#   "Shift_JIS", /* Canonical Name */
#   ..
# };
# ENC_ALIAS("SJIS", "Shift_JIS")
# ENC_REPLICATE("Windows-31J", "Shift_JIS")
# ENC_ALIAS("CP932", "Windows-31J")
#

encodings = []
replicas = {}
aliases = {}
encdir = ARGV[0]
Dir.open(encdir) {|d| d.grep(/.+\.c\z/)}.each do |fn|
  open(File.join(encdir,fn)) do |f|
    orig = nil
    name = nil
    f.each_line do |line|
      break if /^OnigEncodingDefine/o =~ line
    end
    f.each_line do |line|
      break if /"(.*?)"/ =~ line
    end
    encodings << $1 if $1
    f.each_line do |line|
      if /^ENC_REPLICATE\(\s*"([^"]+)"\s*,\s*"([^"]+)"/o =~ line
	replicas[$1] = $2
      elsif /^ENC_ALIAS\(\s*"([^"]+)"\s*,\s*"([^"]+)"/o =~ line
	aliases[$1] = $2
      end
    end
  end
end
p aliases
open('encdb.h', 'wb') do |f|
  f.puts 'static const char *enc_name_list[] = {'
  encodings.each {|name| f.puts'    "%s",' % name}
  replicas.each_key {|name| f.puts'    "%s",' % name}
  aliases.each_key {|name| f.puts'    "%s",' % name}
  f.puts(<<"_TEXT_")
};
#define enc_name_list_size (sizeof(enc_name_list)/sizeof(enc_name_list[0]))

static void enc_init_db(void)
{
    if (!enc_table.replica_name) {
	enc_table.replica_name = st_init_strcasetable();
    }
    if (!enc_table.alias_name) {
	enc_table.alias_name = st_init_strcasetable();
    }
_TEXT_
  replicas.each_pair {|name, orig|
    f.puts'    st_insert(enc_table.replica_name, (st_data_t)"%s", (st_data_t)"%s");' % [name, orig]}
  aliases.each_pair {|name, orig|
    f.puts'    st_insert(enc_table.alias_name, (st_data_t)"%s", (st_data_t)"%s");' % [name, orig]}
  f.puts '}'
end