From: Maxim Mamontov Date: Fri, 20 Sep 2013 22:41:01 +0000 (+0300) Subject: Moved parsers into a separate directory. X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/76fb825a64343da7a8aa9b16c5dbcab709504832 Moved parsers into a separate directory. --- diff --git a/stglibs/srvconf.lib/parser.h b/stglibs/srvconf.lib/parser.h deleted file mode 100644 index 8d695a3a..00000000 --- a/stglibs/srvconf.lib/parser.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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_H__ -#define __STG_STGLIBS_SRVCONF_PARSER_H__ - -namespace STG -{ - -class PARSER -{ -public: - virtual ~PARSER() {} - virtual int ParseStart(const char *el, const char **attr) = 0; - virtual void ParseEnd(const char *el) = 0; -}; - -} // namespace STG - -#endif diff --git a/stglibs/srvconf.lib/parser_auth_by.cpp b/stglibs/srvconf.lib/parser_auth_by.cpp deleted file mode 100644 index 34e490ca..00000000 --- a/stglibs/srvconf.lib/parser_auth_by.cpp +++ /dev/null @@ -1,87 +0,0 @@ -/* - * 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 : Maxim Mamontov - */ - -#include "parser_auth_by.h" - -#include - -#include // strcasecmp - -using namespace STG; - -AUTH_BY::PARSER::PARSER() - : callback(NULL), - data(NULL), - depth(0), - parsingAnswer(false) -{ -} -//----------------------------------------------------------------------------- -int AUTH_BY::PARSER::ParseStart(const char *el, const char **attr) -{ -depth++; -if (depth == 1) - { - if (strcasecmp(el, "AuthorizedBy") == 0) - if (attr && attr[0] && attr[1]) - { - if (strcasecmp(attr[1], "error") == 0) - { - if (attr[2] && attr[3]) - error = attr[3]; - else - error = "User not found."; - } - else - parsingAnswer = true; - } - } -else - { - if (depth == 2) - { - if (parsingAnswer && strcasecmp(el, "Auth") == 0) - { - if (attr && attr[0] && attr[1] && strcasecmp(attr[0], "name") == 0) - info.push_back(attr[1]); - return 0; - } - } - } -return 0; -} -//----------------------------------------------------------------------------- -void AUTH_BY::PARSER::ParseEnd(const char * /*el*/) -{ -depth--; -if (depth == 0) - { - if (callback) - callback(error.empty(), error, info, data); - info.clear(); - error.clear(); - } -} -//----------------------------------------------------------------------------- -void AUTH_BY::PARSER::SetCallback(CALLBACK f, void * d) -{ -callback = f; -data = d; -} diff --git a/stglibs/srvconf.lib/parser_auth_by.h b/stglibs/srvconf.lib/parser_auth_by.h deleted file mode 100644 index c4757cf9..00000000 --- a/stglibs/srvconf.lib/parser_auth_by.h +++ /dev/null @@ -1,55 +0,0 @@ -/* - * 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 : Maxim Mamontov - */ - -#ifndef __STG_STGLIBS_SRVCONF_PARSER_AUTH_BY_H__ -#define __STG_STGLIBS_SRVCONF_PARSER_AUTH_BY_H__ - -#include "parser.h" - -#include "stg/servconf_types.h" - -#include - -namespace STG -{ -namespace AUTH_BY -{ - -class PARSER: public STG::PARSER -{ -public: - PARSER(); - int ParseStart(const char * el, const char ** attr); - void ParseEnd(const char * el); - void SetCallback(CALLBACK f, void * data); - -private: - CALLBACK callback; - void * data; - int depth; - bool parsingAnswer; - INFO info; - std::string error; -}; - -} // namespace AUTH_BY -} // namespace STG - -#endif diff --git a/stglibs/srvconf.lib/parser_check_user.cpp b/stglibs/srvconf.lib/parser_check_user.cpp deleted file mode 100644 index 0cb37522..00000000 --- a/stglibs/srvconf.lib/parser_check_user.cpp +++ /dev/null @@ -1,65 +0,0 @@ -/* - * 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 "parser_check_user.h" - -#include - -#include - -using namespace STG; - -CHECK_USER::PARSER::PARSER() - : callback(NULL), - data(NULL), - depth(0) -{ -} -//----------------------------------------------------------------------------- -int CHECK_USER::PARSER::ParseStart(const char * el, const char ** attr) -{ -depth++; -if (depth == 1) - if (strcasecmp(el, "CheckUser") == 0) - ParseAnswer(el, attr); -return 0; -} -//----------------------------------------------------------------------------- -void CHECK_USER::PARSER::ParseEnd(const char *) -{ -depth--; -} -//----------------------------------------------------------------------------- -void CHECK_USER::PARSER::ParseAnswer(const char *, const char **attr) -{ -if (!callback) - return; -if (attr && attr[0] && attr[1] && strcasecmp(attr[0], "value") == 0) - callback(strcasecmp(attr[1], "ok") == 0, attr[2] && attr[3] ? attr[3] : "", data); -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/parser_check_user.h b/stglibs/srvconf.lib/parser_check_user.h deleted file mode 100644 index 95658c38..00000000 --- a/stglibs/srvconf.lib/parser_check_user.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 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_CHECK_USER_H__ -#define __STG_STGLIBS_SRVCONF_PARSER_CHECK_USER_H__ - -#include "parser.h" - -#include "stg/servconf_types.h" - -namespace STG -{ -namespace CHECK_USER -{ - -class PARSER: public STG::PARSER -{ -public: - PARSER(); - int ParseStart(const char * el, const char ** attr); - void ParseEnd(const char * el); - void SetCallback(CALLBACK f, void * data); - -private: - CALLBACK callback; - void * data; - int depth; - - void ParseAnswer(const char * el, const char ** attr); -}; - -} // namespace CHECK_USER -} // namespace STG - -#endif diff --git a/stglibs/srvconf.lib/parser_chg_user.cpp b/stglibs/srvconf.lib/parser_chg_user.cpp deleted file mode 100644 index b5f4eb73..00000000 --- a/stglibs/srvconf.lib/parser_chg_user.cpp +++ /dev/null @@ -1,71 +0,0 @@ -/* - * 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 "parser_chg_user.h" - -#include - -#include - -using namespace STG; - -CHG_USER::PARSER::PARSER() - : callback(NULL), - data(NULL), - depth(0) -{ -} -//----------------------------------------------------------------------------- -int CHG_USER::PARSER::ParseStart(const char *el, const char **attr) -{ -depth++; -if (depth == 1) - { - if (strcasecmp(el, "SetUser") == 0) - ParseAnswer(el, attr); - else if (strcasecmp(el, "DelUser") == 0) - ParseAnswer(el, attr); - else if (strcasecmp(el, "AddUser") == 0) - ParseAnswer(el, attr); - } -return 0; -} -//----------------------------------------------------------------------------- -void CHG_USER::PARSER::ParseEnd(const char *) -{ -depth--; -} -//----------------------------------------------------------------------------- -void CHG_USER::PARSER::ParseAnswer(const char * /*el*/, const char ** attr) -{ -if (!callback) - return; -if (attr && attr[0] && attr[1]) - callback(strcasecmp(attr[1], "ok") == 0, attr[2] && attr[3] ? attr[3] : "", data); -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/parser_chg_user.h b/stglibs/srvconf.lib/parser_chg_user.h deleted file mode 100644 index 63c528d0..00000000 --- a/stglibs/srvconf.lib/parser_chg_user.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 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_CHG_USER_H__ -#define __STG_STGLIBS_SRVCONF_PARSER_CHG_USER_H__ - -#include "parser.h" - -#include "stg/servconf_types.h" - -namespace STG -{ -namespace CHG_USER -{ - -class PARSER: public STG::PARSER -{ -public: - PARSER(); - int ParseStart(const char * el, const char ** attr); - void ParseEnd(const char * el); - void SetCallback(CALLBACK f, void * data); - -private: - CALLBACK callback; - void * data; - int depth; - - void ParseAnswer(const char * el, const char ** attr); -}; - -} // namespace CHG_USER -} // namespace STG - -#endif diff --git a/stglibs/srvconf.lib/parser_get_user.cpp b/stglibs/srvconf.lib/parser_get_user.cpp deleted file mode 100644 index 2c398914..00000000 --- a/stglibs/srvconf.lib/parser_get_user.cpp +++ /dev/null @@ -1,157 +0,0 @@ -/* - * 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 "parser_get_user.h" - -#include "stg/common.h" - -#include -#include -#include - -#include - -using namespace STG; - -namespace STG -{ - -template <> -bool GetValue(const char ** attr, GET_USER::STAT & value) -{ -if (!attr) - return false; -std::map props; -for (size_t i = 0; i < DIR_NUM; ++i) - { - props.insert(std::pair("su" + x2str(i), &value.su[i])); - props.insert(std::pair("sd" + x2str(i), &value.sd[i])); - props.insert(std::pair("mu" + x2str(i), &value.mu[i])); - props.insert(std::pair("md" + x2str(i), &value.md[i])); - } -size_t pos = 0; -while (attr[pos]) - { - std::string name(ToLower(attr[pos++])); - std::map::iterator it(props.find(name)); - if (it != props.end()) - if (str2x(attr[pos++], *it->second) < 0) - return false; - } -return true; -} - -} - -GET_USER::PARSER::PARSER() - : callback(NULL), - data(NULL), - depth(0), - parsingAnswer(false) -{ - AddParser(propertyParsers, "login", info.login); - AddParser(propertyParsers, "password", info.password); - AddParser(propertyParsers, "cash", info.cash); - AddParser(propertyParsers, "credit", info.credit); - AddParser(propertyParsers, "creditExpire", info.creditExpire); - AddParser(propertyParsers, "lastCash", info.lastCash); - AddParser(propertyParsers, "prepaidTraff", info.prepaidTraff); - AddParser(propertyParsers, "down", info.down); - AddParser(propertyParsers, "passive", info.passive); - AddParser(propertyParsers, "disableDetailStat", info.disableDetailStat); - AddParser(propertyParsers, "connected", info.connected); - AddParser(propertyParsers, "aonline", info.alwaysOnline); - 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, "traff", info.stat); - - for (size_t i = 0; i < USERDATA_NUM; ++i) - AddParser(propertyParsers, "userData" + x2str(i), info.userData[i], GetEncodedValue); -} -//----------------------------------------------------------------------------- -GET_USER::PARSER::~PARSER() -{ - PROPERTY_PARSERS::iterator it(propertyParsers.begin()); - while (it != propertyParsers.end()) - delete (it++)->second; -} -//----------------------------------------------------------------------------- -int GET_USER::PARSER::ParseStart(const char * el, const char ** attr) -{ -depth++; -if (depth == 1) - ParseUser(el, attr); - -if (depth == 2 && parsingAnswer) - ParseUserParams(el, attr); - -return 0; -} -//----------------------------------------------------------------------------- -void GET_USER::PARSER::ParseEnd(const char * /*el*/) -{ -depth--; -if (depth == 0 && parsingAnswer) - { - if (callback) - callback(error.empty(), error, info, data); - error.clear(); - parsingAnswer = false; - } -} -//----------------------------------------------------------------------------- -void GET_USER::PARSER::ParseUser(const char * el, const char ** attr) -{ -if (strcasecmp(el, "user") == 0) - if (attr && attr[0] && attr[1]) - { - if (strcasecmp(attr[1], "error") == 0) - { - if (attr[2] && attr[3]) - error = attr[3]; - else - error = "User not found."; - } - else - parsingAnswer = true; - } - else - parsingAnswer = true; -} -//----------------------------------------------------------------------------- -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/parser_get_user.h b/stglibs/srvconf.lib/parser_get_user.h deleted file mode 100644 index c8c371d7..00000000 --- a/stglibs/srvconf.lib/parser_get_user.h +++ /dev/null @@ -1,62 +0,0 @@ -/* - * 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_USER_H__ -#define __STG_STGLIBS_SRVCONF_PARSER_GET_USER_H__ - -#include "parser.h" -#include "property_parsers.h" - -#include "stg/servconf_types.h" - -#include - -namespace STG -{ -namespace GET_USER -{ - -class PARSER: public STG::PARSER -{ -public: - PARSER(); - 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; - CALLBACK callback; - void * data; - INFO info; - int depth; - bool parsingAnswer; - std::string error; - - void ParseUser(const char * el, const char ** attr); - void ParseUserParams(const char * el, const char ** attr); -}; - -} // namespace GET_USER -} // namespace STG - -#endif diff --git a/stglibs/srvconf.lib/parser_get_users.cpp b/stglibs/srvconf.lib/parser_get_users.cpp deleted file mode 100644 index ecc2917c..00000000 --- a/stglibs/srvconf.lib/parser_get_users.cpp +++ /dev/null @@ -1,85 +0,0 @@ -/* - * 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 "parser_get_users.h" - -#include - -#include - -using namespace STG; - -GET_USERS::PARSER::PARSER() - : callback(NULL), - data(NULL), - depth(0), - parsingAnswer(false) -{ - userParser.SetCallback(&GET_USERS::PARSER::UserCallback, this); -} -//----------------------------------------------------------------------------- -int GET_USERS::PARSER::ParseStart(const char * el, const char ** attr) -{ -depth++; -if (depth == 1 && strcasecmp(el, "users") == 0) - parsingAnswer = true; - -if (depth > 1 && parsingAnswer) - userParser.ParseStart(el, attr); - -return 0; -} -//----------------------------------------------------------------------------- -void GET_USERS::PARSER::ParseEnd(const char * el) -{ -depth--; -if (depth > 0 && parsingAnswer) - userParser.ParseEnd(el); - -if (depth == 0 && parsingAnswer) - { - if (callback) - callback(error.empty(), error, info, data); - error.clear(); - info.clear(); - parsingAnswer = false; - } -} -//----------------------------------------------------------------------------- -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); - if (!result) - parser->SetError(error); - else - parser->AddUser(info); -} diff --git a/stglibs/srvconf.lib/parser_get_users.h b/stglibs/srvconf.lib/parser_get_users.h deleted file mode 100644 index ea9966ee..00000000 --- a/stglibs/srvconf.lib/parser_get_users.h +++ /dev/null @@ -1,64 +0,0 @@ - -/* - * 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 "parser.h" -#include "parser_get_user.h" - -#include "stg/servconf_types.h" - -#include - -namespace STG -{ -namespace GET_USERS -{ - -class PARSER: public STG::PARSER -{ -public: - PARSER(); - int ParseStart(const char * el, const char ** attr); - void ParseEnd(const char * el); - void SetCallback(CALLBACK f, void * data); - -private: - CALLBACK callback; - void * data; - GET_USER::PARSER userParser; - INFO info; - int depth; - bool parsingAnswer; - std::string error; - - void AddUser(const GET_USER::INFO & userInfo); - void SetError(const std::string & e) { error = e; } - - static void UserCallback(bool result, const std::string& reason, const GET_USER::INFO & info, void * data); -}; - -} // namespace GET_USERS -} // namespace STG - -#endif diff --git a/stglibs/srvconf.lib/parser_send_message.cpp b/stglibs/srvconf.lib/parser_send_message.cpp deleted file mode 100644 index 318cc8eb..00000000 --- a/stglibs/srvconf.lib/parser_send_message.cpp +++ /dev/null @@ -1,65 +0,0 @@ -/* - * 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 "parser_send_message.h" - -#include - -#include - -using namespace STG; - -SEND_MESSAGE::PARSER::PARSER() - : callback(NULL), - data(NULL), - depth(0) -{ -} -//----------------------------------------------------------------------------- -int SEND_MESSAGE::PARSER::ParseStart(const char * el, const char ** attr) -{ -depth++; -if (depth == 1) - if (strcasecmp(el, "SendMessageResult") == 0) - ParseAnswer(el, attr); -return 0; -} -//----------------------------------------------------------------------------- -void SEND_MESSAGE::PARSER::ParseEnd(const char * /*el*/) -{ -depth--; -} -//----------------------------------------------------------------------------- -void SEND_MESSAGE::PARSER::ParseAnswer(const char * /*el*/, const char **attr) -{ -if (!callback) - return; -if (attr && attr[0] && attr[1]) - callback(strcasecmp(attr[1], "ok") == 0, attr[1], data); -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/parser_send_message.h b/stglibs/srvconf.lib/parser_send_message.h deleted file mode 100644 index 27903bc0..00000000 --- a/stglibs/srvconf.lib/parser_send_message.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 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_SEND_MESSAGE_H__ -#define __STG_STGLIBS_SRVCONF_PARSER_SEND_MESSAGE_H__ - -#include "parser.h" - -#include "stg/servconf_types.h" - -namespace STG -{ -namespace SEND_MESSAGE -{ - -class PARSER: public STG::PARSER -{ -public: - PARSER(); - int ParseStart(const char * el, const char ** attr); - void ParseEnd(const char * el); - void SetCallback(CALLBACK f, void * data); - -private: - CALLBACK callback; - void * data; - int depth; - - void ParseAnswer(const char * el, const char ** attr); -}; - -} // namespace SEND_MESSAGE -} // namespace STG - -#endif diff --git a/stglibs/srvconf.lib/parser_server_info.cpp b/stglibs/srvconf.lib/parser_server_info.cpp deleted file mode 100644 index d4c610e0..00000000 --- a/stglibs/srvconf.lib/parser_server_info.cpp +++ /dev/null @@ -1,88 +0,0 @@ -/* - * 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 "parser_server_info.h" - -#include "stg/common.h" - -#include // sprintf -#include - -#include - -using namespace STG; - -namespace -{ - -const size_t UNAME_LEN = 256; -const size_t SERV_VER_LEN = 64; -const size_t DIRNAME_LEN = 16; - -} - -SERVER_INFO::PARSER::PARSER() - : callback(NULL), - data(NULL), - depth(0), - parsingAnswer(false) -{ - AddParser(propertyParsers, "uname", info.uname); - AddParser(propertyParsers, "version", info.version); - AddParser(propertyParsers, "tariff", info.tariffType); - AddParser(propertyParsers, "dir_num", info.dirNum); - AddParser(propertyParsers, "users_num", info.usersNum); - AddParser(propertyParsers, "tariff_num", info.tariffNum); - - for (size_t i = 0; i < DIR_NUM; i++) - AddParser(propertyParsers, "dir_name_" + x2str(i), info.dirName[i], GetEncodedValue); -} -//----------------------------------------------------------------------------- -int SERVER_INFO::PARSER::ParseStart(const char *el, const char **attr) -{ -depth++; -if (depth == 1) - if (strcasecmp(el, "ServerInfo") == 0) - parsingAnswer = true; -else - if (depth == 2 && parsingAnswer) - if (!TryParse(propertyParsers, ToLower(el), attr)) - error = "Invalid parameter."; -return 0; -} -//----------------------------------------------------------------------------- -void SERVER_INFO::PARSER::ParseEnd(const char * /*el*/) -{ -depth--; -if (depth == 0 && parsingAnswer) - { - if (callback) - callback(error.empty(), error, info, data); - error.clear(); - parsingAnswer = false; - } -} -//----------------------------------------------------------------------------- -void SERVER_INFO::PARSER::SetCallback(CALLBACK f, void * d) -{ -callback = f; -data = d; -} diff --git a/stglibs/srvconf.lib/parser_server_info.h b/stglibs/srvconf.lib/parser_server_info.h deleted file mode 100644 index 9ac23ee8..00000000 --- a/stglibs/srvconf.lib/parser_server_info.h +++ /dev/null @@ -1,58 +0,0 @@ -/* - * 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_SERVER_INFO_H__ -#define __STG_STGLIBS_SRVCONF_PARSER_SERVER_INFO_H__ - -#include "parser.h" -#include "property_parsers.h" - -#include "stg/servconf_types.h" - -#include - -namespace STG -{ -namespace SERVER_INFO -{ - -class PARSER: public STG::PARSER -{ -public: - PARSER(); - int ParseStart(const char * el, const char ** attr); - void ParseEnd(const char * el); - void SetCallback(CALLBACK f, void * data); - -private: - PROPERTY_PARSERS propertyParsers; - CALLBACK callback; - void * data; - int depth; - bool parsingAnswer; - INFO info; - std::string error; -}; - -} // namespace SERVER_INFO -} // namespace STG - -#endif diff --git a/stglibs/srvconf.lib/parsers/auth_by.cpp b/stglibs/srvconf.lib/parsers/auth_by.cpp new file mode 100644 index 00000000..34e490ca --- /dev/null +++ b/stglibs/srvconf.lib/parsers/auth_by.cpp @@ -0,0 +1,87 @@ +/* + * 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 : Maxim Mamontov + */ + +#include "parser_auth_by.h" + +#include + +#include // strcasecmp + +using namespace STG; + +AUTH_BY::PARSER::PARSER() + : callback(NULL), + data(NULL), + depth(0), + parsingAnswer(false) +{ +} +//----------------------------------------------------------------------------- +int AUTH_BY::PARSER::ParseStart(const char *el, const char **attr) +{ +depth++; +if (depth == 1) + { + if (strcasecmp(el, "AuthorizedBy") == 0) + if (attr && attr[0] && attr[1]) + { + if (strcasecmp(attr[1], "error") == 0) + { + if (attr[2] && attr[3]) + error = attr[3]; + else + error = "User not found."; + } + else + parsingAnswer = true; + } + } +else + { + if (depth == 2) + { + if (parsingAnswer && strcasecmp(el, "Auth") == 0) + { + if (attr && attr[0] && attr[1] && strcasecmp(attr[0], "name") == 0) + info.push_back(attr[1]); + return 0; + } + } + } +return 0; +} +//----------------------------------------------------------------------------- +void AUTH_BY::PARSER::ParseEnd(const char * /*el*/) +{ +depth--; +if (depth == 0) + { + if (callback) + callback(error.empty(), error, info, data); + info.clear(); + 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 new file mode 100644 index 00000000..c4757cf9 --- /dev/null +++ b/stglibs/srvconf.lib/parsers/auth_by.h @@ -0,0 +1,55 @@ +/* + * 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 : Maxim Mamontov + */ + +#ifndef __STG_STGLIBS_SRVCONF_PARSER_AUTH_BY_H__ +#define __STG_STGLIBS_SRVCONF_PARSER_AUTH_BY_H__ + +#include "parser.h" + +#include "stg/servconf_types.h" + +#include + +namespace STG +{ +namespace AUTH_BY +{ + +class PARSER: public STG::PARSER +{ +public: + PARSER(); + int ParseStart(const char * el, const char ** attr); + void ParseEnd(const char * el); + void SetCallback(CALLBACK f, void * data); + +private: + CALLBACK callback; + void * data; + int depth; + bool parsingAnswer; + INFO info; + std::string error; +}; + +} // namespace AUTH_BY +} // namespace STG + +#endif diff --git a/stglibs/srvconf.lib/parsers/base.h b/stglibs/srvconf.lib/parsers/base.h new file mode 100644 index 00000000..8d695a3a --- /dev/null +++ b/stglibs/srvconf.lib/parsers/base.h @@ -0,0 +1,38 @@ +/* + * 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_H__ +#define __STG_STGLIBS_SRVCONF_PARSER_H__ + +namespace STG +{ + +class PARSER +{ +public: + virtual ~PARSER() {} + virtual int ParseStart(const char *el, const char **attr) = 0; + virtual void ParseEnd(const char *el) = 0; +}; + +} // namespace STG + +#endif diff --git a/stglibs/srvconf.lib/parsers/check_user.cpp b/stglibs/srvconf.lib/parsers/check_user.cpp new file mode 100644 index 00000000..0cb37522 --- /dev/null +++ b/stglibs/srvconf.lib/parsers/check_user.cpp @@ -0,0 +1,65 @@ +/* + * 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 "parser_check_user.h" + +#include + +#include + +using namespace STG; + +CHECK_USER::PARSER::PARSER() + : callback(NULL), + data(NULL), + depth(0) +{ +} +//----------------------------------------------------------------------------- +int CHECK_USER::PARSER::ParseStart(const char * el, const char ** attr) +{ +depth++; +if (depth == 1) + if (strcasecmp(el, "CheckUser") == 0) + ParseAnswer(el, attr); +return 0; +} +//----------------------------------------------------------------------------- +void CHECK_USER::PARSER::ParseEnd(const char *) +{ +depth--; +} +//----------------------------------------------------------------------------- +void CHECK_USER::PARSER::ParseAnswer(const char *, const char **attr) +{ +if (!callback) + return; +if (attr && attr[0] && attr[1] && strcasecmp(attr[0], "value") == 0) + callback(strcasecmp(attr[1], "ok") == 0, attr[2] && attr[3] ? attr[3] : "", data); +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 new file mode 100644 index 00000000..95658c38 --- /dev/null +++ b/stglibs/srvconf.lib/parsers/check_user.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_CHECK_USER_H__ +#define __STG_STGLIBS_SRVCONF_PARSER_CHECK_USER_H__ + +#include "parser.h" + +#include "stg/servconf_types.h" + +namespace STG +{ +namespace CHECK_USER +{ + +class PARSER: public STG::PARSER +{ +public: + PARSER(); + int ParseStart(const char * el, const char ** attr); + void ParseEnd(const char * el); + void SetCallback(CALLBACK f, void * data); + +private: + CALLBACK callback; + void * data; + int depth; + + void ParseAnswer(const char * el, const char ** attr); +}; + +} // namespace CHECK_USER +} // namespace STG + +#endif diff --git a/stglibs/srvconf.lib/parsers/chg_user.cpp b/stglibs/srvconf.lib/parsers/chg_user.cpp new file mode 100644 index 00000000..b5f4eb73 --- /dev/null +++ b/stglibs/srvconf.lib/parsers/chg_user.cpp @@ -0,0 +1,71 @@ +/* + * 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 "parser_chg_user.h" + +#include + +#include + +using namespace STG; + +CHG_USER::PARSER::PARSER() + : callback(NULL), + data(NULL), + depth(0) +{ +} +//----------------------------------------------------------------------------- +int CHG_USER::PARSER::ParseStart(const char *el, const char **attr) +{ +depth++; +if (depth == 1) + { + if (strcasecmp(el, "SetUser") == 0) + ParseAnswer(el, attr); + else if (strcasecmp(el, "DelUser") == 0) + ParseAnswer(el, attr); + else if (strcasecmp(el, "AddUser") == 0) + ParseAnswer(el, attr); + } +return 0; +} +//----------------------------------------------------------------------------- +void CHG_USER::PARSER::ParseEnd(const char *) +{ +depth--; +} +//----------------------------------------------------------------------------- +void CHG_USER::PARSER::ParseAnswer(const char * /*el*/, const char ** attr) +{ +if (!callback) + return; +if (attr && attr[0] && attr[1]) + callback(strcasecmp(attr[1], "ok") == 0, attr[2] && attr[3] ? attr[3] : "", data); +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 new file mode 100644 index 00000000..63c528d0 --- /dev/null +++ b/stglibs/srvconf.lib/parsers/chg_user.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_CHG_USER_H__ +#define __STG_STGLIBS_SRVCONF_PARSER_CHG_USER_H__ + +#include "parser.h" + +#include "stg/servconf_types.h" + +namespace STG +{ +namespace CHG_USER +{ + +class PARSER: public STG::PARSER +{ +public: + PARSER(); + int ParseStart(const char * el, const char ** attr); + void ParseEnd(const char * el); + void SetCallback(CALLBACK f, void * data); + +private: + CALLBACK callback; + void * data; + int depth; + + void ParseAnswer(const char * el, const char ** attr); +}; + +} // namespace CHG_USER +} // namespace STG + +#endif diff --git a/stglibs/srvconf.lib/parsers/get_user.cpp b/stglibs/srvconf.lib/parsers/get_user.cpp new file mode 100644 index 00000000..2c398914 --- /dev/null +++ b/stglibs/srvconf.lib/parsers/get_user.cpp @@ -0,0 +1,157 @@ +/* + * 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 "parser_get_user.h" + +#include "stg/common.h" + +#include +#include +#include + +#include + +using namespace STG; + +namespace STG +{ + +template <> +bool GetValue(const char ** attr, GET_USER::STAT & value) +{ +if (!attr) + return false; +std::map props; +for (size_t i = 0; i < DIR_NUM; ++i) + { + props.insert(std::pair("su" + x2str(i), &value.su[i])); + props.insert(std::pair("sd" + x2str(i), &value.sd[i])); + props.insert(std::pair("mu" + x2str(i), &value.mu[i])); + props.insert(std::pair("md" + x2str(i), &value.md[i])); + } +size_t pos = 0; +while (attr[pos]) + { + std::string name(ToLower(attr[pos++])); + std::map::iterator it(props.find(name)); + if (it != props.end()) + if (str2x(attr[pos++], *it->second) < 0) + return false; + } +return true; +} + +} + +GET_USER::PARSER::PARSER() + : callback(NULL), + data(NULL), + depth(0), + parsingAnswer(false) +{ + AddParser(propertyParsers, "login", info.login); + AddParser(propertyParsers, "password", info.password); + AddParser(propertyParsers, "cash", info.cash); + AddParser(propertyParsers, "credit", info.credit); + AddParser(propertyParsers, "creditExpire", info.creditExpire); + AddParser(propertyParsers, "lastCash", info.lastCash); + AddParser(propertyParsers, "prepaidTraff", info.prepaidTraff); + AddParser(propertyParsers, "down", info.down); + AddParser(propertyParsers, "passive", info.passive); + AddParser(propertyParsers, "disableDetailStat", info.disableDetailStat); + AddParser(propertyParsers, "connected", info.connected); + AddParser(propertyParsers, "aonline", info.alwaysOnline); + 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, "traff", info.stat); + + for (size_t i = 0; i < USERDATA_NUM; ++i) + AddParser(propertyParsers, "userData" + x2str(i), info.userData[i], GetEncodedValue); +} +//----------------------------------------------------------------------------- +GET_USER::PARSER::~PARSER() +{ + PROPERTY_PARSERS::iterator it(propertyParsers.begin()); + while (it != propertyParsers.end()) + delete (it++)->second; +} +//----------------------------------------------------------------------------- +int GET_USER::PARSER::ParseStart(const char * el, const char ** attr) +{ +depth++; +if (depth == 1) + ParseUser(el, attr); + +if (depth == 2 && parsingAnswer) + ParseUserParams(el, attr); + +return 0; +} +//----------------------------------------------------------------------------- +void GET_USER::PARSER::ParseEnd(const char * /*el*/) +{ +depth--; +if (depth == 0 && parsingAnswer) + { + if (callback) + callback(error.empty(), error, info, data); + error.clear(); + parsingAnswer = false; + } +} +//----------------------------------------------------------------------------- +void GET_USER::PARSER::ParseUser(const char * el, const char ** attr) +{ +if (strcasecmp(el, "user") == 0) + if (attr && attr[0] && attr[1]) + { + if (strcasecmp(attr[1], "error") == 0) + { + if (attr[2] && attr[3]) + error = attr[3]; + else + error = "User not found."; + } + else + parsingAnswer = true; + } + else + parsingAnswer = true; +} +//----------------------------------------------------------------------------- +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 new file mode 100644 index 00000000..c8c371d7 --- /dev/null +++ b/stglibs/srvconf.lib/parsers/get_user.h @@ -0,0 +1,62 @@ +/* + * 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_USER_H__ +#define __STG_STGLIBS_SRVCONF_PARSER_GET_USER_H__ + +#include "parser.h" +#include "property_parsers.h" + +#include "stg/servconf_types.h" + +#include + +namespace STG +{ +namespace GET_USER +{ + +class PARSER: public STG::PARSER +{ +public: + PARSER(); + 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; + CALLBACK callback; + void * data; + INFO info; + int depth; + bool parsingAnswer; + std::string error; + + void ParseUser(const char * el, const char ** attr); + void ParseUserParams(const char * el, const char ** attr); +}; + +} // namespace GET_USER +} // namespace STG + +#endif diff --git a/stglibs/srvconf.lib/parsers/get_users.cpp b/stglibs/srvconf.lib/parsers/get_users.cpp new file mode 100644 index 00000000..ecc2917c --- /dev/null +++ b/stglibs/srvconf.lib/parsers/get_users.cpp @@ -0,0 +1,85 @@ +/* + * 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 "parser_get_users.h" + +#include + +#include + +using namespace STG; + +GET_USERS::PARSER::PARSER() + : callback(NULL), + data(NULL), + depth(0), + parsingAnswer(false) +{ + userParser.SetCallback(&GET_USERS::PARSER::UserCallback, this); +} +//----------------------------------------------------------------------------- +int GET_USERS::PARSER::ParseStart(const char * el, const char ** attr) +{ +depth++; +if (depth == 1 && strcasecmp(el, "users") == 0) + parsingAnswer = true; + +if (depth > 1 && parsingAnswer) + userParser.ParseStart(el, attr); + +return 0; +} +//----------------------------------------------------------------------------- +void GET_USERS::PARSER::ParseEnd(const char * el) +{ +depth--; +if (depth > 0 && parsingAnswer) + userParser.ParseEnd(el); + +if (depth == 0 && parsingAnswer) + { + if (callback) + callback(error.empty(), error, info, data); + error.clear(); + info.clear(); + parsingAnswer = false; + } +} +//----------------------------------------------------------------------------- +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); + if (!result) + parser->SetError(error); + else + parser->AddUser(info); +} diff --git a/stglibs/srvconf.lib/parsers/get_users.h b/stglibs/srvconf.lib/parsers/get_users.h new file mode 100644 index 00000000..ea9966ee --- /dev/null +++ b/stglibs/srvconf.lib/parsers/get_users.h @@ -0,0 +1,64 @@ + +/* + * 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 "parser.h" +#include "parser_get_user.h" + +#include "stg/servconf_types.h" + +#include + +namespace STG +{ +namespace GET_USERS +{ + +class PARSER: public STG::PARSER +{ +public: + PARSER(); + int ParseStart(const char * el, const char ** attr); + void ParseEnd(const char * el); + void SetCallback(CALLBACK f, void * data); + +private: + CALLBACK callback; + void * data; + GET_USER::PARSER userParser; + INFO info; + int depth; + bool parsingAnswer; + std::string error; + + void AddUser(const GET_USER::INFO & userInfo); + void SetError(const std::string & e) { error = e; } + + static void UserCallback(bool result, const std::string& reason, const GET_USER::INFO & info, void * data); +}; + +} // namespace GET_USERS +} // namespace STG + +#endif diff --git a/stglibs/srvconf.lib/parsers/property.cpp b/stglibs/srvconf.lib/parsers/property.cpp new file mode 100644 index 00000000..5d4577dc --- /dev/null +++ b/stglibs/srvconf.lib/parsers/property.cpp @@ -0,0 +1,55 @@ +/* + * 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 : Maxim Mamontov + */ + +#include "property_parsers.h" + +#include + +bool STG::CheckValue(const char ** attr) +{ +return attr && attr[0] && attr[1] && strcasecmp(attr[0], "value") == 0; +} + +bool STG::GetEncodedValue(const char ** attr, std::string & value) +{ +if (!CheckValue(attr)) + return false; +Decode21str(value, attr[1]); +return true; +} + +bool STG::GetIPValue(const char ** attr, uint32_t & value) +{ +if (!CheckValue(attr)) + return false; +std::string ip(attr[1]); +value = inet_strington(attr[1]); +if (value == 0 && ip != "0.0.0.0") + return false; +return true; +} + +bool STG::TryParse(PROPERTY_PARSERS & parsers, const std::string & name, const char ** attr) +{ + PROPERTY_PARSERS::iterator it(parsers.find(name)); + if (it != parsers.end()) + return it->second->Parse(attr); + return true; // Assume that non-existing params are ok. +} diff --git a/stglibs/srvconf.lib/parsers/property.h b/stglibs/srvconf.lib/parsers/property.h new file mode 100644 index 00000000..3469b987 --- /dev/null +++ b/stglibs/srvconf.lib/parsers/property.h @@ -0,0 +1,103 @@ +/* + * 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 : Maxim Mamontov + */ + +#ifndef __STG_STGLIBS_SRVCONF_PROPERTY_PARSERS_H__ +#define __STG_STGLIBS_SRVCONF_PROPERTY_PARSERS_H__ + +#include "stg/common.h" + +#include +#include + +namespace STG +{ + +class BASE_PROPERTY_PARSER +{ + public: + virtual ~BASE_PROPERTY_PARSER() {} + virtual bool Parse(const char ** attr) = 0; +}; + +template +class PROPERTY_PARSER : public BASE_PROPERTY_PARSER +{ + public: + typedef bool (* FUNC)(const char **, T &); + PROPERTY_PARSER(T & v, FUNC f) : value(v), func(f) {} + virtual bool Parse(const char ** attr) { return func(attr, value); } + private: + T & value; + FUNC func; +}; + +typedef std::map PROPERTY_PARSERS; + +bool CheckValue(const char ** attr); + +template +inline +bool GetValue(const char ** attr, T & value) +{ +if (CheckValue(attr)) + if (str2x(attr[1], value) < 0) + return false; +return true; +} + +template <> +inline +bool GetValue(const char ** attr, std::string & value) +{ +if (!CheckValue(attr)) + return false; +value = attr[1]; +return true; +} + +template <> +inline +bool GetValue(const char ** attr, double & value) +{ +if (CheckValue(attr)) + if (strtodouble2(attr[1], value)) + return false; +return true; +} + +bool GetEncodedValue(const char ** attr, std::string & value); + +bool GetIPValue(const char ** attr, uint32_t& value); + +template +void AddParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, const typename PROPERTY_PARSER::FUNC & func = GetValue); + +template +inline +void AddParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, const typename PROPERTY_PARSER::FUNC & func) +{ + parsers.insert(std::make_pair(ToLower(name), new PROPERTY_PARSER(value, func))); +} + +bool TryParse(PROPERTY_PARSERS & parsers, const std::string & name, const char ** attr); + +} // namespace STG + +#endif diff --git a/stglibs/srvconf.lib/parsers/send_message.cpp b/stglibs/srvconf.lib/parsers/send_message.cpp new file mode 100644 index 00000000..318cc8eb --- /dev/null +++ b/stglibs/srvconf.lib/parsers/send_message.cpp @@ -0,0 +1,65 @@ +/* + * 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 "parser_send_message.h" + +#include + +#include + +using namespace STG; + +SEND_MESSAGE::PARSER::PARSER() + : callback(NULL), + data(NULL), + depth(0) +{ +} +//----------------------------------------------------------------------------- +int SEND_MESSAGE::PARSER::ParseStart(const char * el, const char ** attr) +{ +depth++; +if (depth == 1) + if (strcasecmp(el, "SendMessageResult") == 0) + ParseAnswer(el, attr); +return 0; +} +//----------------------------------------------------------------------------- +void SEND_MESSAGE::PARSER::ParseEnd(const char * /*el*/) +{ +depth--; +} +//----------------------------------------------------------------------------- +void SEND_MESSAGE::PARSER::ParseAnswer(const char * /*el*/, const char **attr) +{ +if (!callback) + return; +if (attr && attr[0] && attr[1]) + callback(strcasecmp(attr[1], "ok") == 0, attr[1], data); +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 new file mode 100644 index 00000000..27903bc0 --- /dev/null +++ b/stglibs/srvconf.lib/parsers/send_message.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_SEND_MESSAGE_H__ +#define __STG_STGLIBS_SRVCONF_PARSER_SEND_MESSAGE_H__ + +#include "parser.h" + +#include "stg/servconf_types.h" + +namespace STG +{ +namespace SEND_MESSAGE +{ + +class PARSER: public STG::PARSER +{ +public: + PARSER(); + int ParseStart(const char * el, const char ** attr); + void ParseEnd(const char * el); + void SetCallback(CALLBACK f, void * data); + +private: + CALLBACK callback; + void * data; + int depth; + + void ParseAnswer(const char * el, const char ** attr); +}; + +} // namespace SEND_MESSAGE +} // namespace STG + +#endif diff --git a/stglibs/srvconf.lib/parsers/server_info.cpp b/stglibs/srvconf.lib/parsers/server_info.cpp new file mode 100644 index 00000000..d4c610e0 --- /dev/null +++ b/stglibs/srvconf.lib/parsers/server_info.cpp @@ -0,0 +1,88 @@ +/* + * 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 "parser_server_info.h" + +#include "stg/common.h" + +#include // sprintf +#include + +#include + +using namespace STG; + +namespace +{ + +const size_t UNAME_LEN = 256; +const size_t SERV_VER_LEN = 64; +const size_t DIRNAME_LEN = 16; + +} + +SERVER_INFO::PARSER::PARSER() + : callback(NULL), + data(NULL), + depth(0), + parsingAnswer(false) +{ + AddParser(propertyParsers, "uname", info.uname); + AddParser(propertyParsers, "version", info.version); + AddParser(propertyParsers, "tariff", info.tariffType); + AddParser(propertyParsers, "dir_num", info.dirNum); + AddParser(propertyParsers, "users_num", info.usersNum); + AddParser(propertyParsers, "tariff_num", info.tariffNum); + + for (size_t i = 0; i < DIR_NUM; i++) + AddParser(propertyParsers, "dir_name_" + x2str(i), info.dirName[i], GetEncodedValue); +} +//----------------------------------------------------------------------------- +int SERVER_INFO::PARSER::ParseStart(const char *el, const char **attr) +{ +depth++; +if (depth == 1) + if (strcasecmp(el, "ServerInfo") == 0) + parsingAnswer = true; +else + if (depth == 2 && parsingAnswer) + if (!TryParse(propertyParsers, ToLower(el), attr)) + error = "Invalid parameter."; +return 0; +} +//----------------------------------------------------------------------------- +void SERVER_INFO::PARSER::ParseEnd(const char * /*el*/) +{ +depth--; +if (depth == 0 && parsingAnswer) + { + if (callback) + callback(error.empty(), error, info, data); + error.clear(); + 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 new file mode 100644 index 00000000..9ac23ee8 --- /dev/null +++ b/stglibs/srvconf.lib/parsers/server_info.h @@ -0,0 +1,58 @@ +/* + * 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_SERVER_INFO_H__ +#define __STG_STGLIBS_SRVCONF_PARSER_SERVER_INFO_H__ + +#include "parser.h" +#include "property_parsers.h" + +#include "stg/servconf_types.h" + +#include + +namespace STG +{ +namespace SERVER_INFO +{ + +class PARSER: public STG::PARSER +{ +public: + PARSER(); + int ParseStart(const char * el, const char ** attr); + void ParseEnd(const char * el); + void SetCallback(CALLBACK f, void * data); + +private: + PROPERTY_PARSERS propertyParsers; + CALLBACK callback; + void * data; + int depth; + bool parsingAnswer; + INFO info; + std::string error; +}; + +} // namespace SERVER_INFO +} // namespace STG + +#endif diff --git a/stglibs/srvconf.lib/property_parsers.cpp b/stglibs/srvconf.lib/property_parsers.cpp deleted file mode 100644 index 5d4577dc..00000000 --- a/stglibs/srvconf.lib/property_parsers.cpp +++ /dev/null @@ -1,55 +0,0 @@ -/* - * 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 : Maxim Mamontov - */ - -#include "property_parsers.h" - -#include - -bool STG::CheckValue(const char ** attr) -{ -return attr && attr[0] && attr[1] && strcasecmp(attr[0], "value") == 0; -} - -bool STG::GetEncodedValue(const char ** attr, std::string & value) -{ -if (!CheckValue(attr)) - return false; -Decode21str(value, attr[1]); -return true; -} - -bool STG::GetIPValue(const char ** attr, uint32_t & value) -{ -if (!CheckValue(attr)) - return false; -std::string ip(attr[1]); -value = inet_strington(attr[1]); -if (value == 0 && ip != "0.0.0.0") - return false; -return true; -} - -bool STG::TryParse(PROPERTY_PARSERS & parsers, const std::string & name, const char ** attr) -{ - PROPERTY_PARSERS::iterator it(parsers.find(name)); - if (it != parsers.end()) - return it->second->Parse(attr); - return true; // Assume that non-existing params are ok. -} diff --git a/stglibs/srvconf.lib/property_parsers.h b/stglibs/srvconf.lib/property_parsers.h deleted file mode 100644 index 3469b987..00000000 --- a/stglibs/srvconf.lib/property_parsers.h +++ /dev/null @@ -1,103 +0,0 @@ -/* - * 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 : Maxim Mamontov - */ - -#ifndef __STG_STGLIBS_SRVCONF_PROPERTY_PARSERS_H__ -#define __STG_STGLIBS_SRVCONF_PROPERTY_PARSERS_H__ - -#include "stg/common.h" - -#include -#include - -namespace STG -{ - -class BASE_PROPERTY_PARSER -{ - public: - virtual ~BASE_PROPERTY_PARSER() {} - virtual bool Parse(const char ** attr) = 0; -}; - -template -class PROPERTY_PARSER : public BASE_PROPERTY_PARSER -{ - public: - typedef bool (* FUNC)(const char **, T &); - PROPERTY_PARSER(T & v, FUNC f) : value(v), func(f) {} - virtual bool Parse(const char ** attr) { return func(attr, value); } - private: - T & value; - FUNC func; -}; - -typedef std::map PROPERTY_PARSERS; - -bool CheckValue(const char ** attr); - -template -inline -bool GetValue(const char ** attr, T & value) -{ -if (CheckValue(attr)) - if (str2x(attr[1], value) < 0) - return false; -return true; -} - -template <> -inline -bool GetValue(const char ** attr, std::string & value) -{ -if (!CheckValue(attr)) - return false; -value = attr[1]; -return true; -} - -template <> -inline -bool GetValue(const char ** attr, double & value) -{ -if (CheckValue(attr)) - if (strtodouble2(attr[1], value)) - return false; -return true; -} - -bool GetEncodedValue(const char ** attr, std::string & value); - -bool GetIPValue(const char ** attr, uint32_t& value); - -template -void AddParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, const typename PROPERTY_PARSER::FUNC & func = GetValue); - -template -inline -void AddParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, const typename PROPERTY_PARSER::FUNC & func) -{ - parsers.insert(std::make_pair(ToLower(name), new PROPERTY_PARSER(value, func))); -} - -bool TryParse(PROPERTY_PARSERS & parsers, const std::string & name, const char ** attr); - -} // namespace STG - -#endif