From: Maxim Mamontov Date: Thu, 6 Feb 2014 06:42:54 +0000 (+0200) Subject: Added XML pretty-printer. X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/7723eaf21d7d3d0d865545a890d74f3724bf23d2?ds=sidebyside Added XML pretty-printer. --- diff --git a/projects/sgconf/Makefile b/projects/sgconf/Makefile index 0098ff5e..e564b246 100644 --- a/projects/sgconf/Makefile +++ b/projects/sgconf/Makefile @@ -9,7 +9,8 @@ PROG = sgconf SRCS = ./main.cpp \ ./common_sg.cpp \ ./options.cpp \ - ./actions.cpp + ./actions.cpp \ + ./xml.cpp STGLIBS = conffiles \ srvconf \ diff --git a/projects/sgconf/main.cpp b/projects/sgconf/main.cpp index 3280e336..2d39cb2c 100644 --- a/projects/sgconf/main.cpp +++ b/projects/sgconf/main.cpp @@ -23,6 +23,7 @@ #include "common_sg.h" #include "sg_error_codes.h" +#include "xml.h" #include "options.h" #include "actions.h" #include "config.h" @@ -134,6 +135,16 @@ array[pos] = value; return true; } +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); +} + void Usage(); void UsageAll(); void UsageImpl(bool full); @@ -1240,7 +1251,7 @@ SGCONF::OPTION_BLOCK & block = blocks.Add("Connection options") .Add("w", "userpass", SGCONF::MakeParamAction(config.userPass, ""), "\tpassword for the administrative login") .Add("a", "address", SGCONF::MakeParamAction(config, ""), "connection params as a single string in format: :@:"); blocks.Add("Raw XML") - .Add("r", "raw", SGCONF::MakeConfAction(), "\t\tmake raw XML request") + .Add("r", "raw", SGCONF::MakeFunc1Action(), "\t\tmake raw XML request") /*blocks.Add("Admins management options") .Add("get-admins", SGCONF::MakeConfAction()) .Add("get-admin", SGCONF::MakeConfAction()) diff --git a/projects/sgconf/xml.cpp b/projects/sgconf/xml.cpp new file mode 100644 index 00000000..9e1d8f80 --- /dev/null +++ b/projects/sgconf/xml.cpp @@ -0,0 +1,66 @@ +#include "xml.h" + +#include + +namespace +{ + +struct ParserState +{ +size_t level; +}; + +std::string Indent(size_t size) +{ +return std::string(level * 4, ' '); +} + +std::string PrintAttr(const char ** attr) +{ +std::string res; +if (attr == NULL) + return res; +while (*attr) + { + if (*(attr + 1) == NULL) + return res; + res += std::string(" ") + *attr + "=\"" + *(attr + 1) + "\""; + ++attr; ++attr; + } +return res; +} + +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"; +++state->level; +} + +void End(void * data, const char * el) +{ +ParserState * state = static_cast(data); +if (el != NULL) + std::cout << Indent(state->level) << "\n"; +--state->level; +} + +} + +void SGCONF::PrintXML(const std::string& xml) +{ +ParserState state = { 0 }; + +XML_Parser parser = XML_ParserCreate(NULL); +XML_ParserReset(parser, NULL); +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::endl; + +XML_ParserFree(parser); +} diff --git a/projects/sgconf/xml.h b/projects/sgconf/xml.h new file mode 100644 index 00000000..25ee35c3 --- /dev/null +++ b/projects/sgconf/xml.h @@ -0,0 +1,33 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Maxim Mamontov + */ + +#ifndef __STG_SGCONF_XML_H__ +#define __STG_SGCONF_XML_H__ + +#include + +namespace SGCONF +{ + +void PrintXML(const std::string& xml); + +} + +#endif diff --git a/stglibs/srvconf.lib/servconf.cpp b/stglibs/srvconf.lib/servconf.cpp index 430de3c1..c7bf3d00 100644 --- a/stglibs/srvconf.lib/servconf.cpp +++ b/stglibs/srvconf.lib/servconf.cpp @@ -60,6 +60,7 @@ class SERVCONF::IMPL public: IMPL(const std::string & server, uint16_t port, const std::string & login, const std::string & password); + ~IMPL() { XML_ParserFree(parser); } const std::string & GetStrError() const; static void Start(void * data, const char * el, const char ** attr);