]> git.stg.codes - stg.git/blob - projects/sgconf_xml/parser.cpp
f2d5993bb9bf2fa86eaf21178e8056c539248a9c
[stg.git] / projects / sgconf_xml / parser.cpp
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <expat.h>
4 #include <string.h>
5
6 #include "stg/common.h"
7 #include "stg/netunit.h"
8 #include "request.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 //      printf ("<traff>\n");
96     int j = 0;
97     uint64_t t;
98     while (attr[j])
99         {
100         str2x(attr[j+1], t);
101         printf ("<%s>%lld</%s>\n", attr[j], t, attr[j]);
102         j+=2;
103         }
104 //      printf ("</traff>\n");
105     return;
106     }
107 else
108     {
109     printf ("<%s>%s</%s>\n", el, attr[1], el);
110     return;
111     }
112 //    }
113 parse_depth++;
114 if (parse_depth == 1)
115     {
116     if (ParseAns(data, el, attr) < 0)
117         {
118         printf("Unexpected token\n");
119         exit(UNKNOWN_ERR_CODE);
120         }
121     if (ParseAns(data, el, attr) == 1)
122         {
123         printf("User not found\n");
124         exit(USER_NOT_FOUND_ERR_CODE);
125         }
126     return;
127     }
128 }
129 //-----------------------------------------------------------------------------
130 void EndElement(void *, const char *el)
131 {
132 parse_depth--;
133 if (strcasecmp(el, "ServerInfo") == 0 || strcasecmp(el, "Tariffs") == 0 || strcasecmp(el, "Admins") == 0 || strcasecmp(el, "Users") == 0 || strcasecmp(el, "tariff") == 0 || strcasecmp(el, "user") == 0)
134     {
135     printf ("</%s>\n", el);
136     }
137 }
138 //---------------------------------------------------------------------------
139 int ParseReply(void *, list<string> * ans)
140 //int ParseReply(void * data, SLIST * ans)
141 {
142 //char answ[ENC_MSG_LEN + 1];
143 int len;
144 int done = 0;
145
146 parse_depth = 0;
147 parser = XML_ParserCreate(NULL);
148
149 if (!parser)
150     {
151     printf("Couldn't allocate memory for parser\n");
152     exit(UNKNOWN_ERR_CODE);
153     }
154
155 XML_ParserReset(parser, NULL);
156 XML_SetElementHandler(parser, StartElement, EndElement);
157
158 list<string>::iterator n = ans->begin();
159 while (n != ans->end())
160     {
161     len = strlen(n->c_str());
162
163     if (++n == ans->end())
164         done = 1;
165     --n;
166
167     if (XML_Parse(parser, n->c_str(), len, done) == XML_STATUS_ERROR)
168         {
169         printf("Parse error at line %d: %s",
170                XML_GetCurrentLineNumber(parser),
171                XML_ErrorString(XML_GetErrorCode(parser)));
172         return st_xml_parse_error;
173         }
174
175     ++n;
176     }
177 XML_ParserFree(parser);
178 return 0;
179 }
180 //-----------------------------------------------------------------------------