]> git.stg.codes - stg.git/blob - stglibs/srvconf.lib/parsers/get_tariff.cpp
Added tariff parsers/serializers.
[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 <map>
28 #include <utility>
29
30 #include <strings.h>
31
32 using namespace STG;
33
34 namespace
35 {
36
37 template <typename A, typename T>
38 class AOS_PARSER : public BASE_PROPERTY_PARSER
39 {
40     public:
41         typedef bool (* FUNC)(const char **, A &, T A::value_type:: *);
42         AOS_PARSER(A & a, T A::value_type:: * fld, FUNC f) : array(a), field(fld), func(f) {}
43         virtual bool Parse(const char ** attr) { return func(attr, array, field); }
44     private:
45         A & array;
46         T A::value_type:: * field;
47         FUNC func;
48 };
49
50 template <typename A, typename T>
51 inline
52 void AddAOSParser(PROPERTY_PARSERS & parsers, const std::string & name, A & array, T A::value_type:: * field, const typename AOS_PARSER<A, T>::FUNC & func)
53 {
54     parsers.insert(std::make_pair(ToLower(name), new AOS_PARSER<A, T>(array, field, func)));
55 }
56
57 bool GetTimeSpan(const char ** attr, DIRPRICE_DATA & value)
58 {
59 int hb = 0;
60 int mb = 0;
61 int he = 0;
62 int me = 0;
63 if (CheckValue(attr))
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 return false;
73 }
74
75 template <typename T>
76 bool GetTraffType(const char ** attr, T & value)
77 {
78 if (!CheckValue(attr))
79     return false;
80 std::string type(attr[1]);
81 if (type == "up")
82     value = TRAFF_UP;
83 else if (type == "down")
84     value = TRAFF_DOWN;
85 else if (type == "up+down")
86     value = TRAFF_UP_DOWN;
87 else if (type == "max")
88     value = TRAFF_MAX;
89 else
90     return false;
91 return true;
92 }
93
94 template <typename A, typename T>
95 bool GetSlashedValue(const char ** attr, A & array, T A::value_type:: * field)
96 {
97 if (!CheckValue(attr))
98     return false;
99 const char * start = attr[1];
100 size_t item = 0;
101 const char * pos = NULL;
102 while ((pos = strchr(start, '/')) && item < array.size())
103     {
104     if (str2x(std::string(start, pos), array[item++].*field))
105             return false;
106     start = pos + 1;
107     }
108 if (item < array.size())
109     if (str2x(start, array[item].*field))
110         return false;
111 return true;
112 }
113
114 } // namespace anonymous
115
116 GET_TARIFF::PARSER::PARSER(CALLBACK f, void * d)
117     : callback(f),
118       data(d),
119       depth(0),
120       parsingAnswer(false)
121 {
122     AddParser(propertyParsers, "fee", info.tariffConf.fee);
123     AddParser(propertyParsers, "passiveCost", info.tariffConf.passiveCost);
124     AddParser(propertyParsers, "free", info.tariffConf.free);
125     AddParser(propertyParsers, "traffType", info.tariffConf.traffType, GetTraffType);
126     for (size_t i = 0; i < DIR_NUM; ++i)
127         AddParser(propertyParsers, "time" + unsigned2str(i), info.dirPrice[i], GetTimeSpan);
128     AddAOSParser(propertyParsers, "priceDayA", info.dirPrice, &DIRPRICE_DATA::priceDayA, GetSlashedValue);
129     AddAOSParser(propertyParsers, "priceDayB", info.dirPrice, &DIRPRICE_DATA::priceDayB, GetSlashedValue);
130     AddAOSParser(propertyParsers, "priceNightA", info.dirPrice, &DIRPRICE_DATA::priceNightA, GetSlashedValue);
131     AddAOSParser(propertyParsers, "priceNightB", info.dirPrice, &DIRPRICE_DATA::priceNightB, GetSlashedValue);
132     AddAOSParser(propertyParsers, "singlePrice", info.dirPrice, &DIRPRICE_DATA::singlePrice, GetSlashedValue);
133     AddAOSParser(propertyParsers, "noDiscount", info.dirPrice, &DIRPRICE_DATA::noDiscount, 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 }