module Encoding::CodePage

Encoding::Codepage adds two methods to the Encoding class to help you look Encodings by their Microsoft® Codae Page Identifier. It doesn't add support to Ruby for Encodings that are not already supported, just makes it easier to find those encodings which are.

At the moment, this list is available on the web at:

Private Class Methods

reverse_lookup() click to toggle source

A Hash from <non-codepage-encoding> to <codepage-encoding> to facilitate running codepage_id in the case where Ruby has both the CodePage encoding and its alias, but thinks they are different Encodings.

# File lib/encoding-codepage.rb, line 72
def self.reverse_lookup
  @reverse_lookup ||= {}
end

Public Instance Methods

codepage(id) click to toggle source

Find an Encoding object from a Micrsoft® Code Page Identifier.

A list of Code Page identifiers can be found at:

NOTE: This library doesn't add support for all Code Pages, it merely allows you to look up existing encodings by their Code Page Identifier.

@param Integer The Code Page Identifier. @return Encoding The Encoding object. @raise ArgumentError The Code Page you tried to find doesn't exist.

# File lib/encoding-codepage.rb, line 23
def codepage(id)
  Encoding.find("CP#{id}")
end
codepage?(id) click to toggle source

Determine whether a Code Page exists with the given Code Page Identifier.

@param Integer The Code Page Identifier. @return [Encoding, nil] The Encoding iff it exists.

# File lib/encoding-codepage.rb, line 43
def codepage?(id)
  exist?("CP#{id}")
end
exist?(name) click to toggle source

Determine whether an Encoding exists with the given name.

@param String The name to search for. @return [Encoding, nil] The Encoding iff it exists.

# File lib/encoding-codepage.rb, line 32
def exist?(name)
  find(name)
rescue ArgumentError => e
  nil
end

Private Instance Methods

codepage_file() click to toggle source
# File lib/encoding-codepage.rb, line 64
def codepage_file
  File.join(File.dirname(__FILE__), "codepages.tsv")
end
load_codepages!() click to toggle source
# File lib/encoding-codepage.rb, line 49
def load_codepages!
  File.read(codepage_file).each_line{ |line|
    next if line.start_with?('#') || line =~ /\A\s*\z/

    number, original, comment = line.split("\t", 3)
    number = Integer(number, 10)

    if encoding = exist?(original.upcase) && !encoding.dummy?
      encoding.replicate "CP#{number}" unless codepage?(number)

      CodePage.reverse_lookup[encoding] = codepage(number)
    end
  }
end