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