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."); PP_COUT_SYNTAX_ERROR(lineCounter, "Expected ']' at the end of the line.");
return EResult::FormatError; return EResult::FormatError;
} }
std::string sectionName = currentLine.substr(1, currentLine.size() - 2); std::string sectionPathStr = currentLine.substr(1, currentLine.size() - 2);
Tools::StringTrim(sectionName); Tools::StringTrim(sectionPathStr);
if (sectionName.empty()) if (sectionPathStr.empty())
{ {
PP_COUT_SYNTAX_ERROR(lineCounter, "Expected section path. Found empty section begin notation."); PP_COUT_SYNTAX_ERROR(lineCounter, "Expected section path. Found empty section begin notation.");
return EResult::FormatError; return EResult::FormatError;
@@ -861,7 +861,7 @@ minipp::EResult minipp::MiniPPFile::Parse(const std::string& path) noexcept
// Create section tree // Create section tree
Section* ubSection = &m_rootSection; Section* ubSection = &m_rootSection;
std::vector<std::string> sectionPath = Tools::SplitByDelimiter(sectionName, '.'); std::vector<std::string> sectionPath = Tools::SplitByDelimiter(sectionPathStr, '.');
EResult result; EResult result;
for (size_t i = 0; i < sectionPath.size(); ++i) 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."); PP_COUT_SYNTAX_ERROR(lineCounter, "Expected key in line.");
return EResult::FormatError; 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()) if (keyValuePair.second.empty())
{ {
PP_COUT_SYNTAX_ERROR(lineCounter, "Empty keys are not allowed"); 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; parsedValue->m_comments = commentBuffer;
commentBuffer.clear(); 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; return EResult::Success;
@@ -993,6 +1004,7 @@ bool minipp::MiniPPFile::Tools::IsNameValid(const std::string& name) noexcept
continue; continue;
return false; return false;
} }
return true; return true;
} }

View File

@@ -5,7 +5,8 @@ year = 2025
completionPercentage = 50.0f completionPercentage = 50.0f
# Should only be true if completionPercentage is 100 # Should only be true if completionPercentage is 100
is_completed = false 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 # This section is about
# the settings of a game window # the settings of a game window

View File

@@ -1,8 +1,9 @@
[game] [game]
name = "Test Game\nNext Line" name = "Test Game\nNext Line"
testTestArg = [["yeah", "new\nline"], ["hallo\ttest\n\\\\"]]
completionPercentage = 50.000000 completionPercentage = 50.000000
version = "1.0.0" 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 year = 2025
# Should only be true if completionPercentage is 100 # Should only be true if completionPercentage is 100
is_completed = false is_completed = false