Class: Rex::Parser::Ini
- Inherits:
-
Hash
- Object
- Hash
- Rex::Parser::Ini
- Defined in:
- lib/rex/parser/ini.rb
Overview
This class parses the contents of an INI file.
Instance Attribute Summary collapse
-
#path ⇒ Object
Returns the value of attribute path.
Class Method Summary collapse
-
.from_file(path) ⇒ Object
Creates a new class instance and reads in the contents of the supplied file path.
-
.from_s(str) ⇒ Object
Creates a new class instance from the supplied string.
Instance Method Summary collapse
-
#add_group(name = 'global', reset = true) ⇒ Object
Adds a group of the supplied name if it doesn’t already exist.
-
#from_file(fpath = nil) ⇒ Object
Reads in the groups from the supplied file path or the instance’s file path.
-
#from_s(str) ⇒ Object
Reads in the groups from the supplied string.
-
#group?(name) ⇒ Boolean
Checks to see if name is a valid group.
-
#initialize(path = nil) ⇒ Ini
constructor
Initializes an ini instance and tries to read in the groups from the file if it exists.
-
#read_groups(fpath) ⇒ Object
protected
Reads in the groups and their attributes from the supplied file path or from the instance’s file path if one was set.
-
#read_groups_string(str) ⇒ Object
protected
Reads groups from the supplied string.
-
#to_file(tpath = nil) ⇒ Object
Writes the group settings to a file.
-
#to_s ⇒ Object
Converts the groups to a string.
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
#path ⇒ Object
Returns the value of attribute path.
123 124 125 |
# File 'lib/rex/parser/ini.rb', line 123 def path @path end |
Class Method Details
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.
67 68 69 |
# File 'lib/rex/parser/ini.rb', line 67 def group?(name) return (self[name] != nil) end |
#read_groups(fpath) ⇒ Object (protected)
Reads in the groups and their attributes from the supplied file path or from the instance’s file path if one was set.
131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/rex/parser/ini.rb', line 131 def read_groups(fpath) # :nodoc: if (!fpath) raise ArgumentError, "No file path specified.", caller end # Read in the contents of the file lines = ::IO.readlines(fpath) # Now read the contents from the supplied string read_groups_string(lines) end |
#read_groups_string(str) ⇒ Object (protected)
Reads groups from the supplied string
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/rex/parser/ini.rb', line 147 def read_groups_string(str) # :nodoc: # Reset the groups hash self.clear # The active group active_group = nil # Walk each line initializing the groups str.each { |line| next if (line.match(/^;/)) # Eliminate cr/lf line.gsub!(/(\n|\r)/, '') # Is it a group [bob]? if (md = line.match(/^\[(.+?)\]/)) active_group = md[1] self[md[1]] = {} # Is it a VAR=VAL? elsif (md = line.match(/^(.+?)=(.*)$/)) if (active_group) var, val = md[1], md[2] # don't clobber datastore nils with "" unless val.empty? self[active_group][var] = val end end end } 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_s ⇒ Object
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 |