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