Sha256: d3a2b4606b0eee113e613c89def52789fbfddca320213c8f1e5d3d0e474f9e19

Contents?: true

Size: 1.32 KB

Versions: 7

Compression:

Stored size: 1.32 KB

Contents

module Apstrings
  require 'fileutils'
  # Class for reading in files and returning an array of its content
  class Reader
    # Reads in a file and returns an array consisting of each line of input
    # cleaned of new line characters
    def self.read(file)
      File.open(file, 'r') do |f|
        str = f.read
        if str.bytesize >= 3 and str.getbyte(0) == 0xFF and str.getbyte(1) == 0xFE
            #UTF-16lE
            encoder = Encoding::Converter.new("UTF-16lE", "UTF-8")
            str = encoder.convert(str)
            str = str.byteslice(3, str.bytesize - 3) # 去掉FFFE
        elsif str.bytesize >= 3 and str.getbyte(0) == 0xFE and str.getbyte(1) == 0xFF
            #UTF-16bE
            encoder = Encoding::Converter.new("UTF-16bE", "UTF-8")
            str = encoder.convert(str)
            str = str.byteslice(3, str.bytesize - 3) # 去掉FEFF
        elsif str.bytesize >= 4 and str.getbyte(0) == 0xEF and str.getbyte(1) == 0xBB and str.getbyte(2) == 0xBF
            #UTF8
            str = str.byteslice(4, str.bytesize - 4) # 去掉EFBBBF
        else
            #ASCII  
            str = str.force_encoding('UTF-8')
        end
        content = str
        content.each_line.inject([]) do |content_array, line|
          line.gsub!("\n","")
          content_array.push(line)
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
apstrings-0.4.1 lib/apstrings/reader.rb
apstrings-0.4.0 lib/apstrings/reader.rb
apstrings-0.3.9 lib/apstrings/reader.rb
apstrings-0.3.8 lib/apstrings/reader.rb
apstrings-0.3.7 lib/apstrings/reader.rb
apstrings-0.3.6 lib/apstrings/reader.rb
apstrings-0.3.5 lib/apstrings/reader.rb