Sha256: d7bf5fd2c17cb5a13a841bb790055421c5415cf05992142b143c53bd5a3bb02e

Contents?: true

Size: 1.8 KB

Versions: 13

Compression:

Stored size: 1.8 KB

Contents

module AssLauncher
  # Common objects and methods
  module Support
    EOF = "\r\n"
    BOM = "\xEF\xBB\xBF".force_encoding('utf-8')

    # v8i file reader-writer
    module V8iFile
      require 'inifile'
      class ReadError < StandardError; end

      # Read v8i content and return array of v8i sections
      # @param io [IO] the input starem opened for read
      # @return [Array<V8iSection>]
      def self.read(io)
        res = []
        inifile = to_inifile(io.read)
        inifile.each_section do |caption|
          res << V8iSection.new(caption, inifile[caption])
        end
        res
      end

      # Read v8i file
      # @param filename [String]
      # @return (see read)
      # @raise [ReadError] if file not exists
      def self.load(filename)
        fail ReadError, "File #{filename} not exist or not a file"\
          unless File.file? filename
        read File.new(filename, 'r:bom|utf-8')
      end

      # Write sections in to output stream
      # @param io [IO] the output stream open for writing
      # @param sections [Array<V8iSection>] sections for write
      def self.write(io, sections)
        sections.each do |s|
          io.write(s.to_s + "\r\n")
        end
      end

      # Save sections in to v8i file
      # @param filename [String]
      # @param sections (see write)
      def self.save(filename, sections)
        f = File.new(filename, 'w')
        write f, sections
        f.close
      end

      private

      def self.to_inifile(content)
        IniFile.new(content: "#{escape_content(content)}", comment: '')
      end

      def self.escape_content(content)
        content.gsub!(BOM, '')
        content.gsub!(/\\\\/, '\\\\\\\\\\')
        %w(r n t 0).each do |l|
          content.gsub!(/\\#{l}/, "\\\\\\#{l}")
        end
        content
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
ass_launcher-0.7.0 lib/ass_launcher/support/v8i_file.rb
ass_launcher-0.6.0 lib/ass_launcher/support/v8i_file.rb
ass_launcher-0.5.0 lib/ass_launcher/support/v8i_file.rb
ass_launcher-0.4.1 lib/ass_launcher/support/v8i_file.rb
ass_launcher-0.4.0 lib/ass_launcher/support/v8i_file.rb
ass_launcher-0.3.4 lib/ass_launcher/support/v8i_file.rb
ass_launcher-0.3.3 lib/ass_launcher/support/v8i_file.rb
ass_launcher-0.3.2 lib/ass_launcher/support/v8i_file.rb
ass_launcher-0.3.1 lib/ass_launcher/support/v8i_file.rb
ass_launcher-0.3.0 lib/ass_launcher/support/v8i_file.rb
ass_launcher-0.2.2 lib/ass_launcher/support/v8i_file.rb
ass_launcher-0.2.1 lib/ass_launcher/support/v8i_file.rb
ass_launcher-0.2.0 lib/ass_launcher/support/v8i_file.rb