Add error messages

This commit is contained in:
2025-02-01 09:16:16 +01:00
parent d1ee7b31ee
commit a51974d8b0

View File

@@ -6,9 +6,10 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
// enabled helpful debug messages via std::cout (for parsing and writing)
#define MINIPP_ENABLE_DEBUG_OUTPUT true #define MINIPP_ENABLE_DEBUG_OUTPUT true
// define MINIPP_IMPLEMENTATION in one single translation unit before including this header! // define MINIPP_IMPLEMENTATION in a single translation unit before including this header!
namespace minipp namespace minipp
{ {
@@ -22,7 +23,7 @@ namespace minipp
FileIOError = -5, FileIOError = -5,
InvalidDataType = -6, InvalidDataType = -6,
FormatError = -7, FormatError = -7,
ArrayDataTypeInconsitency = -8, ArrayDataTypeInconsistency = -8,
/* OK Codes */ /* OK Codes */
Success = +1, Success = +1,
@@ -259,7 +260,10 @@ minipp::EResult minipp::MiniPPFile::Values::StringValue::Parse(const std::string
if (str[i] == '\\') if (str[i] == '\\')
{ {
if (i + 1 >= str.size()) if (i + 1 >= str.size())
{
COUT("Syntax error: '\\' at end of string");
return EResult::FormatError; return EResult::FormatError;
}
switch (str[i + 1]) switch (str[i + 1])
{ {
@@ -279,6 +283,7 @@ minipp::EResult minipp::MiniPPFile::Values::StringValue::Parse(const std::string
m_value.push_back('\\'); m_value.push_back('\\');
break; break;
default: default:
COUT("Syntax error: Unknown escape sequence '\\" << str[i + 1] << "'");
return EResult::FormatError; return EResult::FormatError;
} }
++i; ++i;
@@ -320,6 +325,7 @@ minipp::EResult minipp::MiniPPFile::Values::StringValue::ToString(std::string& d
break; break;
} }
} }
destination = "\"" + sanitizedValue + "\""; destination = "\"" + sanitizedValue + "\"";
return EResult::Success; return EResult::Success;
} }
@@ -336,7 +342,10 @@ minipp::EResult minipp::MiniPPFile::Values::IntValue::Parse(const std::string& s
std::string sanitizedValue = str; std::string sanitizedValue = str;
Tools::RemoveAll(sanitizedValue, '_'); Tools::RemoveAll(sanitizedValue, '_');
if (sanitizedValue.empty()) if (sanitizedValue.empty())
{
COUT("Empty string for integer value.");
return EResult::FormatError; return EResult::FormatError;
}
char lastCharacter = str.back(); char lastCharacter = str.back();
auto rest = str.substr(0, str.size() - 1); auto rest = str.substr(0, str.size() - 1);
@@ -353,7 +362,10 @@ minipp::EResult minipp::MiniPPFile::Values::IntValue::Parse(const std::string& s
else else
{ {
if (!Tools::IsIntegerDecimal(sanitizedValue)) if (!Tools::IsIntegerDecimal(sanitizedValue))
{
COUT("Invalid integer value: " << sanitizedValue);
return EResult::FormatError; return EResult::FormatError;
}
m_value = std::stoll(sanitizedValue); m_value = std::stoll(sanitizedValue);
m_style = EIntStyle::Decimal; m_style = EIntStyle::Decimal;
} }
@@ -392,6 +404,7 @@ minipp::EResult minipp::MiniPPFile::Values::IntValue::ToString(std::string& dest
break; break;
} }
default: default:
COUT("Invalid integer style.");
return EResult::FormatError; return EResult::FormatError;
} }
@@ -412,7 +425,10 @@ minipp::EResult minipp::MiniPPFile::Values::BooleanValue::Parse(const std::strin
else if (str == "false") else if (str == "false")
m_value = false; m_value = false;
else else
{
COUT("Invalid boolean value: " << str << " (may only contain lowercase true and false)");
return EResult::FormatError; return EResult::FormatError;
}
return EResult::Success; return EResult::Success;
} }
@@ -450,7 +466,10 @@ minipp::EResult minipp::MiniPPFile::Values::ArrayValue::Parse(const std::string&
{ {
std::string nValue = str; std::string nValue = str;
if (str.front() != '[' || str.back() != ']') if (str.front() != '[' || str.back() != ']')
{
COUT("Array value must be enclosed in [] brackets.");
return EResult::FormatError; return EResult::FormatError;
}
nValue = str.substr(1, str.size() - 2); nValue = str.substr(1, str.size() - 2);
if (nValue.empty()) if (nValue.empty())
return EResult::Success; return EResult::Success;
@@ -495,7 +514,10 @@ minipp::EResult minipp::MiniPPFile::Values::ArrayValue::Parse(const std::string&
{ {
--bracketCounter; --bracketCounter;
if (bracketCounter < 0) if (bracketCounter < 0)
{
COUT("Array brackets are not balanced. (One ] too much or encountered too early)");
return EResult::FormatError; return EResult::FormatError;
}
else if (bracketCounter >= 1) else if (bracketCounter >= 1)
currentElement += c; currentElement += c;
} }
@@ -510,7 +532,10 @@ minipp::EResult minipp::MiniPPFile::Values::ArrayValue::Parse(const std::string&
} }
if (bracketCounter != 0) if (bracketCounter != 0)
{
COUT("Array brackets are not balanced. (Missing " << bracketCounter << " closing brackets)");
return EResult::FormatError; return EResult::FormatError;
}
if (!currentElement.empty()) if (!currentElement.empty())
elements.push_back(currentElement); elements.push_back(currentElement);
@@ -529,7 +554,7 @@ minipp::EResult minipp::MiniPPFile::Values::ArrayValue::Parse(const std::string&
hasTypeHash = true; hasTypeHash = true;
} }
else if (typeid(*parsed).hash_code() != lastTypeIdHash) else if (typeid(*parsed).hash_code() != lastTypeIdHash)
return EResult::FormatError; return EResult::ArrayDataTypeInconsistency;
m_values.push_back(std::move(parsed)); m_values.push_back(std::move(parsed));
} }
@@ -555,7 +580,7 @@ minipp::EResult minipp::MiniPPFile::Values::ArrayValue::ToString(std::string& de
hasTypeHash = true; hasTypeHash = true;
} }
else if (typeid(*val).hash_code() != lastTypeIdHash) else if (typeid(*val).hash_code() != lastTypeIdHash)
return EResult::ArrayDataTypeInconsitency; return EResult::ArrayDataTypeInconsistency;
ss << buf << ", "; ss << buf << ", ";
} }