]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/sgconfig2/parser_tariff.cpp
29b445340cef2888040056772c56d76b2a94fa44
[stg.git] / projects / stargazer / plugins / configuration / sgconfig2 / parser_tariff.cpp
1 #include <stdio.h>
2 #include <string.h>
3
4 #include "parser.h"
5 //#include "admins.h"
6 //#include "tariff2.h"
7 const int pt_mega = 1024 * 1024;
8 //-----------------------------------------------------------------------------
9 //  GET TARIFFS
10 //-----------------------------------------------------------------------------
11 int PARSER_GET_TARIFFS::ParseStart(void *, const char *el, const char **)
12 {
13 if (strcasecmp(el, "GetTariffs") == 0)
14     {
15     return 0;
16     }
17 return -1;
18 }
19 //-----------------------------------------------------------------------------
20 int PARSER_GET_TARIFFS::ParseEnd(void *, const char *el)
21 {
22 if (strcasecmp(el, "GetTariffs") == 0)
23     {
24     CreateAnswer();
25     return 0;
26     }
27 return -1;
28 }
29 //-----------------------------------------------------------------------------
30 void PARSER_GET_TARIFFS::CreateAnswer()
31 {
32 string s;
33 char vs[100];
34 int hd, hn, md, mn;
35
36 //answerList->clear();
37 answerList->erase(answerList->begin(), answerList->end());
38
39 answerList->push_back("<Tariffs>");
40 int h = tariffs->OpenSearch();
41
42 TARIFF_DATA td;
43 while (tariffs->SearchNext(h, &td) == 0)
44     {
45     s = "<tariff name=\"" + td.tariffConf.name + "\">";
46     //printfd(__FILE__, "%s\n", s.c_str());
47     answerList->push_back(s);
48
49     for (int j = 0; j < DIR_NUM; j++)
50         {
51         hd = td.dirPrice[j].hDay;
52         md = td.dirPrice[j].mDay;
53
54         hn = td.dirPrice[j].hNight;
55         mn = td.dirPrice[j].mNight;
56
57         strprintf(&s, "<Time%d value=\"%d:%d-%d:%d\"/>", j, hd, md, hn, mn);
58         answerList->push_back(s);
59         }
60
61     strprintf(&s, "    <PriceDayA value=\"");
62     for (int i = 0; i < DIR_NUM; i++)
63         {
64         snprintf(vs, 100, "%.5f%s", td.dirPrice[i].priceDayA * pt_mega, i+1 == DIR_NUM?"":"/");
65         s += vs;
66         }
67     s += "\"/>";
68     answerList->push_back(s);
69
70     strprintf(&s, "    <PriceDayB value=\"");
71     for (int i = 0; i < DIR_NUM; i++)
72         {
73         snprintf(vs, 100, "%.5f%s", td.dirPrice[i].priceDayB * pt_mega, i+1 == DIR_NUM?"":"/");
74         s += vs;
75         }
76     s += "\"/>";
77     answerList->push_back(s);
78
79     strprintf(&s, "    <PriceNightA value=\"");
80     for (int i = 0; i < DIR_NUM; i++)
81         {
82         snprintf(vs, 100, "%.5f%s", td.dirPrice[i].priceNightA * pt_mega, i+1 == DIR_NUM?"":"/");
83         s += vs;
84         }
85     s += "\"/>";
86     answerList->push_back(s);
87
88     strprintf(&s, "    <PriceNightB value=\"");
89     for (int i = 0; i < DIR_NUM; i++)
90         {
91         snprintf(vs, 100, "%.5f%s", td.dirPrice[i].priceNightB * pt_mega, i+1 == DIR_NUM?"":"/");
92         s += vs;
93         }
94     s += "\"/>";
95     answerList->push_back(s);
96
97     strprintf(&s, "    <Threshold value=\"");
98     for (int i = 0; i < DIR_NUM; i++)
99         {
100         snprintf(vs, 100, "%d%s", td.dirPrice[i].threshold, i+1 == DIR_NUM?"":"/");
101         s += vs;
102         }
103     s += "\"/>";
104     answerList->push_back(s);
105
106     strprintf(&s, "    <SinglePrice value=\"");
107     for (int i = 0; i < DIR_NUM; i++)
108         {
109         snprintf(vs, 100, "%d%s", td.dirPrice[i].singlePrice, i+1 == DIR_NUM?"":"/");
110         s += vs;
111         }
112     s += "\"/>";
113     answerList->push_back(s);
114
115     strprintf(&s, "    <NoDiscount value=\"");
116     for (int i = 0; i < DIR_NUM; i++)
117         {
118         snprintf(vs, 100, "%d%s", td.dirPrice[i].noDiscount, i+1 == DIR_NUM?"":"/");
119         s += vs;
120         }
121     s += "\"/>";
122     answerList->push_back(s);
123
124     strprintf(&s, "    <Fee value=\"%.5f\"/>", td.tariffConf.fee);
125     answerList->push_back(s);
126     //printfd(__FILE__, "%s\n", s.c_str());
127
128     strprintf(&s, "    <PassiveCost value=\"%.5f\"/>", td.tariffConf.passiveCost);
129     answerList->push_back(s);
130
131     strprintf(&s, "    <Free value=\"%.5f\"/>", td.tariffConf.free);
132     answerList->push_back(s);
133
134     switch (td.tariffConf.traffType)
135         {
136         case TRAFF_UP:
137             answerList->push_back("<TraffType value=\"up\"/>");
138             break;
139         case TRAFF_DOWN:
140             answerList->push_back("<TraffType value=\"down\"/>");
141             break;
142         case TRAFF_UP_DOWN:
143             answerList->push_back("<TraffType value=\"up+down\"/>");
144             break;
145         case TRAFF_MAX:
146             answerList->push_back("<TraffType value=\"max\"/>");
147             break;
148         }
149
150     answerList->push_back("</tariff>");
151     }
152 tariffs->CloseSearch(h);
153 answerList->push_back("</Tariffs>");
154 }
155 //-----------------------------------------------------------------------------
156 //  ADD TARIFF
157 //-----------------------------------------------------------------------------
158 int PARSER_ADD_TARIFF::ParseStart(void *, const char *el, const char **attr)
159 {
160 if (strcasecmp(el, "AddTariff") == 0)
161     {
162     if (attr[1])
163         {
164         tariffToAdd = attr[1];
165         }
166     return 0;
167     }
168 return -1;
169 }
170 //-----------------------------------------------------------------------------
171 int PARSER_ADD_TARIFF::ParseEnd(void *, const char *el)
172 {
173 if (strcasecmp(el, "AddTariff") == 0)
174     {
175     CreateAnswer();
176     return 0;
177     }
178 return -1;
179 }
180 //-----------------------------------------------------------------------------
181 void PARSER_ADD_TARIFF::CreateAnswer()
182 {
183 //answerList->clear();
184 answerList->erase(answerList->begin(), answerList->end());
185
186 if (tariffs->Add(tariffToAdd, *currAdmin) == 0)
187     {
188     answerList->push_back("<AddTariff Result=\"Ok\"/>");
189     }
190 else
191     {
192     string s;
193     strprintf(&s, "<AddTariff Result=\"Error. %s\"/>", tariffs->GetStrError().c_str());
194     answerList->push_back(s);
195     }
196 }
197 //-----------------------------------------------------------------------------
198 //  DEL TARIFF
199 //-----------------------------------------------------------------------------
200 int PARSER_DEL_TARIFF::ParseStart(void *, const char *el, const char **attr)
201 {
202 strError = "";
203 if (strcasecmp(el, "DelTariff") == 0)
204     {
205     tariffToDel = attr[1];
206     return 0;
207     }
208 return -1;
209 }
210 //-----------------------------------------------------------------------------
211 int PARSER_DEL_TARIFF::ParseEnd(void *, const char *el)
212 {
213 if (strcasecmp(el, "DelTariff") == 0)
214     {
215     CreateAnswer();
216     return 0;
217     }
218 return -1;
219 }
220 //-----------------------------------------------------------------------------
221 void PARSER_DEL_TARIFF::CreateAnswer()
222 {
223 //answerList->clear();
224 answerList->erase(answerList->begin(), answerList->end());
225
226 if (tariffs->Del(tariffToDel, *currAdmin) == 0)
227     {
228     answerList->push_back("<DelTariff Result=\"Ok\"/>");
229     }
230 else
231     {
232     string s;
233     strprintf(&s, "<DelTariff Result=\"Error. %s\"/>", tariffs->GetStrError().c_str());
234     answerList->push_back(s);
235     }
236 }
237 //-----------------------------------------------------------------------------
238 //-----------------------------------------------------------------------------
239 //  CHG TARIFF
240 //-----------------------------------------------------------------------------
241 //-----------------------------------------------------------------------------
242 int PARSER_CHG_TARIFF::ParseSlashedIntParams(int paramsNum, const string & s, int * params)
243 {
244 char * str = new char[s.size() + 1];
245 char * p;
246 strcpy(str, s.c_str());
247 p = strtok(str, "/");
248
249 for (int i = 0; i < paramsNum; i++)
250     {
251     if (p == NULL)
252         {
253         delete[] str;
254         return -1;
255         }
256
257     if (str2x(p, params[i]) != 0)
258         {
259         delete[] str;
260         return -1;
261         }
262
263     p = strtok(NULL, "/");
264     }
265
266 delete[] str;
267 return 0;
268 }
269 //-----------------------------------------------------------------------------
270 int PARSER_CHG_TARIFF::ParseSlashedDoubleParams(int paramsNum, const string & s, double * params)
271 {
272 char * str = new char[s.size() + 1];
273 char * p;
274 strcpy(str, s.c_str());
275 p = strtok(str, "/");
276
277 for (int i = 0; i < paramsNum; i++)
278     {
279     if (p == NULL)
280         {
281         delete[] str;
282         return -1;
283         }
284
285     if (strtodouble2(p, params[i]) != 0)
286         {
287         delete[] str;
288         return -1;
289         }
290
291     p = strtok(NULL, "/");
292     }
293
294 delete[] str;
295 return 0;
296 }
297 //-----------------------------------------------------------------------------
298 int PARSER_CHG_TARIFF::ParseStart(void *, const char *el, const char **attr)
299 {
300 char st[50];
301 double price[DIR_NUM];
302 int t[DIR_NUM];
303 depth++;
304
305 if (depth == 1)
306     {
307     if (strcasecmp(el, "SetTariff") == 0)
308         {
309         td.tariffConf.name = attr[1];
310         return 0;
311         }
312     }
313 else
314     {
315     string s;
316
317     if (strcasecmp(el, "PriceDayA") == 0)
318         {
319         s = attr[1];
320         if (ParseSlashedDoubleParams(DIR_NUM, s, price) == 0)
321             for (int j = 0; j < DIR_NUM; j++)
322                 td.dirPrice[j].priceDayA = price[j] / pt_mega;
323         return 0;
324         }
325
326     if (strcasecmp(el, "PriceDayB") == 0)
327         {
328         s = attr[1];
329         if (ParseSlashedDoubleParams(DIR_NUM, s, price) == 0)
330             for (int j = 0; j < DIR_NUM; j++)
331                 td.dirPrice[j].priceDayB = price[j] / pt_mega;
332         return 0;
333         }
334
335
336     if (strcasecmp(el, "PriceNightA") == 0)
337         {
338         s = attr[1];
339         if (ParseSlashedDoubleParams(DIR_NUM, s, price) == 0)
340             for (int j = 0; j < DIR_NUM; j++)
341                 td.dirPrice[j].priceNightA = price[j] / pt_mega;
342         return 0;
343         }
344
345     if (strcasecmp(el, "PriceNightB") == 0)
346         {
347         s = attr[1];
348         if (ParseSlashedDoubleParams(DIR_NUM, s, price) == 0)
349             for (int j = 0; j < DIR_NUM; j++)
350                 td.dirPrice[j].priceNightB = price[j] / pt_mega;
351         return 0;
352         }
353
354     if (strcasecmp(el, "Threshold") == 0)
355         {
356         s = attr[1];
357         if (ParseSlashedIntParams(DIR_NUM, s, t) == 0)
358             for (int j = 0; j < DIR_NUM; j++)
359                 td.dirPrice[j].threshold = t[j];
360         return 0;
361         }
362
363     if (strcasecmp(el, "SinglePrice") == 0)
364         {
365         s = attr[1];
366         if (ParseSlashedIntParams(DIR_NUM, s, t) == 0)
367             for (int j = 0; j < DIR_NUM; j++)
368                 td.dirPrice[j].singlePrice = t[j];
369         return 0;
370         }
371
372     if (strcasecmp(el, "NoDiscount") == 0)
373         {
374         s = attr[1];
375         if (ParseSlashedIntParams(DIR_NUM, s, t) == 0)
376             for (int j = 0; j < DIR_NUM; j++)
377                 td.dirPrice[j].noDiscount = t[j];
378         return 0;
379         }
380
381     for (int j = 0; j < DIR_NUM; j++)
382         {
383         snprintf(st, 50, "Time%d", j);
384         if (strcasecmp(el, st) == 0)
385             {
386             int h1, m1, h2, m2;
387             if (ParseTariffTimeStr(attr[1], h1, m1, h2, m2) == 0)
388                 {
389                 td.dirPrice[j].hDay = h1;
390                 td.dirPrice[j].mDay = m1;
391                 td.dirPrice[j].hNight = h2;
392                 td.dirPrice[j].mNight = m2;
393                 }
394             return 0;
395             }
396         }
397
398     if (strcasecmp(el, "Fee") == 0)
399         {
400         double fee;
401         if (strtodouble2(attr[1], fee) == 0)
402             td.tariffConf.fee = fee;
403         return 0;
404         }
405
406     if (strcasecmp(el, "PassiveCost") == 0)
407         {
408         double pc;
409         if (strtodouble2(attr[1], pc) == 0)
410             td.tariffConf.passiveCost = pc;
411         return 0;
412         }
413     if (strcasecmp(el, "Free") == 0)
414         {
415         double free;
416         if (strtodouble2(attr[1], free) == 0)
417             td.tariffConf.free = free;
418         return 0;
419         }
420
421     if (strcasecmp(el, "TraffType") == 0)
422         {
423         if (strcasecmp(attr[1], "up") == 0)
424             {
425             td.tariffConf.traffType = TRAFF_UP;
426             return 0;
427             }
428
429         if (strcasecmp(attr[1], "down") == 0)
430             {
431             td.tariffConf.traffType = TRAFF_DOWN;
432             return 0;
433             }
434         if (strcasecmp(attr[1], "up+down") == 0)
435             {
436             td.tariffConf.traffType = TRAFF_UP_DOWN;
437             return 0;
438             }
439         if (strcasecmp(attr[1], "max") == 0)
440             {
441             td.tariffConf.traffType = TRAFF_MAX;
442             return 0;
443             }
444         return 0;
445         }
446     }
447 return -1;
448 }
449 //-----------------------------------------------------------------------------
450 int PARSER_CHG_TARIFF::ParseEnd(void *, const char *el)
451 {
452 if (depth == 1)
453     {
454     if (strcasecmp(el, "SetTariff") == 0)
455         {
456         CreateAnswer();
457         depth--;
458         return 0;
459         }
460     }
461
462 depth--;
463 return -1;
464 }
465 //-----------------------------------------------------------------------------
466 void PARSER_CHG_TARIFF::CreateAnswer()
467 {
468 answerList->erase(answerList->begin(), answerList->end());
469
470 if (!td.tariffConf.name.data().empty())
471     {
472     TARIFF_DATA tariffData = td.GetData();
473     if (tariffs->Chg(tariffData, *currAdmin) == 0)
474         {
475         answerList->push_back("<SetTariff Result=\"ok\"/>");
476         return;
477         }
478     else
479         {
480         string s;
481         strprintf(&s, "<SetTariff Result=\"Change tariff error! %s\"/>", tariffs->GetStrError().c_str());
482         answerList->push_back(s);
483         return;
484         }
485     }
486 answerList->push_back("<SetTariff Result=\"Change tariff error!\"/>");
487 }
488 //-----------------------------------------------------------------------------
489