File: charset_alias.rb

# charset_alias.rb

Path: | iconv/charset_alias.rb
Last Update: | Wed Oct 12 21:12:25 AUS Eastern Standard Time 2005

### Required files

rbconfig optparse

### Methods

charset_alias

### Constants

OS | = | Config::CONFIG["target_os"] |  | www.ctan.org/tex-archive/macros/texinfo/texinfo/intl/config.charset Fri, 30 May 2003 00:09:00 GMT‘
SHELL | = | Config::CONFIG['SHELL']

### Public Instance methods

charset_alias(config_charset, mapfile, target = OS)

[Source]

    # File iconv/charset_alias.rb, line 27
    def charset_alias(config_charset, mapfile, target = OS)
      map = Hash::Ordered.new
      comments = []
      match = false
      open(config_charset) do |input|
        input.find {|line| /^case "\$os" in/ =~ line} or return
        input.find {|line|
          /^\s*([-\w\*]+(?:\s*\|\s*[-\w\*]+)*)(?=\))/ =~ line and
          $&.split('|').any? {|pattern| File.fnmatch?(pattern.strip, target)}
        } or return
        input.find do |line|
          case line
          when /^\s*echo "(?:\$\w+\.)?([-\w*]+)\s+([-\w]+)"/
            sys, can = $1, $2
            can.downcase!
            map[can] = sys
            false
          when /^\s*;;/
            true
          else
            false
          end
        end
      end
      case target
      when /linux|-gnu/
    #    map.delete('ascii')
      when /cygwin|os2-emx/
        # get rid of tilde/yen problem.
        map['shift_jis'] = 'cp932'
      end
      st = Hash.new(0)
      map = map.sort.collect do |can, *sys|
        if sys.grep(/^en_us(?=.|$)/i) {break true} == true
          noen = %r"^(?!en_us)\w+_\w+#{Regexp.new($')}$"i #"
          sys.reject! {|s| noen =~ s}
        end
        sys = sys.first
        st[sys] += 1
        [can, sys]
      end
      st.delete_if {|sys, i| i == 1}.empty?
      st.keys.each {|sys| st[sys] = nil}
      st.default = nil
      writer = proc do |f|
        f.puts("require 'iconv.so'")
        f.puts
        f.puts(comments)
        f.puts("class Iconv")
        i = 0
        map.each do |can, sys|
          if s = st[sys]
            sys = s
          elsif st.key?(sys)
            sys = (st[sys] = "sys#{i+=1}") + " = '#{sys}'.freeze"
          else
            sys = "'#{sys}'.freeze"
          end
          f.puts("  charset_map['#{can}'] = #{sys}")
        end
        f.puts("end")
      end
      if mapfile
        open(mapfile, "w", &writer)
      else
        writer[STDOUT]
      end
    end

[Validate]