]> git.stg.codes - stg.git/blob - projects/sgconf/parser.cpp
Removed "using namespace std;" from parser.cpp.
[stg.git] / projects / sgconf / parser.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  */
20
21  /*
22  $Author: nobunaga $
23  $Revision: 1.4 $
24  $Date: 2008/05/11 08:15:07 $
25  */
26
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <expat.h>
31 #include <string.h>
32
33 #include <string>
34 #include <list>
35
36 #include "stg/common.h"
37 #include "stg/netunit.h"
38 #include "stg/request.h"
39
40 int parse_depth = 0;
41 XML_Parser parser;
42 //---------------------------------------------------------------------------
43 int ParseAns(void * data, const char *el, const char **attr)
44 {
45 if (strcasecmp(attr[1], "ok") == 0)
46     {
47     return 0;
48     }
49 if (strcasecmp(attr[1], "error") == 0)
50     {
51     return 1;
52     }
53 if (strcasecmp(attr[1], "err") == 0)
54     {
55     return 1;
56     }
57 return -1;
58 }
59 //---------------------------------------------------------------------------
60 void StartElement(void *data, const char *el, const char **attr)
61 {
62 parse_depth++;
63 if (parse_depth == 1)
64     {
65     if (ParseAns(data, el, attr) < 0)
66         {
67         printf("Unexpected token\n");
68         exit(UNKNOWN_ERR_CODE);
69         }
70     if (ParseAns(data, el, attr) == 1)
71         {
72         printf("User not found\n");
73         exit(USER_NOT_FOUND_ERR_CODE);
74         }
75     return;
76     }
77 }
78 //-----------------------------------------------------------------------------
79 void EndElement(void *data, const char *el)
80 {
81 parse_depth--;
82 }
83 //---------------------------------------------------------------------------
84 int ParseReply(void * data, list<string> * ans)
85 {
86 int done = 0;
87
88 parse_depth = 0;
89 parser = XML_ParserCreate(NULL);
90
91 if (!parser)
92     {
93     printf("Couldn't allocate memory for parser\n");
94     exit(UNKNOWN_ERR_CODE);
95     }
96
97 XML_ParserReset(parser, NULL);
98 XML_SetElementHandler(parser, StartElement, EndElement);
99
100 list<string>::iterator n = ans->begin();
101 while (n != ans->end())
102     {
103     size_t len = strlen(n->c_str());
104
105     if (++n == ans->end())
106         done = 1;
107     --n;
108
109     if (XML_Parse(parser, n->c_str(), len, done) == XML_STATUS_ERROR)
110         {
111         printf("Parse error at line %d:\n%s\n",
112                XML_GetCurrentLineNumber(parser),
113                XML_ErrorString(XML_GetErrorCode(parser)));
114         exit(UNKNOWN_ERR_CODE);
115         }
116
117     ++n;
118     }
119
120 XML_ParserFree(parser);
121 return 0;
122 }
123 //-----------------------------------------------------------------------------
124