diff --git a/src/IniFile.jl b/src/IniFile.jl index 9e180ec..02bda59 100644 --- a/src/IniFile.jl +++ b/src/IniFile.jl @@ -35,42 +35,54 @@ defaults(inifile::Inifile) = inifile.defaults sections(inifile::Inifile) = inifile.sections -function read(inifile::Inifile, stream::IO) +function read(inifile::Inifile, stream::IO, midline_comments=false) current_section = inifile.defaults + previous_line = "" for line in EachLine(stream) s = strip(line) # comments start with # or ; - if length(s) < 3 || s[1] == '#' || s[1] == ';' + if (previous_line == "" && length(s) < 3) || s[1] == '#' || s[1] == ';' continue - elseif s[1] == '[' && s[end] == ']' + end + # Sections are declared between brackets '[]' + if s[1] == '[' && s[end] == ']' section = s[2:end-1] if !haskey(inifile.sections, section) inifile.sections[section] = HTSS() end current_section = inifile.sections[section] + continue + end + # Multiline values. First join with the previous line + s = previous_line * s + if s[end] == '\\' # Should midline-comments be extended? Assume yes. + previous_line = s[1:end-1] + continue else - i = search(s, '=') - j = search(s, ':') - if i == 0 && j == 0 - # TODO: allow multiline values - println("skipping malformed line: $s") - else - idx = min(i, j) - if idx == 0 - idx = max(i, j) - end - key = rstrip(s[1:idx-1]) - val = lstrip(s[idx+1:end]) - current_section[key] = val + previous_line = "" + end + # Process parameter / value pair + i = search(s, '=') + j = search(s, ':') + if i == 0 && j == 0 + println("skipping malformed line: $s") + else + idx = min(i, j) + if idx == 0 + idx = max(i, j) end + key = rstrip(s[1:idx-1]) + val = lstrip(s[idx+1:end]) + val = (midline_comments) ? strip(split(val, ';')[1]) : val + current_section[key] = val end end inifile end -function read(inifile::Inifile, filename::String) +function read(inifile::Inifile, filename::String, midline_comments=false) open(filename) do f - read(inifile, f) + read(inifile, f, midline_comments) end inifile end @@ -114,8 +126,8 @@ function set(inifile::Inifile, section::String, key::String, val::INIVAL) (val == nothing) && return val inifile.sections[section] = HTSS() end - - sec = inifile.sections[section] + + sec = inifile.sections[section] if val == nothing if haskey(sec, key) delete!(sec, key) @@ -139,13 +151,13 @@ function set(inifile::Inifile, key::String, val::INIVAL) val end -get_bool(inifile::Inifile, section::String, key::String) = +get_bool(inifile::Inifile, section::String, key::String) = lowercase(get(inifile, section, key)) == "true" -get_int(inifile::Inifile, section::String, key::String) = +get_int(inifile::Inifile, section::String, key::String) = parseint(get(inifile, section, key)) -get_float(inifile::Inifile, section::String, key::String) = +get_float(inifile::Inifile, section::String, key::String) = parsefloat(get(inifile, section, key)) haskey(inifile::Inifile, section::String, key::String) = diff --git a/test/test.ini b/test/test.ini index a4da90b..1f80e74 100644 --- a/test/test.ini +++ b/test/test.ini @@ -17,3 +17,13 @@ intname1 = 10 delimtest1 = value with : delimtest2 : name = value +[section 4] +midline = value ; comment +multiline1 = This is multiple \ +lines of text that should be \ +formed into a single line\ +. +multiline2 = This is multiple \ +lines of text that; should be \ +formed into a single line \ +without the commented section. diff --git a/test/test.jl b/test/test.jl index 55f926a..019751e 100644 --- a/test/test.jl +++ b/test/test.jl @@ -36,6 +36,19 @@ read(ini, "test.ini") @test get(ini, "section 2", "delimtest1") == "value with :" @test get(ini, "section 2", "delimtest2") == "name = value" +# test comments mid-line +ini2 = Inifile() +read(ini2, "test.ini", true) +@test get(ini, "section 4", "midline") == "value ; comment" +@test get(ini2, "section 4", "midline") == "value" + +# test multi-line values +@test get(ini, "section 4", "multiline1") == "This is multiple lines of text that should be formed into a single line." + +# test multi-line value with comment midline +@test get(ini, "section 4", "multiline2") == "This is multiple lines of text that; should be formed into a single line without the commented section." +@test get(ini2, "section 4", "multiline2") == "This is multiple lines of text that" + # key/section not found @test get(ini, "section 2", "noname") == :notfound @test get(ini, "section 3", "noname") == :notfound @@ -56,4 +69,3 @@ ini = Inifile() read(ini, iob) @test get(ini, "section 3", "strname1") == "new string" @test get(ini, "section 3", "intname1") == "1" -