3 #include "api_action.h"
8 #include "stg/servconf.h"
24 std::string Indent(size_t level)
26 return std::string(level * 4, ' ');
29 std::string PrintAttr(const char ** attr)
36 if (*(attr + 1) == NULL)
38 res += std::string(" ") + *attr + "=\"" + *(attr + 1) + "\"";
44 void Start(void * data, const char * el, const char ** attr)
46 ParserState * state = static_cast<ParserState *>(data);
48 std::cout << Indent(state->level) << "<" << el << PrintAttr(attr) << ">\n";
52 void End(void * data, const char * el)
54 ParserState * state = static_cast<ParserState *>(data);
57 std::cout << Indent(state->level) << "</" << el << ">\n";
60 void PrintXML(const std::string& xml)
62 ParserState state = { 0 };
64 XML_Parser parser = XML_ParserCreate(NULL);
65 XML_ParserReset(parser, NULL);
66 XML_SetElementHandler(parser, Start, End);
67 XML_SetUserData(parser, &state);
69 if (XML_Parse(parser, xml.c_str(), xml.length(), true) == XML_STATUS_ERROR)
70 std::cerr << "XML parse error at line " << XML_GetCurrentLineNumber(parser)
71 << ": '" << XML_ErrorString(XML_GetErrorCode(parser)) << "'"
74 XML_ParserFree(parser);
77 void RawXMLCallback(bool result, const std::string & reason, const std::string & response, void * /*data*/)
81 std::cerr << "Failed to get raw XML response. Reason: '" << reason << "'." << std::endl;
87 bool RawXMLFunction(const SGCONF::CONFIG & config,
88 const std::string & arg,
89 const std::map<std::string, std::string> & /*options*/)
91 return makeProto(config).RawXML(arg, RawXMLCallback, NULL) == STG::st_ok;
96 void SGCONF::AppendXMLOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
99 .Add("r", "raw", SGCONF::MakeAPIAction(commands, "<xml>", RawXMLFunction), "\tmake raw XML request");