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"
33 bool checkValue(const char ** attr)
35 return attr && attr[0] && attr[1] && strcasecmp(attr[0], "value") == 0;
39 T getValue(const char ** attr)
43 if (str2x(attr[1], value) < 0)
49 std::string getValue<std::string>(const char ** attr)
57 double getValue<double>(const char ** attr)
61 if (strtodouble2(attr[1], value) == EINVAL)
67 STAT getValue<STAT>(const char ** attr)
72 std::map<std::string, long long &> props;
73 for (size_t i = 0; i < DIR_NUM; ++i)
75 props.insert("su" + x2str(i), value.su[i]);
76 props.insert("sd" + x2str(i), value.sd[i]);
77 props.insert("mu" + x2str(i), value.mu[i]);
78 props.insert("md" + x2str(i), value.md[i]);
83 std::string name(ToLower(attr[pos++]));
84 std::map<std::string, long long &>::iterator it(props.find(name));
85 if (it != props.end())
86 str2x(attr[pos++], it->second);
91 std::string getEncodedValue(const char ** attr)
94 return Decode21str(attr[1]);
99 void addParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, bool /*encoded*/)
101 parsers.insert(ToLower(name), new PROPERTY_PARSER(value, getValue<T>));
105 void addParser<std::string>(PROPERTY_PARSERS & parsers, const std::string & name, std::string & value, bool encoded)
108 parsers.insert(ToLower(name), new PROPERTY_PARSER(value, getEncodedValue));
110 parsers.insert(ToLower(name), new PROPERTY_PARSER(value, getValue<T>));
113 void tryParse(PROPERTY_PARSERS & parsers, const std::string & name, const char ** attr)
115 PROPERTY_PARSERS::iterator it(parsers.find(name));
116 if (it != parsers.end())
117 it->second->Parse(attr);
120 } // namespace anonymous
122 PARSER_GET_USER::PARSER_GET_USER()
127 addParser(propertyParsers, "login", info.login);
128 addParser(propertyParsers, "password", info.password);
129 addParser(propertyParsers, "cash", info.cash);
130 addParser(propertyParsers, "credit", info.credit);
131 addParser(propertyParsers, "creditExpire", info.creditExpire);
132 addParser(propertyParsers, "lastCash", info.lastCash);
133 addParser(propertyParsers, "prepaidTraff", info.prepaidTraff);
134 addParser(propertyParsers, "down", info.down);
135 addParser(propertyParsers, "passive", info.passive);
136 addParser(propertyParsers, "disableDetailStat", info.disableDetailStat);
137 addParser(propertyParsers, "connected", info.connected);
138 addParser(propertyParsers, "alwaysOnline", info.alwaysOnline);
139 addParser(propertyParsers, "ip", info.ip);
140 addParser(propertyParsers, "ips", info.ips);
141 addParser(propertyParsers, "tariff", info.tariff);
142 addParser(propertyParsers, "group", info.group, true);
143 addParser(propertyParsers, "note", info.note, true);
144 addParser(propertyParsers, "email", info.email, true);
145 addParser(propertyParsers, "name", info.name, true);
146 addParser(propertyParsers, "address", info.address, true);
147 addParser(propertyParsers, "phone", info.phone, true);
148 addParser(propertyParsers, "traff", info.stat);
150 for (size_t i = 0; i < USERDATA_NUM; ++i)
151 addParser(propertyParsers, "userData" + x2str(i), info.userData[i], true);
153 //-----------------------------------------------------------------------------
154 PARSER_GET_USER::~PARSER_GET_USER()
156 PROPERTY_PARSERS::iterator it(propertyParsers.begin());
157 while (it != propertyParsers.end())
158 delete (it++)->second;
160 //-----------------------------------------------------------------------------
161 int PARSER_GET_USER::ParseStart(const char *el, const char **attr)
168 ParseUserParams(el, attr);
172 //-----------------------------------------------------------------------------
173 void PARSER_GET_USER::ParseEnd(const char *)
178 callback(info, data);
180 //-----------------------------------------------------------------------------
181 void PARSER_GET_USER::ParseUser(const char * el, const char ** attr)
183 if (strcasecmp(el, "user") == 0)
184 if (strcasecmp(attr[1], "error") == 0)
187 //-----------------------------------------------------------------------------
188 void PARSER_GET_USER::ParseUserParams(const char * el, const char ** attr)
190 tryParse(propertyParsers, ToLower(el), attr);
192 //-----------------------------------------------------------------------------
193 void PARSER_GET_USER::SetCallback(CALLBACK f, void * d)