ini :: IniFile :: load_string
string
var ini = new IniFile
ini.load_string("""
section1.key1=value1
section1.key2=value2
[section2]
key=value3
""")
assert ini["section1.key1"] == "value1"
assert ini["section1.key2"] == "value2"
assert ini["section2.key"] == "value3"
Returns true
if the parsing finished correctly.
See also stop_on_first_error
and errors
.
# Read INI content from `string`
#
# ~~~
# var ini = new IniFile
# ini.load_string("""
# section1.key1=value1
# section1.key2=value2
# [section2]
# key=value3
# """)
# assert ini["section1.key1"] == "value1"
# assert ini["section1.key2"] == "value2"
# assert ini["section2.key"] == "value3"
# ~~~
#
# Returns `true` if the parsing finished correctly.
#
# See also `stop_on_first_error` and `errors`.
fun load_string(string: String): Bool do
var stream = new StringReader(string)
var last_section = null
var was_error = false
var i = 0
while not stream.eof do
i += 1
var line = stream.read_line.trim
if line.is_empty then
continue
else if line.has_prefix(";") then
continue
else if line.has_prefix("#") then
continue
else if line.has_prefix("[") then
var section = new IniSection(line.substring(1, line.length - 2).trim)
sections.add section
last_section = section
continue
else
var parts = line.split_once_on("=")
if parts.length != 2 then
# FIXME silent skip?
# we definitely need exceptions...
was_error = true
errors.add new IniError("Unexpected string `{line}` at line {i}.")
if stop_on_first_error then return was_error
continue
end
var key = parts[0].trim
var value = parts[1].trim
if last_section != null then
last_section[key] = value
else
self[key] = value
end
end
end
stream.close
return was_error
end
lib/ini/ini.nit:340,2--399,4