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