Skip to content

Commit

Permalink
Fixed IniFile if a section is repeated (the previous was deleted)
Browse files Browse the repository at this point in the history
  • Loading branch information
aslze committed May 25, 2023
1 parent 2539b58 commit 9fc9093
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/IniFile.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <asl/IniFile.h>
#include <asl/TextFile.h>

#define NOSECTION "-"
namespace asl {

IniFile::Section IniFile::Section::clone() const
Expand All @@ -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);
Expand Down Expand Up @@ -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] != ';')
{
Expand All @@ -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);
Expand Down Expand Up @@ -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<String> oldlines = _lines.clone();

Expand Down Expand Up @@ -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--;
Expand Down
3 changes: 3 additions & 0 deletions tests/unittests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand Down

0 comments on commit 9fc9093

Please sign in to comment.