From de7760b87ad8ca38954140a738d66a133f7021f0 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Tue, 3 Jun 2014 08:20:47 +0300 Subject: [PATCH 1/1] Implemented encoding for all parsers. --- stglibs/srvconf.lib/parsers/auth_by.cpp | 3 ++- stglibs/srvconf.lib/parsers/auth_by.h | 3 ++- stglibs/srvconf.lib/parsers/chg_admin.cpp | 2 +- stglibs/srvconf.lib/parsers/chg_admin.h | 2 +- stglibs/srvconf.lib/parsers/chg_corp.cpp | 2 +- stglibs/srvconf.lib/parsers/chg_corp.h | 2 +- stglibs/srvconf.lib/parsers/chg_service.cpp | 4 ++-- stglibs/srvconf.lib/parsers/chg_service.h | 2 +- stglibs/srvconf.lib/parsers/chg_tariff.cpp | 2 +- stglibs/srvconf.lib/parsers/chg_tariff.h | 2 +- stglibs/srvconf.lib/parsers/chg_user.cpp | 24 ++----------------- stglibs/srvconf.lib/parsers/chg_user.h | 3 ++- stglibs/srvconf.lib/parsers/get_admin.cpp | 3 ++- stglibs/srvconf.lib/parsers/get_admin.h | 3 ++- stglibs/srvconf.lib/parsers/get_container.h | 7 +++--- stglibs/srvconf.lib/parsers/get_service.cpp | 2 +- stglibs/srvconf.lib/parsers/resetable_utils.h | 21 ++++++++++++++++ stglibs/srvconf.lib/parsers/simple.cpp | 3 ++- stglibs/srvconf.lib/parsers/simple.h | 3 ++- stglibs/srvconf.lib/servconf.cpp | 24 +++++++++++-------- 20 files changed, 65 insertions(+), 52 deletions(-) diff --git a/stglibs/srvconf.lib/parsers/auth_by.cpp b/stglibs/srvconf.lib/parsers/auth_by.cpp index 61f590bf..143eee26 100644 --- a/stglibs/srvconf.lib/parsers/auth_by.cpp +++ b/stglibs/srvconf.lib/parsers/auth_by.cpp @@ -24,9 +24,10 @@ using namespace STG; -AUTH_BY::PARSER::PARSER(CALLBACK f, void * d) +AUTH_BY::PARSER::PARSER(CALLBACK f, void * d, const std::string & e) : callback(f), data(d), + encoding(e), depth(0), parsingAnswer(false) { diff --git a/stglibs/srvconf.lib/parsers/auth_by.h b/stglibs/srvconf.lib/parsers/auth_by.h index 31f85769..5b2dfe52 100644 --- a/stglibs/srvconf.lib/parsers/auth_by.h +++ b/stglibs/srvconf.lib/parsers/auth_by.h @@ -35,7 +35,7 @@ namespace AUTH_BY class PARSER: public STG::PARSER { public: - PARSER(CALLBACK f, void * data); + PARSER(CALLBACK f, void * data, const std::string & encoding); int ParseStart(const char * el, const char ** attr); void ParseEnd(const char * el); void Failure(const std::string & reason) { callback(false, reason, info, data); } @@ -43,6 +43,7 @@ public: private: CALLBACK callback; void * data; + std::string encoding; int depth; bool parsingAnswer; INFO info; diff --git a/stglibs/srvconf.lib/parsers/chg_admin.cpp b/stglibs/srvconf.lib/parsers/chg_admin.cpp index a4265efa..14679dff 100644 --- a/stglibs/srvconf.lib/parsers/chg_admin.cpp +++ b/stglibs/srvconf.lib/parsers/chg_admin.cpp @@ -27,7 +27,7 @@ using namespace STG; -std::string CHG_ADMIN::Serialize(const ADMIN_CONF_RES & conf) +std::string CHG_ADMIN::Serialize(const ADMIN_CONF_RES & conf, const std::string & /*encoding*/) { std::string params; if (!conf.login.empty()) diff --git a/stglibs/srvconf.lib/parsers/chg_admin.h b/stglibs/srvconf.lib/parsers/chg_admin.h index c10332cd..7041aa8e 100644 --- a/stglibs/srvconf.lib/parsers/chg_admin.h +++ b/stglibs/srvconf.lib/parsers/chg_admin.h @@ -34,7 +34,7 @@ namespace STG namespace CHG_ADMIN { -std::string Serialize(const ADMIN_CONF_RES & conf); +std::string Serialize(const ADMIN_CONF_RES & conf, const std::string & encoding); } // namespace CHG_ADMIN } // namespace STG diff --git a/stglibs/srvconf.lib/parsers/chg_corp.cpp b/stglibs/srvconf.lib/parsers/chg_corp.cpp index dc884220..daa54644 100644 --- a/stglibs/srvconf.lib/parsers/chg_corp.cpp +++ b/stglibs/srvconf.lib/parsers/chg_corp.cpp @@ -29,7 +29,7 @@ using namespace STG; -std::string CHG_CORP::Serialize(const CORP_CONF_RES & conf) +std::string CHG_CORP::Serialize(const CORP_CONF_RES & conf, const std::string & /*encoding*/) { std::ostringstream stream; diff --git a/stglibs/srvconf.lib/parsers/chg_corp.h b/stglibs/srvconf.lib/parsers/chg_corp.h index 8b75fbd4..b30482f7 100644 --- a/stglibs/srvconf.lib/parsers/chg_corp.h +++ b/stglibs/srvconf.lib/parsers/chg_corp.h @@ -34,7 +34,7 @@ namespace STG namespace CHG_CORP { -std::string Serialize(const CORP_CONF_RES & conf); +std::string Serialize(const CORP_CONF_RES & conf, const std::string & encoding); } // namespace CHG_CORP } // namespace STG diff --git a/stglibs/srvconf.lib/parsers/chg_service.cpp b/stglibs/srvconf.lib/parsers/chg_service.cpp index f8627614..eabe11df 100644 --- a/stglibs/srvconf.lib/parsers/chg_service.cpp +++ b/stglibs/srvconf.lib/parsers/chg_service.cpp @@ -29,12 +29,12 @@ using namespace STG; -std::string CHG_SERVICE::Serialize(const SERVICE_CONF_RES & conf) +std::string CHG_SERVICE::Serialize(const SERVICE_CONF_RES & conf, const std::string & encoding) { std::ostringstream stream; appendResetable(stream, "name", conf.name); -appendResetable(stream, "comment", conf.comment); +appendResetable(stream, "comment", MaybeIconv(conf.comment, "koi8-ru", encoding)); appendResetable(stream, "cost", conf.cost); appendResetable(stream, "payDay", conf.payDay); diff --git a/stglibs/srvconf.lib/parsers/chg_service.h b/stglibs/srvconf.lib/parsers/chg_service.h index 10fbf6fb..09464a6e 100644 --- a/stglibs/srvconf.lib/parsers/chg_service.h +++ b/stglibs/srvconf.lib/parsers/chg_service.h @@ -34,7 +34,7 @@ namespace STG namespace CHG_SERVICE { -std::string Serialize(const SERVICE_CONF_RES & conf); +std::string Serialize(const SERVICE_CONF_RES & conf, const std::string & encoding); } // namespace CHG_SERVICE } // namespace STG diff --git a/stglibs/srvconf.lib/parsers/chg_tariff.cpp b/stglibs/srvconf.lib/parsers/chg_tariff.cpp index b2bc52d6..80bb4cb2 100644 --- a/stglibs/srvconf.lib/parsers/chg_tariff.cpp +++ b/stglibs/srvconf.lib/parsers/chg_tariff.cpp @@ -51,7 +51,7 @@ stream << "<" << name << " value=\"" << res << "\"/>"; } // namespace anonymous -std::string CHG_TARIFF::Serialize(const TARIFF_DATA_RES & data) +std::string CHG_TARIFF::Serialize(const TARIFF_DATA_RES & data, const std::string & /*encoding*/) { std::ostringstream stream; diff --git a/stglibs/srvconf.lib/parsers/chg_tariff.h b/stglibs/srvconf.lib/parsers/chg_tariff.h index c5cc0f0c..142b0155 100644 --- a/stglibs/srvconf.lib/parsers/chg_tariff.h +++ b/stglibs/srvconf.lib/parsers/chg_tariff.h @@ -34,7 +34,7 @@ namespace STG namespace CHG_TARIFF { -std::string Serialize(const TARIFF_DATA_RES & data); +std::string Serialize(const TARIFF_DATA_RES & data, const std::string & encoding); } // namespace CHG_TARIFF } // namespace STG diff --git a/stglibs/srvconf.lib/parsers/chg_user.cpp b/stglibs/srvconf.lib/parsers/chg_user.cpp index 3f7effd9..56b52d75 100644 --- a/stglibs/srvconf.lib/parsers/chg_user.cpp +++ b/stglibs/srvconf.lib/parsers/chg_user.cpp @@ -34,30 +34,10 @@ using namespace STG; -namespace -{ - -RESETABLE MaybeEncode(const RESETABLE & value) -{ -RESETABLE res; -if (!value.empty()) - res = Encode12str(value.data()); -return res; -} - -RESETABLE MaybeIconv(const RESETABLE & value, const std::string & fromEncoding, const std::string & toEncoding) -{ -RESETABLE res; -if (!value.empty()) - res = IconvString(value.data(), fromEncoding, toEncoding); -return res; -} - -} - -CHG_USER::PARSER::PARSER(SIMPLE::CALLBACK f, void * d) +CHG_USER::PARSER::PARSER(SIMPLE::CALLBACK f, void * d, const std::string & e) : callback(f), data(d), + encoding(e), depth(0) { } diff --git a/stglibs/srvconf.lib/parsers/chg_user.h b/stglibs/srvconf.lib/parsers/chg_user.h index b3785070..9ac559e0 100644 --- a/stglibs/srvconf.lib/parsers/chg_user.h +++ b/stglibs/srvconf.lib/parsers/chg_user.h @@ -37,7 +37,7 @@ namespace CHG_USER class PARSER: public STG::PARSER { public: - PARSER(SIMPLE::CALLBACK f, void * data); + PARSER(SIMPLE::CALLBACK f, void * data, const std::string & encoding); int ParseStart(const char * el, const char ** attr); void ParseEnd(const char * el); void Failure(const std::string & reason) { callback(false, reason, data); } @@ -45,6 +45,7 @@ public: private: SIMPLE::CALLBACK callback; void * data; + std::string encoding; int depth; void ParseAnswer(const char * el, const char ** attr); diff --git a/stglibs/srvconf.lib/parsers/get_admin.cpp b/stglibs/srvconf.lib/parsers/get_admin.cpp index 8f775d06..3d0e135e 100644 --- a/stglibs/srvconf.lib/parsers/get_admin.cpp +++ b/stglibs/srvconf.lib/parsers/get_admin.cpp @@ -45,9 +45,10 @@ return true; } // namespace STG -GET_ADMIN::PARSER::PARSER(CALLBACK f, void * d) +GET_ADMIN::PARSER::PARSER(CALLBACK f, void * d, const std::string & e) : callback(f), data(d), + encoding(e), depth(0), parsingAnswer(false) { diff --git a/stglibs/srvconf.lib/parsers/get_admin.h b/stglibs/srvconf.lib/parsers/get_admin.h index 06a451ae..f7cb3084 100644 --- a/stglibs/srvconf.lib/parsers/get_admin.h +++ b/stglibs/srvconf.lib/parsers/get_admin.h @@ -39,7 +39,7 @@ class PARSER: public STG::PARSER public: typedef GET_ADMIN::INFO INFO; - PARSER(CALLBACK f, void * data); + PARSER(CALLBACK f, void * data, const std::string & encoding); virtual ~PARSER(); int ParseStart(const char * el, const char ** attr); void ParseEnd(const char * el); @@ -49,6 +49,7 @@ private: PROPERTY_PARSERS propertyParsers; CALLBACK callback; void * data; + std::string encoding; INFO info; int depth; bool parsingAnswer; diff --git a/stglibs/srvconf.lib/parsers/get_container.h b/stglibs/srvconf.lib/parsers/get_container.h index 6cc9bc85..cbfd1ead 100644 --- a/stglibs/srvconf.lib/parsers/get_container.h +++ b/stglibs/srvconf.lib/parsers/get_container.h @@ -38,9 +38,9 @@ class PARSER: public STG::PARSER public: typedef std::vector INFO; typedef void (* CALLBACK)(bool result, const std::string & reason, const INFO & info, void * data); - PARSER(const std::string & t, CALLBACK f, void * d) - : tag(t), callback(f), data(d), - elementParser(&PARSER::ElementCallback, this), + PARSER(const std::string & t, CALLBACK f, void * d, const std::string & e) + : tag(t), callback(f), data(d), encoding(e), + elementParser(&PARSER::ElementCallback, this, e), depth(0), parsingAnswer(false) {} int ParseStart(const char * el, const char ** attr) @@ -75,6 +75,7 @@ private: std::string tag; CALLBACK callback; void * data; + std::string encoding; ELEMENT_PARSER elementParser; INFO info; int depth; diff --git a/stglibs/srvconf.lib/parsers/get_service.cpp b/stglibs/srvconf.lib/parsers/get_service.cpp index b4977cda..d6b16b9b 100644 --- a/stglibs/srvconf.lib/parsers/get_service.cpp +++ b/stglibs/srvconf.lib/parsers/get_service.cpp @@ -36,7 +36,7 @@ GET_SERVICE::PARSER::PARSER(CALLBACK f, void * d, const std::string & e) parsingAnswer(false) { AddParser(propertyParsers, "name", info.name); - AddParser(propertyParsers, "comment", info.comment); + AddParser(propertyParsers, "comment", info.comment, "koi8-ru", GetEncodedValue); AddParser(propertyParsers, "cost", info.cost); AddParser(propertyParsers, "payDay", info.payDay); } diff --git a/stglibs/srvconf.lib/parsers/resetable_utils.h b/stglibs/srvconf.lib/parsers/resetable_utils.h index ed1cd305..088cb327 100644 --- a/stglibs/srvconf.lib/parsers/resetable_utils.h +++ b/stglibs/srvconf.lib/parsers/resetable_utils.h @@ -22,6 +22,7 @@ #define __STG_STGLIBS_SRVCONF_RESETABLE_UTILS_H__ #include "stg/resetable.h" +#include "stg/common.h" #include #include @@ -30,6 +31,7 @@ namespace STG { template +inline void appendResetable(std::ostream & stream, const std::string & name, const T & value) { if (!value.empty()) @@ -37,12 +39,31 @@ if (!value.empty()) } template +inline void appendResetable(std::ostream & stream, const std::string & name, size_t suffix, const T & value) { if (!value.empty()) stream << "<" << name << suffix << " value=\"" << value.data() << "\"/>"; } +inline +RESETABLE MaybeEncode(const RESETABLE & value) +{ +RESETABLE res; +if (!value.empty()) + res = Encode12str(value.data()); +return res; +} + +inline +RESETABLE MaybeIconv(const RESETABLE & value, const std::string & fromEncoding, const std::string & toEncoding) +{ +RESETABLE res; +if (!value.empty()) + res = IconvString(value.data(), fromEncoding, toEncoding); +return res; +} + } // namespace STG #endif diff --git a/stglibs/srvconf.lib/parsers/simple.cpp b/stglibs/srvconf.lib/parsers/simple.cpp index 89537f2c..4749c70f 100644 --- a/stglibs/srvconf.lib/parsers/simple.cpp +++ b/stglibs/srvconf.lib/parsers/simple.cpp @@ -24,10 +24,11 @@ using namespace STG; -SIMPLE::PARSER::PARSER(const std::string & t, CALLBACK f, void * d) +SIMPLE::PARSER::PARSER(const std::string & t, CALLBACK f, void * d, const std::string & e) : tag(t), callback(f), data(d), + encoding(e), depth(0) { } diff --git a/stglibs/srvconf.lib/parsers/simple.h b/stglibs/srvconf.lib/parsers/simple.h index 43dbcd56..244e8ef0 100644 --- a/stglibs/srvconf.lib/parsers/simple.h +++ b/stglibs/srvconf.lib/parsers/simple.h @@ -35,7 +35,7 @@ namespace SIMPLE class PARSER: public STG::PARSER { public: - PARSER(const std::string & tag, CALLBACK f, void * data); + PARSER(const std::string & tag, CALLBACK f, void * data, const std::string & encoding); int ParseStart(const char * el, const char ** attr); void ParseEnd(const char * el); void Failure(const std::string & reason) { callback(false, reason, data); } @@ -44,6 +44,7 @@ private: std::string tag; CALLBACK callback; void * data; + std::string encoding; int depth; void ParseAnswer(const char * el, const char ** attr); diff --git a/stglibs/srvconf.lib/servconf.cpp b/stglibs/srvconf.lib/servconf.cpp index e0c2e58b..4c69c813 100644 --- a/stglibs/srvconf.lib/servconf.cpp +++ b/stglibs/srvconf.lib/servconf.cpp @@ -50,8 +50,10 @@ #include #include +#include #include +#include using namespace STG; @@ -85,6 +87,8 @@ public: return ExecImpl(request, cp); } + const std::string & Encoding() const { return encoding; } + private: NETTRANSACT nt; @@ -161,7 +165,7 @@ return pImpl->Exec("", f, int SERVCONF::ChgAdmin(const ADMIN_CONF_RES & conf, SIMPLE::CALLBACK f, void * data) { -return pImpl->Exec("ChgAdmin", "", f, data); +return pImpl->Exec("ChgAdmin", "Encoding()) + "/>", f, data); } int SERVCONF::AddAdmin(const std::string & login, @@ -171,7 +175,7 @@ int SERVCONF::AddAdmin(const std::string & login, int res = pImpl->Exec("AddAdmin", "", f, data); if (res != st_ok) return res; -return pImpl->Exec("ChgAdmin", "", f, data); +return pImpl->Exec("ChgAdmin", "Encoding()) + "/>", f, data); } int SERVCONF::DelAdmin(const std::string & login, SIMPLE::CALLBACK f, void * data) @@ -193,7 +197,7 @@ return pImpl->Exec("", f, int SERVCONF::ChgTariff(const TARIFF_DATA_RES & tariffData, SIMPLE::CALLBACK f, void * data) { -return pImpl->Exec("SetTariff", "" + CHG_TARIFF::Serialize(tariffData, encoding) + "", f, data); +return pImpl->Exec("SetTariff", "" + CHG_TARIFF::Serialize(tariffData, pImpl->Encoding()) + "", f, data); } int SERVCONF::AddTariff(const std::string & name, @@ -203,7 +207,7 @@ int SERVCONF::AddTariff(const std::string & name, int res = pImpl->Exec("AddTariff", "", f, data); if (res != st_ok) return res; -return pImpl->Exec("SetTariff", "" + CHG_TARIFF::Serialize(tariffData, encoding) + "", f, data); +return pImpl->Exec("SetTariff", "" + CHG_TARIFF::Serialize(tariffData, pImpl->Encoding()) + "", f, data); } int SERVCONF::DelTariff(const std::string & name, SIMPLE::CALLBACK f, void * data) @@ -228,7 +232,7 @@ int SERVCONF::ChgUser(const std::string & login, const USER_STAT_RES & stat, SIMPLE::CALLBACK f, void * data) { -return pImpl->Exec("" + CHG_USER::Serialize(conf, stat, encoding) + "", f, data); +return pImpl->Exec("" + CHG_USER::Serialize(conf, stat, pImpl->Encoding()) + "", f, data); } int SERVCONF::DelUser(const std::string & login, SIMPLE::CALLBACK f, void * data) @@ -244,7 +248,7 @@ int SERVCONF::AddUser(const std::string & login, int res = pImpl->Exec("AddUser", "", f, data); if (res != st_ok) return res; -return pImpl->Exec("" + CHG_USER::Serialize(conf, stat, encoding) + "", f, data); +return pImpl->Exec("" + CHG_USER::Serialize(conf, stat, pImpl->Encoding()) + "", f, data); } int SERVCONF::AuthBy(const std::string & login, AUTH_BY::CALLBACK f, void * data) @@ -276,7 +280,7 @@ return pImpl->Exec("", f int SERVCONF::ChgService(const SERVICE_CONF_RES & conf, SIMPLE::CALLBACK f, void * data) { -return pImpl->Exec("SetService", "" + CHG_SERVICE::Serialize(conf, encoding) + "", f, data); +return pImpl->Exec("SetService", "" + CHG_SERVICE::Serialize(conf, pImpl->Encoding()) + "", f, data); } int SERVCONF::AddService(const std::string & name, @@ -286,7 +290,7 @@ int SERVCONF::AddService(const std::string & name, int res = pImpl->Exec("AddService", "", f, data); if (res != st_ok) return res; -return pImpl->Exec("SetService", "" + CHG_SERVICE::Serialize(conf, encoding) + "", f, data); +return pImpl->Exec("SetService", "" + CHG_SERVICE::Serialize(conf, pImpl->Encoding()) + "", f, data); } int SERVCONF::DelService(const std::string & name, SIMPLE::CALLBACK f, void * data) @@ -308,7 +312,7 @@ return pImpl->Exec("", f, data int SERVCONF::ChgCorp(const CORP_CONF_RES & conf, SIMPLE::CALLBACK f, void * data) { -return pImpl->Exec("SetCorp", "" + CHG_CORP::Serialize(conf, encoding) + "", f, data); +return pImpl->Exec("SetCorp", "" + CHG_CORP::Serialize(conf, pImpl->Encoding()) + "", f, data); } int SERVCONF::AddCorp(const std::string & name, @@ -318,7 +322,7 @@ int SERVCONF::AddCorp(const std::string & name, int res = pImpl->Exec("AddCorp", "", f, data); if (res != st_ok) return res; -return pImpl->Exec("SetCorp", "" + CHG_CORP::Serialize(conf, encoding) + "", f, data); +return pImpl->Exec("SetCorp", "" + CHG_CORP::Serialize(conf, pImpl->Encoding()) + "", f, data); } int SERVCONF::DelCorp(const std::string & name, SIMPLE::CALLBACK f, void * data) -- 2.44.2