X-Git-Url: http://nitlanguage.org diff --git a/lib/ini.nit b/lib/ini.nit index 282f737..2f81240 100644 --- a/lib/ini.nit +++ b/lib/ini.nit @@ -184,12 +184,29 @@ class ConfigTree # assert config["foo.bar.baz"] == "foobarbaz" # assert config["goo.boo.bar"] == "gooboobar" # assert config["goo.boo.baz.bar"] == "gooboobazbar" + # + # Using the array notation + # + # str = """ + # foo[]=a + # foo[]=b + # foo[]=c""" + # str.write_to_file("config4.ini") + # # load file + # config = new ConfigTree("config4.ini") + # print config.to_map.join(":", ",") + # assert config["foo.0"] == "a" + # assert config["foo.1"] == "b" + # assert config["foo.2"] == "c" + # assert config.at("foo").values.join(",") == "a,b,c" fun load do roots.clear var stream = new FileReader.open(ini_file) var path: nullable String = null + var line_number = 0 while not stream.eof do var line = stream.read_line + line_number += 1 if line.is_empty then continue else if line.has_prefix(";") then @@ -201,12 +218,16 @@ class ConfigTree set_node(path, null) else var parts = line.split("=") + assert parts.length > 1 else + print "Error: malformed ini at line {line_number}" + end var key = parts[0].trim var val = parts[1].trim - if path == null then - set_node(key, val) + if path != null then key = "{path}.{key}" + if key.has_suffix("[]") then + set_array(key, val) else - set_node("{path}.{key}", val) + set_node(key,val) end end end @@ -218,6 +239,16 @@ class ConfigTree private var roots = new Array[ConfigNode] + # Append `value` to array at `key` + private fun set_array(key: String, value: nullable String) do + key = key.substring(0, key.length - 2) + var len = 0 + if has_key(key) then + len = get_node(key).children.length + end + set_node("{key}.{len.to_s}", value) + end + private fun set_node(key: String, value: nullable String) do var parts = key.split(".").reversed var k = parts.pop @@ -297,4 +328,3 @@ private class ConfigNode return null end end -