Skip to content

Commit 748165b

Browse files
authored
Merge pull request #16 from SSARCandy/feat/disallow-duplicate-keys
Throw error when encounters duplicate key
2 parents 8317ab6 + 3c39bbe commit 748165b

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

ini/INIReader.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -279,16 +279,13 @@ inline int INIReader::ParseError() const {
279279
break;
280280
case -1:
281281
throw std::runtime_error("ini file not found.");
282-
break;
283282
case -2:
284283
throw std::runtime_error("memory alloc error");
285-
break;
286284
default:
287285
throw std::runtime_error("parse error on line no: " +
288286
std::to_string(_error));
289-
break;
290287
}
291-
return _error;
288+
return 0;
292289
}
293290

294291
inline const std::set<std::string> INIReader::Sections() const {
@@ -408,9 +405,11 @@ inline const bool INIReader::BoolConverter(std::string s) const {
408405
inline int INIReader::ValueHandler(void* user, const char* section,
409406
const char* name, const char* value) {
410407
INIReader* reader = (INIReader*)user;
411-
if (reader->_values[section][name].size() > 0)
412-
reader->_values[section][name] += "\n";
413-
reader->_values[section][name] += value;
408+
if (reader->_values[section][name].size() > 0) {
409+
throw std::runtime_error("duplicate key '" + std::string(name) +
410+
"' in section '" + section + "'.");
411+
}
412+
reader->_values[section][name] = value;
414413
return 1;
415414
}
416415
}

test/fixtures/config.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[section1]
22

33
any=1
4-
any2=true
4+
any2:true
55
not_int = hello
66
not_int_arr = a b c d e
77

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{"./fixtures/duplicate_keys.ini"},
114+
std::runtime_error);
115+
}

0 commit comments

Comments
 (0)