X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/69aaa0bd928ece0837c8e88441c80f1f8cf1fe38..148f79dce19457e24754501db6844bc363c6f3fb:/projects/sgconf/xml.cpp?ds=sidebyside diff --git a/projects/sgconf/xml.cpp b/projects/sgconf/xml.cpp index 9e1d8f80..93a06e23 100644 --- a/projects/sgconf/xml.cpp +++ b/projects/sgconf/xml.cpp @@ -1,5 +1,16 @@ #include "xml.h" +#include "api_action.h" +#include "options.h" +#include "makeproto.h" +#include "config.h" + +#include "stg/servconf.h" + +#include +#include +#include + #include namespace @@ -10,7 +21,7 @@ struct ParserState size_t level; }; -std::string Indent(size_t size) +std::string Indent(size_t level) { return std::string(level * 4, ' '); } @@ -34,21 +45,19 @@ void Start(void * data, const char * el, const char ** attr) { ParserState * state = static_cast(data); if (el != NULL) - std::cout << Indent(state->level) << "<" << el << PrintAttrs(attr) << ">\n"; + std::cout << Indent(state->level) << "<" << el << PrintAttr(attr) << ">\n"; ++state->level; } void End(void * data, const char * el) { ParserState * state = static_cast(data); +--state->level; if (el != NULL) std::cout << Indent(state->level) << "\n"; ---state->level; } -} - -void SGCONF::PrintXML(const std::string& xml) +void PrintXML(const std::string& xml) { ParserState state = { 0 }; @@ -58,9 +67,34 @@ XML_SetElementHandler(parser, Start, End); XML_SetUserData(parser, &state); if (XML_Parse(parser, xml.c_str(), xml.length(), true) == XML_STATUS_ERROR) - std::cerr << "XML parse error at line " << XML_GetCurrentLineNumber(sc->parser) - << ": '" << XML_ErrorString(XML_GetErrorCode(sc->parser)) << "'" + std::cerr << "XML parse error at line " << XML_GetCurrentLineNumber(parser) + << ": '" << XML_ErrorString(XML_GetErrorCode(parser)) << "'" << std::endl; XML_ParserFree(parser); } + +void RawXMLCallback(bool result, const std::string & reason, const std::string & response, void * /*data*/) +{ +if (!result) + { + std::cerr << "Failed to get raw XML response. Reason: '" << reason << "'." << std::endl; + return; + } +PrintXML(response); +} + +bool RawXMLFunction(const SGCONF::CONFIG & config, + const std::string & arg, + const std::map & /*options*/) +{ +return makeProto(config).RawXML(arg, RawXMLCallback, NULL) == STG::st_ok; +} + +} + +void SGCONF::AppendXMLOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks) +{ +blocks.Add("Raw XML") + .Add("r", "raw", SGCONF::MakeAPIAction(commands, "", RawXMLFunction), "\tmake raw XML request"); +}