From: Maxim Mamontov Date: Sun, 20 Oct 2013 10:02:37 +0000 (+0300) Subject: Added corporations management. X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/474a9dc37f5085103d11cff3afa8642538f06a10?ds=sidebyside;hp=--cc Added corporations management. --- 474a9dc37f5085103d11cff3afa8642538f06a10 diff --git a/stglibs/srvconf.lib/Makefile b/stglibs/srvconf.lib/Makefile index ff0da14c..d66d2b3e 100644 --- a/stglibs/srvconf.lib/Makefile +++ b/stglibs/srvconf.lib/Makefile @@ -24,6 +24,9 @@ SRCS = parsers/property.cpp \ parsers/get_services.cpp \ parsers/get_service.cpp \ parsers/chg_service.cpp \ + parsers/get_corporations.cpp \ + parsers/get_corp.cpp \ + parsers/chg_corp.cpp \ netunit.cpp \ servconf.cpp diff --git a/stglibs/srvconf.lib/include/stg/servconf.h b/stglibs/srvconf.lib/include/stg/servconf.h index b7884964..f2afda39 100644 --- a/stglibs/srvconf.lib/include/stg/servconf.h +++ b/stglibs/srvconf.lib/include/stg/servconf.h @@ -16,12 +16,7 @@ /* * Author : Boris Mikhailenko - */ - - /* - $Revision: 1.10 $ - $Date: 2009/03/17 09:52:35 $ - $Author: faust $ + * Author : Maxim Mamontov */ #ifndef __STG_STGLIBS_SERVCONF_H__ @@ -38,6 +33,7 @@ struct USER_CONF_RES; struct USER_STAT_RES; struct TARIFF_DATA_RES; struct SERVICE_CONF_RES; +struct CORP_CONF_RES; namespace STG { @@ -89,6 +85,14 @@ public: SIMPLE::CALLBACK f, void * data); int DelService(const std::string & name, SIMPLE::CALLBACK f, void * data); + int GetCorporations(GET_CORPORATIONS::CALLBACK f, void * data); + int GetCorp(const std::string & name, GET_CORP::CALLBACK f, void * data); + int ChgCorp(const CORP_CONF_RES & conf, SIMPLE::CALLBACK f, void * data); + int AddCorp(const std::string & name, + const CORP_CONF_RES & conf, + SIMPLE::CALLBACK f, void * data); + int DelCorp(const std::string & name, SIMPLE::CALLBACK f, void * data); + const std::string & GetStrError() const; private: diff --git a/stglibs/srvconf.lib/include/stg/servconf_types.h b/stglibs/srvconf.lib/include/stg/servconf_types.h index 6f454bc3..98add644 100644 --- a/stglibs/srvconf.lib/include/stg/servconf_types.h +++ b/stglibs/srvconf.lib/include/stg/servconf_types.h @@ -41,6 +41,7 @@ struct ADMIN_CONF; struct TARIFF_DATA; struct SERVICE_CONF; +struct CORP_CONF; namespace STG { @@ -207,6 +208,22 @@ typedef void (* CALLBACK)(bool result, const std::string & reason, const INFO & } +namespace GET_CORP +{ + +typedef CORP_CONF INFO; +typedef void (* CALLBACK)(bool result, const std::string & reason, const INFO & info, void * data); + +} + +namespace GET_CORPORATIONS +{ + +typedef std::vector INFO; +typedef void (* CALLBACK)(bool result, const std::string & reason, const INFO & info, void * data); + +} + } // namespace STG #endif diff --git a/stglibs/srvconf.lib/parsers/chg_corp.cpp b/stglibs/srvconf.lib/parsers/chg_corp.cpp new file mode 100644 index 00000000..e566e2ae --- /dev/null +++ b/stglibs/srvconf.lib/parsers/chg_corp.cpp @@ -0,0 +1,50 @@ +/* + * 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 "chg_corp.h" + +#include "stg/corp_conf.h" +#include "stg/common.h" + +#include + +using namespace STG; + +namespace +{ + +template +void appendResetable(std::ostream & stream, const std::string & name, const T & value) +{ +if (!value.empty()) + stream << "<" << name << " value=\"" << value.data() << "\"/>"; +} + +} // namespace anonymous + +std::string CHG_CORP::Serialize(const CORP_CONF_RES & conf) +{ +std::ostringstream stream; + +appendResetable(stream, "name", conf.name); +appendResetable(stream, "cash", conf.cash); + +return stream.str(); +} diff --git a/stglibs/srvconf.lib/parsers/chg_corp.h b/stglibs/srvconf.lib/parsers/chg_corp.h new file mode 100644 index 00000000..8b75fbd4 --- /dev/null +++ b/stglibs/srvconf.lib/parsers/chg_corp.h @@ -0,0 +1,42 @@ +/* + * 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_CHG_CORP_H__ +#define __STG_STGLIBS_SRVCONF_PARSER_CHG_CORP_H__ + +#include "base.h" + +#include "stg/servconf_types.h" + +#include + +struct CORP_CONF_RES; + +namespace STG +{ +namespace CHG_CORP +{ + +std::string Serialize(const CORP_CONF_RES & conf); + +} // namespace CHG_CORP +} // namespace STG + +#endif diff --git a/stglibs/srvconf.lib/parsers/get_corp.cpp b/stglibs/srvconf.lib/parsers/get_corp.cpp new file mode 100644 index 00000000..339d1eb2 --- /dev/null +++ b/stglibs/srvconf.lib/parsers/get_corp.cpp @@ -0,0 +1,97 @@ +/* + * 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 "get_corp.h" + +#include "parsers/property.h" + +#include "stg/common.h" + +#include + +using namespace STG; + +GET_CORP::PARSER::PARSER(CALLBACK f, void * d) + : callback(f), + data(d), + depth(0), + parsingAnswer(false) +{ + AddParser(propertyParsers, "name", info.name); + AddParser(propertyParsers, "cash", info.cash); +} +//----------------------------------------------------------------------------- +GET_CORP::PARSER::~PARSER() +{ + PROPERTY_PARSERS::iterator it(propertyParsers.begin()); + while (it != propertyParsers.end()) + delete (it++)->second; +} +//----------------------------------------------------------------------------- +int GET_CORP::PARSER::ParseStart(const char * el, const char ** attr) +{ +depth++; +if (depth == 1) + ParseCorp(el, attr); + +if (depth == 2 && parsingAnswer) + ParseCorpParams(el, attr); + +return 0; +} +//----------------------------------------------------------------------------- +void GET_CORP::PARSER::ParseEnd(const char * /*el*/) +{ +depth--; +if (depth == 0 && parsingAnswer) + { + if (callback) + callback(error.empty(), error, info, data); + error.clear(); + parsingAnswer = false; + } +} +//----------------------------------------------------------------------------- +void GET_CORP::PARSER::ParseCorp(const char * el, const char ** attr) +{ +if (strcasecmp(el, "corp") == 0) + { + if (attr && attr[0] && attr[1]) + { + if (strcasecmp(attr[1], "error") == 0) + { + if (attr[2] && attr[3]) + error = attr[3]; + else + error = "Corp not found."; + } + else + parsingAnswer = true; + } + else + parsingAnswer = true; + } +} +//----------------------------------------------------------------------------- +void GET_CORP::PARSER::ParseCorpParams(const char * el, const char ** attr) +{ +if (!TryParse(propertyParsers, ToLower(el), attr)) + error = "Invalid parameter."; +} diff --git a/stglibs/srvconf.lib/parsers/get_corp.h b/stglibs/srvconf.lib/parsers/get_corp.h new file mode 100644 index 00000000..80b1e943 --- /dev/null +++ b/stglibs/srvconf.lib/parsers/get_corp.h @@ -0,0 +1,61 @@ +/* + * 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_GET_CORP_H__ +#define __STG_STGLIBS_SRVCONF_PARSER_GET_CORP_H__ + +#include "base.h" +#include "property.h" + +#include "stg/corp_conf.h" +#include "stg/servconf_types.h" + +#include + +namespace STG +{ +namespace GET_CORP +{ + +class PARSER: public STG::PARSER +{ +public: + PARSER(CALLBACK f, void * data); + virtual ~PARSER(); + int ParseStart(const char * el, const char ** attr); + void ParseEnd(const char * el); + +private: + PROPERTY_PARSERS propertyParsers; + CALLBACK callback; + void * data; + INFO info; + int depth; + bool parsingAnswer; + std::string error; + + void ParseCorp(const char * el, const char ** attr); + void ParseCorpParams(const char * el, const char ** attr); +}; + +} // namespace GET_CORP +} // namespace STG + +#endif diff --git a/stglibs/srvconf.lib/parsers/get_corporations.cpp b/stglibs/srvconf.lib/parsers/get_corporations.cpp new file mode 100644 index 00000000..669e145e --- /dev/null +++ b/stglibs/srvconf.lib/parsers/get_corporations.cpp @@ -0,0 +1,78 @@ +/* + * 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 "get_corporations.h" + +#include "stg/corp_conf.h" + +#include + +using namespace STG; + +GET_CORPORATIONS::PARSER::PARSER(CALLBACK f, void * d) + : callback(f), + data(d), + corpParser(&GET_CORPORATIONS::PARSER::CorpCallback, this), + depth(0), + parsingAnswer(false) +{ +} +//----------------------------------------------------------------------------- +int GET_CORPORATIONS::PARSER::ParseStart(const char * el, const char ** attr) +{ +depth++; +if (depth == 1 && strcasecmp(el, "corporations") == 0) + parsingAnswer = true; + +if (depth > 1 && parsingAnswer) + corpParser.ParseStart(el, attr); + +return 0; +} +//----------------------------------------------------------------------------- +void GET_CORPORATIONS::PARSER::ParseEnd(const char * el) +{ +depth--; +if (depth > 0 && parsingAnswer) + corpParser.ParseEnd(el); + +if (depth == 0 && parsingAnswer) + { + if (callback) + callback(error.empty(), error, info, data); + error.clear(); + info.clear(); + parsingAnswer = false; + } +} +//----------------------------------------------------------------------------- +void GET_CORPORATIONS::PARSER::AddCorp(const GET_CORP::INFO & corpInfo) +{ +info.push_back(corpInfo); +} +//----------------------------------------------------------------------------- +void GET_CORPORATIONS::PARSER::CorpCallback(bool result, const std::string & error, const GET_CORP::INFO & info, void * data) +{ + GET_CORPORATIONS::PARSER * parser = static_cast(data); + if (!result) + parser->SetError(error); + else + parser->AddCorp(info); +} diff --git a/stglibs/srvconf.lib/parsers/get_corporations.h b/stglibs/srvconf.lib/parsers/get_corporations.h new file mode 100644 index 00000000..347d102c --- /dev/null +++ b/stglibs/srvconf.lib/parsers/get_corporations.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 : Maxim Mamontov + */ + +#ifndef __STG_STGLIBS_SRVCONF_PARSER_GET_CORPORATIONS_H__ +#define __STG_STGLIBS_SRVCONF_PARSER_GET_CORPORATIONS_H__ + +#include "base.h" +#include "get_corp.h" + +#include "stg/corp_conf.h" +#include "stg/servconf_types.h" + +#include + +namespace STG +{ +namespace GET_CORPORATIONS +{ + +class PARSER: public STG::PARSER +{ +public: + PARSER(CALLBACK f, void * data); + int ParseStart(const char * el, const char ** attr); + void ParseEnd(const char * el); + +private: + CALLBACK callback; + void * data; + GET_CORP::PARSER corpParser; + INFO info; + int depth; + bool parsingAnswer; + std::string error; + + void AddCorp(const GET_CORP::INFO & corpInfo); + void SetError(const std::string & e) { error = e; } + + static void CorpCallback(bool result, const std::string& reason, const GET_CORP::INFO & info, void * data); +}; + +} // namespace GET_CORPORATIONS +} // namespace STG + +#endif diff --git a/stglibs/srvconf.lib/servconf.cpp b/stglibs/srvconf.lib/servconf.cpp index 11e755ba..e80c3ffa 100644 --- a/stglibs/srvconf.lib/servconf.cpp +++ b/stglibs/srvconf.lib/servconf.cpp @@ -16,6 +16,7 @@ /* * Author : Boris Mikhailenko + * Author : Maxim Mamontov */ #include "stg/servconf.h" @@ -43,6 +44,10 @@ #include "parsers/get_service.h" #include "parsers/chg_service.h" +#include "parsers/get_corporations.h" +#include "parsers/get_corp.h" +#include "parsers/chg_corp.h" + #include "parsers/base.h" #include "stg/common.h" @@ -275,6 +280,38 @@ int SERVCONF::DelService(const std::string & name, SIMPLE::CALLBACK f, void * da return pImpl->Exec("DelService", "", f, data); } +// -- Corporations -- + +int SERVCONF::GetCorporations(GET_CORPORATIONS::CALLBACK f, void * data) +{ +return pImpl->Exec("", f, data); +} + +int SERVCONF::GetCorp(const std::string & name, GET_CORP::CALLBACK f, void * data) +{ +return pImpl->Exec("", f, data); +} + +int SERVCONF::ChgCorp(const CORP_CONF_RES & conf, SIMPLE::CALLBACK f, void * data) +{ +return pImpl->Exec("SetCorp", "" + CHG_CORP::Serialize(conf) + "", f, data); +} + +int SERVCONF::AddCorp(const std::string & name, + const CORP_CONF_RES & conf, + SIMPLE::CALLBACK f, void * data) +{ +int res = pImpl->Exec("AddCorp", "", f, data); +if (res != st_ok) + return res; +return pImpl->Exec("SetCorp", "" + CHG_CORP::Serialize(conf) + "", f, data); +} + +int SERVCONF::DelCorp(const std::string & name, SIMPLE::CALLBACK f, void * data) +{ +return pImpl->Exec("DelCorp", "", f, data); +} + const std::string & SERVCONF::GetStrError() const { return pImpl->GetStrError();