]> git.stg.codes - stg.git/blob - stglibs/srvconf.lib/parser_get_user.cpp
Different small fixes plus error logging.
[stg.git] / stglibs / srvconf.lib / parser_get_user.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 : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
20  */
21
22 #include "stg/parser_get_user.h"
23
24 #include "stg/common.h"
25
26 #include <utility>
27 #include <cstddef>
28
29 #include <strings.h>
30
31 namespace
32 {
33
34 bool checkValue(const char ** attr)
35 {
36 return attr && attr[0] && attr[1] && strcasecmp(attr[0], "value") == 0;
37 }
38
39 template <typename T>
40 T getValue(const char ** attr)
41 {
42 T value = 0;
43 if (checkValue(attr))
44     if (str2x(attr[1], value) < 0)
45         return 0;
46 return value;
47 }
48
49 template <>
50 std::string getValue<std::string>(const char ** attr)
51 {
52 if (checkValue(attr))
53     return attr[1];
54 return "";
55 }
56
57 template <>
58 double getValue<double>(const char ** attr)
59 {
60 double value = 0;
61 if (checkValue(attr))
62     if (strtodouble2(attr[1], value))
63         return 0;
64 return value;
65 }
66
67 template <>
68 PARSER_GET_USER::STAT getValue<PARSER_GET_USER::STAT>(const char ** attr)
69 {
70 PARSER_GET_USER::STAT value;
71 if (!attr)
72     return value;
73 std::map<std::string, long long *> props;
74 for (size_t i = 0; i < DIR_NUM; ++i)
75     {
76     props.insert(std::pair<std::string, long long *>("su" + x2str(i), &value.su[i]));
77     props.insert(std::pair<std::string, long long *>("sd" + x2str(i), &value.sd[i]));
78     props.insert(std::pair<std::string, long long *>("mu" + x2str(i), &value.mu[i]));
79     props.insert(std::pair<std::string, long long *>("md" + x2str(i), &value.md[i]));
80     }
81 size_t pos = 0;
82 while (attr[pos])
83     {
84         std::string name(ToLower(attr[pos++]));
85         std::map<std::string, long long *>::iterator it(props.find(name));
86         if (it != props.end())
87             str2x(attr[pos++], *it->second);
88     }
89 return value;
90 }
91
92 std::string getEncodedValue(const char ** attr)
93 {
94 std::string value;
95 if (checkValue(attr))
96     Decode21str(value, attr[1]);
97 return value;
98 }
99
100 template <typename T>
101 void addParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, bool encoded = false);
102
103 template <typename T>
104 void addParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, bool /*encoded*/)
105 {
106     parsers.insert(std::make_pair(ToLower(name), new PROPERTY_PARSER<T>(value, getValue<T>)));
107 }
108
109 template <>
110 void addParser<std::string>(PROPERTY_PARSERS & parsers, const std::string & name, std::string & value, bool encoded)
111 {
112     if (encoded)
113         parsers.insert(std::make_pair(ToLower(name), new PROPERTY_PARSER<std::string>(value, getEncodedValue)));
114     else
115         parsers.insert(std::make_pair(ToLower(name), new PROPERTY_PARSER<std::string>(value, getValue<std::string>)));
116 }
117
118 void tryParse(PROPERTY_PARSERS & parsers, const std::string & name, const char ** attr)
119 {
120     PROPERTY_PARSERS::iterator it(parsers.find(name));
121     if (it != parsers.end())
122         it->second->Parse(attr);
123 }
124
125 } // namespace anonymous
126
127 PARSER_GET_USER::PARSER_GET_USER()
128     : callback(NULL),
129       data(NULL),
130       depth(0)
131 {
132     addParser(propertyParsers, "login", info.login);
133     addParser(propertyParsers, "password", info.password);
134     addParser(propertyParsers, "cash", info.cash);
135     addParser(propertyParsers, "credit", info.credit);
136     addParser(propertyParsers, "creditExpire", info.creditExpire);
137     addParser(propertyParsers, "lastCash", info.lastCash);
138     addParser(propertyParsers, "prepaidTraff", info.prepaidTraff);
139     addParser(propertyParsers, "down", info.down);
140     addParser(propertyParsers, "passive", info.passive);
141     addParser(propertyParsers, "disableDetailStat", info.disableDetailStat);
142     addParser(propertyParsers, "connected", info.connected);
143     addParser(propertyParsers, "alwaysOnline", info.alwaysOnline);
144     addParser(propertyParsers, "ip", info.ip);
145     addParser(propertyParsers, "ips", info.ips);
146     addParser(propertyParsers, "tariff", info.tariff);
147     addParser(propertyParsers, "group", info.group, true);
148     addParser(propertyParsers, "note", info.note, true);
149     addParser(propertyParsers, "email", info.email, true);
150     addParser(propertyParsers, "name", info.name, true);
151     addParser(propertyParsers, "address", info.address, true);
152     addParser(propertyParsers, "phone", info.phone, true);
153     addParser(propertyParsers, "traff", info.stat);
154
155     for (size_t i = 0; i < USERDATA_NUM; ++i)
156         addParser(propertyParsers, "userData" + x2str(i), info.userData[i], true);
157 }
158 //-----------------------------------------------------------------------------
159 PARSER_GET_USER::~PARSER_GET_USER()
160 {
161     PROPERTY_PARSERS::iterator it(propertyParsers.begin());
162     while (it != propertyParsers.end())
163         delete (it++)->second;
164 }
165 //-----------------------------------------------------------------------------
166 int PARSER_GET_USER::ParseStart(const char *el, const char **attr)
167 {
168 depth++;
169 if (depth == 1)
170     ParseUser(el, attr);
171
172 if (depth == 2)
173     ParseUserParams(el, attr);
174
175 return 0;
176 }
177 //-----------------------------------------------------------------------------
178 void PARSER_GET_USER::ParseEnd(const char *)
179 {
180 depth--;
181 if (depth == 0)
182     if (callback)
183         callback(info, data);
184 }
185 //-----------------------------------------------------------------------------
186 void PARSER_GET_USER::ParseUser(const char * el, const char ** attr)
187 {
188 if (strcasecmp(el, "user") == 0)
189     if (strcasecmp(attr[1], "error") == 0)
190         info.login = "";
191 }
192 //-----------------------------------------------------------------------------
193 void PARSER_GET_USER::ParseUserParams(const char * el, const char ** attr)
194 {
195 tryParse(propertyParsers, ToLower(el), attr);
196 }
197 //-----------------------------------------------------------------------------
198 void PARSER_GET_USER::SetCallback(CALLBACK f, void * d)
199 {
200 callback = f;
201 data = d;
202 }