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
readonly
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.
-
#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 |
#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 |