Skip to content

Commit 963c924

Browse files
author
David Chu
committed
feat: throw error when encounters duplicate key
1 parent 8317ab6 commit 963c924

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

ini/INIReader.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,9 +408,11 @@ inline const bool INIReader::BoolConverter(std::string s) const {
408408
inline int INIReader::ValueHandler(void* user, const char* section,
409409
const char* name, const char* value) {
410410
INIReader* reader = (INIReader*)user;
411-
if (reader->_values[section][name].size() > 0)
412-
reader->_values[section][name] += "\n";
413-
reader->_values[section][name] += value;
411+
if (reader->_values[section][name].size() > 0) {
412+
throw std::runtime_error("duplicate key '" + std::string(name) +
413+
"' in section '" + section + "'.");
414+
}
415+
reader->_values[section][name] = value;
414416
return 1;
415417
}
416418
}

test/fixtures/duplicate_keys.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[bad_section]
2+
foo = hello
3+
bar = 42
4+
foo = world

test/tests.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,8 @@ TEST(INIReader, read_big_file) {
108108
EXPECT_EQ(v, i);
109109
}
110110
}
111+
112+
TEST(INIReader, dulicate_keys) {
113+
EXPECT_THROW(INIReader r{"./fixtures/duplicate_keys.ini"},
114+
std::runtime_error);
115+
}

0 commit comments

Comments
 (0)