]> git.stg.codes - stg.git/blob - libs/srvconf/parsers/server_info.cpp
Public interfaces: part 1
[stg.git] / libs / srvconf / parsers / server_info.cpp
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
20  */
21
22 #include "server_info.h"
23
24 #include "stg/common.h"
25
26 #include <cstdio> // sprintf
27
28 #include <strings.h>
29
30 using namespace STG;
31
32 ServerInfo::Parser::Parser(Callback f, void* d, const std::string& e)
33     : callback(f),
34       data(d),
35       encoding(e),
36       depth(0),
37       parsingAnswer(false)
38 {
39     addParser(propertyParsers, "uname", info.uname);
40     addParser(propertyParsers, "version", info.version);
41     addParser(propertyParsers, "tariff", info.tariffType);
42     addParser(propertyParsers, "dir_num", info.dirNum);
43     addParser(propertyParsers, "user_num", info.usersNum);
44     addParser(propertyParsers, "tariff_num", info.tariffNum);
45
46     for (size_t i = 0; i < DIR_NUM; i++)
47         addParser(propertyParsers, "dir_name_" + std::to_string(i), info.dirName[i], getEncodedValue);
48 }
49 //-----------------------------------------------------------------------------
50 int ServerInfo::Parser::ParseStart(const char* el, const char** attr)
51 {
52     depth++;
53     if (depth == 1)
54     {
55         if (strcasecmp(el, "GetServerInfo") == 0)
56             parsingAnswer = true;
57     }
58     else
59     {
60         if (depth == 2 && parsingAnswer)
61             if (!tryParse(propertyParsers, ToLower(el), attr, encoding))
62                 error = "Invalid parameter.";
63     }
64     return 0;
65 }
66 //-----------------------------------------------------------------------------
67 void ServerInfo::Parser::ParseEnd(const char* /*el*/)
68 {
69     depth--;
70     if (depth == 0 && parsingAnswer)
71     {
72         if (callback)
73             callback(error.empty(), error, info, data);
74         error.clear();
75         parsingAnswer = false;
76     }
77 }