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;
40 const double pt_mega = 1024 * 1024;
42 template <typename A, typename C, typename F>
43 std::string AOS2String(const A & array, size_t size, const F C::* field, F multiplier)
46 for (size_t i = 0; i < size; ++i)
50 res += x2str((array[i].*field) * multiplier);
55 template <typename A, typename C, typename F>
56 bool String2AOS(const std::string & source, A & array, size_t size, RESETABLE<F> C::* field, F divisor)
59 std::string::size_type from = 0;
60 std::string::size_type pos = 0;
61 while (index < size && (pos = source.find('/', from)) != std::string::npos)
63 if (str2x(source.substr(from, pos - from), (array[index].*field).data()))
65 (array[index].*field).data() /= divisor;
69 if (str2x(source.substr(from), (array[index].*field).data()))
71 (array[index].*field).data() /= divisor;
77 void GET_TARIFFS::CreateAnswer()
79 answer = GetOpenTag();
81 std::list<TARIFF_DATA> dataList;
82 m_tariffs.GetTariffsData(&dataList);
83 std::list<TARIFF_DATA>::const_iterator it = dataList.begin();
84 for (; it != dataList.end(); ++it)
86 answer += "<tariff name=\"" + it->tariffConf.name + "\">";
88 for (size_t i = 0; i < DIR_NUM; i++)
89 answer += "<Time" + x2str(i) + " value=\"" +
90 x2str(it->dirPrice[i].hDay) + ":" + x2str(it->dirPrice[i].mDay) + "-" +
91 x2str(it->dirPrice[i].hNight) + ":" + x2str(it->dirPrice[i].mNight) + "\"/>";
93 answer += "<PriceDayA value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::priceDayA, pt_mega) + "\"/>" +
94 "<PriceDayB value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::priceDayB, pt_mega) + "\"/>" +
95 "<PriceNightA value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::priceNightA, pt_mega) + "\"/>" +
96 "<PriceNightB value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::priceNightB, pt_mega) + "\"/>" +
97 "<Threshold value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::threshold, 1) + "\"/>" +
98 "<SinglePrice value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::singlePrice, 1) + "\"/>" +
99 "<NoDiscount value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::noDiscount, 1) + "\"/>" +
100 "<Fee value=\"" + x2str(it->tariffConf.fee) + "\"/>" +
101 "<PassiveCost value=\"" + x2str(it->tariffConf.passiveCost) + "\"/>" +
102 "<Free value=\"" + x2str(it->tariffConf.free) + "\"/>" +
103 "<TraffType value=\"" + TARIFF::TraffTypeToString(it->tariffConf.traffType) + "\"/>" +
104 "<Period value=\"" + TARIFF::PeriodToString(it->tariffConf.period) + "\"/>" +
108 answer += GetCloseTag();
111 int ADD_TARIFF::Start(void *, const char * el, const char ** attr)
113 if (strcasecmp(el, tag.c_str()) != 0)
123 void ADD_TARIFF::CreateAnswer()
125 if (m_tariffs.Add(tariff, &currAdmin) == 0)
126 answer = "<" + tag + " Result=\"Ok\"/>";
128 answer = "<" + tag + " Result=\"Error. " + m_tariffs.GetStrError() + "\"/>";
131 int DEL_TARIFF::Start(void *, const char * el, const char ** attr)
133 if (strcasecmp(el, tag.c_str()) != 0)
143 void DEL_TARIFF::CreateAnswer()
145 if (m_users.TariffInUse(tariff))
146 answer = "<" + tag + " Result=\"Error. Tariff \'" + tariff + "\' cannot be deleted, it is in use.\"/>";
147 else if (m_tariffs.Del(tariff, &currAdmin) == 0)
148 answer = "<" + tag + " Result=\"Ok\"/>";
150 answer = "<" + tag + " Result=\"Error. " + m_tariffs.GetStrError() + "\"/>";
153 int CHG_TARIFF::Start(void *, const char * el, const char ** attr)
159 if (strcasecmp(el, tag.c_str()) == 0)
161 td.tariffConf.name = attr[1];
167 if (strcasecmp(el, "PriceDayA") == 0)
169 if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::priceDayA, pt_mega))
170 return -1; // TODO: log it
175 if (strcasecmp(el, "PriceDayB") == 0)
177 if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::priceDayB, pt_mega))
178 return -1; // TODO: log it
183 if (strcasecmp(el, "PriceNightA") == 0)
185 if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::priceNightA, pt_mega))
186 return -1; // TODO: log it
191 if (strcasecmp(el, "PriceNightB") == 0)
193 if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::priceNightB, pt_mega))
194 return -1; // TODO: log it
199 if (strcasecmp(el, "Threshold") == 0)
201 if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::threshold, 1))
202 return -1; // TODO: log it
207 if (strcasecmp(el, "SinglePrice") == 0)
209 if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::singlePrice, 1))
210 return -1; // TODO: log it
215 if (strcasecmp(el, "NoDiscount") == 0)
217 if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::noDiscount, 1))
218 return -1; // TODO: log it
223 for (int j = 0; j < DIR_NUM; j++)
226 snprintf(st, 50, "Time%d", j);
227 if (strcasecmp(el, st) == 0)
233 if (ParseTariffTimeStr(attr[1], h1, m1, h2, m2) == 0)
235 td.dirPrice[j].hDay = h1;
236 td.dirPrice[j].mDay = m1;
237 td.dirPrice[j].hNight = h2;
238 td.dirPrice[j].mNight = m2;
244 if (strcasecmp(el, "Fee") == 0)
247 if (strtodouble2(attr[1], fee) == 0)
248 td.tariffConf.fee = fee;
252 if (strcasecmp(el, "PassiveCost") == 0)
255 if (strtodouble2(attr[1], pc) == 0)
256 td.tariffConf.passiveCost = pc;
260 if (strcasecmp(el, "Free") == 0)
263 if (strtodouble2(attr[1], free) == 0)
264 td.tariffConf.free = free;
268 if (strcasecmp(el, "TraffType") == 0)
270 td.tariffConf.traffType = TARIFF::StringToTraffType(attr[1]);
274 if (strcasecmp(el, "Period") == 0)
276 td.tariffConf.period = TARIFF::StringToPeriod(attr[1]);
283 int CHG_TARIFF::End(void *, const char * el)
287 if (strcasecmp(el, tag.c_str()) != 0)
296 void CHG_TARIFF::CreateAnswer()
298 if (!td.tariffConf.name.data().empty())
300 TARIFF_DATA tariffData = td.GetData();
301 if (m_tariffs.Chg(tariffData, &currAdmin) == 0)
302 answer = "<" + tag + " Result=\"ok\"/>";
304 answer = "<" + tag + " Result=\"Change tariff error! " + m_tariffs.GetStrError() + "\"/>";
307 answer = "<" + tag + " Result=\"Change tariff error!\"/>";