]> git.stg.codes - stg.git/blob - projects/sgconf_xml/parser.cpp
Variable scope reduction.
[stg.git] / projects / sgconf_xml / parser.cpp
1 #include "stg/common.h"
2 #include "stg/netunit.h"
3 #include "request.h"
4
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <expat.h>
8 #include <string.h>
9
10 int parse_depth = 0;
11 XML_Parser parser;
12 //---------------------------------------------------------------------------
13 int ParseAns(void *, const char *el, const char **attr)
14 {
15 if (strcasecmp(el, "ServerInfo") == 0 || strcasecmp(el, "Tariffs") == 0 || strcasecmp(el, "Admins") == 0 || strcasecmp(el, "Users") == 0 || strcasecmp(el, "user") == 0)
16     {
17     return 0;
18     }
19 if (strcasecmp(attr[1], "ok") == 0)
20     {
21     return 0;
22     }
23 if (strcasecmp(attr[1], "error") == 0)
24     {
25     return 1;
26     }
27 if (strcasecmp(attr[1], "err") == 0)
28     {
29     return 1;
30     }
31 return -1;
32 }
33 //---------------------------------------------------------------------------
34 void StartElement(void *data, const char *el, const char **attr)
35 {
36 if (strcasecmp(el, "ServerInfo") == 0 || strcasecmp(el, "Tariffs") == 0 || strcasecmp(el, "Admins") == 0 || strcasecmp(el, "Users") == 0)
37     {
38     printf ("<%s>\n", el);
39     return;
40     }
41
42 if (strcasecmp(el, "tariff") == 0)
43     {
44     if (strcasecmp(attr[0], "name") == 0)
45         {
46         printf ("<%s %s=\"%s\">\n", el, attr[0], attr[1]);
47         printf ("<%s>%s</%s>\n", attr[0], attr[1], attr[0]);
48         }
49     else
50         {
51         printf ("<%s>%s", el, attr[1]);
52         }
53     return;
54     }
55
56 if (strcasecmp(el, "admin") == 0)
57     {
58     printf ("<%s %s=\"%s\">\n", el, attr[0], attr[1]);
59     int i = 0;
60     while (attr[i])
61         {
62         printf ("<%s>%s</%s>\n", attr[i], attr[i+1], attr[i]);
63         i+=2;
64         }
65     printf ("</admin>\n");
66     return;
67     }
68
69 if (strcasecmp(el, "user") == 0)
70     {
71     if (strcasecmp(attr[0], "login") == 0)
72         {
73         printf ("<%s %s=\"%s\">\n", el, attr[0], attr[1]);
74         printf ("<%s>%s</%s>\n", attr[0], attr[1], attr[0]);
75         }
76     else
77         {
78         printf ("<%s>\n", el);
79         }
80     return;
81     }
82
83 if (strncasecmp(el, "dir_name_", 9) == 0 || strcasecmp(el, "address") == 0 || strcasecmp(el, "email") == 0 || strcasecmp(el, "group") == 0 || strcasecmp(el, "note") == 0 || strcasecmp(el, "phone") == 0 || strcasecmp(el, "name") == 0 || strncasecmp(el, "UserData", 8) == 0)
84     {
85     char * str_tmp;
86     str_tmp = new char[strlen(attr[1]) + 1];
87     Decode21(str_tmp, attr[1]);
88     printf ("<%s>%s</%s>\n", el, str_tmp, el);
89     delete[] str_tmp;
90     return;
91     }
92
93 if (strcasecmp(el, "traff") == 0)
94     {
95     int j = 0;
96     while (attr[j])
97         {
98         uint64_t t;
99         str2x(attr[j+1], t);
100         printf ("<%s>%lld</%s>\n", attr[j], t, attr[j]);
101         j+=2;
102         }
103     return;
104     }
105 else
106     {
107     printf ("<%s>%s</%s>\n", el, attr[1], el);
108     return;
109     }
110 parse_depth++;
111 if (parse_depth == 1)
112     {
113     if (ParseAns(data, el, attr) < 0)
114         {
115         printf("Unexpected token\n");
116         exit(UNKNOWN_ERR_CODE);
117         }
118     if (ParseAns(data, el, attr) == 1)
119         {
120         printf("User not found\n");
121         exit(USER_NOT_FOUND_ERR_CODE);
122         }
123     return;
124     }
125 }
126 //-----------------------------------------------------------------------------
127 void EndElement(void *, const char *el)
128 {
129 parse_depth--;
130 if (strcasecmp(el, "ServerInfo") == 0 ||
131     strcasecmp(el, "Tariffs") == 0 ||
132     strcasecmp(el, "Admins") == 0 ||
133     strcasecmp(el, "Users") == 0 ||
134     strcasecmp(el, "tariff") == 0 ||
135     strcasecmp(el, "user") == 0)
136     printf ("</%s>\n", el);
137 }
138 //---------------------------------------------------------------------------
139 int ParseReply(void *, list<string> * ans)
140 {
141 int done = 0;
142
143 parse_depth = 0;
144 parser = XML_ParserCreate(NULL);
145
146 if (!parser)
147     {
148     printf("Couldn't allocate memory for parser\n");
149     exit(UNKNOWN_ERR_CODE);
150     }
151
152 XML_ParserReset(parser, NULL);
153 XML_SetElementHandler(parser, StartElement, EndElement);
154
155 list<string>::iterator n = ans->begin();
156 while (n != ans->end())
157     {
158     int len = strlen(n->c_str());
159
160     if (++n == ans->end())
161         done = 1;
162     --n;
163
164     if (XML_Parse(parser, n->c_str(), len, done) == XML_STATUS_ERROR)
165         {
166         printf("Parse error at line %d: %s",
167                XML_GetCurrentLineNumber(parser),
168                XML_ErrorString(XML_GetErrorCode(parser)));
169         return st_xml_parse_error;
170         }
171
172     ++n;
173     }
174 XML_ParserFree(parser);
175 return 0;
176 }
177 //-----------------------------------------------------------------------------