3 #include "api_action.h"
7 #include "stg/servconf.h"
23 std::string Indent(size_t level)
25 return std::string(level * 4, ' ');
28 std::string PrintAttr(const char ** attr)
35 if (*(attr + 1) == NULL)
37 res += std::string(" ") + *attr + "=\"" + *(attr + 1) + "\"";
43 void Start(void * data, const char * el, const char ** attr)
45 ParserState * state = static_cast<ParserState *>(data);
47 std::cout << Indent(state->level) << "<" << el << PrintAttr(attr) << ">\n";
51 void End(void * data, const char * el)
53 ParserState * state = static_cast<ParserState *>(data);
56 std::cout << Indent(state->level) << "</" << el << ">\n";
59 void PrintXML(const std::string& xml)
61 ParserState state = { 0 };
63 XML_Parser parser = XML_ParserCreate(NULL);
64 XML_ParserReset(parser, NULL);
65 XML_SetElementHandler(parser, Start, End);
66 XML_SetUserData(parser, &state);
68 if (XML_Parse(parser, xml.c_str(), xml.length(), true) == XML_STATUS_ERROR)
69 std::cerr << "XML parse error at line " << XML_GetCurrentLineNumber(parser)
70 << ": '" << XML_ErrorString(XML_GetErrorCode(parser)) << "'"
73 XML_ParserFree(parser);
76 void RawXMLCallback(bool result, const std::string & reason, const std::string & response, void * /*data*/)
80 std::cerr << "Failed to get raw XML response. Reason: '" << reason << "'." << std::endl;
86 bool RawXMLFunction(const SGCONF::CONFIG & config,
87 const std::string & arg,
88 const std::map<std::string, std::string> & /*options*/)
90 STG::SERVCONF proto(config.server.data(),
92 config.userName.data(),
93 config.userPass.data());
94 return proto.RawXML(arg, RawXMLCallback, NULL) == STG::st_ok;
99 void SGCONF::AppendXMLOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
101 blocks.Add("Raw XML")
102 .Add("r", "raw", SGCONF::MakeAPIAction(commands, "<xml>", RawXMLFunction), "\tmake raw XML request");