X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/4ab6759cf046b076e454cbf01bc4b299e4e63794..afb00c045e0ea58e39c0d7a7921bedbf26d1860d:/projects/sgconf_xml/parser.cpp diff --git a/projects/sgconf_xml/parser.cpp b/projects/sgconf_xml/parser.cpp new file mode 100644 index 00000000..12a1c7a2 --- /dev/null +++ b/projects/sgconf_xml/parser.cpp @@ -0,0 +1,181 @@ +#include +#include +#include +#include + +#include "common.h" +#include "netunit.h" +#include "request.h" + +int parse_depth = 0; +XML_Parser parser; +//--------------------------------------------------------------------------- +int ParseAns(void * data, const char *el, const char **attr) +{ +if (strcasecmp(el, "ServerInfo") == 0 || strcasecmp(el, "Tariffs") == 0 || strcasecmp(el, "Admins") == 0 || strcasecmp(el, "Users") == 0 || strcasecmp(el, "user") == 0) + { + return 0; + } +if (strcasecmp(attr[1], "ok") == 0) + { + return 0; + } +if (strcasecmp(attr[1], "error") == 0) + { + return 1; + } +if (strcasecmp(attr[1], "err") == 0) + { + return 1; + } +return -1; +} +//--------------------------------------------------------------------------- +void StartElement(void *data, const char *el, const char **attr) +{ +if (strcasecmp(el, "ServerInfo") == 0 || strcasecmp(el, "Tariffs") == 0 || strcasecmp(el, "Admins") == 0 || strcasecmp(el, "Users") == 0) + { + printf ("<%s>\n", el); + return; + } + +if (strcasecmp(el, "tariff") == 0) + { + if (strcasecmp(attr[0], "name") == 0) + { + printf ("<%s %s=\"%s\">\n", el, attr[0], attr[1]); + printf ("<%s>%s\n", attr[0], attr[1], attr[0]); + } + else + { + printf ("<%s>%s", el, attr[1]); + } + return; + } + +if (strcasecmp(el, "admin") == 0) + { + printf ("<%s %s=\"%s\">\n", el, attr[0], attr[1]); + int i = 0; + while (attr[i]) + { + printf ("<%s>%s\n", attr[i], attr[i+1], attr[i]); + i+=2; + } + printf ("\n"); + return; + } + +if (strcasecmp(el, "user") == 0) + { + if (strcasecmp(attr[0], "login") == 0) + { + printf ("<%s %s=\"%s\">\n", el, attr[0], attr[1]); + printf ("<%s>%s\n", attr[0], attr[1], attr[0]); + } + else + { + printf ("<%s>\n", el); + } + return; + } + +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) + { + char * str_tmp; + str_tmp = new char[strlen(attr[1]) + 1]; + Decode21(str_tmp, attr[1]); + printf ("<%s>%s\n", el, str_tmp, el); + delete[] str_tmp; + return; + } + +if (strcasecmp(el, "traff") == 0) + { +// printf ("\n"); + int j = 0; + uint64_t t; + while (attr[j]) + { + str2x(attr[j+1], t); + printf ("<%s>%lld\n", attr[j], t, attr[j]); + j+=2; + } +// printf ("\n"); + return; + } +else + { + printf ("<%s>%s\n", el, attr[1], el); + return; + } +// } +parse_depth++; +if (parse_depth == 1) + { + if (ParseAns(data, el, attr) < 0) + { + printf("Unexpected token\n"); + exit(UNKNOWN_ERR_CODE); + } + if (ParseAns(data, el, attr) == 1) + { + printf("User not found\n"); + exit(USER_NOT_FOUND_ERR_CODE); + } + return; + } +} +//----------------------------------------------------------------------------- +void EndElement(void *data, const char *el) +{ +parse_depth--; +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) + { + printf ("\n", el); + } +} +//--------------------------------------------------------------------------- +int ParseReply(void * data, list * ans) +//int ParseReply(void * data, SLIST * ans) +{ +//char answ[ENC_MSG_LEN + 1]; +int len; +int done = 0; + +parse_depth = 0; +parser = XML_ParserCreate(NULL); + +if (!parser) + { + printf("Couldn't allocate memory for parser\n"); + exit(UNKNOWN_ERR_CODE); + } + +XML_ParserReset(parser, NULL); +XML_SetElementHandler(parser, StartElement, EndElement); + +list::iterator n = ans->begin(); +while (n != ans->end()) + { + len = strlen(n->c_str()); + + if (++n == ans->end()) + done = 1; + n--; + + if (XML_Parse(parser, n->c_str(), len, done) == XML_STATUS_ERROR) + { + char s[128]; + printf(s, "Parse error at line %d: %s", + XML_GetCurrentLineNumber(parser), + XML_ErrorString(XML_GetErrorCode(parser))); + return st_xml_parse_error; + } + + ++n; + } +XML_ParserFree(parser); +return 0; +} +//-----------------------------------------------------------------------------