From 89e0b786d01246d1e8e57cc5da855372e27e7b63 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Sun, 18 Jan 2015 13:28:36 +0100 Subject: Added function for parsing complete strings (as needed in the xml parser) --- src/core/common/VariantReader.cpp | 16 +++++++++++++++- src/core/common/VariantReader.hpp | 20 ++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) (limited to 'src/core/common') diff --git a/src/core/common/VariantReader.cpp b/src/core/common/VariantReader.cpp index faad40c..bded618 100644 --- a/src/core/common/VariantReader.cpp +++ b/src/core/common/VariantReader.cpp @@ -618,8 +618,22 @@ std::pair VariantReader::parseGenericToken( v.setMagic(res.second.c_str()); return std::make_pair(res.first, v); } else { - return std::make_pair(res.first, Variant{res.second.c_str()}); + return std::make_pair(res.first, Variant::fromString(res.second)); } } + +std::pair VariantReader::parseGenericString( + const std::string &str, Logger &logger) +{ + CharReader reader{str}; + LoggerFork loggerFork = logger.fork(); + std::pair res = + parseGenericToken(reader, loggerFork, std::unordered_set{}, true); + if (reader.atEnd()) { + loggerFork.commit(); + return res; + } + return std::make_pair(true, Variant::fromString(str)); +} } diff --git a/src/core/common/VariantReader.hpp b/src/core/common/VariantReader.hpp index abf529c..8aaffd8 100644 --- a/src/core/common/VariantReader.hpp +++ b/src/core/common/VariantReader.hpp @@ -229,6 +229,26 @@ public: CharReader &reader, Logger &logger, const std::unordered_set &delims, bool extractUnescapedStrings = false); + + /** + * Tries to parse the most specific item from the given string. The + * resulting variant represents the value that has been read. If the end of + * the string was not reached while parsing an element, the result is + * returned as string. + * + * @param str is the string from which the value should be read. + * @param logger is the logger instance to which errors or warnings will be + * written. + * @return a pair indicating whether the operation was successful and the + * extracted variant value. Note that the variant value most times contains + * some meaningful data that can be worked with even if the operation was + * not successful (e.g. if a syntax error is encountered while reading an + * array, the successfully read elements will still be in the returned + * variant.) Information on why the operation has failed is passed to the + * logger. + */ + static std::pair parseGenericString( + const std::string &str, Logger &logger); }; } -- cgit v1.2.3