From: Maxim Mamontov Date: Thu, 8 Aug 2013 08:28:49 +0000 (+0300) Subject: Factored out users' parser. X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/26d5d878270d01123669ba6a38c306c398ed7b8d Factored out users' parser. --- diff --git a/stglibs/srvconf.lib/include/stg/parser_get_users.h b/stglibs/srvconf.lib/include/stg/parser_get_users.h new file mode 100644 index 00000000..07d76cc5 --- /dev/null +++ b/stglibs/srvconf.lib/include/stg/parser_get_users.h @@ -0,0 +1,53 @@ + +/* + * 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 : Boris Mikhailenko + * Author : Maxim Mamontov + */ + +#ifndef __STG_STGLIBS_SRVCONF_PARSER_GET_USERS_H__ +#define __STG_STGLIBS_SRVCONF_PARSER_GET_USERS_H__ + +#include "stg/parser.h" + +#include + +class PARSER_GET_USERS: public PARSER +{ +public: + typedef std::vector INFO; + typedef void (* CALLBACK)(const INFO & info, void * data); + + PARSER_GET_USERS(); + int ParseStart(const char * el, const char ** attr); + void ParseEnd(const char * el); + void SetCallback(CALLBACK f, void * data); +private: + CALLBACK callback; + void * data; + PARSER_GET_USER userParser; + INFO info; + int depth; + + void AddUser(const PARSER_GET_USER::INFO & userInfo); + void ParseUsers(const char * el, const char ** attr); + + static void UserCallback(const PARSER_GET_USER::INFO & info, void * data); +}; + +#endif diff --git a/stglibs/srvconf.lib/include/stg/servconf.h b/stglibs/srvconf.lib/include/stg/servconf.h index 9248fb0f..e5d5dd64 100644 --- a/stglibs/srvconf.lib/include/stg/servconf.h +++ b/stglibs/srvconf.lib/include/stg/servconf.h @@ -71,25 +71,6 @@ private: bool error; }; //----------------------------------------------------------------------------- -class PARSER_GET_USERS: public PARSER -{ -public: - PARSER_GET_USERS(); - int ParseStart(const char *el, const char **attr); - void ParseEnd(const char *el); - void ParseUsers(const char *el, const char **attr); - void ParseUser(const char *el, const char **attr); - void ParseUserParams(const char *el, const char **attr); - void ParseUserLoadStat(const char * el, const char ** attr); - void SetUserDataRecvCb(RecvUserDataCb_t, void * data); -private: - RecvUserDataCb_t RecvUserDataCb; - void * userDataCb; - USERDATA user; - int depth; - bool error; -}; -//----------------------------------------------------------------------------- class PARSER_SEND_MESSAGE: public PARSER { public: @@ -115,7 +96,7 @@ public: void SetAdmLogin(const char * login); void SetAdmPassword(const char * password); - void SetUserDataRecvCb(RecvUserDataCb_t, void * data); + void SetGetUsers(PARSER_GET_USERS::CALLBACK f, void * data); void SetAuthByCallback(PARSER_AUTH_BY::CALLBACK f, void * data); void SetServerInfoCallback(PARSER_SERVER_INFO::CALLBACK f, void * data); void SetChgUserCb(RecvChgUserCb_t, void * data); @@ -156,7 +137,7 @@ private: int error; XML_Parser parser; - RecvUserDataCb_t RecvUserDataCb; + PARSER_GET_USERS::CALLBACK getUsersCallback; PARSER_GET_USER::CALLBACK getUserCallback; PARSER_AUTH_BY::CALLBACK authByCallback; PARSER_SERVER_INFO::CALLBACK serverInfoCallback; @@ -166,7 +147,7 @@ private: void * getUserData; void * authByData; - void * getUsersDataDataCb; + void * getUsersData; void * serverInfoData; void * chgUserDataCb; void * checkUserData; diff --git a/stglibs/srvconf.lib/parser.cpp b/stglibs/srvconf.lib/parser.cpp index f3f3278e..f259f581 100644 --- a/stglibs/srvconf.lib/parser.cpp +++ b/stglibs/srvconf.lib/parser.cpp @@ -39,348 +39,6 @@ using namespace std; -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -PARSER_GET_USERS::PARSER_GET_USERS() - : RecvUserDataCb(NULL), - userDataCb(NULL), - user(), - depth(0), - error(false) -{ -} -//----------------------------------------------------------------------------- -int PARSER_GET_USERS::ParseStart(const char *el, const char **attr) -{ -depth++; -if (depth == 1) - { - ParseUsers(el, attr); - } - -if (depth == 2) - { - ParseUser(el, attr); - } - -if (depth == 3) - { - ParseUserParams(el, attr); - } -return 0; -} -//----------------------------------------------------------------------------- -void PARSER_GET_USERS::ParseEnd(const char *) -{ -depth--; -if (depth == 1) - { - if (RecvUserDataCb) - { - RecvUserDataCb(&user, userDataCb); - } - } -} -//----------------------------------------------------------------------------- -void PARSER_GET_USERS::ParseUsers(const char * el, const char ** attr) -{ -if (strcasecmp(el, "users") == 0) - { - if (*attr != NULL) - return; - return; - } -} -//----------------------------------------------------------------------------- -void PARSER_GET_USERS::ParseUser(const char * el, const char ** attr) -{ -if (el && attr[0]) - { - if (strcasecmp(el, "user") != 0) - { - return; - } - - if (strcasecmp(attr[0], "login") != 0) - { - return; - } - user.login = attr[1]; - } -} -//----------------------------------------------------------------------------- -void PARSER_GET_USERS::ParseUserParams(const char * el, const char ** attr) -{ -if (strcasecmp(el, "cash") == 0) - { - if (strtodouble2(attr[1], user.cash) < 0) - { - return; - } - } - -/*if (strcasecmp(el, "LastCash") == 0) - { - if (strtodouble2(attr[1], user.lastCash) < 0) - { - MessageDlg("Error in answer", mtError, TMsgDlgButtons() << mbOK, 0); - return 0; - } - }*/ - -/*if (strcasecmp(el, "LastActivityTime") == 0) - { - if (strtol(attr[1], user.lastActivityTime) < 0) - { - MessageDlg("Error in answer", mtError, TMsgDlgButtons() << mbOK, 0); - return 0; - } - }*/ - - -/*if (strcasecmp(el, "LastTimeCash") == 0) - { - if (strtol(attr[1], user.lastTimeCash) < 0) - { - MessageDlg("Error in answer", mtError, TMsgDlgButtons() << mbOK, 0); - return 0; - } - }*/ - -/*if (strcasecmp(el, "CashExpire") == 0) - { - if (strtol(attr[1], user.cashExpire) < 0) - { - MessageDlg("Error in answer", mtError, TMsgDlgButtons() << mbOK, 0); - return 0; - } - }*/ - -if (strcasecmp(el, "credit") == 0) - { - if (strtodouble2(attr[1], user.credit) < 0) - { - return; - } - } - -if (strcasecmp(el, "creditExpire") == 0) - { - if (str2x(attr[1], user.creditExpire) < 0) - { - return; - } - } - -/*if (strcasecmp(el, "freemb") == 0) - { - if (strtodouble2(attr[1], user.freeMb) < 0) - { - MessageDlg("Error in answer", mtError, TMsgDlgButtons() << mbOK, 0); - return 0; - } - }*/ - -if (strcasecmp(el, "down") == 0) - { - if (str2x(attr[1], user.down) < 0) - { - return; - } - } - -if (strcasecmp(el, "passive") == 0) - { - if (str2x(attr[1], user.passive) < 0) - { - return; - } - } - -if (strcasecmp(el, "disableDetailStat") == 0) - { - if (str2x(attr[1], user.disableDetailStat) < 0) - { - return; - } - } - - -if (strcasecmp(el, "status") == 0) - { - if (str2x(attr[1], user.connected) < 0) - { - return; - } - } - -if (strcasecmp(el, "aonline") == 0) - { - if (str2x(attr[1], user.alwaysOnline) < 0) - { - return; - } - } - -if (strcasecmp(el, "currip") == 0) - { - user.ip = inet_addr(attr[1]); - } - -if (strcasecmp(el, "ip") == 0) - { - user.ips = attr[1]; - } - - -if (strcasecmp(el, "tariff") == 0) - { - //KOIToWin(user.tariff, *(attr+1), TARIFF_LEN); - user.tariff = attr[1]; - return; - } - -if (strcasecmp(el, "password") == 0) - { - user.password = *(attr+1); - return; - } - -if (strcasecmp(el, "iface") == 0) - { - user.iface = attr[1]; - return; - } - -/*if (strcasecmp(el, "name") == 0) - { - / *char nameEnc[REALNM_LEN * 2 + 1]; - char name[REALNM_LEN]; - strncpy(nameEnc, attr[1], REALNM_LEN * 2 + 1); - Decode21(name, nameEnc); - KOIToWin(user.realName, name, REALNM_LEN);* / - Decode21str(user.realName, attr[1]); - return; - }*/ - -if (strcasecmp(el, "address") == 0) - { - /*char addressEnc[ADDR_LEN * 2 + 1]; - char address[ADDR_LEN]; - strncpy(addressEnc, attr[1], ADDR_LEN * 2 + 1); - Decode21(address, addressEnc); - KOIToWin(user.address, address, ADDR_LEN);*/ - Decode21str(user.address, attr[1]); - return; - } - -if (strcasecmp(el, "phone") == 0) - { - /*char phoneEnc[PHONE_LEN * 2 + 1]; - char phone[PHONE_LEN]; - strncpy(phoneEnc, attr[1], PHONE_LEN * 2 + 1); - Decode21(phone, phoneEnc); - KOIToWin(user.phone, phone, PHONE_LEN);*/ - Decode21str(user.phone, attr[1]); - return; - } - -if (strcasecmp(el, "note") == 0) - { - /*char noteEnc[NOTE_LEN * 2 + 1]; - char note[NOTE_LEN]; - strncpy(noteEnc, attr[1], NOTE_LEN * 2 + 1);*/ - //KOIToWin(user.note, note, NOTE_LEN); - //user.note = note; - Decode21str(user.note, attr[1]); - return; - } - -if (strcasecmp(el, "email") == 0) - { - /*char emailEnc[EMAIL_LEN * 2 + 1]; - char email[EMAIL_LEN]; - strncpy(emailEnc, attr[1], EMAIL_LEN * 2 + 1); - Decode21(email, emailEnc); - //KOIToWin(user.email, email, EMAIL_LEN); - user.email = email;*/ - Decode21str(user.email, attr[1]); - return; - } - -if (strcasecmp(el, "group") == 0) - { - /*char groupEnc[GROUP_LEN * 2 + 1]; - char group[GROUP_LEN]; - strncpy(groupEnc, attr[1], GROUP_LEN * 2 + 1); - Decode21(group, groupEnc); - //KOIToWin(user.group, group, GROUP_LEN); - user.group = group;*/ - Decode21str(user.group, attr[1]); - return; - } - -if (strcasecmp(el, "traff") == 0) - { - ParseUserLoadStat(el, attr); - return; - } - -} -//----------------------------------------------------------------------------- -void PARSER_GET_USERS::ParseUserLoadStat(const char *, const char ** attr) -{ -int i = 0; -char dir[6]; -while (attr[i]) - { - for (int j = 0; j < DIR_NUM; j++) - { - sprintf(dir, "MU%d", j); - if (strcasecmp(dir, attr[i]) == 0) - { - str2x(attr[i+1], user.stat.mu[j]); - break; - } - } - for (int j = 0; j < DIR_NUM; j++) - { - sprintf(dir, "MD%d", j); - if (strcasecmp(dir, attr[i]) == 0) - { - str2x(attr[i+1], user.stat.md[j]); - break; - } - } - for (int j = 0; j < DIR_NUM; j++) - { - sprintf(dir, "SU%d", j); - if (strcasecmp(dir, attr[i]) == 0) - { - str2x(attr[i+1], user.stat.su[j]); - break; - } - } - for (int j = 0; j < DIR_NUM; j++) - { - sprintf(dir, "SD%d", j); - if (strcasecmp(dir, attr[i]) == 0) - { - str2x(attr[i+1], user.stat.sd[j]); - break; - } - } - i+=2; - } -return; -} -//----------------------------------------------------------------------------- -void PARSER_GET_USERS::SetUserDataRecvCb(RecvUserDataCb_t f, void * data) -{ -RecvUserDataCb = f; -userDataCb = data; -} //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- diff --git a/stglibs/srvconf.lib/parser_get_users.cpp b/stglibs/srvconf.lib/parser_get_users.cpp new file mode 100644 index 00000000..8f83211f --- /dev/null +++ b/stglibs/srvconf.lib/parser_get_users.cpp @@ -0,0 +1,76 @@ +/* + * 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 : Boris Mikhailenko + * Author : Maxim Mamontov + */ + +#include "stg/parser_get_users.h" + +PARSER_GET_USERS::PARSER_GET_USERS() + : callabck(NULL), + data(NULL), + depth(0) +{ + userParser.SetCallback(&PARSER_GET_USERS::UserCallback, this); +} +//----------------------------------------------------------------------------- +int PARSER_GET_USERS::ParseStart(const char * el, const char ** attr) +{ +depth++; +if (depth == 1) + ParseUsers(el, attr); + +if (depth > 1) + userParser.ParseStart(el, attr); + +return 0; +} +//----------------------------------------------------------------------------- +void PARSER_GET_USERS::ParseEnd(const char * el) +{ +depth--; +if (depth > 0) + userParser.ParseEnd(el); + +if (depth == 0) + if (callback) + callback(&info, data); +} +//----------------------------------------------------------------------------- +void PARSER_GET_USERS::ParseUsers(const char * el, const char ** /*attr*/) +{ +if (strcasecmp(el, "users") == 0) + info.clear(); +} +//----------------------------------------------------------------------------- +void PARSER_GET_USERS::AddUser(const PARSER_GET_USER::INFO & userInfo) +{ +info.push_back(userInfo); +} +//----------------------------------------------------------------------------- +void PARSER_GET_USERS::SetCallback(CALLBACK f, void * d) +{ +callback = f; +data = data; +} +//----------------------------------------------------------------------------- +void PARSER_GET_USERS::UserCallback(const PARSER_GET_USER::INFO & info, void * data) +{ + PARSER_GET_USERS * parser = static_cast(data); + parser->AddUser(info); +} diff --git a/stglibs/srvconf.lib/servconf.cpp b/stglibs/srvconf.lib/servconf.cpp index 83f087b3..7b2c85ec 100644 --- a/stglibs/srvconf.lib/servconf.cpp +++ b/stglibs/srvconf.lib/servconf.cpp @@ -139,7 +139,7 @@ snprintf(request, 255, "", l); int ret; currParser = &parserGetUser; -((PARSER_GET_USER*)currParser)->SetUserDataRecvCb(RecvGetUserDataCb, getUserDataDataCb); +((PARSER_GET_USER*)currParser)->SetCallback(getUserCallback, getUserData); nt.Reset(); nt.SetRxCallback(this, AnsRecv); @@ -200,7 +200,7 @@ char request[] = ""; int ret; currParser = &parserGetUsers; -((PARSER_GET_USERS*)currParser)->SetUserDataRecvCb(RecvUserDataCb, getUsersDataDataCb); +((PARSER_GET_USERS*)currParser)->SetCallback(getUsersCallback, getUsersData); nt.Reset(); nt.SetRxCallback(this, AnsRecv); @@ -389,10 +389,10 @@ void SERVCONF::End(const char *el) currParser->ParseEnd(el); } //----------------------------------------------------------------------------- -void SERVCONF::SetUserDataRecvCb(RecvUserDataCb_t f, void * data) +void SERVCONF::SetGetUsersCallback(PARSER_GET_USERS::CALLBACK f, void * data) { -RecvUserDataCb = f; -getUsersDataDataCb = data; +getUsersCallback = f; +getUsersData = data; } //----------------------------------------------------------------------------- void SERVCONF::SetGetUserCallback(PARSER_GET_USER::CALLBACK f, void * data)