From 908dee0db9288bb455e9f1b68dd81ab50897a677 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Mon, 2 Jun 2014 22:33:26 +0300 Subject: [PATCH] Implemented transcoders for some getters. --- stglibs/srvconf.lib/parsers/chg_user.cpp | 30 +++++++++++++-------- stglibs/srvconf.lib/parsers/chg_user.h | 2 +- stglibs/srvconf.lib/parsers/get_corp.cpp | 5 ++-- stglibs/srvconf.lib/parsers/get_corp.h | 3 ++- stglibs/srvconf.lib/parsers/get_service.cpp | 5 ++-- stglibs/srvconf.lib/parsers/get_service.h | 3 ++- stglibs/srvconf.lib/parsers/get_tariff.cpp | 7 ++--- stglibs/srvconf.lib/parsers/get_tariff.h | 3 ++- stglibs/srvconf.lib/parsers/get_user.cpp | 21 ++++++++------- stglibs/srvconf.lib/parsers/get_user.h | 3 ++- stglibs/srvconf.lib/parsers/server_info.cpp | 5 ++-- stglibs/srvconf.lib/parsers/server_info.h | 3 ++- 12 files changed, 54 insertions(+), 36 deletions(-) diff --git a/stglibs/srvconf.lib/parsers/chg_user.cpp b/stglibs/srvconf.lib/parsers/chg_user.cpp index 01361d08..3f7effd9 100644 --- a/stglibs/srvconf.lib/parsers/chg_user.cpp +++ b/stglibs/srvconf.lib/parsers/chg_user.cpp @@ -45,6 +45,14 @@ if (!value.empty()) 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) @@ -84,7 +92,7 @@ else callback(false, "Invalid response.", data); } -std::string CHG_USER::Serialize(const USER_CONF_RES & conf, const USER_STAT_RES & stat) +std::string CHG_USER::Serialize(const USER_CONF_RES & conf, const USER_STAT_RES & stat, const std::string & encoding) { std::ostringstream stream; @@ -104,16 +112,16 @@ if (!conf.nextTariff.empty()) else if (!conf.tariffName.empty()) stream << ""; -appendResetable(stream, "note", MaybeEncode(conf.note)); -appendResetable(stream, "name", MaybeEncode(conf.realName)); // TODO: name -> realName -appendResetable(stream, "address", MaybeEncode(conf.address)); -appendResetable(stream, "email", MaybeEncode(conf.email)); -appendResetable(stream, "phone", MaybeEncode(conf.phone)); -appendResetable(stream, "group", MaybeEncode(conf.group)); -appendResetable(stream, "corp", conf.group); +appendResetable(stream, "note", MaybeIconv(MaybeEncode(conf.note), "koi8-ru", encoding)); +appendResetable(stream, "name", MaybeIconv(MaybeEncode(conf.realName), "koi8-ru", encoding)); // TODO: name -> realName +appendResetable(stream, "address", MaybeIconv(MaybeEncode(conf.address), "koi8-ru", encoding)); +appendResetable(stream, "email", MaybeIconv(MaybeEncode(conf.email), "koi8-ru", encoding)); +appendResetable(stream, "phone", MaybeIconv(MaybeEncode(conf.phone), "koi8-ru", encoding)); +appendResetable(stream, "group", MaybeIconv(MaybeEncode(conf.group), "koi8-ru", encoding)); +appendResetable(stream, "corp", conf.corp); for (size_t i = 0; i < conf.userdata.size(); ++i) - appendResetable(stream, "userdata", i, MaybeEncode(conf.userdata[i])); + appendResetable(stream, "userdata", i, MaybeIconv(MaybeEncode(conf.userdata[i]), "koi8-ru", encoding)); if (!conf.services.empty()) { @@ -126,9 +134,9 @@ if (!conf.services.empty()) // Stat if (!stat.cashAdd.empty()) - stream << ""; + stream << ""; else if (!stat.cashSet.empty()) - stream << ""; + stream << ""; appendResetable(stream, "freeMb", stat.freeMb); diff --git a/stglibs/srvconf.lib/parsers/chg_user.h b/stglibs/srvconf.lib/parsers/chg_user.h index 075dcd28..b3785070 100644 --- a/stglibs/srvconf.lib/parsers/chg_user.h +++ b/stglibs/srvconf.lib/parsers/chg_user.h @@ -50,7 +50,7 @@ private: void ParseAnswer(const char * el, const char ** attr); }; -std::string Serialize(const USER_CONF_RES & conf, const USER_STAT_RES & stat); +std::string Serialize(const USER_CONF_RES & conf, const USER_STAT_RES & stat, const std::string & encoding); } // namespace CHG_USER } // namespace STG diff --git a/stglibs/srvconf.lib/parsers/get_corp.cpp b/stglibs/srvconf.lib/parsers/get_corp.cpp index 339d1eb2..732a4d69 100644 --- a/stglibs/srvconf.lib/parsers/get_corp.cpp +++ b/stglibs/srvconf.lib/parsers/get_corp.cpp @@ -28,9 +28,10 @@ using namespace STG; -GET_CORP::PARSER::PARSER(CALLBACK f, void * d) +GET_CORP::PARSER::PARSER(CALLBACK f, void * d, const std::string & e) : callback(f), data(d), + encoding(e), depth(0), parsingAnswer(false) { @@ -92,6 +93,6 @@ if (strcasecmp(el, "corp") == 0) //----------------------------------------------------------------------------- void GET_CORP::PARSER::ParseCorpParams(const char * el, const char ** attr) { -if (!TryParse(propertyParsers, ToLower(el), attr)) +if (!TryParse(propertyParsers, ToLower(el), attr, encoding)) error = "Invalid parameter."; } diff --git a/stglibs/srvconf.lib/parsers/get_corp.h b/stglibs/srvconf.lib/parsers/get_corp.h index 86576c96..b9d87983 100644 --- a/stglibs/srvconf.lib/parsers/get_corp.h +++ b/stglibs/srvconf.lib/parsers/get_corp.h @@ -39,7 +39,7 @@ class PARSER: public STG::PARSER public: typedef GET_CORP::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); @@ -50,6 +50,7 @@ private: CALLBACK callback; void * data; INFO info; + std::string encoding; int depth; bool parsingAnswer; std::string error; diff --git a/stglibs/srvconf.lib/parsers/get_service.cpp b/stglibs/srvconf.lib/parsers/get_service.cpp index 105b7395..b4977cda 100644 --- a/stglibs/srvconf.lib/parsers/get_service.cpp +++ b/stglibs/srvconf.lib/parsers/get_service.cpp @@ -28,9 +28,10 @@ using namespace STG; -GET_SERVICE::PARSER::PARSER(CALLBACK f, void * d) +GET_SERVICE::PARSER::PARSER(CALLBACK f, void * d, const std::string & e) : callback(f), data(d), + encoding(e), depth(0), parsingAnswer(false) { @@ -94,6 +95,6 @@ if (strcasecmp(el, "service") == 0) //----------------------------------------------------------------------------- void GET_SERVICE::PARSER::ParseServiceParams(const char * el, const char ** attr) { -if (!TryParse(propertyParsers, ToLower(el), attr)) +if (!TryParse(propertyParsers, ToLower(el), attr, encoding)) error = "Invalid parameter."; } diff --git a/stglibs/srvconf.lib/parsers/get_service.h b/stglibs/srvconf.lib/parsers/get_service.h index d752abf2..fe29ec09 100644 --- a/stglibs/srvconf.lib/parsers/get_service.h +++ b/stglibs/srvconf.lib/parsers/get_service.h @@ -38,7 +38,7 @@ class PARSER: public STG::PARSER { public: typedef GET_SERVICE::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: CALLBACK callback; void * data; INFO info; + std::string encoding; int depth; bool parsingAnswer; std::string error; diff --git a/stglibs/srvconf.lib/parsers/get_tariff.cpp b/stglibs/srvconf.lib/parsers/get_tariff.cpp index 1bb9d0db..0ffaabb6 100644 --- a/stglibs/srvconf.lib/parsers/get_tariff.cpp +++ b/stglibs/srvconf.lib/parsers/get_tariff.cpp @@ -39,7 +39,7 @@ class AOS_PARSER : public BASE_PROPERTY_PARSER public: typedef bool (* FUNC)(const char **, A &, T A::value_type:: *); AOS_PARSER(A & a, T A::value_type:: * fld, FUNC f) : array(a), field(fld), func(f) {} - virtual bool Parse(const char ** attr, const std::string & /*attrName*/) { return func(attr, array, field); } + virtual bool Parse(const char ** attr, const std::string & /*attrName*/, const std::string & /*fromEncoding*/) { return func(attr, array, field); } private: A & array; T A::value_type:: * field; @@ -127,9 +127,10 @@ return true; } // namespace anonymous -GET_TARIFF::PARSER::PARSER(CALLBACK f, void * d) +GET_TARIFF::PARSER::PARSER(CALLBACK f, void * d, const std::string & e) : callback(f), data(d), + encoding(e), depth(0), parsingAnswer(false) { @@ -207,6 +208,6 @@ if (strcasecmp(el, "tariff") == 0) //----------------------------------------------------------------------------- void GET_TARIFF::PARSER::ParseTariffParams(const char * el, const char ** attr) { -if (!TryParse(propertyParsers, ToLower(el), attr)) +if (!TryParse(propertyParsers, ToLower(el), attr, encoding)) error = std::string("Invalid parameter '") + el + "'."; } diff --git a/stglibs/srvconf.lib/parsers/get_tariff.h b/stglibs/srvconf.lib/parsers/get_tariff.h index 79300149..be78fdbd 100644 --- a/stglibs/srvconf.lib/parsers/get_tariff.h +++ b/stglibs/srvconf.lib/parsers/get_tariff.h @@ -39,7 +39,7 @@ class PARSER: public STG::PARSER public: typedef GET_TARIFF::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_user.cpp b/stglibs/srvconf.lib/parsers/get_user.cpp index 7168c348..8379f657 100644 --- a/stglibs/srvconf.lib/parsers/get_user.cpp +++ b/stglibs/srvconf.lib/parsers/get_user.cpp @@ -60,9 +60,10 @@ return true; } -GET_USER::PARSER::PARSER(CALLBACK f, void * d) +GET_USER::PARSER::PARSER(CALLBACK f, void * d, const std::string & e) : callback(f), data(d), + encoding(e), depth(0), parsingAnswer(false) { @@ -82,19 +83,19 @@ GET_USER::PARSER::PARSER(CALLBACK f, void * d) AddParser(propertyParsers, "currIP", info.ip, GetIPValue); AddParser(propertyParsers, "ip", info.ips); AddParser(propertyParsers, "tariff", info.tariff); - AddParser(propertyParsers, "group", info.group, GetEncodedValue); - AddParser(propertyParsers, "note", info.note, GetEncodedValue); - AddParser(propertyParsers, "email", info.email, GetEncodedValue); - AddParser(propertyParsers, "name", info.name, GetEncodedValue); - AddParser(propertyParsers, "address", info.address, GetEncodedValue); - AddParser(propertyParsers, "phone", info.phone, GetEncodedValue); + AddParser(propertyParsers, "group", info.group, "koi8-ru", GetEncodedValue); + AddParser(propertyParsers, "note", info.note, "koi8-ru", GetEncodedValue); + AddParser(propertyParsers, "email", info.email, "koi8-ru", GetEncodedValue); + AddParser(propertyParsers, "name", info.name, "koi8-ru", GetEncodedValue); + AddParser(propertyParsers, "address", info.address, "koi8-ru", GetEncodedValue); + AddParser(propertyParsers, "phone", info.phone, "koi8-ru", GetEncodedValue); AddParser(propertyParsers, "corp", info.corp); AddParser(propertyParsers, "traff", info.stat); AddParser(propertyParsers, "pingTime", info.pingTime); AddParser(propertyParsers, "lastActivityTime", info.lastActivityTime); for (size_t i = 0; i < USERDATA_NUM; ++i) - AddParser(propertyParsers, "userData" + unsigned2str(i), info.userData[i], GetEncodedValue); + AddParser(propertyParsers, "userData" + unsigned2str(i), info.userData[i], "koi8-ru", GetEncodedValue); } //----------------------------------------------------------------------------- GET_USER::PARSER::~PARSER() @@ -157,10 +158,10 @@ if (strcasecmp(el, "user") == 0) void GET_USER::PARSER::ParseUserParams(const char * el, const char ** attr) { if (strcasecmp(el, "AuthorizedBy") != 0 && - !TryParse(propertyParsers, ToLower(el), attr)) + !TryParse(propertyParsers, ToLower(el), attr, encoding)) error = "Invalid parameter."; else if (strcasecmp(el, "Services") != 0 && - !TryParse(propertyParsers, ToLower(el), attr)) + !TryParse(propertyParsers, ToLower(el), attr, encoding)) error = "Invalid parameter."; } //----------------------------------------------------------------------------- diff --git a/stglibs/srvconf.lib/parsers/get_user.h b/stglibs/srvconf.lib/parsers/get_user.h index 99163c37..a0e3e3a4 100644 --- a/stglibs/srvconf.lib/parsers/get_user.h +++ b/stglibs/srvconf.lib/parsers/get_user.h @@ -39,7 +39,7 @@ class PARSER: public STG::PARSER public: typedef GET_USER::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/server_info.cpp b/stglibs/srvconf.lib/parsers/server_info.cpp index fbefb2d4..50dbb2cc 100644 --- a/stglibs/srvconf.lib/parsers/server_info.cpp +++ b/stglibs/srvconf.lib/parsers/server_info.cpp @@ -38,9 +38,10 @@ const size_t DIRNAME_LEN = 16; } -SERVER_INFO::PARSER::PARSER(CALLBACK f, void * d) +SERVER_INFO::PARSER::PARSER(CALLBACK f, void * d, const std::string & e) : callback(f), data(d), + encoding(e), depth(0), parsingAnswer(false) { @@ -66,7 +67,7 @@ if (depth == 1) else { if (depth == 2 && parsingAnswer) - if (!TryParse(propertyParsers, ToLower(el), attr)) + if (!TryParse(propertyParsers, ToLower(el), attr, encoding)) error = "Invalid parameter."; } return 0; diff --git a/stglibs/srvconf.lib/parsers/server_info.h b/stglibs/srvconf.lib/parsers/server_info.h index 1865db7f..03906389 100644 --- a/stglibs/srvconf.lib/parsers/server_info.h +++ b/stglibs/srvconf.lib/parsers/server_info.h @@ -37,7 +37,7 @@ namespace SERVER_INFO 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); } @@ -46,6 +46,7 @@ private: PROPERTY_PARSERS propertyParsers; CALLBACK callback; void * data; + std::string encoding; int depth; bool parsingAnswer; INFO info; -- 2.43.2