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 : Boris Mikhailenko <stg34@stargazer.dp.ua>
19 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
22 #include "parser_tariffs.h"
24 #include "stg/tariffs.h"
25 #include "stg/users.h"
26 #include "stg/common.h"
27 #include "stg/resetable.h"
29 #include <cstdio> // snprintf
32 using STG::PARSER::GET_TARIFFS;
33 using STG::PARSER::ADD_TARIFF;
34 using STG::PARSER::DEL_TARIFF;
35 using STG::PARSER::CHG_TARIFF;
37 const char * GET_TARIFFS::tag = "GetTariffs";
38 const char * ADD_TARIFF::tag = "AddTariff";
39 const char * DEL_TARIFF::tag = "DelTariff";
40 const char * CHG_TARIFF::tag = "SetTariff";
45 const double pt_mega = 1024 * 1024;
47 template <typename A, typename C, typename F>
48 std::string AOS2String(const A & array, size_t size, const F C::* field, F multiplier)
51 for (size_t i = 0; i < size; ++i)
55 res += x2str((array[i].*field) * multiplier);
60 template <typename A, typename C, typename F>
61 bool String2AOS(const std::string & source, A & array, size_t size, RESETABLE<F> C::* field, F divisor)
64 std::string::size_type from = 0;
65 std::string::size_type pos = 0;
66 while (index < size && (pos = source.find('/', from)) != std::string::npos)
68 if (str2x(source.substr(from, pos - from), (array[index].*field).data()))
70 (array[index].*field).data() /= divisor;
74 if (str2x(source.substr(from), (array[index].*field).data()))
76 (array[index].*field).data() /= divisor;
82 void GET_TARIFFS::CreateAnswer()
84 m_answer = GetOpenTag();
86 std::list<TARIFF_DATA> dataList;
87 m_tariffs.GetTariffsData(&dataList);
88 std::list<TARIFF_DATA>::const_iterator it = dataList.begin();
89 for (; it != dataList.end(); ++it)
91 m_answer += "<tariff name=\"" + it->tariffConf.name + "\">";
93 for (size_t i = 0; i < DIR_NUM; i++)
94 m_answer += "<Time" + x2str(i) + " value=\"" +
95 x2str(it->dirPrice[i].hDay) + ":" + x2str(it->dirPrice[i].mDay) + "-" +
96 x2str(it->dirPrice[i].hNight) + ":" + x2str(it->dirPrice[i].mNight) + "\"/>";
98 m_answer += "<PriceDayA value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::priceDayA, pt_mega) + "\"/>" +
99 "<PriceDayB value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::priceDayB, pt_mega) + "\"/>" +
100 "<PriceNightA value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::priceNightA, pt_mega) + "\"/>" +
101 "<PriceNightB value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::priceNightB, pt_mega) + "\"/>" +
102 "<Threshold value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::threshold, 1) + "\"/>" +
103 "<SinglePrice value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::singlePrice, 1) + "\"/>" +
104 "<NoDiscount value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::noDiscount, 1) + "\"/>" +
105 "<Fee value=\"" + x2str(it->tariffConf.fee) + "\"/>" +
106 "<PassiveCost value=\"" + x2str(it->tariffConf.passiveCost) + "\"/>" +
107 "<Free value=\"" + x2str(it->tariffConf.free) + "\"/>" +
108 "<TraffType value=\"" + TARIFF::TraffTypeToString(it->tariffConf.traffType) + "\"/>" +
109 "<Period value=\"" + TARIFF::PeriodToString(it->tariffConf.period) + "\"/>" +
113 m_answer += GetCloseTag();
116 int ADD_TARIFF::Start(void *, const char * el, const char ** attr)
118 if (strcasecmp(el, m_tag.c_str()) != 0)
128 void ADD_TARIFF::CreateAnswer()
130 if (m_tariffs.Add(tariff, &m_currAdmin) == 0)
131 m_answer = "<" + m_tag + " Result=\"Ok\"/>";
133 m_answer = "<" + m_tag + " Result=\"Error. " + m_tariffs.GetStrError() + "\"/>";
136 int DEL_TARIFF::Start(void *, const char * el, const char ** attr)
138 if (strcasecmp(el, m_tag.c_str()) != 0)
148 void DEL_TARIFF::CreateAnswer()
150 if (m_users.TariffInUse(tariff))
151 m_answer = "<" + m_tag + " Result=\"Error. Tariff \'" + tariff + "\' cannot be deleted, it is in use.\"/>";
152 else if (m_tariffs.Del(tariff, &m_currAdmin) == 0)
153 m_answer = "<" + m_tag + " Result=\"Ok\"/>";
155 m_answer = "<" + m_tag + " Result=\"Error. " + m_tariffs.GetStrError() + "\"/>";
158 int CHG_TARIFF::Start(void *, const char * el, const char ** attr)
164 if (strcasecmp(el, m_tag.c_str()) == 0)
166 td.tariffConf.name = attr[1];
172 if (strcasecmp(el, "PriceDayA") == 0)
174 if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::priceDayA, pt_mega))
175 return -1; // TODO: log it
180 if (strcasecmp(el, "PriceDayB") == 0)
182 if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::priceDayB, pt_mega))
183 return -1; // TODO: log it
188 if (strcasecmp(el, "PriceNightA") == 0)
190 if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::priceNightA, pt_mega))
191 return -1; // TODO: log it
196 if (strcasecmp(el, "PriceNightB") == 0)
198 if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::priceNightB, pt_mega))
199 return -1; // TODO: log it
204 if (strcasecmp(el, "Threshold") == 0)
206 if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::threshold, 1))
207 return -1; // TODO: log it
212 if (strcasecmp(el, "SinglePrice") == 0)
214 if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::singlePrice, 1))
215 return -1; // TODO: log it
220 if (strcasecmp(el, "NoDiscount") == 0)
222 if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::noDiscount, 1))
223 return -1; // TODO: log it
228 for (int j = 0; j < DIR_NUM; j++)
231 snprintf(st, 50, "Time%d", j);
232 if (strcasecmp(el, st) == 0)
238 if (ParseTariffTimeStr(attr[1], h1, m1, h2, m2) == 0)
240 td.dirPrice[j].hDay = h1;
241 td.dirPrice[j].mDay = m1;
242 td.dirPrice[j].hNight = h2;
243 td.dirPrice[j].mNight = m2;
249 if (strcasecmp(el, "Fee") == 0)
252 if (strtodouble2(attr[1], fee) == 0)
253 td.tariffConf.fee = fee;
257 if (strcasecmp(el, "PassiveCost") == 0)
260 if (strtodouble2(attr[1], pc) == 0)
261 td.tariffConf.passiveCost = pc;
265 if (strcasecmp(el, "Free") == 0)
268 if (strtodouble2(attr[1], free) == 0)
269 td.tariffConf.free = free;
273 if (strcasecmp(el, "TraffType") == 0)
275 td.tariffConf.traffType = TARIFF::StringToTraffType(attr[1]);
279 if (strcasecmp(el, "Period") == 0)
281 td.tariffConf.period = TARIFF::StringToPeriod(attr[1]);
288 void CHG_TARIFF::CreateAnswer()
290 if (!td.tariffConf.name.data().empty())
292 TARIFF_DATA tariffData = td.GetData();
293 if (m_tariffs.Chg(tariffData, &m_currAdmin) == 0)
294 m_answer = "<" + m_tag + " Result=\"ok\"/>";
296 m_answer = "<" + m_tag + " Result=\"Change tariff error! " + m_tariffs.GetStrError() + "\"/>";
299 m_answer = "<" + m_tag + " Result=\"Change tariff error!\"/>";