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 : Boris Mikhailenko <stg34@stargazer.dp.ua>
19 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
22 #include "stg/parser_get_user.h"
24 #include "stg/common.h"
34 bool checkValue(const char ** attr)
36 return attr && attr[0] && attr[1] && strcasecmp(attr[0], "value") == 0;
40 T getValue(const char ** attr)
44 if (str2x(attr[1], value) < 0)
50 std::string getValue<std::string>(const char ** attr)
58 double getValue<double>(const char ** attr)
62 if (strtodouble2(attr[1], value))
68 PARSER_GET_USER::STAT getValue<PARSER_GET_USER::STAT>(const char ** attr)
70 PARSER_GET_USER::STAT value;
73 std::map<std::string, long long *> props;
74 for (size_t i = 0; i < DIR_NUM; ++i)
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]));
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);
92 std::string getEncodedValue(const char ** attr)
96 Decode21str(value, attr[1]);
100 uint32_t getIPValue(const char ** attr)
102 if (checkValue(attr))
103 return inet_strington(attr[1]);
107 template <typename T>
108 void addParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, const typename PROPERTY_PARSER<T>::FUNC & func = getValue<T>);
110 template <typename T>
111 void addParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, const typename PROPERTY_PARSER<T>::FUNC & func)
113 parsers.insert(std::make_pair(ToLower(name), new PROPERTY_PARSER<T>(value, func)));
116 void tryParse(PROPERTY_PARSERS & parsers, const std::string & name, const char ** attr)
118 PROPERTY_PARSERS::iterator it(parsers.find(name));
119 if (it != parsers.end())
120 it->second->Parse(attr);
123 } // namespace anonymous
125 PARSER_GET_USER::PARSER_GET_USER()
130 addParser(propertyParsers, "login", info.login);
131 addParser(propertyParsers, "password", info.password);
132 addParser(propertyParsers, "cash", info.cash);
133 addParser(propertyParsers, "credit", info.credit);
134 addParser(propertyParsers, "creditExpire", info.creditExpire);
135 addParser(propertyParsers, "lastCash", info.lastCash);
136 addParser(propertyParsers, "prepaidTraff", info.prepaidTraff);
137 addParser(propertyParsers, "down", info.down);
138 addParser(propertyParsers, "passive", info.passive);
139 addParser(propertyParsers, "disableDetailStat", info.disableDetailStat);
140 addParser(propertyParsers, "connected", info.connected);
141 addParser(propertyParsers, "alwaysOnline", info.alwaysOnline);
142 addParser(propertyParsers, "currIP", info.ip, getIPValue);
143 addParser(propertyParsers, "ip", info.ips);
144 addParser(propertyParsers, "tariff", info.tariff);
145 addParser(propertyParsers, "group", info.group, getEncodedValue);
146 addParser(propertyParsers, "note", info.note, getEncodedValue);
147 addParser(propertyParsers, "email", info.email, getEncodedValue);
148 addParser(propertyParsers, "name", info.name, getEncodedValue);
149 addParser(propertyParsers, "address", info.address, getEncodedValue);
150 addParser(propertyParsers, "phone", info.phone, getEncodedValue);
151 addParser(propertyParsers, "traff", info.stat);
153 for (size_t i = 0; i < USERDATA_NUM; ++i)
154 addParser(propertyParsers, "userData" + x2str(i), info.userData[i], getEncodedValue);
156 //-----------------------------------------------------------------------------
157 PARSER_GET_USER::~PARSER_GET_USER()
159 PROPERTY_PARSERS::iterator it(propertyParsers.begin());
160 while (it != propertyParsers.end())
161 delete (it++)->second;
163 //-----------------------------------------------------------------------------
164 int PARSER_GET_USER::ParseStart(const char *el, const char **attr)
171 ParseUserParams(el, attr);
175 //-----------------------------------------------------------------------------
176 void PARSER_GET_USER::ParseEnd(const char *)
181 callback(info, data);
183 //-----------------------------------------------------------------------------
184 void PARSER_GET_USER::ParseUser(const char * el, const char ** attr)
186 if (strcasecmp(el, "user") == 0)
187 if (strcasecmp(attr[1], "error") == 0)
190 //-----------------------------------------------------------------------------
191 void PARSER_GET_USER::ParseUserParams(const char * el, const char ** attr)
193 tryParse(propertyParsers, ToLower(el), attr);
195 //-----------------------------------------------------------------------------
196 void PARSER_GET_USER::SetCallback(CALLBACK f, void * d)