cost(), payDay()
{}
+SERVICE_CONF_RES(const SERVICE_CONF & rhs)
+ : name(rhs.name), comment(rhs.comment),
+ cost(rhs.cost), payDay(rhs.payDay)
+{}
+
SERVICE_CONF_RES & operator=(const SERVICE_CONF & conf)
{
name = conf.name;
return sc;
}
+void Splice(const SERVICE_CONF_RES & rhs)
+{
+name.splice(rhs.name);
+comment.splice(rhs.comment);
+cost.splice(rhs.cost);
+payDay.splice(rhs.payDay);
+}
+
RESETABLE<std::string> name;
RESETABLE<std::string> comment;
RESETABLE<double> cost;
virtual int Add(const SERVICE_CONF & service, const ADMIN * admin) = 0;
virtual int Del(const std::string & name, const ADMIN * admin) = 0;
virtual int Change(const SERVICE_CONF & service, const ADMIN * admin) = 0;
- virtual bool Find(const std::string & name, SERVICE_CONF * service) = 0;
+ virtual bool Find(const std::string & name, SERVICE_CONF * service) const = 0;
virtual bool Exists(const std::string & name) const = 0;
virtual const std::string & GetStrError() const = 0;
virtual size_t Count() const = 0;
./parser_tariffs.cpp \
./parser_admins.cpp \
./parser_users.cpp \
+ ./parser_services.cpp \
./parser_message.cpp \
./parser_auth_by.cpp \
./parser_user_info.cpp \
--- /dev/null
+/*
+ * 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 <faust@stargazer.dp.ua>
+ */
+
+#include "parser_services.h"
+
+#include "stg/services.h"
+
+#include <strings.h> // strcasecmp
+
+using STG::PARSER::GET_SERVICES;
+using STG::PARSER::GET_SERVICE;
+using STG::PARSER::ADD_SERVICE;
+using STG::PARSER::DEL_SERVICE;
+using STG::PARSER::CHG_SERVICE;
+
+const char * GET_SERVICES::tag = "GetServices";
+const char * GET_SERVICE::tag = "AddService";
+const char * ADD_SERVICE::tag = "AddService";
+const char * DEL_SERVICE::tag = "DelService";
+const char * CHG_SERVICE::tag = "ChgService";
+
+void GET_SERVICES::CreateAnswer()
+{
+ // TODO: no priviledges implemented yet
+ /*const PRIV * priv = m_currAdmin.GetPriv();
+ if (!priv->serviceChg)
+ {
+ m_answer = "<Error Result=\"Error. Access denied.\"/>";
+ return;
+ }*/
+
+ m_answer = "<Services>";
+ SERVICE_CONF conf;
+ int h = m_services.OpenSearch();
+ while (m_services.SearchNext(h, &conf) == 0)
+ {
+ m_answer += "<Service name=\"" + conf.name +
+ "\" comment=\"" + Encode12str(conf.comment) +
+ "\" cost=\"" + x2str(conf.cost) +
+ "\" payDay=\"" + x2str(conf.payDay) + "\"/>";
+ }
+ m_services.CloseSearch(h);
+ m_answer += "</Services>";
+}
+
+int GET_SERVICE::Start(void *, const char * el, const char ** attr)
+{
+ if (strcasecmp(el, m_tag.c_str()) == 0)
+ {
+ m_name = attr[1];
+ return 0;
+ }
+ return -1;
+}
+
+void GET_SERVICE::CreateAnswer()
+{
+ // TODO: no priviledges implemented yet
+ /*const PRIV * priv = m_currAdmin.GetPriv();
+ if (!priv->serviceChg)
+ {
+ m_answer = "<Error Result=\"Error. Access denied.\"/>";
+ return;
+ }*/
+
+ SERVICE_CONF conf;
+ if (!m_services.Find(m_name, &conf))
+ m_answer = "<Error result=\"Service '" + m_name + "' does not exist.\"/>";
+ else
+ m_answer += "<" + m_tag + " name=\"" + conf.name +
+ "\" comment=\"" + Encode12str(conf.comment) +
+ "\" cost=\"" + x2str(conf.cost) +
+ "\" payDay=\"" + x2str(conf.payDay) + "\"/>";
+}
+
+int ADD_SERVICE::Start(void *, const char * el, const char ** attr)
+{
+ if (strcasecmp(el, m_tag.c_str()) == 0)
+ {
+ m_name = attr[1];
+ return 0;
+ }
+ return -1;
+}
+
+void ADD_SERVICE::CreateAnswer()
+{
+ SERVICE_CONF conf(m_name);
+ if (m_services.Add(conf, &m_currAdmin) == 0)
+ m_answer = "<" + m_tag + " result=\"Ok\"/>";
+ else
+ m_answer = "<" + m_tag + " result=\"Error. " + m_services.GetStrError() + "\"/>";
+}
+
+int DEL_SERVICE::Start(void *, const char * el, const char ** attr)
+{
+ if (strcasecmp(el, m_tag.c_str()) == 0)
+ {
+ m_name = attr[1];
+ return 0;
+ }
+ return -1;
+}
+
+void DEL_SERVICE::CreateAnswer()
+{
+ if (m_services.Del(m_name, &m_currAdmin) == 0)
+ m_answer = "<" + m_tag + " result=\"Ok\"/>";
+ else
+ m_answer = "<" + m_tag + " result=\"Error. " + m_services.GetStrError() + "\"/>";
+}
+
+int CHG_SERVICE::Start(void *, const char * el, const char ** attr)
+{
+ if (strcasecmp(el, m_tag.c_str()) == 0)
+ {
+ for (size_t i = 0; i < 8; i += 2)
+ {
+ printfd(__FILE__, "PARSER_CHG_SERVICE::attr[%d] = %s\n", i, attr[i]);
+ if (attr[i] == NULL)
+ break;
+
+ if (strcasecmp(attr[i], "name") == 0)
+ {
+ m_service.name = attr[i + 1];
+ continue;
+ }
+
+ if (strcasecmp(attr[i], "comment") == 0)
+ {
+ m_service.comment = Decode21str(attr[i + 1]);
+ continue;
+ }
+
+ if (strcasecmp(attr[i], "cost") == 0)
+ {
+ double cost = 0;
+ if (str2x(attr[i + 1], cost) == 0)
+ m_service.cost = cost;
+ // TODO: log it
+ continue;
+ }
+
+ if (strcasecmp(attr[i], "payDay") == 0)
+ {
+ unsigned payDay;
+ if (str2x(attr[i + 1], payDay) == 0)
+ m_service.payDay = payDay;
+ // TODO: log it
+ continue;
+ }
+ }
+
+ return 0;
+ }
+ return -1;
+}
+
+void CHG_SERVICE::CreateAnswer()
+{
+ if (m_service.name.empty())
+ {
+ m_answer = "<" + m_tag + " result=\"Empty service name.\"/>";
+ return;
+ }
+
+ if (!m_services.Exists(m_service.name.const_data()))
+ {
+ m_answer = "<" + m_tag + " result = \"Service '" + m_service.name.const_data() + "' does not exist.\"/>";
+ return;
+ }
+
+ SERVICE_CONF orig;
+ m_services.Find(m_service.name.const_data(), &orig);
+
+ m_service.Splice(orig);
+
+ if (m_services.Change(m_service.GetData(), &m_currAdmin) != 0)
+ m_answer = "<" + m_tag + " result = \"" + m_services.GetStrError() + "\"/>";
+ else
+ m_answer = "<" + m_tag + " result = \"Ok\"/>";
+}
--- /dev/null
+/*
+ * 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 <faust@stargazer.dp.ua>
+ */
+
+#ifndef __STG_SGCONFIG_PARSER_SERVICES_H__
+#define __STG_SGCONFIG_PARSER_SERVICES_H__
+
+#include "parser.h"
+
+#include "stg/service_conf.h"
+
+#include "stg/common.h"
+
+#include <string>
+
+class SERVICES;
+
+namespace STG
+{
+namespace PARSER
+{
+
+class GET_SERVICES: public BASE_PARSER
+{
+ public:
+ class FACTORY : public BASE_PARSER::FACTORY
+ {
+ public:
+ FACTORY(const SERVICES & services) : m_services(services) {}
+ virtual BASE_PARSER * create(const ADMIN & admin) { return new GET_SERVICES(admin, m_services); }
+ static void Register(REGISTRY & registry, const SERVICES & services)
+ { registry[ToLower(tag)] = new FACTORY(services); }
+ private:
+ const SERVICES & m_services;
+ };
+
+ static const char * tag;
+
+ GET_SERVICES(const ADMIN & admin, const SERVICES & services)
+ : BASE_PARSER(admin, tag), m_services(services) {}
+
+ private:
+ const SERVICES & m_services;
+
+ void CreateAnswer();
+};
+
+class GET_SERVICE: public BASE_PARSER
+{
+ public:
+ class FACTORY : public BASE_PARSER::FACTORY
+ {
+ public:
+ FACTORY(const SERVICES & services) : m_services(services) {}
+ virtual BASE_PARSER * create(const ADMIN & admin) { return new GET_SERVICE(admin, m_services); }
+ static void Register(REGISTRY & registry, SERVICES & services)
+ { registry[ToLower(tag)] = new FACTORY(services); }
+ private:
+ const SERVICES & m_services;
+ };
+
+ static const char * tag;
+
+ GET_SERVICE(const ADMIN & admin, const SERVICES & services)
+ : BASE_PARSER(admin, tag), m_services(services) {}
+ int Start(void * data, const char * el, const char ** attr);
+
+ private:
+ std::string m_name;
+ const SERVICES & m_services;
+
+ void CreateAnswer();
+};
+
+class ADD_SERVICE: public BASE_PARSER
+{
+ public:
+ class FACTORY : public BASE_PARSER::FACTORY
+ {
+ public:
+ FACTORY(SERVICES & services) : m_services(services) {}
+ virtual BASE_PARSER * create(const ADMIN & admin) { return new ADD_SERVICE(admin, m_services); }
+ static void Register(REGISTRY & registry, SERVICES & services)
+ { registry[ToLower(tag)] = new FACTORY(services); }
+ private:
+ SERVICES & m_services;
+ };
+
+ static const char * tag;
+
+ ADD_SERVICE(const ADMIN & admin, SERVICES & services)
+ : BASE_PARSER(admin, tag), m_services(services) {}
+ int Start(void * data, const char * el, const char ** attr);
+
+ private:
+ std::string m_name;
+ SERVICES & m_services;
+
+ void CreateAnswer();
+};
+
+class DEL_SERVICE: public BASE_PARSER
+{
+ public:
+ class FACTORY : public BASE_PARSER::FACTORY
+ {
+ public:
+ FACTORY(SERVICES & services) : m_services(services) {}
+ virtual BASE_PARSER * create(const ADMIN & admin) { return new DEL_SERVICE(admin, m_services); }
+ static void Register(REGISTRY & registry, SERVICES & services)
+ { registry[ToLower(tag)] = new FACTORY(services); }
+ private:
+ SERVICES & m_services;
+ };
+
+ static const char * tag;
+
+ DEL_SERVICE(const ADMIN & admin, SERVICES & services)
+ : BASE_PARSER(admin, tag), m_services(services) {}
+ int Start(void * data, const char * el, const char ** attr);
+
+ private:
+ std::string m_name;
+ SERVICES & m_services;
+
+ void CreateAnswer();
+};
+
+class CHG_SERVICE: public BASE_PARSER
+{
+ public:
+ class FACTORY : public BASE_PARSER::FACTORY
+ {
+ public:
+ FACTORY(SERVICES & services) : m_services(services) {}
+ virtual BASE_PARSER * create(const ADMIN & admin) { return new CHG_SERVICE(admin, m_services); }
+ static void Register(REGISTRY & registry, SERVICES & services)
+ { registry[ToLower(tag)] = new FACTORY(services); }
+ private:
+ SERVICES & m_services;
+ };
+
+ static const char * tag;
+
+ CHG_SERVICE(const ADMIN & admin, SERVICES & services)
+ : BASE_PARSER(admin, tag), m_services(services) {}
+ int Start(void * data, const char * el, const char ** attr);
+
+ private:
+ SERVICE_CONF_RES m_service;
+ SERVICES & m_services;
+
+ void CreateAnswer();
+};
+
+} // namespace PARSER
+} // namespace STG
+
+#endif
return false;
}
//-----------------------------------------------------------------------------
-bool SERVICES_IMPL::Find(const std::string & name, SERVICE_CONF * service)
+bool SERVICES_IMPL::Find(const std::string & name, SERVICE_CONF * service) const
{
assert(service != NULL && "Pointer to service is not null");
if (data.empty())
return false;
-srv_iter si(find(data.begin(), data.end(), SERVICE_CONF(name)));
+const_srv_iter si(find(data.begin(), data.end(), SERVICE_CONF(name)));
if (si != data.end())
{
int Add(const SERVICE_CONF & service, const ADMIN * admin);
int Del(const std::string & name, const ADMIN * admin);
int Change(const SERVICE_CONF & service, const ADMIN * admin);
- bool Find(const std::string & name, SERVICE_CONF * service);
+ bool Find(const std::string & name, SERVICE_CONF * service) const;
bool Exists(const std::string & name) const;
const std::string & GetStrError() const { return strError; }