From: Maxim Mamontov Date: Fri, 20 Sep 2013 22:23:31 +0000 (+0300) Subject: Moved base and utility classes from global scope. X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/f384309c9c663a4b381d2e1a8545693514835395?ds=inline;hp=-c Moved base and utility classes from global scope. --- f384309c9c663a4b381d2e1a8545693514835395 diff --git a/stglibs/srvconf.lib/include/stg/parser.h b/stglibs/srvconf.lib/include/stg/parser.h deleted file mode 100644 index 8d695a3a..00000000 --- a/stglibs/srvconf.lib/include/stg/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/include/stg/property_parsers.h b/stglibs/srvconf.lib/include/stg/property_parsers.h deleted file mode 100644 index d1e6a893..00000000 --- a/stglibs/srvconf.lib/include/stg/property_parsers.h +++ /dev/null @@ -1,98 +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 -#include - -#include "stg/common.h" - -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); - -#endif diff --git a/stglibs/srvconf.lib/parser.h b/stglibs/srvconf.lib/parser.h new file mode 100644 index 00000000..8d695a3a --- /dev/null +++ b/stglibs/srvconf.lib/parser.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/parser_auth_by.h b/stglibs/srvconf.lib/parser_auth_by.h index 4e94267c..c4757cf9 100644 --- a/stglibs/srvconf.lib/parser_auth_by.h +++ b/stglibs/srvconf.lib/parser_auth_by.h @@ -21,11 +21,10 @@ #ifndef __STG_STGLIBS_SRVCONF_PARSER_AUTH_BY_H__ #define __STG_STGLIBS_SRVCONF_PARSER_AUTH_BY_H__ -#include "stg/parser.h" +#include "parser.h" #include "stg/servconf_types.h" -#include #include namespace STG @@ -37,9 +36,10 @@ class PARSER: public STG::PARSER { public: PARSER(); - int ParseStart(const char *el, const char **attr); - void ParseEnd(const char *el); + int ParseStart(const char * el, const char ** attr); + void ParseEnd(const char * el); void SetCallback(CALLBACK f, void * data); + private: CALLBACK callback; void * data; diff --git a/stglibs/srvconf.lib/parser_check_user.h b/stglibs/srvconf.lib/parser_check_user.h index 11d6ab96..95658c38 100644 --- a/stglibs/srvconf.lib/parser_check_user.h +++ b/stglibs/srvconf.lib/parser_check_user.h @@ -22,10 +22,9 @@ #ifndef __STG_STGLIBS_SRVCONF_PARSER_CHECK_USER_H__ #define __STG_STGLIBS_SRVCONF_PARSER_CHECK_USER_H__ -#include "stg/parser.h" -#include "stg/servconf_types.h" +#include "parser.h" -#include +#include "stg/servconf_types.h" namespace STG { @@ -35,11 +34,11 @@ 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; diff --git a/stglibs/srvconf.lib/parser_chg_user.h b/stglibs/srvconf.lib/parser_chg_user.h index d74dea76..63c528d0 100644 --- a/stglibs/srvconf.lib/parser_chg_user.h +++ b/stglibs/srvconf.lib/parser_chg_user.h @@ -22,7 +22,8 @@ #ifndef __STG_STGLIBS_SRVCONF_PARSER_CHG_USER_H__ #define __STG_STGLIBS_SRVCONF_PARSER_CHG_USER_H__ -#include "stg/parser.h" +#include "parser.h" + #include "stg/servconf_types.h" namespace STG @@ -33,11 +34,11 @@ 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; diff --git a/stglibs/srvconf.lib/parser_get_user.cpp b/stglibs/srvconf.lib/parser_get_user.cpp index 45f5e6ea..2c398914 100644 --- a/stglibs/srvconf.lib/parser_get_user.cpp +++ b/stglibs/srvconf.lib/parser_get_user.cpp @@ -31,6 +31,9 @@ using namespace STG; +namespace STG +{ + template <> bool GetValue(const char ** attr, GET_USER::STAT & value) { @@ -56,6 +59,8 @@ while (attr[pos]) return true; } +} + GET_USER::PARSER::PARSER() : callback(NULL), data(NULL), diff --git a/stglibs/srvconf.lib/parser_get_user.h b/stglibs/srvconf.lib/parser_get_user.h index 871dd211..c8c371d7 100644 --- a/stglibs/srvconf.lib/parser_get_user.h +++ b/stglibs/srvconf.lib/parser_get_user.h @@ -22,15 +22,13 @@ #ifndef __STG_STGLIBS_SRVCONF_PARSER_GET_USER_H__ #define __STG_STGLIBS_SRVCONF_PARSER_GET_USER_H__ -#include "stg/parser.h" +#include "parser.h" +#include "property_parsers.h" -#include "stg/property_parsers.h" #include "stg/servconf_types.h" #include -#include - namespace STG { namespace GET_USER @@ -39,12 +37,12 @@ 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); + 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; @@ -54,8 +52,8 @@ private: bool parsingAnswer; std::string error; - void ParseUser(const char *el, const char **attr); - void ParseUserParams(const char *el, const char **attr); + void ParseUser(const char * el, const char ** attr); + void ParseUserParams(const char * el, const char ** attr); }; } // namespace GET_USER diff --git a/stglibs/srvconf.lib/parser_get_users.h b/stglibs/srvconf.lib/parser_get_users.h index 55258186..ea9966ee 100644 --- a/stglibs/srvconf.lib/parser_get_users.h +++ b/stglibs/srvconf.lib/parser_get_users.h @@ -23,12 +23,12 @@ #ifndef __STG_STGLIBS_SRVCONF_PARSER_GET_USERS_H__ #define __STG_STGLIBS_SRVCONF_PARSER_GET_USERS_H__ -#include "stg/parser.h" -#include "stg/servconf_types.h" - +#include "parser.h" #include "parser_get_user.h" -#include +#include "stg/servconf_types.h" + +#include namespace STG { @@ -38,11 +38,11 @@ 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; diff --git a/stglibs/srvconf.lib/parser_send_message.h b/stglibs/srvconf.lib/parser_send_message.h index fc1f020d..27903bc0 100644 --- a/stglibs/srvconf.lib/parser_send_message.h +++ b/stglibs/srvconf.lib/parser_send_message.h @@ -22,10 +22,9 @@ #ifndef __STG_STGLIBS_SRVCONF_PARSER_SEND_MESSAGE_H__ #define __STG_STGLIBS_SRVCONF_PARSER_SEND_MESSAGE_H__ -#include "stg/parser.h" -#include "stg/servconf_types.h" +#include "parser.h" -#include +#include "stg/servconf_types.h" namespace STG { @@ -39,6 +38,7 @@ public: int ParseStart(const char * el, const char ** attr); void ParseEnd(const char * el); void SetCallback(CALLBACK f, void * data); + private: CALLBACK callback; void * data; diff --git a/stglibs/srvconf.lib/parser_server_info.h b/stglibs/srvconf.lib/parser_server_info.h index 2e91106c..9ac23ee8 100644 --- a/stglibs/srvconf.lib/parser_server_info.h +++ b/stglibs/srvconf.lib/parser_server_info.h @@ -22,9 +22,9 @@ #ifndef __STG_STGLIBS_SRVCONF_PARSER_SERVER_INFO_H__ #define __STG_STGLIBS_SRVCONF_PARSER_SERVER_INFO_H__ -#include "stg/parser.h" +#include "parser.h" +#include "property_parsers.h" -#include "stg/property_parsers.h" #include "stg/servconf_types.h" #include @@ -37,11 +37,11 @@ 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; diff --git a/stglibs/srvconf.lib/property_parsers.cpp b/stglibs/srvconf.lib/property_parsers.cpp index fcb9020a..5d4577dc 100644 --- a/stglibs/srvconf.lib/property_parsers.cpp +++ b/stglibs/srvconf.lib/property_parsers.cpp @@ -18,16 +18,16 @@ * Author : Maxim Mamontov */ -#include "stg/property_parsers.h" +#include "property_parsers.h" #include -bool CheckValue(const char ** attr) +bool STG::CheckValue(const char ** attr) { return attr && attr[0] && attr[1] && strcasecmp(attr[0], "value") == 0; } -bool GetEncodedValue(const char ** attr, std::string & value) +bool STG::GetEncodedValue(const char ** attr, std::string & value) { if (!CheckValue(attr)) return false; @@ -35,7 +35,7 @@ Decode21str(value, attr[1]); return true; } -bool GetIPValue(const char ** attr, uint32_t & value) +bool STG::GetIPValue(const char ** attr, uint32_t & value) { if (!CheckValue(attr)) return false; @@ -46,7 +46,7 @@ if (value == 0 && ip != "0.0.0.0") return true; } -bool TryParse(PROPERTY_PARSERS & parsers, const std::string & name, const char ** attr) +bool STG::TryParse(PROPERTY_PARSERS & parsers, const std::string & name, const char ** attr) { PROPERTY_PARSERS::iterator it(parsers.find(name)); if (it != parsers.end()) diff --git a/stglibs/srvconf.lib/property_parsers.h b/stglibs/srvconf.lib/property_parsers.h new file mode 100644 index 00000000..3469b987 --- /dev/null +++ b/stglibs/srvconf.lib/property_parsers.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