]> git.stg.codes - stg.git/blob - stglibs/srvconf.lib/parser_server_info.cpp
057d99a569e8524622090362d7e8b8e039247d7e
[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 bool checkValue(const char ** attr)
39 {
40 return attr && attr[0] && attr[1] && strcasecmp(attr[0], "value") == 0;
41 }
42
43 int getIntValue(const char ** attr)
44 {
45 int value = -1;
46 if (checkValue(attr))
47     if (str2x(attr[1], value) < 0)
48         return -1;
49 return value;
50 }
51
52 std::string getStringValue(const char ** attr)
53 {
54 if (checkValue(attr))
55     return attr[1];
56 return "";
57 }
58
59 }
60
61 PARSER_SERVER_INFO::PARSER_SERVER_INFO()
62     : callback(NULL),
63       data(NULL),
64       depth(0)
65 {
66 }
67 //-----------------------------------------------------------------------------
68 int PARSER_SERVER_INFO::ParseStart(const char *el, const char **attr)
69 {
70 depth++;
71 if (depth == 1)
72     {
73     if (strcasecmp(el, "ServerInfo") != 0)
74         {
75         //printf("%s\n", el);
76         }
77     }
78 else
79     {
80     if (depth == 2)
81         {
82         if (strcasecmp(el, "uname") == 0)
83             {
84             info.uname = getStringValue(attr);
85             return 0;
86             }
87         if (strcasecmp(el, "version") == 0)
88             {
89             info.version = getStringValue(attr);
90             return 0;
91             }
92         if (strcasecmp(el, "tariff") == 0)
93             {
94             info.tariffType = getIntValue(attr);
95             return 0;
96             }
97         if (strcasecmp(el, "dir_num") == 0)
98             {
99             info.dirNum = getIntValue(attr);
100             return 0;
101             }
102         if (strcasecmp(el, "users_num") == 0)
103             {
104             info.usersNum = getIntValue(attr);
105             return 0;
106             }
107         if (strcasecmp(el, "tariff_num") == 0)
108             {
109             info.tariffNum = getIntValue(attr);
110             return 0;
111             }
112
113         for (int j = 0; j < DIR_NUM; j++)
114             {
115             char str[16];
116             sprintf(str, "dir_name_%d", j);
117             if (strcasecmp(el, str) == 0)
118                 ParseDirName(attr, j);
119             }
120
121         }
122     }
123 return 0;
124 }
125 //-----------------------------------------------------------------------------
126 void PARSER_SERVER_INFO::ParseEnd(const char * /*el*/)
127 {
128 depth--;
129 if (depth == 0 && callback)
130     callback(info, data);
131 }
132 //-----------------------------------------------------------------------------
133 void PARSER_SERVER_INFO::SetCallback(CALLBACK f, void * d)
134 {
135 callback = f;
136 data = d;
137 }
138 //-----------------------------------------------------------------------------
139 void PARSER_SERVER_INFO::ParseDirName(const char **attr, int d)
140 {
141 if (checkValue(attr))
142     {
143     char str[2 * DIRNAME_LEN + 1];
144     Decode21(str, attr[1]);
145     info.dirName[d] = str;
146     }
147 }