From 08f4df7b04dfae4ee105fc15cd250c227a135690 Mon Sep 17 00:00:00 2001 From: "Marian W." Date: Sun, 2 Feb 2025 22:53:00 +0100 Subject: [PATCH] Improve error handling --- minipp/minipp.hpp | 22 +++++++++++++++++----- minipp/test.mini | 3 ++- minipp/test_out.mini | 3 ++- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/minipp/minipp.hpp b/minipp/minipp.hpp index 02fa61a..0261622 100644 --- a/minipp/minipp.hpp +++ b/minipp/minipp.hpp @@ -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 sectionPath = Tools::SplitByDelimiter(sectionName, '.'); + std::vector 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; } diff --git a/minipp/test.mini b/minipp/test.mini index 94202f7..f92f6bc 100644 --- a/minipp/test.mini +++ b/minipp/test.mini @@ -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 diff --git a/minipp/test_out.mini b/minipp/test_out.mini index 6fc1222..fd0e33f 100644 --- a/minipp/test_out.mini +++ b/minipp/test_out.mini @@ -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