2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
21 #include "parser_services.h"
23 #include "stg/services.h"
25 #include <strings.h> // strcasecmp
27 using STG::PARSER::GET_SERVICES;
28 using STG::PARSER::GET_SERVICE;
29 using STG::PARSER::ADD_SERVICE;
30 using STG::PARSER::DEL_SERVICE;
31 using STG::PARSER::CHG_SERVICE;
33 const char * GET_SERVICES::tag = "GetServices";
34 const char * GET_SERVICE::tag = "AddService";
35 const char * ADD_SERVICE::tag = "AddService";
36 const char * DEL_SERVICE::tag = "DelService";
37 const char * CHG_SERVICE::tag = "SetService";
39 void GET_SERVICES::CreateAnswer()
41 // TODO: no priviledges implemented yet
42 /*const PRIV * priv = m_currAdmin.GetPriv();
43 if (!priv->serviceChg)
45 m_answer = "<Error Result=\"Error. Access denied.\"/>";
49 m_answer = "<Services>";
51 int h = m_services.OpenSearch();
52 while (m_services.SearchNext(h, &conf) == 0)
54 m_answer += "<Service name=\"" + conf.name +
55 "\" comment=\"" + Encode12str(conf.comment) +
56 "\" cost=\"" + std::to_string(conf.cost) +
57 "\" payDay=\"" + std::to_string(conf.payDay) + "\"/>";
59 m_services.CloseSearch(h);
60 m_answer += "</Services>";
63 int GET_SERVICE::Start(void *, const char * el, const char ** attr)
65 if (strcasecmp(el, m_tag.c_str()) == 0)
73 void GET_SERVICE::CreateAnswer()
75 // TODO: no priviledges implemented yet
76 /*const PRIV * priv = m_currAdmin.GetPriv();
77 if (!priv->serviceChg)
79 m_answer = "<Error Result=\"Error. Access denied.\"/>";
84 if (!m_services.Find(m_name, &conf))
85 m_answer = "<Error result=\"Service '" + m_name + "' does not exist.\"/>";
87 m_answer += "<" + m_tag + " name=\"" + conf.name +
88 "\" comment=\"" + Encode12str(conf.comment) +
89 "\" cost=\"" + std::to_string(conf.cost) +
90 "\" payDay=\"" + std::to_string(conf.payDay) + "\"/>";
93 int ADD_SERVICE::Start(void *, const char * el, const char ** attr)
95 if (strcasecmp(el, m_tag.c_str()) == 0)
103 void ADD_SERVICE::CreateAnswer()
105 ServiceConf conf(m_name);
106 if (m_services.Add(conf, &m_currAdmin) == 0)
107 m_answer = "<" + m_tag + " result=\"Ok\"/>";
109 m_answer = "<" + m_tag + " result=\"" + m_services.GetStrError() + "\"/>";
112 int DEL_SERVICE::Start(void *, const char * el, const char ** attr)
114 if (strcasecmp(el, m_tag.c_str()) == 0)
122 void DEL_SERVICE::CreateAnswer()
124 if (m_services.Del(m_name, &m_currAdmin) == 0)
125 m_answer = "<" + m_tag + " result=\"Ok\"/>";
127 m_answer = "<" + m_tag + " result=\"" + m_services.GetStrError() + "\"/>";
130 int CHG_SERVICE::Start(void *, const char * el, const char ** attr)
132 if (strcasecmp(el, m_tag.c_str()) == 0)
134 for (size_t i = 0; i < 8; i += 2)
139 if (strcasecmp(attr[i], "name") == 0)
141 m_service.name = attr[i + 1];
145 if (strcasecmp(attr[i], "comment") == 0)
147 m_service.comment = Decode21str(attr[i + 1]);
151 if (strcasecmp(attr[i], "cost") == 0)
154 if (str2x(attr[i + 1], cost) == 0)
155 m_service.cost = cost;
157 printfd(__FILE__, "Bad cast from '%s' to double\n", attr[i + 1]);
162 if (strcasecmp(attr[i], "payDay") == 0)
165 if (str2x(attr[i + 1], payDay) == 0)
166 m_service.payDay = payDay;
168 printfd(__FILE__, "Bad cast from '%s' to unsigned\n", attr[i + 1]);
179 void CHG_SERVICE::CreateAnswer()
181 if (m_service.name.empty())
183 m_answer = "<" + m_tag + " result=\"Empty service name.\"/>";
187 if (!m_services.Exists(m_service.name.const_data()))
189 m_answer = "<" + m_tag + " result = \"Service '" + m_service.name.const_data() + "' does not exist.\"/>";
194 m_services.Find(m_service.name.const_data(), &orig);
196 ServiceConfOpt conf(orig);
197 conf.splice(m_service);
199 if (m_services.Change(conf.get({}), &m_currAdmin) != 0)
200 m_answer = "<" + m_tag + " result = \"" + m_services.GetStrError() + "\"/>";
202 m_answer = "<" + m_tag + " result = \"Ok\"/>";