1 #include <cstdio> // snprintf
6 const int pt_mega = 1024 * 1024;
7 //-----------------------------------------------------------------------------
9 //-----------------------------------------------------------------------------
10 int PARSER_GET_TARIFFS::ParseStart(void *, const char * el, const char **)
12 if (strcasecmp(el, "GetTariffs") == 0)
18 //-----------------------------------------------------------------------------
19 int PARSER_GET_TARIFFS::ParseEnd(void *, const char * el)
21 if (strcasecmp(el, "GetTariffs") == 0)
28 //-----------------------------------------------------------------------------
29 void PARSER_GET_TARIFFS::CreateAnswer()
35 answerList->erase(answerList->begin(), answerList->end());
37 answerList->push_back("<Tariffs>");
39 std::list<TARIFF_DATA> dataList;
40 tariffs->GetTariffsData(&dataList);
41 std::list<TARIFF_DATA>::const_iterator it = dataList.begin();
42 for (; it != dataList.end(); ++it)
44 s = "<tariff name=\"" + it->tariffConf.name + "\">";
45 answerList->push_back(s);
47 for (int j = 0; j < DIR_NUM; j++)
49 hd = it->dirPrice[j].hDay;
50 md = it->dirPrice[j].mDay;
52 hn = it->dirPrice[j].hNight;
53 mn = it->dirPrice[j].mNight;
55 strprintf(&s, "<Time%d value=\"%d:%d-%d:%d\"/>", j, hd, md, hn, mn);
56 answerList->push_back(s);
59 strprintf(&s, " <PriceDayA value=\"");
60 for (int i = 0; i < DIR_NUM; i++)
62 snprintf(vs, 100, "%.5f%s", it->dirPrice[i].priceDayA * pt_mega, i+1 == DIR_NUM?"":"/");
66 answerList->push_back(s);
68 strprintf(&s, " <PriceDayB value=\"");
69 for (int i = 0; i < DIR_NUM; i++)
71 snprintf(vs, 100, "%.5f%s", it->dirPrice[i].priceDayB * pt_mega, i+1 == DIR_NUM?"":"/");
75 answerList->push_back(s);
77 strprintf(&s, " <PriceNightA value=\"");
78 for (int i = 0; i < DIR_NUM; i++)
80 snprintf(vs, 100, "%.5f%s", it->dirPrice[i].priceNightA * pt_mega, i+1 == DIR_NUM?"":"/");
84 answerList->push_back(s);
86 strprintf(&s, " <PriceNightB value=\"");
87 for (int i = 0; i < DIR_NUM; i++)
89 snprintf(vs, 100, "%.5f%s", it->dirPrice[i].priceNightB * pt_mega, i+1 == DIR_NUM?"":"/");
93 answerList->push_back(s);
95 strprintf(&s, " <Threshold value=\"");
96 for (int i = 0; i < DIR_NUM; i++)
98 snprintf(vs, 100, "%d%s", it->dirPrice[i].threshold, i+1 == DIR_NUM?"":"/");
102 answerList->push_back(s);
104 strprintf(&s, " <SinglePrice value=\"");
105 for (int i = 0; i < DIR_NUM; i++)
107 snprintf(vs, 100, "%d%s", it->dirPrice[i].singlePrice, i+1 == DIR_NUM?"":"/");
111 answerList->push_back(s);
113 strprintf(&s, " <NoDiscount value=\"");
114 for (int i = 0; i < DIR_NUM; i++)
116 snprintf(vs, 100, "%d%s", it->dirPrice[i].noDiscount, i+1 == DIR_NUM?"":"/");
120 answerList->push_back(s);
122 strprintf(&s, " <Fee value=\"%.5f\"/>", it->tariffConf.fee);
123 answerList->push_back(s);
125 strprintf(&s, " <PassiveCost value=\"%.5f\"/>", it->tariffConf.passiveCost);
126 answerList->push_back(s);
128 strprintf(&s, " <Free value=\"%.5f\"/>", it->tariffConf.free);
129 answerList->push_back(s);
131 switch (it->tariffConf.traffType)
134 answerList->push_back("<TraffType value=\"up\"/>");
137 answerList->push_back("<TraffType value=\"down\"/>");
140 answerList->push_back("<TraffType value=\"up+down\"/>");
143 answerList->push_back("<TraffType value=\"max\"/>");
147 answerList->push_back("</tariff>");
149 answerList->push_back("</Tariffs>");
151 //-----------------------------------------------------------------------------
153 //-----------------------------------------------------------------------------
154 int PARSER_ADD_TARIFF::ParseStart(void *, const char * el, const char ** attr)
156 if (strcasecmp(el, "AddTariff") == 0)
160 tariffToAdd = attr[1];
166 //-----------------------------------------------------------------------------
167 int PARSER_ADD_TARIFF::ParseEnd(void *, const char * el)
169 if (strcasecmp(el, "AddTariff") == 0)
176 //-----------------------------------------------------------------------------
177 void PARSER_ADD_TARIFF::CreateAnswer()
179 //answerList->clear();
180 answerList->erase(answerList->begin(), answerList->end());
182 if (tariffs->Add(tariffToAdd, currAdmin) == 0)
184 answerList->push_back("<AddTariff Result=\"Ok\"/>");
189 strprintf(&s, "<AddTariff Result=\"Error. %s\"/>", tariffs->GetStrError().c_str());
190 answerList->push_back(s);
193 //-----------------------------------------------------------------------------
195 //-----------------------------------------------------------------------------
196 int PARSER_DEL_TARIFF::ParseStart(void *, const char * el, const char ** attr)
199 if (strcasecmp(el, "DelTariff") == 0)
201 tariffToDel = attr[1];
206 //-----------------------------------------------------------------------------
207 int PARSER_DEL_TARIFF::ParseEnd(void *, const char * el)
209 if (strcasecmp(el, "DelTariff") == 0)
216 //-----------------------------------------------------------------------------
217 void PARSER_DEL_TARIFF::CreateAnswer()
219 //answerList->clear();
220 answerList->erase(answerList->begin(), answerList->end());
222 if (users->TariffInUse(tariffToDel))
225 strprintf(&s, "<DelTariff Result=\"Error. Tariff \'%s\' cannot be deleted. Tariff in use.\"/>", tariffToDel.c_str());
226 answerList->push_back(s);
230 if (tariffs->Del(tariffToDel, currAdmin) == 0)
232 answerList->push_back("<DelTariff Result=\"Ok\"/>");
237 strprintf(&s, "<DelTariff Result=\"Error. %s\"/>", tariffs->GetStrError().c_str());
238 answerList->push_back(s);
241 //-----------------------------------------------------------------------------
242 //-----------------------------------------------------------------------------
244 //-----------------------------------------------------------------------------
245 //-----------------------------------------------------------------------------
246 int PARSER_CHG_TARIFF::ParseSlashedIntParams(int paramsNum, const string & s, int * params)
248 char * str = new char[s.size() + 1];
250 strcpy(str, s.c_str());
251 p = strtok(str, "/");
253 for (int i = 0; i < paramsNum; i++)
261 if (str2x(p, params[i]) != 0)
267 p = strtok(NULL, "/");
273 //-----------------------------------------------------------------------------
274 int PARSER_CHG_TARIFF::ParseSlashedDoubleParams(int paramsNum, const string & s, double * params)
276 char * str = new char[s.size() + 1];
278 strcpy(str, s.c_str());
279 p = strtok(str, "/");
281 for (int i = 0; i < paramsNum; i++)
289 if (strtodouble2(p, params[i]) != 0)
295 p = strtok(NULL, "/");
301 //-----------------------------------------------------------------------------
302 int PARSER_CHG_TARIFF::ParseStart(void *, const char * el, const char ** attr)
305 double price[DIR_NUM];
311 if (strcasecmp(el, "SetTariff") == 0)
313 td.tariffConf.name = attr[1];
321 if (strcasecmp(el, "PriceDayA") == 0)
324 if (ParseSlashedDoubleParams(DIR_NUM, s, price) == 0)
325 for (int j = 0; j < DIR_NUM; j++)
326 td.dirPrice[j].priceDayA = price[j] / pt_mega;
330 if (strcasecmp(el, "PriceDayB") == 0)
333 if (ParseSlashedDoubleParams(DIR_NUM, s, price) == 0)
334 for (int j = 0; j < DIR_NUM; j++)
335 td.dirPrice[j].priceDayB = price[j] / pt_mega;
340 if (strcasecmp(el, "PriceNightA") == 0)
343 if (ParseSlashedDoubleParams(DIR_NUM, s, price) == 0)
344 for (int j = 0; j < DIR_NUM; j++)
345 td.dirPrice[j].priceNightA = price[j] / pt_mega;
349 if (strcasecmp(el, "PriceNightB") == 0)
352 if (ParseSlashedDoubleParams(DIR_NUM, s, price) == 0)
353 for (int j = 0; j < DIR_NUM; j++)
354 td.dirPrice[j].priceNightB = price[j] / pt_mega;
358 if (strcasecmp(el, "Threshold") == 0)
361 if (ParseSlashedIntParams(DIR_NUM, s, t) == 0)
362 for (int j = 0; j < DIR_NUM; j++)
363 td.dirPrice[j].threshold = t[j];
367 if (strcasecmp(el, "SinglePrice") == 0)
370 if (ParseSlashedIntParams(DIR_NUM, s, t) == 0)
371 for (int j = 0; j < DIR_NUM; j++)
372 td.dirPrice[j].singlePrice = t[j];
376 if (strcasecmp(el, "NoDiscount") == 0)
379 if (ParseSlashedIntParams(DIR_NUM, s, t) == 0)
380 for (int j = 0; j < DIR_NUM; j++)
381 td.dirPrice[j].noDiscount = t[j];
385 for (int j = 0; j < DIR_NUM; j++)
387 snprintf(st, 50, "Time%d", j);
388 if (strcasecmp(el, st) == 0)
391 if (ParseTariffTimeStr(attr[1], h1, m1, h2, m2) == 0)
393 td.dirPrice[j].hDay = h1;
394 td.dirPrice[j].mDay = m1;
395 td.dirPrice[j].hNight = h2;
396 td.dirPrice[j].mNight = m2;
402 if (strcasecmp(el, "Fee") == 0)
405 if (strtodouble2(attr[1], fee) == 0)
406 td.tariffConf.fee = fee;
410 if (strcasecmp(el, "PassiveCost") == 0)
413 if (strtodouble2(attr[1], pc) == 0)
414 td.tariffConf.passiveCost = pc;
417 if (strcasecmp(el, "Free") == 0)
420 if (strtodouble2(attr[1], free) == 0)
421 td.tariffConf.free = free;
425 if (strcasecmp(el, "TraffType") == 0)
427 if (strcasecmp(attr[1], "up") == 0)
429 td.tariffConf.traffType = TRAFF_UP;
433 if (strcasecmp(attr[1], "down") == 0)
435 td.tariffConf.traffType = TRAFF_DOWN;
438 if (strcasecmp(attr[1], "up+down") == 0)
440 td.tariffConf.traffType = TRAFF_UP_DOWN;
443 if (strcasecmp(attr[1], "max") == 0)
445 td.tariffConf.traffType = TRAFF_MAX;
453 //-----------------------------------------------------------------------------
454 int PARSER_CHG_TARIFF::ParseEnd(void *, const char * el)
458 if (strcasecmp(el, "SetTariff") == 0)
469 //-----------------------------------------------------------------------------
470 void PARSER_CHG_TARIFF::CreateAnswer()
472 answerList->erase(answerList->begin(), answerList->end());
474 if (!td.tariffConf.name.data().empty())
476 TARIFF_DATA tariffData = td.GetData();
477 if (tariffs->Chg(tariffData, currAdmin) == 0)
479 answerList->push_back("<SetTariff Result=\"ok\"/>");
485 strprintf(&s, "<SetTariff Result=\"Change tariff error! %s\"/>", tariffs->GetStrError().c_str());
486 answerList->push_back(s);
490 answerList->push_back("<SetTariff Result=\"Change tariff error!\"/>");
492 //-----------------------------------------------------------------------------