mirror of https://github.com/oxen-io/lokinet
add error messages to ini parser and catch base cases.
This commit is contained in:
parent
9bc7508b5b
commit
0cc8517bca
|
@ -2,6 +2,7 @@
|
|||
#include <fstream>
|
||||
#include <list>
|
||||
#include <iostream>
|
||||
#include <util/logger.hpp>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
|
@ -19,6 +20,7 @@ namespace llarp
|
|||
return false;
|
||||
f.read(m_Data.data(), m_Data.size());
|
||||
}
|
||||
m_FileName = fname;
|
||||
return Parse();
|
||||
}
|
||||
|
||||
|
@ -27,6 +29,7 @@ namespace llarp
|
|||
{
|
||||
m_Data.resize(str.size());
|
||||
std::copy(str.begin(), str.end(), m_Data.begin());
|
||||
m_FileName = "<anonymous string>";
|
||||
return Parse();
|
||||
}
|
||||
|
||||
|
@ -63,9 +66,10 @@ namespace llarp
|
|||
}
|
||||
|
||||
String_t sectName;
|
||||
|
||||
size_t lineno = 0;
|
||||
for(const auto& line : lines)
|
||||
{
|
||||
lineno ++;
|
||||
String_t realLine;
|
||||
auto comment = line.find_first_of(';');
|
||||
if(comment == String_t::npos)
|
||||
|
@ -116,11 +120,19 @@ namespace llarp
|
|||
// sect.k = v
|
||||
String_t k = realLine.substr(k_start, k_end - k_start);
|
||||
String_t v = realLine.substr(v_start, 1 + (v_end - v_start));
|
||||
if(k.size() == 0 || v.size() == 0)
|
||||
{
|
||||
LogError(m_FileName, " invalid line (", lineno,"): '", line, "'");
|
||||
return false;
|
||||
}
|
||||
Section_t& sect = m_Config[sectName];
|
||||
sect.emplace(k, v);
|
||||
}
|
||||
else // malformed?
|
||||
{
|
||||
LogError(m_FileName, " invalid line (", lineno,"): '", line, "'");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -47,6 +47,7 @@ namespace llarp
|
|||
|
||||
std::vector< char > m_Data;
|
||||
Config_impl_t m_Config;
|
||||
std::string m_FileName;
|
||||
};
|
||||
|
||||
} // namespace llarp
|
||||
|
|
|
@ -51,6 +51,11 @@ TEST_F(TestINIParser, TestParseSectionDuplicateKeys)
|
|||
ASSERT_EQ(num, size_t(2));
|
||||
}
|
||||
|
||||
TEST_F(TestINIParser, TestNoKey)
|
||||
{
|
||||
ASSERT_FALSE(parser.LoadString("[test]\n=1090\n"));
|
||||
}
|
||||
|
||||
TEST_F(TestINIParser, TestParseInvalid)
|
||||
{
|
||||
ASSERT_FALSE(
|
||||
|
|
Loading…
Reference in New Issue