]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/sgconfig/parser_tariffs.cpp
Split sgconfig parsers into seaparate files.
[stg.git] / projects / stargazer / plugins / configuration / sgconfig / parser_tariffs.cpp
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
20  */
21
22 #include "parser_tariffs.h"
23
24 #include "stg/tariffs.h"
25 #include "stg/users.h"
26 #include "stg/common.h"
27 #include "stg/resetable.h"
28
29 #include <cstdio> // snprintf
30 #include <cstring>
31
32 using STG::PARSER::GET_TARIFFS;
33 using STG::PARSER::ADD_TARIFF;
34 using STG::PARSER::DEL_TARIFF;
35 using STG::PARSER::CHG_TARIFF;
36
37 namespace
38 {
39
40 const double pt_mega = 1024 * 1024;
41
42 template <typename A, typename C, typename F>
43 std::string AOS2String(const A & array, size_t size, const F C::* field, F multiplier)
44 {
45     std::string res;
46     for (size_t i = 0; i < size; ++i)
47     {
48         if (!res.empty())
49             res += "/";
50         res += x2str((array[i].*field) * multiplier);
51     }
52     return res;
53 }
54
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)
57 {
58     size_t index = 0;
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)
62     {
63         if (str2x(source.substr(from, pos - from), (array[index].*field).data()))
64             return false;
65         (array[index].*field).data() /= divisor;
66         from = pos + 1;
67         ++index;
68     }
69     if (str2x(source.substr(from), (array[index].*field).data()))
70         return false;
71     (array[index].*field).data() /= divisor;
72     return true;
73 }
74
75 }
76
77 void GET_TARIFFS::CreateAnswer()
78 {
79     answer = GetOpenTag();
80
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)
85         {
86         answer += "<tariff name=\"" + it->tariffConf.name + "\">";
87
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) + "\"/>";
92
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) + "\"/>" +
105                   "</tariff>";
106         }
107
108     answer += GetCloseTag();
109 }
110
111 int ADD_TARIFF::Start(void *, const char * el, const char ** attr)
112 {
113     if (strcasecmp(el, tag.c_str()) != 0)
114         return -1;
115
116     if (attr[1] == NULL)
117         return -1;
118
119     tariff = attr[1];
120     return 0;
121 }
122
123 void ADD_TARIFF::CreateAnswer()
124 {
125     if (m_tariffs.Add(tariff, &currAdmin) == 0)
126         answer = "<" + tag + " Result=\"Ok\"/>";
127     else
128         answer = "<" + tag + " Result=\"Error. " + m_tariffs.GetStrError() + "\"/>";
129 }
130
131 int DEL_TARIFF::Start(void *, const char * el, const char ** attr)
132 {
133     if (strcasecmp(el, tag.c_str()) != 0)
134         return -1;
135
136     if (attr[1] == NULL)
137         return -1;
138
139     tariff = attr[1];
140     return 0;
141 }
142
143 void DEL_TARIFF::CreateAnswer()
144 {
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\"/>";
149     else
150         answer = "<" + tag + " Result=\"Error. " + m_tariffs.GetStrError() + "\"/>";
151 }
152
153 int CHG_TARIFF::Start(void *, const char * el, const char ** attr)
154 {
155     depth++;
156
157     if (depth == 1)
158     {
159         if (strcasecmp(el, tag.c_str()) == 0)
160         {
161             td.tariffConf.name = attr[1];
162             return 0;
163         }
164     }
165     else
166     {
167         if (strcasecmp(el, "PriceDayA") == 0)
168         {
169             if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::priceDayA, pt_mega))
170                 return -1; // TODO: log it
171             else
172                 return 0;
173         }
174
175         if (strcasecmp(el, "PriceDayB") == 0)
176         {
177             if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::priceDayB, pt_mega))
178                 return -1; // TODO: log it
179             else
180                 return 0;
181         }
182
183         if (strcasecmp(el, "PriceNightA") == 0)
184         {
185             if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::priceNightA, pt_mega))
186                 return -1; // TODO: log it
187             else
188                 return 0;
189         }
190
191         if (strcasecmp(el, "PriceNightB") == 0)
192         {
193             if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::priceNightB, pt_mega))
194                 return -1; // TODO: log it
195             else
196                 return 0;
197         }
198
199         if (strcasecmp(el, "Threshold") == 0)
200         {
201             if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::threshold, 1))
202                 return -1; // TODO: log it
203             else
204                 return 0;
205         }
206
207         if (strcasecmp(el, "SinglePrice") == 0)
208         {
209             if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::singlePrice, 1))
210                 return -1; // TODO: log it
211             else
212                 return 0;
213         }
214
215         if (strcasecmp(el, "NoDiscount") == 0)
216         {
217             if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::noDiscount, 1))
218                 return -1; // TODO: log it
219             else
220                 return 0;
221         }
222
223         for (int j = 0; j < DIR_NUM; j++)
224         {
225             char st[50];
226             snprintf(st, 50, "Time%d", j);
227             if (strcasecmp(el, st) == 0)
228             {
229                 int h1 = 0;
230                 int m1 = 0;
231                 int h2 = 0;
232                 int m2 = 0;
233                 if (ParseTariffTimeStr(attr[1], h1, m1, h2, m2) == 0)
234                     {
235                     td.dirPrice[j].hDay = h1;
236                     td.dirPrice[j].mDay = m1;
237                     td.dirPrice[j].hNight = h2;
238                     td.dirPrice[j].mNight = m2;
239                     }
240                 return 0;
241             }
242         }
243
244         if (strcasecmp(el, "Fee") == 0)
245         {
246             double fee;
247             if (strtodouble2(attr[1], fee) == 0)
248                 td.tariffConf.fee = fee;
249             return 0;
250         }
251
252         if (strcasecmp(el, "PassiveCost") == 0)
253         {
254             double pc;
255             if (strtodouble2(attr[1], pc) == 0)
256                 td.tariffConf.passiveCost = pc;
257             return 0;
258         }
259
260         if (strcasecmp(el, "Free") == 0)
261         {
262             double free;
263             if (strtodouble2(attr[1], free) == 0)
264                 td.tariffConf.free = free;
265             return 0;
266         }
267
268         if (strcasecmp(el, "TraffType") == 0)
269         {
270             td.tariffConf.traffType = TARIFF::StringToTraffType(attr[1]);
271             return 0;
272         }
273
274         if (strcasecmp(el, "Period") == 0)
275         {
276             td.tariffConf.period = TARIFF::StringToPeriod(attr[1]);
277             return 0;
278         }
279     }
280     return -1;
281 }
282
283 int CHG_TARIFF::End(void *, const char * el)
284 {
285     if (depth == 1)
286     {
287         if (strcasecmp(el, tag.c_str()) != 0)
288             return -1;
289         CreateAnswer();
290     }
291
292     --depth;
293     return 0;
294 }
295
296 void CHG_TARIFF::CreateAnswer()
297 {
298     if (!td.tariffConf.name.data().empty())
299     {
300         TARIFF_DATA tariffData = td.GetData();
301         if (m_tariffs.Chg(tariffData, &currAdmin) == 0)
302             answer = "<" + tag + " Result=\"ok\"/>";
303         else
304             answer = "<" + tag + " Result=\"Change tariff error! " + m_tariffs.GetStrError() + "\"/>";
305     }
306     else
307         answer = "<" + tag + " Result=\"Change tariff error!\"/>";
308 }