//-----------------------------------------------------------------------------
struct GetUserData
{
+ GetUserData(REQUEST & req, bool res) : request(req), result(res) {}
REQUEST & request;
bool result;
};
//-----------------------------------------------------------------------------
int RecvSetUserAnswer(const char * ans, void * d)
{
-GetUserCbData * gucbd;
-gucbd = (GetUserCbData *)d;
-
-bool * result = gucbd->result;
-
-//REQUEST * req = (REQUEST *)gucbd->data;
+GetUserData * data = static_cast<GetUserData *>(d);
-//printf("ans=%s\n", ans);
-if (strcasecmp("Ok", ans) == 0)
- *result = true;
-else
- *result = false;
+data->result = (strcasecmp("Ok", ans) == 0);
return 0;
}
{
string name;
RESETABLE<string> reqParam;
- string * value;
+ const string * value;
};
//-----------------------------------------------------------------------------
void GetUserCallback(const PARSER_GET_USER::INFO & info, void * d)
{
GetUserData * data = static_cast<GetUserData *>(d);
-REQUEST * req = (REQUEST *)gucbd->data;
-
if (info.login == "")
{
data->result = false;
if (!data->request.cash.res_empty())
cout << "cash=" << info.cash << endl;
-if (!data->requst.credit.res_empty())
+if (!data->request.credit.res_empty())
cout << "credit=" << info.credit << endl;
-if (!data->requst.creditExpire.res_empty())
+if (!data->request.creditExpire.res_empty())
{
char buf[32];
struct tm brokenTime;
cout << "creditExpire=" << buf << endl;
}
-if (!data->requst.down.res_empty())
+if (!data->request.down.res_empty())
cout << "down=" << info.down << endl;
-if (!data->requst.passive.res_empty())
+if (!data->request.passive.res_empty())
cout << "passive=" << info.passive << endl;
-if (!data->requst.disableDetailStat.res_empty())
+if (!data->request.disableDetailStat.res_empty())
cout << "disableDetailStat=" << info.disableDetailStat << endl;
-if (!data->requst.alwaysOnline.res_empty())
+if (!data->request.alwaysOnline.res_empty())
cout << "alwaysOnline=" << info.alwaysOnline << endl;
-if (!data->requst.prepaidTraff.res_empty())
+if (!data->request.prepaidTraff.res_empty())
cout << "prepaidTraff=" << info.prepaidTraff << endl;
for (int i = 0; i < DIR_NUM; i++)
{
- if (!data->requst.sessionUpload[i].res_empty())
+ if (!data->request.sessionUpload[i].res_empty())
cout << "session upload for dir" << i << "=" << info.stat.su[i] << endl;
- if (!data->requst.sessionDownload[i].res_empty())
+ if (!data->request.sessionDownload[i].res_empty())
cout << "session download for dir" << i << "=" << info.stat.sd[i] << endl;
}
for (int i = 0; i < DIR_NUM; i++)
{
- if (!data->requst.monthUpload[i].res_empty())
+ if (!data->request.monthUpload[i].res_empty())
cout << "month upload for dir" << i << "=" << info.stat.mu[i] << endl;
- if (!data->requst.monthDownload[i].res_empty())
+ if (!data->request.monthDownload[i].res_empty())
cout << "month download for dir" << i << "=" << info.stat.md[i] << endl;
}
for (int i = 0; i < USERDATA_NUM; i++)
{
- if (!data->requst.ud[i].res_empty())
+ if (!data->request.userData[i].res_empty())
{
string str;
ConvertFromKOI8(info.userData[i], &str);
StringReqParams strReqParams[] =
{
- {"note", data->requst.note, &info.note},
- {"name", data->requst.name, &info.name},
- {"address", data->requst.address, &info.address},
- {"email", data->requst.email, &info.email},
- {"phone", data->requst.phone, &info.phone},
- {"group", data->requst.group, &info.group},
- {"tariff", data->requst.tariff, &info.tariff},
- {"password", data->requst.usrPasswd, &info.password},
- {"ip", data->requst.ips, &info.ips} // IP-address of user
+ {"note", data->request.note, &info.note},
+ {"name", data->request.name, &info.name},
+ {"address", data->request.address, &info.address},
+ {"email", data->request.email, &info.email},
+ {"phone", data->request.phone, &info.phone},
+ {"group", data->request.group, &info.group},
+ {"tariff", data->request.tariff, &info.tariff},
+ {"password", data->request.usrPasswd, &info.password},
+ {"ip", data->request.ips, &info.ips} // IP-address of user
};
for (unsigned i = 0; i < sizeof(strReqParams) / sizeof(StringReqParams); i++)
{
bool result = false;
-
sc.SetServer(server.c_str());
sc.SetPort(port);
sc.SetAdmLogin(admLogin.c_str());
sc.SetAdmPassword(admPasswd.c_str());
-// TODO Good variable name :)
-GetUserCbData gucbd;
-
-gucbd.data = data;
-gucbd.result = &result;
+REQUEST request;
+GetUserData cbdata(request, false);
if (isMessage)
{
- sc.SetSendMessageCb(RecvSetUserAnswer, &gucbd);
+ sc.SetSendMessageCb(RecvSetUserAnswer, &cbdata);
sc.MsgUser(str.c_str());
}
else
{
- sc.SetChgUserCb(RecvSetUserAnswer, &gucbd);
+ sc.SetChgUserCb(RecvSetUserAnswer, &cbdata);
sc.ChgUser(str.c_str());
}
{
SERVCONF sc;
-bool result = false;
-
sc.SetServer(server.c_str());
sc.SetPort(port);
sc.SetAdmLogin(admLogin.c_str());
sc.SetAdmPassword(admPasswd.c_str());
// TODO Good variable name :)
-GetUserCbData gucbd;
-
-gucbd.data = &request;
-gucbd.result = &result;
+GetUserData data(request, false);
-sc.SetGetUserCallback(GetUserCallback, &gucbd);
+sc.SetGetUserCallback(GetUserCallback, &data);
sc.GetUser(login.c_str());
-if (result)
+if (data.result)
{
printf("Ok\n");
return 0;
#include "common_sg.h"
#include "sg_error_codes.h"
-using namespace std;
+namespace
+{
+
+template <typename T>
+struct ARRAY_TYPE;
+
+template <typename T>
+struct ARRAY_TYPE<T[]>
+{
+typedef T type;
+};
+
+template <typename T, size_t N>
+struct ARRAY_TYPE<T[N]>
+{
+typedef T type;
+};
+
+template <typename T>
+bool SetArrayItem(T & array, const char * index, const typename ARRAY_TYPE<T>::type & value)
+{
+size_t pos = 0;
+if (str2x(index, pos))
+ return false;
+array[pos] = value;
+return true;
+}
+
+} // namespace anonymous
time_t stgTime;
int dPresent = false;
for (int i = 0; i < DIR_NUM; i++)
{
- if (!req->u[i].res_empty())
+ if (!req->monthUpload[i].res_empty())
{
if (!uPresent && !dPresent)
{
}
stringstream ss;
- ss << req->u[i].const_data();
+ ss << req->monthUpload[i].const_data();
//sprintf(str, "MU%d=\"%lld\" ", i, req->u[i].const_data());
sprintf(str, "MU%d=\"%s\" ", i, ss.str().c_str());
strcat(r, str);
}
- if (!req->d[i].res_empty())
+ if (!req->monthDownload[i].res_empty())
{
if (!uPresent && !dPresent)
{
}
stringstream ss;
- ss << req->d[i].const_data();
+ ss << req->monthDownload[i].const_data();
+ sprintf(str, "MD%d=\"%s\" ", i, ss.str().c_str());
+ strcat(r, str);
+ }
+ if (!req->sessionUpload[i].res_empty())
+ {
+ if (!uPresent && !dPresent)
+ {
+ sprintf(str, "<traff ");
+ strcat(r, str);
+ uPresent = true;
+ }
+
+ stringstream ss;
+ ss << req->sessionUpload[i].const_data();
+ //sprintf(str, "MU%d=\"%lld\" ", i, req->u[i].const_data());
+ sprintf(str, "MU%d=\"%s\" ", i, ss.str().c_str());
+ strcat(r, str);
+ }
+ if (!req->sessionDownload[i].res_empty())
+ {
+ if (!uPresent && !dPresent)
+ {
+ sprintf(str, "<traff ");
+ strcat(r, str);
+ dPresent = true;
+ }
+
+ stringstream ss;
+ ss << req->sessionDownload[i].const_data();
sprintf(str, "MD%d=\"%s\" ", i, ss.str().c_str());
strcat(r, str);
}
for (int i = 0; i < USERDATA_NUM; i++)
{
- if (!req->ud[i].res_empty())
+ if (!req->userData[i].res_empty())
{
string ud;
- Encode12str(ud, req->ud[i]);
+ Encode12str(ud, req->userData[i]);
sprintf(str, "<userdata%d value=\"%s\"/>", i, ud.c_str());
strcat(r, str);
}
//-----------------------------------------------------------------------------
int CheckParameters(REQUEST * req)
{
-int u = false;
-int d = false;
-int ud = false;
-int a = !req->admLogin.res_empty()
+bool su = false;
+bool sd = false;
+bool mu = false;
+bool md = false;
+bool ud = false;
+bool a = !req->admLogin.res_empty()
&& !req->admPasswd.res_empty()
&& !req->server.res_empty()
&& !req->port.res_empty()
&& !req->login.res_empty();
-int b = !req->cash.res_empty()
+bool b = !req->cash.res_empty()
|| !req->setCash.res_empty()
|| !req->credit.res_empty()
|| !req->prepaidTraff.res_empty()
for (int i = 0; i < DIR_NUM; i++)
{
- if (req->u[i].res_empty())
+ if (req->sessionUpload[i].res_empty())
{
- u = true;
+ su = true;
break;
}
}
for (int i = 0; i < DIR_NUM; i++)
{
- if (req->d[i].res_empty())
+ if (req->sessionDownload[i].res_empty())
{
- d = true;
+ sd = true;
break;
}
}
for (int i = 0; i < DIR_NUM; i++)
{
- if (req->ud[i].res_empty())
+ if (req->monthUpload[i].res_empty())
+ {
+ mu = true;
+ break;
+ }
+ }
+
+for (int i = 0; i < DIR_NUM; i++)
+ {
+ if (req->monthDownload[i].res_empty())
+ {
+ md = true;
+ break;
+ }
+ }
+
+for (int i = 0; i < DIR_NUM; i++)
+ {
+ if (req->userData[i].res_empty())
{
ud = true;
break;
//printf("a=%d, b=%d, u=%d, d=%d ud=%d\n", a, b, u, d, ud);
-return a && (b || u || d || ud);
+return a && (b || su || sd || mu || md || ud);
}
//-----------------------------------------------------------------------------
int CheckParametersGet(REQUEST * req)
req.group = " ";
break;
- case 'I': //IP-address of user
- req.ips = " ";
- break;
+ case 'I': //IP-address of user
+ req.ips = " ";
+ break;
case 'S': //Detail stat status
req.disableDetailStat = " ";
break;
case 500: //U
- //printf("U%d\n", c - 500);
- req.sessionUpload[optarg] = 1;
+ SetArrayItem(req.sessionUpload, optarg, 1);
+ //req.sessionUpload[optarg] = 1;
break;
case 501:
- //printf("U%d\n", c - 500);
- req.sessionDownload[optarg] = 1;
+ SetArrayItem(req.sessionDownload, optarg, 1);
+ //req.sessionDownload[optarg] = 1;
break;
case 502:
- //printf("U%d\n", c - 500);
- req.monthUpload[optarg] = 1;
+ SetArrayItem(req.monthUpload, optarg, 1);
+ //req.monthUpload[optarg] = 1;
break;
case 503:
- //printf("U%d\n", c - 500);
- req.monthDownload[optarg] = 1;
+ SetArrayItem(req.monthDownload, optarg, 1);
+ //req.monthDownload[optarg] = 1;
break;
case 700: //UserData
- //printf("UD%d\n", c - 700);
- req.ud[optarg] = " ";
+ SetArrayItem(req.userData, optarg, std::string(" "));
+ //req.userData[optarg] = " ";
break;
case 800:
case '?':
case ':':
- //printf ("Unknown option \n");
missedOptionArg = true;
break;
break;
case 500: //U
- //printf("U%d\n", c - 500);
- req.sesionUpload[optarg] = ParseTraff(argv[optind++]);
+ SetArrayItem(req.sessionUpload, optarg, ParseTraff(argv[optind++]));
+ //req.sessionUpload[optarg] = ParseTraff(argv[optind++]);
break;
case 501:
- //printf("U%d\n", c - 500);
- req.sessionDownload[optarg] = ParseTraff(argv[optind++]);
+ SetArrayItem(req.sessionDownload, optarg, ParseTraff(argv[optind++]));
+ //req.sessionDownload[optarg] = ParseTraff(argv[optind++]);
break;
case 502:
- //printf("U%d\n", c - 500);
- req.monthUpload[optarg] = ParseTraff(argv[optind++]);
+ SetArrayItem(req.monthUpload, optarg, ParseTraff(argv[optind++]));
+ //req.monthUpload[optarg] = ParseTraff(argv[optind++]);
break;
case 503:
- //printf("U%d\n", c - 500);
- req.monthDownload[optarg] = ParseTraff(argv[optind++]);
+ SetArrayItem(req.monthDownload, optarg, ParseTraff(argv[optind++]));
+ //req.monthDownload[optarg] = ParseTraff(argv[optind++]);
break;
case 700: //UserData
ParseAnyString(argv[optind++], &str);
- //printf("UD%d\n", c - 700);
- req.userData[optarg] = str;
+ SetArrayItem(req.userData, optarg, str);
+ //req.userData[optarg] = str;
break;
case '?':
- //printf("Missing option argument\n");
missedOptionArg = true;
break;
case ':':
- //printf("Missing option argument\n");
missedOptionArg = true;
break;