diff --git a/minipp/minipp.hpp b/minipp/minipp.hpp index 2ad99d0..ed1b2aa 100644 --- a/minipp/minipp.hpp +++ b/minipp/minipp.hpp @@ -312,6 +312,62 @@ namespace minipp #define PP_COUT_SYNTAX_ERROR(line, msg) PP_COUT("Error in line " << line << ": " << msg) +std::unique_ptr minipp::MiniPPFile::Value::ParseValue(std::string value) +{ + char valueFirstChar = value.front(); + char valueLastChar = value.back(); + if (valueFirstChar == '"') + { + if (valueLastChar != '"') + return nullptr; + + // is string + value = value.substr(1, value.size() - 2); + auto strValue = std::make_unique(); + auto parseResult = strValue->Parse(value); + if (!IsResultOk(parseResult)) + return nullptr; + + return strValue; + } + else if (valueLastChar == 'e') + { + auto boolValue = std::make_unique(); + auto parseResult = boolValue->Parse(value); + if (!IsResultOk(parseResult)) + return nullptr; + + return boolValue; + } + else if (valueLastChar == 'f') + { + auto floatValue = std::make_unique(); + auto parseResult = floatValue->Parse(value); + if (!IsResultOk(parseResult)) + return nullptr; + + return floatValue; + } + else if (valueLastChar == ']') + { + auto arrayValue = std::make_unique(); + auto parseResult = arrayValue->Parse(value); + if (!IsResultOk(parseResult)) + return nullptr; + + return arrayValue; + } + else + { + auto intValue = std::make_unique(); + auto parseResult = intValue->Parse(value); + if (!IsResultOk(parseResult)) + return nullptr; + + return intValue; + } +} + #pragma region Value Types minipp::EResult minipp::MiniPPFile::Values::StringValue::Parse(const std::string& str) noexcept @@ -524,18 +580,12 @@ minipp::MiniPPFile::Values::ArrayValue::~ArrayValue() minipp::EResult minipp::MiniPPFile::Values::ArrayValue::Parse(const std::string& str) noexcept { - std::string nValue = str; - if (str.front() != '[' || str.back() != ']') { PP_COUT("Array value must be enclosed in [] brackets."); return EResult::FormatError; } - nValue = str.substr(1, str.size() - 2); - if (nValue.empty()) - return EResult::Success; - int64_t bracketCounter = 0; bool isInString = false; // we may encounter array value separators "," inside strings; we need to ignore those @@ -714,62 +764,6 @@ minipp::EResult minipp::MiniPPFile::Section::SetSubSection(const std::string& na return EResult::Success; } -std::unique_ptr minipp::MiniPPFile::Value::ParseValue(std::string value) -{ - char valueFirstChar = value.front(); - char valueLastChar = value.back(); - if (valueFirstChar == '"') - { - if (valueLastChar != '"') - return nullptr; - - // is string - value = value.substr(1, value.size() - 2); - auto strValue = std::make_unique(); - auto parseResult = strValue->Parse(value); - if (!IsResultOk(parseResult)) - return nullptr; - - return strValue; - } - else if (valueLastChar == 'e') - { - auto boolValue = std::make_unique(); - auto parseResult = boolValue->Parse(value); - if (!IsResultOk(parseResult)) - return nullptr; - - return boolValue; - } - else if (valueLastChar == 'f') - { - auto floatValue = std::make_unique(); - auto parseResult = floatValue->Parse(value); - if (!IsResultOk(parseResult)) - return nullptr; - - return floatValue; - } - else if (valueLastChar == ']') - { - auto arrayValue = std::make_unique(); - auto parseResult = arrayValue->Parse(value); - if (!IsResultOk(parseResult)) - return nullptr; - - return arrayValue; - } - else - { - auto intValue = std::make_unique(); - auto parseResult = intValue->Parse(value); - if (!IsResultOk(parseResult)) - return nullptr; - - return intValue; - } -} - minipp::EResult minipp::MiniPPFile::WriteSection(const Section* section, std::ofstream& ofs, std::string partTreeName) noexcept { if (section->m_values.size() > 0)