Skip to content

Commit

Permalink
Change exceptionWhenDuplicateField_ to overwriteDuplicateFields_
Browse files Browse the repository at this point in the history
  • Loading branch information
amelsmajic committed Jan 22, 2024
1 parent 3a783a7 commit 2d229f5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
16 changes: 9 additions & 7 deletions include/inicpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ namespace ini
char esc_ = '\\';
std::vector<std::string> commentPrefixes_ = { "#" , ";" };
bool multiLineValues_ = false;
bool exceptionWhenDuplicateField_ = false;
bool overwriteDuplicateFields_ = true;

void eraseComment(const std::string &commentPrefix,
std::string &str,
Expand Down Expand Up @@ -582,12 +582,14 @@ namespace ini
multiLineValues_ = enable;
}

/** Sets whether or not to throw an exception if duplicate fields are found in a section.
* Default is false.
* @param enable enable or disable? */
void setExceptionWhenDuplicateField(bool enable)
/** Sets whether or not overwriting duplicate fields is allowed.
* If overwriting duplicate fields is not allowed,
* an exception is thrown when a duplicate field is found inside a section.
* Default is true.
* @param allowed Is overwriting duplicate fields allowed or not? */
void allowOverwriteDuplicateFields(bool allowed)
{
exceptionWhenDuplicateField_ = enable;
overwriteDuplicateFields_ = allowed;
}

/** Tries to decode a ini file from the given input stream.
Expand Down Expand Up @@ -679,7 +681,7 @@ namespace ini
// retrieve field name and value
std::string name = line.substr(0, pos);
trim(name);
if (exceptionWhenDuplicateField_ && currentSection->count(name))
if (!overwriteDuplicateFields_ && currentSection->count(name) != 0)
{
std::stringstream ss;
ss << "l." << lineNo
Expand Down
8 changes: 4 additions & 4 deletions test/test_inifile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ TEST_CASE("parse section with duplicate field", "IniFile")
REQUIRE(inif["Foo"]["bar"].as<std::string>() == "world");
}

TEST_CASE("parse section with duplicate field and exceptionWhenDuplicateField_ set to disabled", "IniFile")
TEST_CASE("parse section with duplicate field and overwriteDuplicateFields_ set to true", "IniFile")
{
ini::IniFile inif;
inif.setExceptionWhenDuplicateField(false);
inif.allowOverwriteDuplicateFields(true);
inif.decode("[Foo]\nbar=hello\nbar=world");

REQUIRE(inif.size() == 1);
Expand Down Expand Up @@ -877,9 +877,9 @@ TEST_CASE("spaces are not taken into account in sections", "IniFile")
REQUIRE(inif.find("Foo") != inif.end());
}

TEST_CASE("parse section with duplicate field and exceptionWhenDuplicateField_ set to enabled", "IniFile")
TEST_CASE("parse section with duplicate field and overwriteDuplicateFields_ set to false", "IniFile")
{
ini::IniFile inif;
inif.setExceptionWhenDuplicateField(true);
inif.allowOverwriteDuplicateFields(false);
REQUIRE_THROWS(inif.decode("[Foo]\nbar=hello\nbar=world"));
}

0 comments on commit 2d229f5

Please sign in to comment.