]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/sgconfig/configproto.cpp
Fix prefix incremental and decremental operators to postfix
[stg.git] / projects / stargazer / plugins / configuration / sgconfig / configproto.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  *    Date: 27.10.2002
19  */
20
21 /*
22  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
23  */
24
25  /*
26  $Revision: 1.22 $
27  $Date: 2010/10/04 20:24:14 $
28  $Author: faust $
29  */
30
31
32 #include <unistd.h>
33
34 #include "configproto.h"
35
36 //-----------------------------------------------------------------------------
37 void ParseXMLStart(void *data, const char *el, const char **attr)
38 {
39 CONFIGPROTO * cp = static_cast<CONFIGPROTO *>(data);
40
41 if (cp->currParser)
42     {
43     cp->currParser->SetAnswerList(&cp->answerList);
44     cp->currParser->SetCurrAdmin(*cp->currAdmin);
45     cp->currParser->ParseStart(data, el, attr);
46     }
47 else
48     {
49     for (unsigned int i = 0; i < cp->dataParser.size(); i++)
50         {
51         cp->dataParser[i]->SetAnswerList(&cp->answerList);
52         cp->dataParser[i]->SetCurrAdmin(*cp->currAdmin);
53         cp->dataParser[i]->Reset();
54         if (cp->dataParser[i]->ParseStart(data, el, attr) == 0)
55             {
56             cp->currParser = cp->dataParser[i];
57             break;
58             }
59         else
60             {
61             cp->dataParser[i]->Reset();
62             }
63         }
64     }
65 }
66 //-----------------------------------------------------------------------------
67 void ParseXMLEnd(void *data, const char *el)
68 {
69 CONFIGPROTO * cp = static_cast<CONFIGPROTO *>(data);
70 if (cp->currParser)
71     {
72     if (cp->currParser->ParseEnd(data, el) == 0)
73         {
74         cp->currParser = NULL;
75         }
76     }
77 else
78     {
79     for (unsigned int i = 0; i < cp->dataParser.size(); i++)
80         {
81         if (cp->dataParser[i]->ParseEnd(data, el) == 0)
82             {
83             break;
84             }
85         }
86     }
87 }
88 //-----------------------------------------------------------------------------
89 CONFIGPROTO::CONFIGPROTO()
90     : adminIP(0),
91       port(0),
92       nonstop(1),
93       state(0),
94       currAdmin(),
95       WriteServLog(GetStgLogger()),
96       listenSocket(0),
97       admins(NULL),
98       currParser(NULL)
99 {
100 dataParser.push_back(&parserGetServInfo);
101
102 dataParser.push_back(&parserGetUsers);
103 dataParser.push_back(&parserGetUser);
104 dataParser.push_back(&parserChgUser);
105 dataParser.push_back(&parserAddUser);
106 dataParser.push_back(&parserDelUser);
107 dataParser.push_back(&parserCheckUser);
108 dataParser.push_back(&parserSendMessage);
109
110 dataParser.push_back(&parserGetTariffs);
111 dataParser.push_back(&parserAddTariff);
112 dataParser.push_back(&parserDelTariff);
113 dataParser.push_back(&parserChgTariff);
114
115 dataParser.push_back(&parserGetAdmins);
116 dataParser.push_back(&parserChgAdmin);
117 dataParser.push_back(&parserDelAdmin);
118 dataParser.push_back(&parserAddAdmin);
119
120 xmlParser = XML_ParserCreate(NULL);
121
122 if (!xmlParser)
123     {
124     WriteServLog("Couldn't allocate memory for parser.");
125     exit(1);
126     }
127
128 }
129 //-----------------------------------------------------------------------------
130 CONFIGPROTO::~CONFIGPROTO()
131 {
132 XML_ParserFree(xmlParser);
133 }
134 //-----------------------------------------------------------------------------
135 int CONFIGPROTO::ParseCommand()
136 {
137 list<string>::iterator n;
138 int done = 0;
139 char str[9];
140 int len;
141
142 if (requestList.empty())
143     return 0;
144
145 n = requestList.begin();
146
147 strncpy(str, (*n).c_str(), 8);
148 str[8] = 0;
149
150 XML_ParserReset(xmlParser, NULL);
151 XML_SetElementHandler(xmlParser, ParseXMLStart, ParseXMLEnd);
152 XML_SetUserData(xmlParser, this);
153
154 while(nonstop)
155     {
156     strncpy(str, (*n).c_str(), 8);
157     str[8] = 0;
158     len = strlen(str);
159
160     ++n;
161     if (n == requestList.end())
162         done = 1;
163     --n;
164
165     if (XML_Parse(xmlParser, (*n).c_str(), len, done) == XML_STATUS_ERROR)
166         {
167         WriteServLog("Invalid configuration request");
168         printfd(__FILE__, "Parse error at line %d:\n%s\n",
169            XML_GetCurrentLineNumber(xmlParser),
170            XML_ErrorString(XML_GetErrorCode(xmlParser)));
171         if (currParser)
172             {
173             printfd(__FILE__, "Parser reset\n");
174             currParser->Reset();
175             currParser = NULL;
176             }
177
178         return -1;
179         }
180
181     if (done)
182         return 0;
183
184     ++n;
185     }
186
187 return 0;
188 }
189 //-----------------------------------------------------------------------------
190 void CONFIGPROTO::SetPort(uint16_t p)
191 {
192 port = p;
193 }
194 //-----------------------------------------------------------------------------
195 void CONFIGPROTO::SetAdmins(ADMINS * a)
196 {
197 admins = a;
198 for (unsigned int i = 0; i < dataParser.size(); i++)
199     {
200     dataParser[i]->SetAdmins(a);
201     }
202
203 }
204 //-----------------------------------------------------------------------------
205 void CONFIGPROTO::SetUsers(USERS * u)
206 {
207 for (unsigned int i = 0; i < dataParser.size(); i++)
208     {
209     dataParser[i]->SetUsers(u);
210     }
211
212 }
213 //-----------------------------------------------------------------------------
214 void CONFIGPROTO::SetTariffs(TARIFFS * t)
215 {
216 for (unsigned int i = 0; i < dataParser.size(); i++)
217     {
218     dataParser[i]->SetTariffs(t);
219     }
220 }
221 //-----------------------------------------------------------------------------
222 void CONFIGPROTO::SetStore(STORE * s)
223 {
224 for (unsigned int i = 0; i < dataParser.size(); i++)
225     {
226     dataParser[i]->SetStore(s);
227     }
228 }
229 //-----------------------------------------------------------------------------
230 void CONFIGPROTO::SetStgSettings(const SETTINGS * s)
231 {
232 for (unsigned int i = 0; i < dataParser.size(); i++)
233     {
234     dataParser[i]->SetStgSettings(s);
235     }
236 }
237 //-----------------------------------------------------------------------------
238 const string & CONFIGPROTO::GetStrError() const
239 {
240 return errorStr;
241 }
242 //-----------------------------------------------------------------------------
243 uint32_t CONFIGPROTO::GetAdminIP() const
244 {
245 return adminIP;
246 }
247 //-----------------------------------------------------------------------------