Skip to content

Commit

Permalink
add requote option
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuafarr committed Jan 25, 2012
1 parent f11e6db commit 531890a
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions lib/inifile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ class Error < StandardError; end
# IniFile.load( filename )
# IniFile.load( filename, options )
#
# Open the given _filename_ and load the contetns of the INI file.
# Open the given _filename_ and load the contents of the INI file.
# The following _options_ can be passed to this method:
#
# :comment => ';' The line comment character(s)
# :parameter => '=' The parameter / value separator
# :default => nil The default section name if there is no section
# :liberal => false Allow paramater values which don't start with quotes but have quotes in them
# :requote => false Requote parameter values after parsing
#
def self.load( filename, opts = {} )
new(filename, opts)
Expand All @@ -51,6 +52,7 @@ def initialize( filename, opts = {} )
@param = opts[:parameter] || '='
@default = opts[:default]
@liberal = opts[:liberal]
@requote = opts[:requote]
@encoding = opts[:encoding]
@ini = Hash.new {|h,k| h[k] = Hash.new}

Expand Down Expand Up @@ -314,19 +316,23 @@ def parse
line = line.chomp

# mutline start
# create tmp variables to indicate that a multine has started
# create tmp variables to indicate that a multiline has started
# and the next lines of the ini file will be checked
# against the other mutline rgxps.
# against the other multiline rgxps.
if line =~ @rgxp_multiline_start then

tmp_param = $1.strip
tmp_value = $2 + "\n"

# the mutline end-delimiter is found
# the multiline end-delimiter is found
# clear the tmp vars and add the param / value pair to the section
elsif line =~ @rgxp_multiline_end && tmp_param != "" then

section[tmp_param] = tmp_value + $1
if @requote then
section[tmp_param] = '"' + tmp_value + $1 + '"'
else
section[tmp_param] = tmp_value + $1
end
tmp_value, tmp_param = "", ""

# anything else between multiline start and end
Expand All @@ -351,7 +357,11 @@ def parse
if section == nil and @default != nil then
section = @ini[@default]
end
section[$1.strip] = $2.strip
if @requote
section[$1.strip] = '"' + $2.strip + '"'
else
section[$1.strip] = $2.strip
end
rescue NoMethodError
raise Error, "parameter encountered before first section"
end
Expand Down

0 comments on commit 531890a

Please sign in to comment.