]> git.stg.codes - stg.git/blob - stglibs/srvconf.lib/parser_server_info.cpp
Better handling of errors from server. Refactoring.
[stg.git] / stglibs / srvconf.lib / parser_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 "stg/parser_server_info.h"
23
24 #include "stg/common.h"
25
26 #include <cstdio> // sprintf
27 #include <cstddef>
28
29 #include <strings.h>
30
31 namespace
32 {
33
34 const size_t UNAME_LEN    = 256;
35 const size_t SERV_VER_LEN = 64;
36 const size_t DIRNAME_LEN  = 16;
37
38 }
39
40 PARSER_SERVER_INFO::PARSER_SERVER_INFO()
41     : callback(NULL),
42       data(NULL),
43       depth(0)
44 {
45     AddParser(propertyParsers, "uname", info.uname);
46     AddParser(propertyParsers, "version", info.version);
47     AddParser(propertyParsers, "tariff", info.tariffType);
48     AddParser(propertyParsers, "dir_num", info.dirNum);
49     AddParser(propertyParsers, "users_num", info.usersNum);
50     AddParser(propertyParsers, "tariff_num", info.tariffNum);
51
52     for (size_t i = 0; i < DIR_NUM; i++)
53         AddParser(propertyParsers, "dir_name_" + x2str(i), info.dirName[i], GetEncodedValue);
54 }
55 //-----------------------------------------------------------------------------
56 int PARSER_SERVER_INFO::ParseStart(const char *el, const char **attr)
57 {
58 depth++;
59 if (depth == 1)
60     if (strcasecmp(el, "ServerInfo") != 0)
61         error = "Invalid response.";
62 else
63     if (depth == 2)
64         if (!TryParse(propertyParsers, ToLower(el), attr))
65             error = "Invalid parameter.";
66 return 0;
67 }
68 //-----------------------------------------------------------------------------
69 void PARSER_SERVER_INFO::ParseEnd(const char * /*el*/)
70 {
71 depth--;
72 if (depth == 0)
73     {
74     if (callback)
75         callback(error.empty(), error, info, data);
76     error.clear();
77     }
78 }
79 //-----------------------------------------------------------------------------
80 void PARSER_SERVER_INFO::SetCallback(CALLBACK f, void * d)
81 {
82 callback = f;
83 data = d;
84 }