]> git.stg.codes - stg.git/blobdiff - stglibs/srvconf.lib/parser_get_user.cpp
Moved GET_USER and GET_USERS parsers from global scope.
[stg.git] / stglibs / srvconf.lib / parser_get_user.cpp
index cef40da2c04ef2f28acf8521d33f513d04099f90..45f5e6ea92da090e8feb4bbb9ef373c022ea657d 100644 (file)
  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
  */
 
-#include "stg/parser_get_user.h"
+#include "parser_get_user.h"
 
 #include "stg/common.h"
 
-namespace
-{
-
-bool checkValue(const char ** attr)
-{
-return attr && attr[0] && attr[1] && strcasecmp(attr[0], "value") == 0;
-}
+#include <map>
+#include <utility>
+#include <cstddef>
 
-template <typename T>
-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<std::string>(const char ** attr)
-{
-if (checkValue(attr))
-    return attr[1];
-return "";
-}
+#include <strings.h>
 
-template <>
-double getValue<double>(const char ** attr)
-{
-double value = 0;
-if (checkValue(attr))
-    if (strtodouble2(attr[1], value) == EINVAL)
-        return 0;
-return value;
-}
+using namespace STG;
 
 template <>
-STAT getValue<STAT>(const char ** attr)
+bool GetValue<GET_USER::STAT>(const char ** attr, GET_USER::STAT & value)
 {
-STAT value;
 if (!attr)
-    return value;
-std::map<std::string, long long &> props;
+    return false;
+std::map<std::string, long long *> props;
 for (size_t i = 0; i < DIR_NUM; ++i)
     {
-    props.insert("su" + x2str(i), value.su[i]);
-    props.insert("sd" + x2str(i), value.sd[i]);
-    props.insert("mu" + x2str(i), value.mu[i]);
-    props.insert("md" + x2str(i), value.md[i]);
+    props.insert(std::pair<std::string, long long *>("su" + x2str(i), &value.su[i]));
+    props.insert(std::pair<std::string, long long *>("sd" + x2str(i), &value.sd[i]));
+    props.insert(std::pair<std::string, long long *>("mu" + x2str(i), &value.mu[i]));
+    props.insert(std::pair<std::string, long long *>("md" + x2str(i), &value.md[i]));
     }
 size_t pos = 0;
 while (attr[pos])
     {
         std::string name(ToLower(attr[pos++]));
-        std::map<std::string, long long &>::iterator it(props.find(name));
+        std::map<std::string, long long *>::iterator it(props.find(name));
         if (it != props.end())
-            str2x(attr[pos++], it->second);
+            if (str2x(attr[pos++], *it->second) < 0)
+                return false;
     }
-return value;
-}
-
-std::string getEncodedValue(const char ** attr)
-{
-if (checkValue(attr))
-    return Decode21str(attr[1]);
-return "";
+return true;
 }
 
-template <typename T>
-void addParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, bool /*encoded*/)
-{
-    parsers.insert(ToLower(name), new PROPERTY_PARSER(value, getValue<T>));
-}
-
-template <>
-void addParser<std::string>(PROPERTY_PARSERS & parsers, const std::string & name, std::string & value, bool encoded)
-{
-    if (encoded)
-        parsers.insert(ToLower(name), new PROPERTY_PARSER(value, getEncodedValue));
-    else
-        parsers.insert(ToLower(name), new PROPERTY_PARSER(value, getValue<T>));
-}
-
-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()
+GET_USER::PARSER::PARSER()
     : 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, "ip", info.ip);
-    addParser(propertyParsers, "ips", info.ips);
-    addParser(propertyParsers, "tariff", info.tariff);
-    addParser(propertyParsers, "group", info.group, true);
-    addParser(propertyParsers, "note", info.note, true);
-    addParser(propertyParsers, "email", info.email, true);
-    addParser(propertyParsers, "name", info.name, true);
-    addParser(propertyParsers, "address", info.address, true);
-    addParser(propertyParsers, "phone", info.phone, true);
-    addParser(propertyParsers, "traff", info.stat);
+      depth(0),
+      parsingAnswer(false)
+{
+    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, "aonline", 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], true);
+        AddParser(propertyParsers, "userData" + x2str(i), info.userData[i], GetEncodedValue);
 }
 //-----------------------------------------------------------------------------
-PARSER_GET_USER::~PARSER_GET_USER()
+GET_USER::PARSER::~PARSER()
 {
     PROPERTY_PARSERS::iterator it(propertyParsers.begin());
     while (it != propertyParsers.end())
         delete (it++)->second;
 }
 //-----------------------------------------------------------------------------
-int PARSER_GET_USER::ParseStart(const char *el, const char **attr)
+int GET_USER::PARSER::ParseStart(const char * el, const char ** attr)
 {
 depth++;
 if (depth == 1)
     ParseUser(el, attr);
 
-if (depth == 2)
+if (depth == 2 && parsingAnswer)
     ParseUserParams(el, attr);
 
 return 0;
 }
 //-----------------------------------------------------------------------------
-void PARSER_GET_USER::ParseEnd(const char *)
+void GET_USER::PARSER::ParseEnd(const char * /*el*/)
 {
 depth--;
-if (depth == 0)
+if (depth == 0 && parsingAnswer)
+    {
     if (callback)
-        callback(info, data);
+        callback(error.empty(), error, info, data);
+    error.clear();
+    parsingAnswer = false;
+    }
 }
 //-----------------------------------------------------------------------------
-void PARSER_GET_USER::ParseUser(const char * el, const char ** attr)
+void GET_USER::PARSER::ParseUser(const char * el, const char ** attr)
 {
 if (strcasecmp(el, "user") == 0)
-    if (strcasecmp(attr[1], "error") == 0)
-        user.login = "";
+    if (attr && attr[0] && attr[1])
+        {
+        if (strcasecmp(attr[1], "error") == 0)
+            {
+            if (attr[2] && attr[3])
+                error = attr[3];
+            else
+                error = "User not found.";
+            }
+        else
+            parsingAnswer = true;
+        }
+    else
+        parsingAnswer = true;
 }
 //-----------------------------------------------------------------------------
-void PARSER_GET_USER::ParseUserParams(const char * el, const char ** attr)
+void GET_USER::PARSER::ParseUserParams(const char * el, const char ** attr)
 {
-tryParse(propertyParsers, ToLower(el), attr);
+if (!TryParse(propertyParsers, ToLower(el), attr))
+    error = "Invalid parameter.";
 }
 //-----------------------------------------------------------------------------
-void PARSER_GET_USER::SetCallback(CALLBACK f, void * d)
+void GET_USER::PARSER::SetCallback(CALLBACK f, void * d)
 {
 callback = f;
 data = d;