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