From 2da962edada912d8081f7e15db0ac887b98b179e Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Tue, 8 Oct 2013 00:21:37 +0300 Subject: [PATCH] Simplified parser interfaces. --- include/stg/admin_conf.h | 24 +++++++++- stglibs/srvconf.lib/include/stg/servconf.h | 7 ++- .../srvconf.lib/include/stg/servconf_types.h | 47 +++++++++++++++++-- stglibs/srvconf.lib/parsers/auth_by.cpp | 14 ++---- stglibs/srvconf.lib/parsers/auth_by.h | 3 +- stglibs/srvconf.lib/parsers/check_user.cpp | 14 ++---- stglibs/srvconf.lib/parsers/check_user.h | 3 +- stglibs/srvconf.lib/parsers/chg_user.cpp | 14 ++---- stglibs/srvconf.lib/parsers/chg_user.h | 3 +- stglibs/srvconf.lib/parsers/get_user.cpp | 13 ++--- stglibs/srvconf.lib/parsers/get_user.h | 3 +- stglibs/srvconf.lib/parsers/get_users.cpp | 16 ++----- stglibs/srvconf.lib/parsers/get_users.h | 3 +- stglibs/srvconf.lib/parsers/send_message.cpp | 14 ++---- stglibs/srvconf.lib/parsers/send_message.h | 3 +- stglibs/srvconf.lib/parsers/server_info.cpp | 13 ++--- stglibs/srvconf.lib/parsers/server_info.h | 3 +- stglibs/srvconf.lib/servconf.cpp | 45 +++++++++--------- 18 files changed, 122 insertions(+), 120 deletions(-) diff --git a/include/stg/admin_conf.h b/include/stg/admin_conf.h index df6d9e22..d906a654 100644 --- a/include/stg/admin_conf.h +++ b/include/stg/admin_conf.h @@ -7,9 +7,10 @@ #ifndef ADMIN_CONF_H #define ADMIN_CONF_H -#include - #include "os_int.h" +#include "resetable.h" + +#include #define ADM_LOGIN_LEN (32) #define ADM_PASSWD_LEN (32) @@ -75,6 +76,25 @@ struct ADMIN_CONF std::string password; }; //----------------------------------------------------------------------------- +struct ADMIN_CONF_RES +{ + ADMIN_CONF_RES() + {} + ADMIN_CONF_RES(const ADMIN_CONF_RES & rhs) + : priv(rhs.priv), + login(rhs.login), + password(rhs.password) + {} + ADMIN_CONF_RES & operator=(const ADMIN_CONF_RES & rhs) + { + priv = rhs.priv; + login = rhs.login; + password = rhs.password; + } + RESETABLE priv; + RESETABLE login; + RESETABLE password; +}; #include "admin_conf.inc.h" diff --git a/stglibs/srvconf.lib/include/stg/servconf.h b/stglibs/srvconf.lib/include/stg/servconf.h index e1bb9fb1..0f5292d9 100644 --- a/stglibs/srvconf.lib/include/stg/servconf.h +++ b/stglibs/srvconf.lib/include/stg/servconf.h @@ -29,6 +29,7 @@ #include "stg/servconf_types.h" +#include "stg/admin_conf.h" #include "stg/os_int.h" #include @@ -45,9 +46,11 @@ public: int ServerInfo(SERVER_INFO::CALLBACK f, void * data); - /*int GetAdmins(GET_ADMINS::CALLBACK f, void * data); + int GetAdmins(GET_ADMINS::CALLBACK f, void * data); int GetAdmin(const std::string & login, GET_ADMIN::CALLBACK f, void * data); - int ChgAdmin(const std::string & request, CHG_ADMIN::CALLBACK f, void * data);*/ + int ChgAdmin(const std::string & login, const ADMIN_CONF_RES & conf, CHG_ADMIN::CALLBACK f, void * data); + int AddAdmin(const std::string & login, const ADMIN_CONF & conf, GET_ADMIN::CALLBACK f, void * data); + int DelAdmin(const std::string & login, DEL_ADMIN::CALLBACK f, void * data); int GetUsers(GET_USERS::CALLBACK f, void * data); int GetUser(const std::string & login, GET_USER::CALLBACK f, void * data); diff --git a/stglibs/srvconf.lib/include/stg/servconf_types.h b/stglibs/srvconf.lib/include/stg/servconf_types.h index 1af9d5ab..09f9b970 100644 --- a/stglibs/srvconf.lib/include/stg/servconf_types.h +++ b/stglibs/srvconf.lib/include/stg/servconf_types.h @@ -38,6 +38,8 @@ #define ENC_MSG_LEN (8) +struct ADMIN_CONF; + namespace STG { @@ -65,6 +67,8 @@ confLoginCipher, confData }; +typedef void (* SIMPLE_CALLBACK)(bool result, const std::string & reason, void * data); + namespace AUTH_BY { @@ -93,7 +97,7 @@ typedef void (* CALLBACK)(bool result, const std::string & reason, const INFO & namespace CHECK_USER { -typedef int (* CALLBACK)(bool result, const std::string & reason, void * data); +typedef SIMPLE_CALLBACK CALLBACK; } // namespace CHECK_USER @@ -151,14 +155,51 @@ typedef void (* CALLBACK)(bool result, const std::string & reason, const INFO & namespace CHG_USER { -typedef void (* CALLBACK)(bool result, const std::string & reason, void * data); +typedef SIMPLE_CALLBACK CALLBACK; } namespace SEND_MESSAGE { -typedef void (* CALLBACK)(bool result, const std::string & reason, void * data); +typedef SIMPLE_CALLBACK CALLBACK; + +} + +namespace GET_ADMIN +{ + +typedef ADMIN_CONF INFO; +typedef void (* CALLBACK)(bool result, const std::string & reason, const INFO & info, void * data); + +} + +namespace GET_ADMINS +{ + +typedef std::vector INFO; +typedef void (* CALLBACK)(bool result, const std::string & reason, const INFO & info, void * data); + +} + +namespace ADD_ADMIN +{ + +typedef SIMPLE_CALLBACK CALLBACK; + +} + +namespace DEL_ADMIN +{ + +typedef SIMPLE_CALLBACK CALLBACK; + +} + +namespace CHG_ADMIN +{ + +typedef SIMPLE_CALLBACK CALLBACK; } diff --git a/stglibs/srvconf.lib/parsers/auth_by.cpp b/stglibs/srvconf.lib/parsers/auth_by.cpp index 99e3c021..61f590bf 100644 --- a/stglibs/srvconf.lib/parsers/auth_by.cpp +++ b/stglibs/srvconf.lib/parsers/auth_by.cpp @@ -20,15 +20,13 @@ #include "auth_by.h" -#include - #include // strcasecmp using namespace STG; -AUTH_BY::PARSER::PARSER() - : callback(NULL), - data(NULL), +AUTH_BY::PARSER::PARSER(CALLBACK f, void * d) + : callback(f), + data(d), depth(0), parsingAnswer(false) { @@ -79,9 +77,3 @@ if (depth == 0) error.clear(); } } -//----------------------------------------------------------------------------- -void AUTH_BY::PARSER::SetCallback(CALLBACK f, void * d) -{ -callback = f; -data = d; -} diff --git a/stglibs/srvconf.lib/parsers/auth_by.h b/stglibs/srvconf.lib/parsers/auth_by.h index a4bab9dd..f285d2bf 100644 --- a/stglibs/srvconf.lib/parsers/auth_by.h +++ b/stglibs/srvconf.lib/parsers/auth_by.h @@ -35,10 +35,9 @@ namespace AUTH_BY class PARSER: public STG::PARSER { public: - PARSER(); + PARSER(CALLBACK f, void * data); int ParseStart(const char * el, const char ** attr); void ParseEnd(const char * el); - void SetCallback(CALLBACK f, void * data); private: CALLBACK callback; diff --git a/stglibs/srvconf.lib/parsers/check_user.cpp b/stglibs/srvconf.lib/parsers/check_user.cpp index 8bf81e24..5f131292 100644 --- a/stglibs/srvconf.lib/parsers/check_user.cpp +++ b/stglibs/srvconf.lib/parsers/check_user.cpp @@ -21,15 +21,13 @@ #include "check_user.h" -#include - #include using namespace STG; -CHECK_USER::PARSER::PARSER() - : callback(NULL), - data(NULL), +CHECK_USER::PARSER::PARSER(CALLBACK f, void * d) + : callback(f), + data(d), depth(0) { } @@ -57,9 +55,3 @@ if (attr && attr[0] && attr[1] && strcasecmp(attr[0], "value") == 0) else callback(false, "Invalid response.", data); } -//----------------------------------------------------------------------------- -void CHECK_USER::PARSER::SetCallback(CALLBACK f, void * d) -{ -callback = f; -data = d; -} diff --git a/stglibs/srvconf.lib/parsers/check_user.h b/stglibs/srvconf.lib/parsers/check_user.h index 25740f02..71dfec13 100644 --- a/stglibs/srvconf.lib/parsers/check_user.h +++ b/stglibs/srvconf.lib/parsers/check_user.h @@ -34,10 +34,9 @@ namespace CHECK_USER class PARSER: public STG::PARSER { public: - PARSER(); + PARSER(CALLBACK f, void * data); int ParseStart(const char * el, const char ** attr); void ParseEnd(const char * el); - void SetCallback(CALLBACK f, void * data); private: CALLBACK callback; diff --git a/stglibs/srvconf.lib/parsers/chg_user.cpp b/stglibs/srvconf.lib/parsers/chg_user.cpp index a7d35925..f05c21fd 100644 --- a/stglibs/srvconf.lib/parsers/chg_user.cpp +++ b/stglibs/srvconf.lib/parsers/chg_user.cpp @@ -21,15 +21,13 @@ #include "chg_user.h" -#include - #include using namespace STG; -CHG_USER::PARSER::PARSER() - : callback(NULL), - data(NULL), +CHG_USER::PARSER::PARSER(CALLBACK f, void * d) + : callback(f), + data(d), depth(0) { } @@ -63,9 +61,3 @@ if (attr && attr[0] && attr[1]) else callback(false, "Invalid response.", data); } -//----------------------------------------------------------------------------- -void CHG_USER::PARSER::SetCallback(CALLBACK f, void * d) -{ -callback = f; -data = d; -} diff --git a/stglibs/srvconf.lib/parsers/chg_user.h b/stglibs/srvconf.lib/parsers/chg_user.h index 424866bc..c3ec1888 100644 --- a/stglibs/srvconf.lib/parsers/chg_user.h +++ b/stglibs/srvconf.lib/parsers/chg_user.h @@ -34,10 +34,9 @@ namespace CHG_USER class PARSER: public STG::PARSER { public: - PARSER(); + PARSER(CALLBACK f, void * data); int ParseStart(const char * el, const char ** attr); void ParseEnd(const char * el); - void SetCallback(CALLBACK f, void * data); private: CALLBACK callback; diff --git a/stglibs/srvconf.lib/parsers/get_user.cpp b/stglibs/srvconf.lib/parsers/get_user.cpp index 71e6cba6..e1f06e8b 100644 --- a/stglibs/srvconf.lib/parsers/get_user.cpp +++ b/stglibs/srvconf.lib/parsers/get_user.cpp @@ -25,7 +25,6 @@ #include #include -#include #include @@ -61,9 +60,9 @@ return true; } -GET_USER::PARSER::PARSER() - : callback(NULL), - data(NULL), +GET_USER::PARSER::PARSER(CALLBACK f, void * d) + : callback(f), + data(d), depth(0), parsingAnswer(false) { @@ -149,9 +148,3 @@ void GET_USER::PARSER::ParseUserParams(const char * el, const char ** attr) if (!TryParse(propertyParsers, ToLower(el), attr)) error = "Invalid parameter."; } -//----------------------------------------------------------------------------- -void GET_USER::PARSER::SetCallback(CALLBACK f, void * d) -{ -callback = f; -data = d; -} diff --git a/stglibs/srvconf.lib/parsers/get_user.h b/stglibs/srvconf.lib/parsers/get_user.h index 190b5784..12050752 100644 --- a/stglibs/srvconf.lib/parsers/get_user.h +++ b/stglibs/srvconf.lib/parsers/get_user.h @@ -37,11 +37,10 @@ namespace GET_USER class PARSER: public STG::PARSER { public: - PARSER(); + PARSER(CALLBACK f, void * data); virtual ~PARSER(); int ParseStart(const char * el, const char ** attr); void ParseEnd(const char * el); - void SetCallback(CALLBACK f, void * data); private: PROPERTY_PARSERS propertyParsers; diff --git a/stglibs/srvconf.lib/parsers/get_users.cpp b/stglibs/srvconf.lib/parsers/get_users.cpp index cf62f357..6c385b0c 100644 --- a/stglibs/srvconf.lib/parsers/get_users.cpp +++ b/stglibs/srvconf.lib/parsers/get_users.cpp @@ -21,19 +21,17 @@ #include "get_users.h" -#include - #include using namespace STG; -GET_USERS::PARSER::PARSER() - : callback(NULL), - data(NULL), +GET_USERS::PARSER::PARSER(CALLBACK f, void * d) + : callback(f), + data(d), + userParser(&GET_USERS::PARSER::UserCallback, this), depth(0), parsingAnswer(false) { - userParser.SetCallback(&GET_USERS::PARSER::UserCallback, this); } //----------------------------------------------------------------------------- int GET_USERS::PARSER::ParseStart(const char * el, const char ** attr) @@ -69,12 +67,6 @@ void GET_USERS::PARSER::AddUser(const GET_USER::INFO & userInfo) info.push_back(userInfo); } //----------------------------------------------------------------------------- -void GET_USERS::PARSER::SetCallback(CALLBACK f, void * d) -{ -callback = f; -data = d; -} -//----------------------------------------------------------------------------- void GET_USERS::PARSER::UserCallback(bool result, const std::string & error, const GET_USER::INFO & info, void * data) { GET_USERS::PARSER * parser = static_cast(data); diff --git a/stglibs/srvconf.lib/parsers/get_users.h b/stglibs/srvconf.lib/parsers/get_users.h index ac648a27..c2b86221 100644 --- a/stglibs/srvconf.lib/parsers/get_users.h +++ b/stglibs/srvconf.lib/parsers/get_users.h @@ -38,10 +38,9 @@ namespace GET_USERS class PARSER: public STG::PARSER { public: - PARSER(); + PARSER(CALLBACK f, void * data); int ParseStart(const char * el, const char ** attr); void ParseEnd(const char * el); - void SetCallback(CALLBACK f, void * data); private: CALLBACK callback; diff --git a/stglibs/srvconf.lib/parsers/send_message.cpp b/stglibs/srvconf.lib/parsers/send_message.cpp index c3a91789..053583f4 100644 --- a/stglibs/srvconf.lib/parsers/send_message.cpp +++ b/stglibs/srvconf.lib/parsers/send_message.cpp @@ -21,15 +21,13 @@ #include "send_message.h" -#include - #include using namespace STG; -SEND_MESSAGE::PARSER::PARSER() - : callback(NULL), - data(NULL), +SEND_MESSAGE::PARSER::PARSER(CALLBACK f, void * d) + : callback(f), + data(d), depth(0) { } @@ -57,9 +55,3 @@ if (attr && attr[0] && attr[1]) else callback(false, "Invalid response.", data); } -//----------------------------------------------------------------------------- -void SEND_MESSAGE::PARSER::SetCallback(CALLBACK f, void * d) -{ -callback = f; -data = d; -} diff --git a/stglibs/srvconf.lib/parsers/send_message.h b/stglibs/srvconf.lib/parsers/send_message.h index 7e4ded22..13132a0d 100644 --- a/stglibs/srvconf.lib/parsers/send_message.h +++ b/stglibs/srvconf.lib/parsers/send_message.h @@ -34,10 +34,9 @@ namespace SEND_MESSAGE class PARSER: public STG::PARSER { public: - PARSER(); + PARSER(CALLBACK f, void * data); int ParseStart(const char * el, const char ** attr); void ParseEnd(const char * el); - void SetCallback(CALLBACK f, void * data); private: CALLBACK callback; diff --git a/stglibs/srvconf.lib/parsers/server_info.cpp b/stglibs/srvconf.lib/parsers/server_info.cpp index 1b637858..da6b85e0 100644 --- a/stglibs/srvconf.lib/parsers/server_info.cpp +++ b/stglibs/srvconf.lib/parsers/server_info.cpp @@ -24,7 +24,6 @@ #include "stg/common.h" #include // sprintf -#include #include @@ -39,9 +38,9 @@ const size_t DIRNAME_LEN = 16; } -SERVER_INFO::PARSER::PARSER() - : callback(NULL), - data(NULL), +SERVER_INFO::PARSER::PARSER(CALLBACK f, void * d) + : callback(f), + data(d), depth(0), parsingAnswer(false) { @@ -80,9 +79,3 @@ if (depth == 0 && parsingAnswer) parsingAnswer = false; } } -//----------------------------------------------------------------------------- -void SERVER_INFO::PARSER::SetCallback(CALLBACK f, void * d) -{ -callback = f; -data = d; -} diff --git a/stglibs/srvconf.lib/parsers/server_info.h b/stglibs/srvconf.lib/parsers/server_info.h index 7bcbbba4..7f5b8e9b 100644 --- a/stglibs/srvconf.lib/parsers/server_info.h +++ b/stglibs/srvconf.lib/parsers/server_info.h @@ -37,10 +37,9 @@ namespace SERVER_INFO class PARSER: public STG::PARSER { public: - PARSER(); + PARSER(CALLBACK f, void * data); int ParseStart(const char * el, const char ** attr); void ParseEnd(const char * el); - void SetCallback(CALLBACK f, void * data); private: PROPERTY_PARSERS propertyParsers; diff --git a/stglibs/srvconf.lib/servconf.cpp b/stglibs/srvconf.lib/servconf.cpp index 325ee5f2..8b4e259b 100644 --- a/stglibs/srvconf.lib/servconf.cpp +++ b/stglibs/srvconf.lib/servconf.cpp @@ -45,12 +45,19 @@ public: IMPL(const std::string & server, uint16_t port, const std::string & login, const std::string & password); + int ServerInfo(SERVER_INFO::CALLBACK f, void * data); + + int GetAdmins(GET_ADMINS::CALLBACK f, void * data); + int GetAdmin(const std::string & login, GET_ADMIN::CALLBACK f, void * data); + int ChgAdmin(const std::string & login, const ADMIN_CONF_RES & conf, CHG_ADMIN::CALLBACK f, void * data); + int AddAdmin(const std::string & login, const ADMIN_CONF & conf, GET_ADMIN::CALLBACK f, void * data); + int DelAdmin(const std::string & login, DEL_ADMIN::CALLBACK f, void * data); + int GetUsers(GET_USERS::CALLBACK f, void * data); int GetUser(const std::string & login, GET_USER::CALLBACK f, void * data); int ChgUser(const std::string & request, CHG_USER::CALLBACK f, void * data); int AuthBy(const std::string & login, AUTH_BY::CALLBACK f, void * data); int SendMessage(const std::string & request, SEND_MESSAGE::CALLBACK f, void * data); - int ServerInfo(SERVER_INFO::CALLBACK f, void * data); int CheckUser(const std::string & login, const std::string & password, CHECK_USER::CALLBACK f, void * data); const std::string & GetStrError() const; @@ -58,14 +65,6 @@ public: static void End(void * data, const char * el); private: - GET_USERS::PARSER parserGetUsers; - GET_USER::PARSER parserGetUser; - AUTH_BY::PARSER parserAuthBy; - SERVER_INFO::PARSER parserServerInfo; - CHG_USER::PARSER parserChgUser; - CHECK_USER::PARSER parserCheckUser; - SEND_MESSAGE::PARSER parserSendMessage; - NETTRANSACT nt; std::string errorMsg; @@ -154,44 +153,44 @@ nt.SetRxCallback(this, AnsRecv); //----------------------------------------------------------------------------- int SERVCONF::IMPL::GetUser(const std::string & login, GET_USER::CALLBACK f, void * data) { -parserGetUser.SetCallback(f, data); -return Exec("", parserGetUser); +GET_USER::PARSER parser(f, data); +return Exec("", parser); } //----------------------------------------------------------------------------- int SERVCONF::IMPL::AuthBy(const std::string & login, AUTH_BY::CALLBACK f, void * data) { -parserAuthBy.SetCallback(f, data); -return Exec("", parserAuthBy); +AUTH_BY::PARSER parser(f, data); +return Exec("", parser); } //----------------------------------------------------------------------------- int SERVCONF::IMPL::GetUsers(GET_USERS::CALLBACK f, void * data) { -parserGetUsers.SetCallback(f, data); -return Exec("", parserGetUsers); +GET_USERS::PARSER parser(f, data); +return Exec("", parser); } //----------------------------------------------------------------------------- int SERVCONF::IMPL::ServerInfo(SERVER_INFO::CALLBACK f, void * data) { -parserServerInfo.SetCallback(f, data); -return Exec("", parserServerInfo); +SERVER_INFO::PARSER parser(f, data); +return Exec("", parser); } //----------------------------------------------------------------------------- int SERVCONF::IMPL::ChgUser(const std::string & request, CHG_USER::CALLBACK f, void * data) { -parserChgUser.SetCallback(f, data); -return Exec(request, parserChgUser); +CHG_USER::PARSER parser(f, data); +return Exec(request, parser); } //----------------------------------------------------------------------------- int SERVCONF::IMPL::SendMessage(const std::string & request, SEND_MESSAGE::CALLBACK f, void * data) { -parserSendMessage.SetCallback(f, data); -return Exec(request, parserSendMessage); +SEND_MESSAGE::PARSER parser(f, data); +return Exec(request, parser); } //----------------------------------------------------------------------------- int SERVCONF::IMPL::CheckUser(const std::string & login, const std::string & password, CHECK_USER::CALLBACK f, void * data) { -parserCheckUser.SetCallback(f, data); -return Exec("", parserCheckUser); +CHECK_USER::PARSER parser(f, data); +return Exec("", parser); } //----------------------------------------------------------------------------- void SERVCONF::IMPL::Start(void * data, const char * el, const char ** attr) -- 2.43.2