]> git.stg.codes - stg.git/blob - stglibs/srvconf.lib/parsers/chg_tariff.cpp
Added tariff parsers/serializers.
[stg.git] / stglibs / srvconf.lib / parsers / chg_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 "chg_tariff.h"
22
23 #include "stg/tariff_conf.h"
24 #include "stg/common.h"
25
26 #include <sstream>
27
28 #include <strings.h>
29
30 using namespace STG;
31
32 namespace
33 {
34
35 template <typename T>
36 void appendResetable(std::ostream & stream, const std::string & name, const T & value)
37 {
38 if (!value.empty())
39     stream << "<" << name << " value=\"" << value.data() << "\"/>";
40 }
41
42 template <typename A, typename T>
43 void appendSlashedResetable(std::ostream & stream, const std::string & name, const A & array, T A::value_type:: * field)
44 {
45 std::string res;
46 for (typename A::size_type i = 0; i < array.size(); ++i)
47     {
48     if ((array[i].*field).empty()) // All values must be set
49         return;
50     if (!res.empty())
51         res += "/";
52     res += x2str((array[i].*field).data());
53     }
54 stream << "<" << name << " value=\"" << res << "\"/>";
55 }
56
57 } // namespace anonymous
58
59 std::string CHG_TARIFF::Serialize(const TARIFF_DATA_RES & data)
60 {
61 std::ostringstream stream;
62
63 appendResetable(stream, "fee", data.tariffConf.fee);
64 appendResetable(stream, "passiveCost", data.tariffConf.passiveCost);
65 appendResetable(stream, "free", data.tariffConf.free);
66
67 if (!data.tariffConf.traffType.empty())
68     switch (data.tariffConf.traffType.data())
69         {
70         case TRAFF_UP: stream << "<traffType value=\"up\"/>"; break;
71         case TRAFF_DOWN: stream << "<traffType value=\"down\"/>"; break;
72         case TRAFF_UP_DOWN: stream << "<traffType value=\"up+down\"/>"; break;
73         case TRAFF_MAX: stream << "<traffType value=\"max\"/>"; break;
74         }
75
76 for (size_t i = 0; i < DIR_NUM; ++i)
77     if (!data.dirPrice[i].hDay.empty() &&
78         !data.dirPrice[i].mDay.empty() &&
79         !data.dirPrice[i].hNight.empty() &&
80         !data.dirPrice[i].mNight.empty())
81         stream << "<time" << i << " value=\"" << data.dirPrice[i].hDay.data() << ":"
82                                               << data.dirPrice[i].mDay.data() << "-"
83                                               << data.dirPrice[i].hNight.data() << ":"
84                                               << data.dirPrice[i].mNight.data() << "\"/>";
85
86 appendSlashedResetable(stream, "priceDayA", data.dirPrice, &DIRPRICE_DATA_RES::priceDayA);
87 appendSlashedResetable(stream, "priceDayB", data.dirPrice, &DIRPRICE_DATA_RES::priceDayB);
88 appendSlashedResetable(stream, "priceNightA", data.dirPrice, &DIRPRICE_DATA_RES::priceNightA);
89 appendSlashedResetable(stream, "priceNightB", data.dirPrice, &DIRPRICE_DATA_RES::priceNightB);
90 appendSlashedResetable(stream, "singlePrice", data.dirPrice, &DIRPRICE_DATA_RES::singlePrice);
91 appendSlashedResetable(stream, "noDiscount", data.dirPrice, &DIRPRICE_DATA_RES::noDiscount);
92
93 return stream.str();
94 }