Sha256: 86bdbcac271d0acdd232ef7d68da48ae077d4fa583ca42f62493f0b8a6b8357a

Contents?: true

Size: 944 Bytes

Versions: 1

Compression:

Stored size: 944 Bytes

Contents

class IniReader
  def initialize( string = '')
    @section_hash = {}
    parse(StringIO.new(string))
  end

  def self.parse(filename)
    reader = IniReader.new
    reader.send(:parse, File.open(filename, "r"))
    return reader
  end

  #returns the hash for a section of the inifile
  def []( section_name )
    return @section_hash[section_name]
  end

  private
  def parse(io)
    current_section = nil
    io.each do |line|
      line = line.strip
      if line[0] == ';'
        next
      elsif ( matchdata = line.match(/^\[(.+)\]$/) ) != nil
        current_section = @section_hash[matchdata[1].to_sym] || {}
        @section_hash[matchdata[1].to_sym] = current_section
      elsif ( matchdata = line.match(/^([^=]*)=(.*)$/) )
        current_section[matchdata[1].to_sym] = matchdata[2].to_s
      elsif line.strip == ""
        next
      else
        raise Exception.new("Could not parse line: #{line}")
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
inireader-0.1.2 lib/inireader/base.rb