]> git.stg.codes - stg.git/blob - stglibs/srvconf.lib/parsers/server_info.cpp
Simplified parser interfaces.
[stg.git] / stglibs / srvconf.lib / 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 namespace
33 {
34
35 const size_t UNAME_LEN    = 256;
36 const size_t SERV_VER_LEN = 64;
37 const size_t DIRNAME_LEN  = 16;
38
39 }
40
41 SERVER_INFO::PARSER::PARSER(CALLBACK f, void * d)
42     : callback(f),
43       data(d),
44       depth(0),
45       parsingAnswer(false)
46 {
47     AddParser(propertyParsers, "uname", info.uname);
48     AddParser(propertyParsers, "version", info.version);
49     AddParser(propertyParsers, "tariff", info.tariffType);
50     AddParser(propertyParsers, "dir_num", info.dirNum);
51     AddParser(propertyParsers, "users_num", info.usersNum);
52     AddParser(propertyParsers, "tariff_num", info.tariffNum);
53
54     for (size_t i = 0; i < DIR_NUM; i++)
55         AddParser(propertyParsers, "dir_name_" + x2str(i), info.dirName[i], GetEncodedValue);
56 }
57 //-----------------------------------------------------------------------------
58 int SERVER_INFO::PARSER::ParseStart(const char *el, const char **attr)
59 {
60 depth++;
61 if (depth == 1)
62     if (strcasecmp(el, "ServerInfo") == 0)
63         parsingAnswer = true;
64 else
65     if (depth == 2 && parsingAnswer)
66         if (!TryParse(propertyParsers, ToLower(el), attr))
67             error = "Invalid parameter.";
68 return 0;
69 }
70 //-----------------------------------------------------------------------------
71 void SERVER_INFO::PARSER::ParseEnd(const char * /*el*/)
72 {
73 depth--;
74 if (depth == 0 && parsingAnswer)
75     {
76     if (callback)
77         callback(error.empty(), error, info, data);
78     error.clear();
79     parsingAnswer = false;
80     }
81 }