X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/46b0747592074017ff0ea4b33d4a7194235886e5..d3c78e84233473904620d9806d6558604f1b57f9:/libs/srvconf/parsers/get_container.h diff --git a/libs/srvconf/parsers/get_container.h b/libs/srvconf/parsers/get_container.h index cbfd1ead..919afe9d 100644 --- a/libs/srvconf/parsers/get_container.h +++ b/libs/srvconf/parsers/get_container.h @@ -18,8 +18,7 @@ * Author : Maxim Mamontov */ -#ifndef __STG_STGLIBS_SRVCONF_PARSER_GET_CONTAINER_H__ -#define __STG_STGLIBS_SRVCONF_PARSER_GET_CONTAINER_H__ +#pragma once #include "base.h" @@ -29,76 +28,76 @@ namespace STG { -namespace GET_CONTAINER +namespace GetContainer { -template -class PARSER: public STG::PARSER +template +class Parser: public STG::Parser { -public: - typedef std::vector INFO; - typedef void (* CALLBACK)(bool result, const std::string & reason, const INFO & info, void * data); - PARSER(const std::string & t, CALLBACK f, void * d, const std::string & e) - : tag(t), callback(f), data(d), encoding(e), - elementParser(&PARSER::ElementCallback, this, e), - depth(0), parsingAnswer(false) - {} - int ParseStart(const char * el, const char ** attr) - { - depth++; - if (depth == 1 && strcasecmp(el, tag.c_str()) == 0) - parsingAnswer = true; - - if (depth > 1 && parsingAnswer) - elementParser.ParseStart(el, attr); - - return 0; - } - void ParseEnd(const char * el) - { - depth--; - if (depth > 0 && parsingAnswer) - elementParser.ParseEnd(el); - - if (depth == 0 && parsingAnswer) + public: + using Info = std::vector; + using Callback = void (*)(bool result, const std::string& reason, const Info& info, void* data); + + Parser(const std::string& t, Callback f, void* d, const std::string& e) + : tag(t), callback(f), data(d), encoding(e), + elementParser(&Parser::ElementCallback, this, e), + depth(0), parsingAnswer(false) + {} + + int ParseStart(const char* el, const char** attr) override + { + depth++; + if (depth == 1 && strcasecmp(el, tag.c_str()) == 0) + parsingAnswer = true; + + if (depth > 1 && parsingAnswer) + elementParser.ParseStart(el, attr); + + return 0; + } + void ParseEnd(const char* el) override { - if (callback) - callback(error.empty(), error, info, data); - error.clear(); - info.clear(); - parsingAnswer = false; + depth--; + if (depth > 0 && parsingAnswer) + elementParser.ParseEnd(el); + + if (depth == 0 && parsingAnswer) + { + if (callback) + callback(error.empty(), error, info, data); + error.clear(); + info.clear(); + parsingAnswer = false; + } + } + void Failure(const std::string & reason) override { callback(false, reason, info, data); } + + private: + std::string tag; + Callback callback; + void* data; + std::string encoding; + ElementParser elementParser; + Info info; + int depth; + bool parsingAnswer; + std::string error; + + void AddElement(const typename ElementParser::Info& elementInfo) + { + info.push_back(elementInfo); + } + void SetError(const std::string& e) { error = e; } + + static void ElementCallback(bool result, const std::string& reason, const typename ElementParser::Info& info, void* data) + { + auto parser = static_cast*>(data); + if (!result) + parser->SetError(reason); + else + parser->AddElement(info); } - } - void Failure(const std::string & reason) { callback(false, reason, info, data); } - -private: - std::string tag; - CALLBACK callback; - void * data; - std::string encoding; - ELEMENT_PARSER elementParser; - INFO info; - int depth; - bool parsingAnswer; - std::string error; - - void AddElement(const typename ELEMENT_PARSER::INFO & elementInfo) - { - info.push_back(elementInfo); - } - void SetError(const std::string & e) { error = e; } - - static void ElementCallback(bool result, const std::string& reason, const typename ELEMENT_PARSER::INFO & info, void * data) - { - PARSER * parser = static_cast *>(data); - if (!result) - parser->SetError(reason); - else - parser->AddElement(info); - } }; } // namespace GET_CONTAINER } // namespace STG - -#endif