Skip to content

Commit

Permalink
More simplifications in IniFile
Browse files Browse the repository at this point in the history
  • Loading branch information
aslze committed Jun 8, 2024
1 parent 205eed8 commit 30d77b1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
4 changes: 4 additions & 0 deletions include/asl/IniFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ class ASL_API IniFile
{
return _sections[_currentTitle][String(index + 1) << '\\' << field];
}
/**
Returns the names of sections in the file
*/
Array<String> sectionNames() const { return _sections.keys(); }

/**
Returns all keys and values as a map with keys as "section/key"
Expand Down
11 changes: 6 additions & 5 deletions src/IniFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ IniFile::IniFile(const String& fname, bool shouldwrite)
if(!file) {
return;
}
_lines.reserve(16);
_ok = true;
Section* cursection = &_sections[NOSECTION];
while(!file.end())
{
String line=file.readLine();
Expand All @@ -37,9 +39,7 @@ IniFile::IniFile(const String& fname, bool shouldwrite)
if(end < 0)
continue;
String name = line.substring(1, end);
_currentTitle = name;
if (!_sections.has(_currentTitle))
_sections[_currentTitle] = Section();
cursection = &_sections[name];
}
else if(line[i0] != '#' && line[i0] > 47 && line[i0] != ';')
{
Expand All @@ -60,15 +60,16 @@ IniFile::IniFile(const String& fname, bool shouldwrite)
}
String key = line.substring(0,i);
String value = line.substring(i+1);
_sections[_currentTitle][key.trim().replaceme('/', '\\')] = value.trim();
(*cursection)[key.trim().replaceme('/', '\\')] = value.trim();
}
}
_currentTitle = NOSECTION;
for(int i=_lines.length()-1; i>0 && _lines[i][0] == '\0'; i--)
{
_lines.resize(_lines.length()-1);
}

if (_sections[NOSECTION].length() == 0)
_sections.remove(NOSECTION);
}

IniFile::Section& IniFile::section(const String& name)
Expand Down
7 changes: 6 additions & 1 deletion tests/unittests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@ ASL_TEST(File)
ASL_TEST(IniFile)
{
{
TextFile("config.ini").write(""); // clear before
IniFile file("config.ini");
file["global"] = "global value";
file["sec1/field1"] = "value1";
file.set("sec1/field2", "value2");
file["sec2/field"] = "value3";
file.set("sec2/field", "value3");
}
{
IniFile file("config.ini");
Expand All @@ -80,6 +81,8 @@ ASL_TEST(IniFile)
Dic<> all = file.values();
ASL_ASSERT(all["sec1/field1"] == "value1");
ASL_ASSERT(file.values("sec1")["field1"] == "value1");

ASL_ASSERT(file.sectionNames().length() == 3);
}
}

Expand Down Expand Up @@ -192,6 +195,8 @@ String join(const Array<String>& a)
ASL_TEST(Array)
{
Array<int> a;
a.reserve(4);
ASL_ASSERT(a.length() == 0);
a << 3 << -5 << 10 << 0;

ASL_ASSERT(a.length() == 4);
Expand Down

0 comments on commit 30d77b1

Please sign in to comment.