]> git.stg.codes - stg.git/blob - stglibs/srvconf.lib/parsers/get_tariff.cpp
Lots of stylistic fixes.
[stg.git] / stglibs / srvconf.lib / 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 AOS_PARSER : public BASE_PROPERTY_PARSER
38 {
39     public:
40         typedef bool (* FUNC)(const char **, A &, T A::value_type:: *);
41         AOS_PARSER(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(PROPERTY_PARSERS & parsers, const std::string & name, A & array, T A::value_type:: * field, const typename AOS_PARSER<A, T>::FUNC & func)
52 {
53     parsers.insert(std::make_pair(ToLower(name), new AOS_PARSER<A, T>(array, field, func)));
54 }
55
56 bool GetTimeSpan(const char ** attr, DIRPRICE_DATA & 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 std::string type(attr[1]);
82 if (type == "up")
83     value = TRAFF_UP;
84 else if (type == "down")
85     value = TRAFF_DOWN;
86 else if (type == "up+down")
87     value = TRAFF_UP_DOWN;
88 else if (type == "max")
89     value = TRAFF_MAX;
90 else
91     return false;
92 return true;
93 }
94
95 template <typename T>
96 bool GetPeriod(const char ** attr, T & value, const std::string & attrName)
97 {
98 if (!CheckValue(attr, attrName))
99     return false;
100 std::string type(attr[1]);
101 if (type == "day")
102     value = TARIFF::DAY;
103 else if (type == "month")
104     value = TARIFF::MONTH;
105 else
106     return false;
107 return true;
108 }
109
110 template <typename A, typename T>
111 bool GetSlashedValue(const char ** attr, A & array, T A::value_type:: * field)
112 {
113 if (!CheckValue(attr, "value"))
114     return false;
115 const char * start = attr[1];
116 size_t item = 0;
117 const char * pos = NULL;
118 while ((pos = strchr(start, '/')) && item < array.size())
119     {
120     if (str2x(std::string(start, pos), array[item++].*field))
121             return false;
122     start = pos + 1;
123     }
124 if (item < array.size())
125     if (str2x(start, array[item].*field))
126         return false;
127 return true;
128 }
129
130 } // namespace anonymous
131
132 GET_TARIFF::PARSER::PARSER(CALLBACK f, void * d, const std::string & e)
133     : callback(f),
134       data(d),
135       encoding(e),
136       depth(0),
137       parsingAnswer(false)
138 {
139     AddParser(propertyParsers, "fee", info.tariffConf.fee);
140     AddParser(propertyParsers, "passiveCost", info.tariffConf.passiveCost);
141     AddParser(propertyParsers, "free", info.tariffConf.free);
142     AddParser(propertyParsers, "traffType", info.tariffConf.traffType, GetTraffType);
143     AddParser(propertyParsers, "period", info.tariffConf.period, GetPeriod);
144     for (size_t i = 0; i < DIR_NUM; ++i)
145         AddParser(propertyParsers, "time" + unsigned2str(i), info.dirPrice[i], GetTimeSpan);
146     AddAOSParser(propertyParsers, "priceDayA", info.dirPrice, &DIRPRICE_DATA::priceDayA, GetSlashedValue);
147     AddAOSParser(propertyParsers, "priceDayB", info.dirPrice, &DIRPRICE_DATA::priceDayB, GetSlashedValue);
148     AddAOSParser(propertyParsers, "priceNightA", info.dirPrice, &DIRPRICE_DATA::priceNightA, GetSlashedValue);
149     AddAOSParser(propertyParsers, "priceNightB", info.dirPrice, &DIRPRICE_DATA::priceNightB, GetSlashedValue);
150     AddAOSParser(propertyParsers, "singlePrice", info.dirPrice, &DIRPRICE_DATA::singlePrice, GetSlashedValue);
151     AddAOSParser(propertyParsers, "noDiscount", info.dirPrice, &DIRPRICE_DATA::noDiscount, GetSlashedValue);
152     AddAOSParser(propertyParsers, "threshold", info.dirPrice, &DIRPRICE_DATA::threshold, GetSlashedValue);
153 }
154 //-----------------------------------------------------------------------------
155 GET_TARIFF::PARSER::~PARSER()
156 {
157     PROPERTY_PARSERS::iterator it(propertyParsers.begin());
158     while (it != propertyParsers.end())
159         delete (it++)->second;
160 }
161 //-----------------------------------------------------------------------------
162 int GET_TARIFF::PARSER::ParseStart(const char * el, const char ** attr)
163 {
164 depth++;
165 if (depth == 1)
166     ParseTariff(el, attr);
167
168 if (depth == 2 && parsingAnswer)
169     ParseTariffParams(el, attr);
170
171 return 0;
172 }
173 //-----------------------------------------------------------------------------
174 void GET_TARIFF::PARSER::ParseEnd(const char * /*el*/)
175 {
176 depth--;
177 if (depth == 0 && parsingAnswer)
178     {
179     if (callback)
180         callback(error.empty(), error, info, data);
181     error.clear();
182     parsingAnswer = false;
183     }
184 }
185 //-----------------------------------------------------------------------------
186 void GET_TARIFF::PARSER::ParseTariff(const char * el, const char ** attr)
187 {
188 if (strcasecmp(el, "tariff") == 0)
189     {
190     if (attr && attr[0] && attr[1])
191         {
192         if (strcasecmp(attr[1], "error") == 0)
193             {
194             if (attr[2] && attr[3])
195                 error = attr[3];
196             else
197                 error = "Tariff not found.";
198             }
199         else
200             {
201             parsingAnswer = true;
202             if (strcasecmp(attr[0], "name") == 0)
203                 info.tariffConf.name = attr[1];
204             }
205         }
206     else
207         parsingAnswer = true;
208     }
209 }
210 //-----------------------------------------------------------------------------
211 void GET_TARIFF::PARSER::ParseTariffParams(const char * el, const char ** attr)
212 {
213 if (!TryParse(propertyParsers, ToLower(el), attr, encoding))
214     error = std::string("Invalid parameter '") + el + "'.";
215 }