]> git.stg.codes - stg.git/blob - stglibs/srvconf.lib/parsers/get_tariff.cpp
Merge branch 'log-unauth-reasons'
[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) { 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)
57 {
58 int hb = 0;
59 int mb = 0;
60 int he = 0;
61 int me = 0;
62 if (CheckValue(attr))
63     if (ParseTariffTimeStr(attr[1], hb, mb, he, me) == 0)
64         {
65         value.hDay = hb;
66         value.mDay = mb;
67         value.hNight = he;
68         value.mNight = me;
69         return true;
70         }
71 return false;
72 }
73
74 template <typename T>
75 bool GetTraffType(const char ** attr, T & value)
76 {
77 if (!CheckValue(attr))
78     return false;
79 std::string type(attr[1]);
80 if (type == "up")
81     value = TRAFF_UP;
82 else if (type == "down")
83     value = TRAFF_DOWN;
84 else if (type == "up+down")
85     value = TRAFF_UP_DOWN;
86 else if (type == "max")
87     value = TRAFF_MAX;
88 else
89     return false;
90 return true;
91 }
92
93 template <typename A, typename T>
94 bool GetSlashedValue(const char ** attr, A & array, T A::value_type:: * field)
95 {
96 if (!CheckValue(attr))
97     return false;
98 const char * start = attr[1];
99 size_t item = 0;
100 const char * pos = NULL;
101 while ((pos = strchr(start, '/')) && item < array.size())
102     {
103     if (str2x(std::string(start, pos), array[item++].*field))
104             return false;
105     start = pos + 1;
106     }
107 if (item < array.size())
108     if (str2x(start, array[item].*field))
109         return false;
110 return true;
111 }
112
113 } // namespace anonymous
114
115 GET_TARIFF::PARSER::PARSER(CALLBACK f, void * d)
116     : callback(f),
117       data(d),
118       depth(0),
119       parsingAnswer(false)
120 {
121     AddParser(propertyParsers, "fee", info.tariffConf.fee);
122     AddParser(propertyParsers, "passiveCost", info.tariffConf.passiveCost);
123     AddParser(propertyParsers, "free", info.tariffConf.free);
124     AddParser(propertyParsers, "traffType", info.tariffConf.traffType, GetTraffType);
125     for (size_t i = 0; i < DIR_NUM; ++i)
126         AddParser(propertyParsers, "time" + unsigned2str(i), info.dirPrice[i], GetTimeSpan);
127     AddAOSParser(propertyParsers, "priceDayA", info.dirPrice, &DIRPRICE_DATA::priceDayA, GetSlashedValue);
128     AddAOSParser(propertyParsers, "priceDayB", info.dirPrice, &DIRPRICE_DATA::priceDayB, GetSlashedValue);
129     AddAOSParser(propertyParsers, "priceNightA", info.dirPrice, &DIRPRICE_DATA::priceNightA, GetSlashedValue);
130     AddAOSParser(propertyParsers, "priceNightB", info.dirPrice, &DIRPRICE_DATA::priceNightB, GetSlashedValue);
131     AddAOSParser(propertyParsers, "singlePrice", info.dirPrice, &DIRPRICE_DATA::singlePrice, GetSlashedValue);
132     AddAOSParser(propertyParsers, "noDiscount", info.dirPrice, &DIRPRICE_DATA::noDiscount, GetSlashedValue);
133     AddAOSParser(propertyParsers, "threshold", info.dirPrice, &DIRPRICE_DATA::threshold, GetSlashedValue);
134 }
135 //-----------------------------------------------------------------------------
136 GET_TARIFF::PARSER::~PARSER()
137 {
138     PROPERTY_PARSERS::iterator it(propertyParsers.begin());
139     while (it != propertyParsers.end())
140         delete (it++)->second;
141 }
142 //-----------------------------------------------------------------------------
143 int GET_TARIFF::PARSER::ParseStart(const char * el, const char ** attr)
144 {
145 depth++;
146 if (depth == 1)
147     ParseTariff(el, attr);
148
149 if (depth == 2 && parsingAnswer)
150     ParseTariffParams(el, attr);
151
152 return 0;
153 }
154 //-----------------------------------------------------------------------------
155 void GET_TARIFF::PARSER::ParseEnd(const char * /*el*/)
156 {
157 depth--;
158 if (depth == 0 && parsingAnswer)
159     {
160     if (callback)
161         callback(error.empty(), error, info, data);
162     error.clear();
163     parsingAnswer = false;
164     }
165 }
166 //-----------------------------------------------------------------------------
167 void GET_TARIFF::PARSER::ParseTariff(const char * el, const char ** attr)
168 {
169 if (strcasecmp(el, "tariff") == 0)
170     {
171     if (attr && attr[0] && attr[1])
172         {
173         if (strcasecmp(attr[1], "error") == 0)
174             {
175             if (attr[2] && attr[3])
176                 error = attr[3];
177             else
178                 error = "Tariff not found.";
179             }
180         else
181             parsingAnswer = true;
182         }
183     else
184         parsingAnswer = true;
185     }
186 }
187 //-----------------------------------------------------------------------------
188 void GET_TARIFF::PARSER::ParseTariffParams(const char * el, const char ** attr)
189 {
190 if (!TryParse(propertyParsers, ToLower(el), attr))
191     error = "Invalid parameter.";
192 }