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
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
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
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
Private Instance Methods
# File lib/encoding-codepage.rb, line 64 def codepage_file File.join(File.dirname(__FILE__), "codepages.tsv") end
# 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