From 186fb2b18b89a0280b8cc4b5ea8f7a59d3bd0853 Mon Sep 17 00:00:00 2001
From: Maxim Mamontov <faust.madf@gmail.com>
Date: Tue, 27 May 2014 20:07:32 +0300
Subject: [PATCH] Some refactoring.

---
 projects/sgconf/main.cpp | 69 +++-------------------------------------
 projects/sgconf/xml.cpp  | 25 +++++++++++++++
 projects/sgconf/xml.h    |  7 ++++
 3 files changed, 37 insertions(+), 64 deletions(-)

diff --git a/projects/sgconf/main.cpp b/projects/sgconf/main.cpp
index 625e8564..6a71f652 100644
--- a/projects/sgconf/main.cpp
+++ b/projects/sgconf/main.cpp
@@ -30,44 +30,16 @@
 #include "actions.h"
 #include "config.h"
 
-#include "stg/servconf.h"
-#include "stg/user_conf.h"
-#include "stg/user_stat.h"
-#include "stg/common.h"
-
-#include <cerrno>
-#include <clocale>
-#include <cstdio>
-#include <cstdlib>
-#include <cstring>
 #include <string>
-#include <sstream>
+#include <iostream>
 
-#include <unistd.h>
-#include <getopt.h>
-#include <iconv.h>
-#include <langinfo.h>
+#include <cstdlib> // getenv
+#include <cstring> // basename
 
-namespace
-{
-
-template <typename T>
-struct ARRAY_TYPE
-{
-typedef typename T::value_type type;
-};
+#include <unistd.h> // access
 
-template <typename T>
-struct ARRAY_TYPE<T[]>
-{
-typedef T type;
-};
-
-template <typename T, size_t N>
-struct ARRAY_TYPE<T[N]>
+namespace
 {
-typedef T type;
-};
 
 template <typename T>
 struct nullary_function
@@ -144,26 +116,6 @@ CONST_METHOD1_ADAPTER<C, A, R> Method1Adapt(R (C::* func)(A) const, C & obj)
 return CONST_METHOD1_ADAPTER<C, A, R>(func, obj);
 }
 
-template <typename T>
-bool SetArrayItem(T & array, const char * index, const typename ARRAY_TYPE<T>::type & value)
-{
-size_t pos = 0;
-if (str2x(index, pos))
-    return false;
-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;
-    }
-SGCONF::PrintXML(response);
-}
-
 void Version(const std::string & self)
 {
 std::cout << self << ", version: 2.0.0-alpha.\n";
@@ -396,17 +348,6 @@ ACTION * MakeAPIAction(COMMANDS & commands,
 return new API_ACTION(commands, "", false, funPtr);
 }
 
-bool 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;
-}
-
 } // namespace SGCONF
 
 time_t stgTime;
diff --git a/projects/sgconf/xml.cpp b/projects/sgconf/xml.cpp
index bf5589fe..ec8b9178 100644
--- a/projects/sgconf/xml.cpp
+++ b/projects/sgconf/xml.cpp
@@ -1,5 +1,9 @@
 #include "xml.h"
 
+#include "config.h"
+
+#include "stg/servconf.h"
+
 #include <iostream>
 
 #include <expat.h>
@@ -48,6 +52,16 @@ if (el != NULL)
     std::cout << Indent(state->level) << "</" << el << ">\n";
 }
 
+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);
+}
+
 }
 
 void SGCONF::PrintXML(const std::string& xml)
@@ -66,3 +80,14 @@ if (XML_Parse(parser, xml.c_str(), xml.length(), true) == XML_STATUS_ERROR)
 
 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;
+}
diff --git a/projects/sgconf/xml.h b/projects/sgconf/xml.h
index 25ee35c3..549a69b3 100644
--- a/projects/sgconf/xml.h
+++ b/projects/sgconf/xml.h
@@ -22,12 +22,19 @@
 #define __STG_SGCONF_XML_H__
 
 #include <string>
+#include <map>
 
 namespace SGCONF
 {
 
+struct CONFIG;
+
 void PrintXML(const std::string& xml);
 
+bool RawXMLFunction(const SGCONF::CONFIG & config,
+                    const std::string & arg,
+                    const std::map<std::string, std::string> & /*options*/);
+
 }
 
 #endif
-- 
2.44.2