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.



130
131
132
# File 'lib/rex/parser/ini.rb', line 130

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.



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

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

  return self[name]
end

#each_group(&block) ⇒ Object

Enumerates the groups hash keys.



55
56
57
58
59
# File 'lib/rex/parser/ini.rb', line 55

def each_group(&block)
  self.keys.each { |k|
    yield
  }
end

#from_file(fpath = nil) ⇒ Object

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



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

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.



97
98
99
# File 'lib/rex/parser/ini.rb', line 97

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)


74
75
76
# File 'lib/rex/parser/ini.rb', line 74

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

#to_file(tpath = nil) ⇒ Object

Writes the group settings to a file.



104
105
106
107
108
109
110
# File 'lib/rex/parser/ini.rb', line 104

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.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rex/parser/ini.rb', line 115

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