Class: Rex::Parser::Ini

Inherits:
Hash
  • Object
show all
Defined in:
lib/rex/parser/ini.rb

Overview

This class parses the contents of an INI file.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ Ini

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



41
42
43
44
45
46
47
48
49
50
# File 'lib/rex/parser/ini.rb', line 41

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

Instance Attribute Details

#pathObject

Returns the value of attribute path.



123
124
125
# File 'lib/rex/parser/ini.rb', line 123

def path
  @path
end

Class Method Details

.from_file(path) ⇒ Object

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



22
23
24
25
26
# File 'lib/rex/parser/ini.rb', line 22

def self.from_file(path)
  ini = Ini.new(path)
  ini.from_file
  return ini
end

.from_s(str) ⇒ Object

Creates a new class instance from the supplied string.



31
32
33
34
35
# File 'lib/rex/parser/ini.rb', line 31

def self.from_s(str)
  ini = Ini.new
  ini.from_s(str)
  return ini
end

Instance Method Details

#add_group(name = 'global', reset = true) ⇒ Object

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



57
58
59
60
61
62
# File 'lib/rex/parser/ini.rb', line 57

def add_group(name = 'global', reset = true)
  self[name] = {} if (reset == true)
  self[name] = {} if (!self[name])

  return self[name]
end

#from_file(fpath = nil) ⇒ Object

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



81
82
83
84
85
# File 'lib/rex/parser/ini.rb', line 81

def from_file(fpath = nil)
  fpath = path if (!fpath)

  read_groups(fpath)
end

#from_s(str) ⇒ Object

Reads in the groups from the supplied string.



90
91
92
# File 'lib/rex/parser/ini.rb', line 90

def from_s(str)
  read_groups_string(str.split("\n"))
end

#group?(name) ⇒ Boolean

Checks to see if name is a valid group.

Returns:

  • (Boolean)


67
68
69
# File 'lib/rex/parser/ini.rb', line 67

def group?(name)
  return (self[name] != nil)
end

#to_file(tpath = nil) ⇒ Object

Writes the group settings to a file.



97
98
99
100
101
102
103
# File 'lib/rex/parser/ini.rb', line 97

def to_file(tpath = nil)
  tpath = path if (!tpath)

  f = File.new(tpath, "w")
  f.write(to_s)
  f.close
end

#to_sObject

Converts the groups to a string.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/rex/parser/ini.rb', line 108

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