From 6e76c4540e37280d0c161e7d7035e2e9022b18ce Mon Sep 17 00:00:00 2001 From: deva Date: Wed, 26 Mar 2008 13:04:30 +0000 Subject: Implemented a SAXPaser class, and made the macro and xml parsers use it. --- server/src/macro_parser.cc | 145 +++++++++++++-------------------------------- 1 file changed, 41 insertions(+), 104 deletions(-) (limited to 'server/src/macro_parser.cc') diff --git a/server/src/macro_parser.cc b/server/src/macro_parser.cc index aba7fda..84572d3 100644 --- a/server/src/macro_parser.cc +++ b/server/src/macro_parser.cc @@ -37,47 +37,41 @@ #include #include -#include -#include -#include - -class MacroParser { -public: - Macro *macro; - std::vector< Widget* > stack; - bool done; -}; - -static void start_hndl(void *p, const char *el, const char **attr) +MacroParser::MacroParser(std::string name, Macro ¯o) { - MacroParser *parser = (MacroParser*)XML_GetUserData(p); - - // printf("Start tag [%s]\n", el); + this->macro = ¯o; - // Convert to comfy C++ values... - std::string name = el; - std::map< std::string, std::string > attributes; - - while(*attr) { - std::string at_name = *attr; - attr++; - std::string at_value = *attr; - attr++; + std::string macrofile = std::string(XML) + "/" + name + ".xml"; + fd = open(macrofile.c_str(), O_RDONLY); - attributes.insert(make_pair(at_name, at_value)); + if(fd == -1) { + printf("Cannot open file \"%s\"...", macrofile.c_str()); + printf("failed!\n"); + return; } +} - // Do something reasonable with them... +MacroParser::~MacroParser() +{ + if(fd != -1) close(fd); +} - if(name == "include") { +int MacroParser::readData(char *data, size_t size) +{ + return read(fd, data, size); +} +void MacroParser::startTag(std::string name, std::map< std::string, std::string> attributes) +{ + if(name == "include") { Macro inc; - parse_macro(attributes["name"], inc); + MacroParser parser(attributes["name"], inc); + parser.parse(); WidgetList::iterator w = inc.widgets.front().widgets.begin(); while(w != inc.widgets.front().widgets.end()) { - parser->stack.back()->widgets.push_back(*w); + stack.back()->widgets.push_back(*w); w++; } @@ -86,9 +80,9 @@ static void start_hndl(void *p, const char *el, const char **attr) if(name == "macro") { - parser->macro->name = attributes["name"]; - parser->macro->version = attributes["version"]; - parser->macro->format = attributes["resume"]; + macro->name = attributes["name"]; + macro->version = attributes["version"]; + macro->format = attributes["resume"]; return; // Don't do further parsing of this tag. } @@ -110,14 +104,14 @@ static void start_hndl(void *p, const char *el, const char **attr) Widget macrolist; Widget *wp; - if(parser->stack.size() > 0) {// We only pushback the child if there is a parent. - parser->stack.back()->widgets.push_back(macrolist); - wp = &parser->stack.back()->widgets.back(); + if(stack.size() > 0) {// We only pushback the child if there is a parent. + stack.back()->widgets.push_back(macrolist); + wp = &stack.back()->widgets.back(); } else { - parser->macro->widgets.push_back(macrolist); - wp = &parser->macro->widgets.back(); + macro->widgets.push_back(macrolist); + wp = ¯o->widgets.back(); } - parser->stack.push_back(wp); + stack.push_back(wp); wp->type = "listbox"; wp->properties["name"] = attributes["name"]; @@ -187,82 +181,25 @@ static void start_hndl(void *p, const char *el, const char **attr) Widget *wp; - if(parser->stack.size() > 0) {// We only pushback the child if there is a parent. - parser->stack.back()->widgets.push_back(widget); - wp = &parser->stack.back()->widgets.back(); + if(stack.size() > 0) {// We only pushback the child if there is a parent. + stack.back()->widgets.push_back(widget); + wp = &stack.back()->widgets.back(); } else { - parser->macro->widgets.push_back(widget); - wp = &parser->macro->widgets.back(); + macro->widgets.push_back(widget); + wp = ¯o->widgets.back(); } - parser->stack.push_back(wp); + stack.push_back(wp); std::map< std::string, std::string >::iterator i = attributes.begin(); while(i != attributes.end()) { - // WidgetProperty prop; - // prop.name = i->first; - // prop.value = i->second; - // wp->properties.push_back(prop); wp->properties[i->first] = i->second; i++; } } -static void end_hndl(void *p, const char *el) -{ - MacroParser *parser = (MacroParser*)XML_GetUserData(p); - - // printf("End tag [%s]\n", el); - - if(std::string("include") != el) parser->stack.pop_back(); - - if(!strcmp(el, "macro")) parser->done = true; -} - -void parse_macro(std::string name, Macro ¯o) +void MacroParser::endTag(std::string name) { - - XML_Parser p = XML_ParserCreate(NULL); - if (! p) { - fprintf(stderr, "Couldn't allocate memory for parser\n"); - // throw Exception(...); - return; - } - - MacroParser parser; - parser.macro = ¯o; - parser.done = false; - - XML_SetUserData(p, &parser); - XML_UseParserAsHandlerArg(p); - XML_SetElementHandler(p, start_hndl, end_hndl); - - std::string macrofile = std::string(XML) + "/" + name + ".xml"; - int fd = open(macrofile.c_str(), O_RDONLY); - - if(fd == -1) { - printf("Cannot open file \"%s\"...", macrofile.c_str()); - printf("failed!\n"); - return; - } - - while(!parser.done) { - char buf[32]; - int len; - - memset(buf, 0, sizeof(buf)); - len = read(fd, buf, sizeof(buf) - 1); - - parser.done = len == 0; - - if (! XML_Parse(p, buf, len, parser.done)) { - fprintf(stderr, "Parse error at line %d:\n%s\n", - XML_GetCurrentLineNumber(p), - XML_ErrorString(XML_GetErrorCode(p))); - // throw Exception(...); - return; - } - } - - // printf("%d requests\n", transaction.requests.size()); + if(name != "include") stack.pop_back(); + // if(!strcmp(el, "macro")) done = true; } -- cgit v1.2.3