1 #include "stg/common.h"
2 #include "stg/netunit.h"
12 //---------------------------------------------------------------------------
13 int ParseAns(void *, const char *el, const char **attr)
15 if (strcasecmp(el, "ServerInfo") == 0 || strcasecmp(el, "Tariffs") == 0 || strcasecmp(el, "Admins") == 0 || strcasecmp(el, "Users") == 0 || strcasecmp(el, "user") == 0)
19 if (strcasecmp(attr[1], "ok") == 0)
23 if (strcasecmp(attr[1], "error") == 0)
27 if (strcasecmp(attr[1], "err") == 0)
33 //---------------------------------------------------------------------------
34 void StartElement(void *data, const char *el, const char **attr)
36 if (strcasecmp(el, "ServerInfo") == 0 || strcasecmp(el, "Tariffs") == 0 || strcasecmp(el, "Admins") == 0 || strcasecmp(el, "Users") == 0)
38 printf ("<%s>\n", el);
42 if (strcasecmp(el, "tariff") == 0)
44 if (strcasecmp(attr[0], "name") == 0)
46 printf ("<%s %s=\"%s\">\n", el, attr[0], attr[1]);
47 printf ("<%s>%s</%s>\n", attr[0], attr[1], attr[0]);
51 printf ("<%s>%s", el, attr[1]);
56 if (strcasecmp(el, "admin") == 0)
58 printf ("<%s %s=\"%s\">\n", el, attr[0], attr[1]);
62 printf ("<%s>%s</%s>\n", attr[i], attr[i+1], attr[i]);
65 printf ("</admin>\n");
69 if (strcasecmp(el, "user") == 0)
71 if (strcasecmp(attr[0], "login") == 0)
73 printf ("<%s %s=\"%s\">\n", el, attr[0], attr[1]);
74 printf ("<%s>%s</%s>\n", attr[0], attr[1], attr[0]);
78 printf ("<%s>\n", el);
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)
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);
93 if (strcasecmp(el, "traff") == 0)
100 printf ("<%s>%lld</%s>\n", attr[j], t, attr[j]);
107 printf ("<%s>%s</%s>\n", el, attr[1], el);
111 if (parse_depth == 1)
113 if (ParseAns(data, el, attr) < 0)
115 printf("Unexpected token\n");
116 exit(UNKNOWN_ERR_CODE);
118 if (ParseAns(data, el, attr) == 1)
120 printf("User not found\n");
121 exit(USER_NOT_FOUND_ERR_CODE);
126 //-----------------------------------------------------------------------------
127 void EndElement(void *, const char *el)
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);
138 //---------------------------------------------------------------------------
139 int ParseReply(void *, list<string> * ans)
144 parser = XML_ParserCreate(NULL);
148 printf("Couldn't allocate memory for parser\n");
149 exit(UNKNOWN_ERR_CODE);
152 XML_ParserReset(parser, NULL);
153 XML_SetElementHandler(parser, StartElement, EndElement);
155 list<string>::iterator n = ans->begin();
156 while (n != ans->end())
158 int len = strlen(n->c_str());
160 if (++n == ans->end())
164 if (XML_Parse(parser, n->c_str(), len, done) == XML_STATUS_ERROR)
166 printf("Parse error at line %d: %s",
167 XML_GetCurrentLineNumber(parser),
168 XML_ErrorString(XML_GetErrorCode(parser)));
169 return st_xml_parse_error;
174 XML_ParserFree(parser);
177 //-----------------------------------------------------------------------------