Improve error handling

This commit is contained in:
2025-02-02 22:53:00 +01:00
parent 4e54e828eb
commit 08f4df7b04
3 changed files with 21 additions and 7 deletions

View File

@@ -851,9 +851,9 @@ minipp::EResult minipp::MiniPPFile::Parse(const std::string& path) noexcept
PP_COUT_SYNTAX_ERROR(lineCounter, "Expected ']' at the end of the line.");
return EResult::FormatError;
}
std::string sectionName = currentLine.substr(1, currentLine.size() - 2);
Tools::StringTrim(sectionName);
if (sectionName.empty())
std::string sectionPathStr = currentLine.substr(1, currentLine.size() - 2);
Tools::StringTrim(sectionPathStr);
if (sectionPathStr.empty())
{
PP_COUT_SYNTAX_ERROR(lineCounter, "Expected section path. Found empty section begin notation.");
return EResult::FormatError;
@@ -861,7 +861,7 @@ minipp::EResult minipp::MiniPPFile::Parse(const std::string& path) noexcept
// Create section tree
Section* ubSection = &m_rootSection;
std::vector<std::string> sectionPath = Tools::SplitByDelimiter(sectionName, '.');
std::vector<std::string> sectionPath = Tools::SplitByDelimiter(sectionPathStr, '.');
EResult result;
for (size_t i = 0; i < sectionPath.size(); ++i)
{
@@ -907,6 +907,12 @@ minipp::EResult minipp::MiniPPFile::Parse(const std::string& path) noexcept
PP_COUT_SYNTAX_ERROR(lineCounter, "Expected key in line.");
return EResult::FormatError;
}
if (!Tools::IsNameValid(keyValuePair.first))
{
PP_COUT_SYNTAX_ERROR(lineCounter, "Invalid key name. (\"" << keyValuePair.first << "\") May only contain [a - z][A - Z][0 - 9] and _.");
return EResult::FormatError;
}
if (keyValuePair.second.empty())
{
PP_COUT_SYNTAX_ERROR(lineCounter, "Empty keys are not allowed");
@@ -921,7 +927,12 @@ minipp::EResult minipp::MiniPPFile::Parse(const std::string& path) noexcept
parsedValue->m_comments = commentBuffer;
commentBuffer.clear();
currentSection->SetValue(keyValuePair.first, std::move(parsedValue), false);
auto valueSetResult = currentSection->SetValue(keyValuePair.first, std::move(parsedValue), false);
if (valueSetResult != EResult::Success)
{
PP_COUT_SYNTAX_ERROR(lineCounter, "Key already present: " << keyValuePair.first);
return valueSetResult;
}
}
return EResult::Success;
@@ -993,6 +1004,7 @@ bool minipp::MiniPPFile::Tools::IsNameValid(const std::string& name) noexcept
continue;
return false;
}
return true;
}

View File

@@ -5,7 +5,8 @@ year = 2025
completionPercentage = 50.0f
# Should only be true if completionPercentage is 100
is_completed = false
testargs = ["this is a \"test\"", "this is\n the next line"]
testargs = ["this is a \\\"test\"", "this is\n the next line"]
testTestArg = [["yeah", "new\nline"], ["hallo\ttest\n\\\\"]]
# This section is about
# the settings of a game window

View File

@@ -1,8 +1,9 @@
[game]
name = "Test Game\nNext Line"
testTestArg = [["yeah", "new\nline"], ["hallo\ttest\n\\\\"]]
completionPercentage = 50.000000
version = "1.0.0"
testargs = ["this is a \"test\"", "this is\n the next line"]
testargs = ["this is a \\\"test\"", "this is\n the next line"]
year = 2025
# Should only be true if completionPercentage is 100
is_completed = false