class Rex::Parser::Ini

This class parses the contents of an INI file.

Attributes

path[R]

Public Class Methods

from_file(path) click to toggle source

Creates a new class instance and reads in the contents of the supplied file path.

# File lib/rex/parser/ini.rb, line 21
def self.from_file(path)
  ini = Ini.new(path)
  ini.from_file
  return ini
end
from_s(str) click to toggle source

Creates a new class instance from the supplied string.

# File lib/rex/parser/ini.rb, line 30
def self.from_s(str)
  ini = Ini.new
  ini.from_s(str)
  return ini
end
new(path = nil) click to toggle source

Initializes an ini instance and tries to read in the groups from the file if it exists.

# File lib/rex/parser/ini.rb, line 40
def initialize(path = nil)
  self.path = path

  # Try to synchronize ourself with the file if we
  # have one
  begin
    self.from_file if (self.path)
  rescue
  end
end

Public Instance Methods

add_group(name = 'global', reset = true) click to toggle source

Adds a group of the supplied name if it doesn’t already exist.

# File lib/rex/parser/ini.rb, line 63
def add_group(name = 'global', reset = true)
  self[name] = {} if (reset == true)
  self[name] = {} if (!self[name])

  return self[name]
end
each_group() { || ... } click to toggle source

Enumerates the groups hash keys.

# File lib/rex/parser/ini.rb, line 54
def each_group(&block)
  self.keys.each { |k|
    yield
  }
end
from_file(fpath = nil) click to toggle source

Reads in the groups from the supplied file path or the instance’s file path.

# File lib/rex/parser/ini.rb, line 87
def from_file(fpath = nil)
  fpath = path if (!fpath)

  read_groups(fpath)
end
from_s(str) click to toggle source

Reads in the groups from the supplied string.

# File lib/rex/parser/ini.rb, line 96
def from_s(str)
  read_groups_string(str.split("\n"))
end
group?(name) click to toggle source

Checks to see if name is a valid group.

# File lib/rex/parser/ini.rb, line 73
def group?(name)
  return (self[name] != nil)
end
to_file(tpath = nil) click to toggle source

Writes the group settings to a file.

# File lib/rex/parser/ini.rb, line 103
def to_file(tpath = nil)
  tpath = path if (!tpath)

  f = File.new(tpath, "w")
  f.write(to_s)
  f.close
end
to_s() click to toggle source

Converts the groups to a string.

# File lib/rex/parser/ini.rb, line 114
def to_s
  str = ''
  keys.sort.each { |k|
    str << "[#{k}]\n"

    self[k].each_pair { |var, val|
      str << "#{var}=#{val}\n"
    }

    str << "\n";
  }

  return str
end