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.
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.
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
18 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
21 #include "get_tariff.h"
23 #include "parsers/property.h"
25 #include "stg/common.h"
36 template <typename A, typename T>
37 class AOS_PARSER : public BASE_PROPERTY_PARSER
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); }
45 T A::value_type:: * field;
49 template <typename A, typename T>
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)
53 parsers.insert(std::make_pair(ToLower(name), new AOS_PARSER<A, T>(array, field, func)));
56 bool GetTimeSpan(const char ** attr, DIRPRICE_DATA & value, const std::string & attrName)
58 if (CheckValue(attr, attrName))
64 if (ParseTariffTimeStr(attr[1], hb, mb, he, me) == 0)
77 bool GetTraffType(const char ** attr, T & value, const std::string & attrName)
79 if (!CheckValue(attr, attrName))
81 value = TARIFF::StringToTraffType(attr[1]);
86 bool GetPeriod(const char ** attr, T & value, const std::string & attrName)
88 if (!CheckValue(attr, attrName))
90 std::string type(attr[1]);
93 else if (type == "month")
94 value = TARIFF::MONTH;
100 template <typename T>
101 bool GetChangePolicy(const char ** attr, T & value, const std::string & attrName)
103 if (!CheckValue(attr, attrName))
105 std::string type(attr[1]);
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;
119 template <typename T>
120 bool GetChangePolicyTimeout(const char ** attr, T & value, const std::string & attrName)
122 if (!CheckValue(attr, attrName))
124 value = readTime(attr[1]);
128 template <typename A, typename T>
129 bool GetSlashedValue(const char ** attr, A & array, T A::value_type:: * field)
131 if (!CheckValue(attr, "value"))
133 const char * start = attr[1];
135 const char * pos = NULL;
136 while ((pos = strchr(start, '/')) && item < array.size())
138 if (str2x(std::string(start, pos), array[item++].*field))
142 if (item < array.size())
143 if (str2x(start, array[item].*field))
148 } // namespace anonymous
150 GET_TARIFF::PARSER::PARSER(CALLBACK f, void * d, const std::string & e)
157 AddParser(propertyParsers, "fee", info.tariffConf.fee);
158 AddParser(propertyParsers, "passiveCost", info.tariffConf.passiveCost);
159 AddParser(propertyParsers, "free", info.tariffConf.free);
160 AddParser(propertyParsers, "traffType", info.tariffConf.traffType, GetTraffType);
161 AddParser(propertyParsers, "period", info.tariffConf.period, GetPeriod);
162 AddParser(propertyParsers, "changePolicy", info.tariffConf.changePolicy, GetChangePolicy);
163 AddParser(propertyParsers, "changePolicyTimeout", info.tariffConf.changePolicyTimeout, GetChangePolicyTimeout);
164 for (size_t i = 0; i < DIR_NUM; ++i)
165 AddParser(propertyParsers, "time" + unsigned2str(i), info.dirPrice[i], GetTimeSpan);
166 AddAOSParser(propertyParsers, "priceDayA", info.dirPrice, &DIRPRICE_DATA::priceDayA, GetSlashedValue);
167 AddAOSParser(propertyParsers, "priceDayB", info.dirPrice, &DIRPRICE_DATA::priceDayB, GetSlashedValue);
168 AddAOSParser(propertyParsers, "priceNightA", info.dirPrice, &DIRPRICE_DATA::priceNightA, GetSlashedValue);
169 AddAOSParser(propertyParsers, "priceNightB", info.dirPrice, &DIRPRICE_DATA::priceNightB, GetSlashedValue);
170 AddAOSParser(propertyParsers, "singlePrice", info.dirPrice, &DIRPRICE_DATA::singlePrice, GetSlashedValue);
171 AddAOSParser(propertyParsers, "noDiscount", info.dirPrice, &DIRPRICE_DATA::noDiscount, GetSlashedValue);
172 AddAOSParser(propertyParsers, "threshold", info.dirPrice, &DIRPRICE_DATA::threshold, GetSlashedValue);
174 //-----------------------------------------------------------------------------
175 GET_TARIFF::PARSER::~PARSER()
177 PROPERTY_PARSERS::iterator it(propertyParsers.begin());
178 while (it != propertyParsers.end())
179 delete (it++)->second;
181 //-----------------------------------------------------------------------------
182 int GET_TARIFF::PARSER::ParseStart(const char * el, const char ** attr)
186 ParseTariff(el, attr);
188 if (depth == 2 && parsingAnswer)
189 ParseTariffParams(el, attr);
193 //-----------------------------------------------------------------------------
194 void GET_TARIFF::PARSER::ParseEnd(const char * /*el*/)
197 if (depth == 0 && parsingAnswer)
200 callback(error.empty(), error, info, data);
202 parsingAnswer = false;
205 //-----------------------------------------------------------------------------
206 void GET_TARIFF::PARSER::ParseTariff(const char * el, const char ** attr)
208 if (strcasecmp(el, "tariff") == 0)
210 if (attr && attr[0] && attr[1])
212 if (strcasecmp(attr[1], "error") == 0)
214 if (attr[2] && attr[3])
217 error = "Tariff not found.";
221 parsingAnswer = true;
222 if (strcasecmp(attr[0], "name") == 0)
223 info.tariffConf.name = attr[1];
227 parsingAnswer = true;
230 //-----------------------------------------------------------------------------
231 void GET_TARIFF::PARSER::ParseTariffParams(const char * el, const char ** attr)
233 if (!TryParse(propertyParsers, ToLower(el), attr, encoding))
234 error = std::string("Invalid parameter '") + el + "'.";