]> git.stg.codes - stg.git/blobdiff - projects/sgconf/xml.cpp
Some refactoring.
[stg.git] / projects / sgconf / xml.cpp
index 9e1d8f80c39948fffd9a13626f2e303f16c64ba1..ec8b9178a06305891cdca5c3ad84fa9503500d32 100644 (file)
@@ -1,5 +1,11 @@
 #include "xml.h"
 
+#include "config.h"
+
+#include "stg/servconf.h"
+
+#include <iostream>
+
 #include <expat.h>
 
 namespace
@@ -10,7 +16,7 @@ struct ParserState
 size_t level;
 };
 
-std::string Indent(size_t size)
+std::string Indent(size_t level)
 {
 return std::string(level * 4, ' ');
 }
@@ -34,16 +40,26 @@ void Start(void * data, const char * el, const char ** attr)
 {
 ParserState * state = static_cast<ParserState *>(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<ParserState *>(data);
+--state->level;
 if (el != NULL)
     std::cout << Indent(state->level) << "</" << el << ">\n";
---state->level;
+}
+
+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;
+    }
+SGCONF::PrintXML(response);
 }
 
 }
@@ -58,9 +74,20 @@ 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);
 }
+
+bool SGCONF::RawXMLFunction(const SGCONF::CONFIG & config,
+                            const std::string & arg,
+                            const std::map<std::string, std::string> & /*options*/)
+{
+    STG::SERVCONF proto(config.server.data(),
+                        config.port.data(),
+                        config.userName.data(),
+                        config.userPass.data());
+    return proto.RawXML(arg, RawXMLCallback, NULL) == STG::st_ok;
+}