]> git.stg.codes - stg.git/blob - libs/srvconf/parsers/get_tariff.cpp
Public interfaces: part 1
[stg.git] / libs / srvconf / parsers / get_tariff.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 : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21 #include "get_tariff.h"
22
23 //#include "parsers/property.h"
24
25 #include "stg/common.h"
26
27 #include <utility>
28
29 #include <strings.h>
30
31 using namespace STG;
32
33 namespace
34 {
35
36 template <typename A, typename T>
37 class AoSParser : public BasePropertyParser
38 {
39     public:
40         using Func = bool (*)(const char**, A &, T A::value_type::*);
41         AoSParser(A& a, T A::value_type::* fld, Func f) : array(a), field(fld), func(f) {}
42         virtual bool Parse(const char** attr, const std::string& /*attrName*/, const std::string& /*fromEncoding*/) { return func(attr, array, field); }
43     private:
44         A& array;
45         T A::value_type::* field;
46         Func func;
47 };
48
49 template <typename A, typename T>
50 inline
51 void addAOSParser(PropertyParsers& parsers, const std::string& name, A& array, T A::value_type::* field, const typename AoSParser<A, T>::Func& func)
52 {
53     parsers.insert(std::make_pair(ToLower(name), new AoSParser<A, T>(array, field, func)));
54 }
55
56 bool getTimeSpan(const char** attr, DirPriceData& value, const std::string& attrName)
57 {
58     if (checkValue(attr, attrName))
59     {
60         int hb = 0;
61         int mb = 0;
62         int he = 0;
63         int me = 0;
64         if (ParseTariffTimeStr(attr[1], hb, mb, he, me) == 0)
65         {
66             value.hDay = hb;
67             value.mDay = mb;
68             value.hNight = he;
69             value.mNight = me;
70             return true;
71         }
72     }
73     return false;
74 }
75
76 template <typename T>
77 bool getTraffType(const char** attr, T& value, const std::string& attrName)
78 {
79     if (!checkValue(attr, attrName))
80         return false;
81     value = Tariff::parseTraffType(attr[1]);
82     return true;
83 }
84
85 template <typename T>
86 bool getPeriod(const char** attr, T& value, const std::string& attrName)
87 {
88     if (!checkValue(attr, attrName))
89         return false;
90     std::string type(attr[1]);
91     if (type == "day")
92         value = Tariff::DAY;
93     else if (type == "month")
94         value = Tariff::MONTH;
95     else
96         return false;
97     return true;
98 }
99
100 template <typename T>
101 bool getChangePolicy(const char** attr, T& value, const std::string& attrName)
102 {
103     if (!checkValue(attr, attrName))
104         return false;
105     std::string type(attr[1]);
106     if (type == "allow")
107         value = Tariff::ALLOW;
108     else if (type == "to_cheap")
109         value = Tariff::TO_CHEAP;
110     else if (type == "to_expensive")
111         value = Tariff::TO_EXPENSIVE;
112     else if (type == "deny")
113         value = Tariff::DENY;
114     else
115         return false;
116     return true;
117 }
118
119 template <typename A, typename T>
120 bool getSlashedValue(const char** attr, A& array, T A::value_type::* field)
121 {
122     if (!checkValue(attr, "value"))
123         return false;
124     const char* start = attr[1];
125     size_t item = 0;
126     const char* pos = NULL;
127     while ((pos = strchr(start, '/')) && item < array.size())
128     {
129         if (str2x(std::string(start, pos), array[item++].*field))
130                 return false;
131         start = pos + 1;
132     }
133     if (item < array.size())
134         if (str2x(start, array[item].*field))
135             return false;
136     return true;
137 }
138
139 } // namespace anonymous
140
141 GetTariff::Parser::Parser(Callback f, void* d, const std::string& e)
142     : callback(f),
143       data(d),
144       encoding(e),
145       depth(0),
146       parsingAnswer(false)
147 {
148     addParser(propertyParsers, "fee", info.tariffConf.fee);
149     addParser(propertyParsers, "passiveCost", info.tariffConf.passiveCost);
150     addParser(propertyParsers, "free", info.tariffConf.free);
151     addParser(propertyParsers, "traffType", info.tariffConf.traffType, getTraffType);
152     addParser(propertyParsers, "period", info.tariffConf.period, getPeriod);
153     addParser(propertyParsers, "changePolicy", info.tariffConf.changePolicy, getChangePolicy);
154     addParser(propertyParsers, "changePolicyTimeout", info.tariffConf.changePolicyTimeout);
155     for (size_t i = 0; i < DIR_NUM; ++i)
156         addParser(propertyParsers, "time" + std::to_string(i), info.dirPrice[i], getTimeSpan);
157     addAOSParser(propertyParsers, "priceDayA", info.dirPrice, &DirPriceData::priceDayA, getSlashedValue);
158     addAOSParser(propertyParsers, "priceDayB", info.dirPrice, &DirPriceData::priceDayB, getSlashedValue);
159     addAOSParser(propertyParsers, "priceNightA", info.dirPrice, &DirPriceData::priceNightA, getSlashedValue);
160     addAOSParser(propertyParsers, "priceNightB", info.dirPrice, &DirPriceData::priceNightB, getSlashedValue);
161     addAOSParser(propertyParsers, "singlePrice", info.dirPrice, &DirPriceData::singlePrice, getSlashedValue);
162     addAOSParser(propertyParsers, "noDiscount", info.dirPrice, &DirPriceData::noDiscount, getSlashedValue);
163     addAOSParser(propertyParsers, "threshold", info.dirPrice, &DirPriceData::threshold, getSlashedValue);
164 }
165 //-----------------------------------------------------------------------------
166 GetTariff::Parser::~Parser()
167 {
168     auto it = propertyParsers.begin();
169     while (it != propertyParsers.end())
170         delete (it++)->second;
171 }
172 //-----------------------------------------------------------------------------
173 int GetTariff::Parser::ParseStart(const char* el, const char** attr)
174 {
175     depth++;
176     if (depth == 1)
177         ParseTariff(el, attr);
178
179     if (depth == 2 && parsingAnswer)
180         ParseTariffParams(el, attr);
181
182     return 0;
183 }
184 //-----------------------------------------------------------------------------
185 void GetTariff::Parser::ParseEnd(const char* /*el*/)
186 {
187     depth--;
188     if (depth == 0 && parsingAnswer)
189     {
190         if (callback)
191             callback(error.empty(), error, info, data);
192         error.clear();
193         parsingAnswer = false;
194     }
195 }
196 //-----------------------------------------------------------------------------
197 void GetTariff::Parser::ParseTariff(const char* el, const char** attr)
198 {
199     if (strcasecmp(el, "tariff") == 0)
200     {
201         if (attr && attr[0] && attr[1])
202         {
203             if (strcasecmp(attr[1], "error") == 0)
204             {
205                 if (attr[2] && attr[3])
206                     error = attr[3];
207                 else
208                     error = "Tariff not found.";
209             }
210             else
211             {
212                 parsingAnswer = true;
213                 if (strcasecmp(attr[0], "name") == 0)
214                     info.tariffConf.name = attr[1];
215             }
216         }
217         else
218             parsingAnswer = true;
219     }
220 }
221 //-----------------------------------------------------------------------------
222 void GetTariff::Parser::ParseTariffParams(const char* el, const char** attr)
223 {
224     if (!tryParse(propertyParsers, ToLower(el), attr, encoding))
225         error = std::string("Invalid parameter '") + el + "'.";
226 }