From 9fc909365c7dcda867833018fcab442f3988fa69 Mon Sep 17 00:00:00 2001 From: Alvaro Date: Thu, 25 May 2023 22:41:24 +0200 Subject: [PATCH] Fixed IniFile if a section is repeated (the previous was deleted) --- src/IniFile.cpp | 14 ++++++++------ tests/unittests.cpp | 3 +++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/IniFile.cpp b/src/IniFile.cpp index 9103309..c0c6eab 100644 --- a/src/IniFile.cpp +++ b/src/IniFile.cpp @@ -1,6 +1,7 @@ #include #include +#define NOSECTION "-" namespace asl { IniFile::Section IniFile::Section::clone() const @@ -15,7 +16,7 @@ IniFile::IniFile(const String& name, bool shouldwrite) { _modified = false; _filename = name; - _currentTitle = "-"; + _currentTitle = NOSECTION; _ok = false; _shouldwrite = shouldwrite; TextFile file(name, File::READ); @@ -44,7 +45,8 @@ IniFile::IniFile(const String& name, bool shouldwrite) continue; String name = line.substring(1, end); _currentTitle = name; - _sections[_currentTitle] = Section(_currentTitle); + if (!_sections.has(_currentTitle)) + _sections[_currentTitle] = Section(_currentTitle); } else if(line[i0] != '#' && line[i0] > 47 && line[i0] != ';') { @@ -69,7 +71,7 @@ IniFile::IniFile(const String& name, bool shouldwrite) _sections[_currentTitle][key] = value; } } - _currentTitle = "-"; + _currentTitle = NOSECTION; for(int i=_lines.length()-1; i>0 && _lines[i][0] == '\0'; i--) { _lines.resize(_lines.length()-1); @@ -147,7 +149,7 @@ void IniFile::write(const String& fname) foreach(Section & s, newsec) s = s.clone(); - Section* section = &_sections["-"]; + Section* section = &_sections[NOSECTION]; Array oldlines = _lines.clone(); @@ -202,9 +204,9 @@ void IniFile::write(const String& fname) int j=-1; for(int i=0; i<_lines.length(); i++) { - if(section.title()=="-" || _lines[i]=='['+section.title()+']') + if (section.title() == NOSECTION || _lines[i] == '[' + section.title() + ']') { - int k = (section.title()=="-")? 0 : 1; + int k = (section.title() == NOSECTION) ? 0 : 1; for(j=i+k; j<_lines.length() && _lines[j][0]!='['; j++) {} j--; diff --git a/tests/unittests.cpp b/tests/unittests.cpp index d0d567e..a736f3a 100644 --- a/tests/unittests.cpp +++ b/tests/unittests.cpp @@ -88,6 +88,9 @@ ASL_TEST(IniFile) ASL_ASSERT(file.arraysize("list") == 2); ASL_ASSERT(file.array("x", 0) == "7"); ASL_ASSERT(file.array("y", 1) == "3"); + Dic<> all = file.values(); + ASL_ASSERT(all["sec1/field1"] == "value1"); + ASL_ASSERT(file.values("sec1")["field1"] == "value1"); } }