/* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* * Author : Boris Mikhailenko * Author : Maxim Mamontov */ #include "stg/parser_get_user.h" #include "stg/common.h" #include #include #include namespace { bool checkValue(const char ** attr) { return attr && attr[0] && attr[1] && strcasecmp(attr[0], "value") == 0; } template T getValue(const char ** attr) { T value = 0; if (checkValue(attr)) if (str2x(attr[1], value) < 0) return 0; return value; } template <> std::string getValue(const char ** attr) { if (checkValue(attr)) return attr[1]; return ""; } template <> double getValue(const char ** attr) { double value = 0; if (checkValue(attr)) if (strtodouble2(attr[1], value)) return 0; return value; } template <> PARSER_GET_USER::STAT getValue(const char ** attr) { PARSER_GET_USER::STAT value; if (!attr) return value; std::map props; for (size_t i = 0; i < DIR_NUM; ++i) { props.insert(std::pair("su" + x2str(i), &value.su[i])); props.insert(std::pair("sd" + x2str(i), &value.sd[i])); props.insert(std::pair("mu" + x2str(i), &value.mu[i])); props.insert(std::pair("md" + x2str(i), &value.md[i])); } size_t pos = 0; while (attr[pos]) { std::string name(ToLower(attr[pos++])); std::map::iterator it(props.find(name)); if (it != props.end()) str2x(attr[pos++], *it->second); } return value; } std::string getEncodedValue(const char ** attr) { std::string value; if (checkValue(attr)) Decode21str(value, attr[1]); return value; } uint32_t getIPValue(const char ** attr) { if (checkValue(attr)) return inet_strington(attr[1]); return 0; } template void addParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, const typename PROPERTY_PARSER::FUNC & func = getValue); template void addParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, const typename PROPERTY_PARSER::FUNC & func) { parsers.insert(std::make_pair(ToLower(name), new PROPERTY_PARSER(value, func))); } void tryParse(PROPERTY_PARSERS & parsers, const std::string & name, const char ** attr) { PROPERTY_PARSERS::iterator it(parsers.find(name)); if (it != parsers.end()) it->second->Parse(attr); } } // namespace anonymous PARSER_GET_USER::PARSER_GET_USER() : callback(NULL), data(NULL), depth(0) { addParser(propertyParsers, "login", info.login); addParser(propertyParsers, "password", info.password); addParser(propertyParsers, "cash", info.cash); addParser(propertyParsers, "credit", info.credit); addParser(propertyParsers, "creditExpire", info.creditExpire); addParser(propertyParsers, "lastCash", info.lastCash); addParser(propertyParsers, "prepaidTraff", info.prepaidTraff); addParser(propertyParsers, "down", info.down); addParser(propertyParsers, "passive", info.passive); addParser(propertyParsers, "disableDetailStat", info.disableDetailStat); addParser(propertyParsers, "connected", info.connected); addParser(propertyParsers, "alwaysOnline", info.alwaysOnline); addParser(propertyParsers, "currIP", info.ip, getIPValue); addParser(propertyParsers, "ip", info.ips); addParser(propertyParsers, "tariff", info.tariff); addParser(propertyParsers, "group", info.group, getEncodedValue); addParser(propertyParsers, "note", info.note, getEncodedValue); addParser(propertyParsers, "email", info.email, getEncodedValue); addParser(propertyParsers, "name", info.name, getEncodedValue); addParser(propertyParsers, "address", info.address, getEncodedValue); addParser(propertyParsers, "phone", info.phone, getEncodedValue); addParser(propertyParsers, "traff", info.stat); for (size_t i = 0; i < USERDATA_NUM; ++i) addParser(propertyParsers, "userData" + x2str(i), info.userData[i], getEncodedValue); } //----------------------------------------------------------------------------- PARSER_GET_USER::~PARSER_GET_USER() { PROPERTY_PARSERS::iterator it(propertyParsers.begin()); while (it != propertyParsers.end()) delete (it++)->second; } //----------------------------------------------------------------------------- int PARSER_GET_USER::ParseStart(const char *el, const char **attr) { depth++; if (depth == 1) ParseUser(el, attr); if (depth == 2) ParseUserParams(el, attr); return 0; } //----------------------------------------------------------------------------- void PARSER_GET_USER::ParseEnd(const char *) { depth--; if (depth == 0) if (callback) callback(info, data); } //----------------------------------------------------------------------------- void PARSER_GET_USER::ParseUser(const char * el, const char ** attr) { if (strcasecmp(el, "user") == 0) if (strcasecmp(attr[1], "error") == 0) info.login = ""; } //----------------------------------------------------------------------------- void PARSER_GET_USER::ParseUserParams(const char * el, const char ** attr) { tryParse(propertyParsers, ToLower(el), attr); } //----------------------------------------------------------------------------- void PARSER_GET_USER::SetCallback(CALLBACK f, void * d) { callback = f; data = d; }