zaela-Server/common/xml_parser.cpp

102 lines
3.2 KiB
C++
Raw Permalink Normal View History

2013-05-09 10:44:08 -04:00
/* EQEMu: Everquest Server Emulator
Copyright (C) 2001-2006 EQEMu Development Team (http://eqemulator.net)
2013-02-16 16:14:39 -08:00
2013-05-09 10:44:08 -04:00
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
2013-05-06 13:07:41 -04:00
2013-05-09 10:44:08 -04:00
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY except by those people which sell it, which
2013-02-16 16:14:39 -08:00
are required to give you total support for your newly bought product;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR
2013-05-09 10:44:08 -04:00
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
2013-05-06 13:07:41 -04:00
2013-05-09 10:44:08 -04:00
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2013-02-16 16:14:39 -08:00
*/
#include "global_define.h"
2014-08-21 19:33:02 -07:00
#include "xml_parser.h"
2013-02-16 16:14:39 -08:00
XMLParser::XMLParser() {
ParseOkay = false;
}
bool XMLParser::ParseFile(const char *file, const char *root_ele) {
std::map<std::string,ElementHandler>::iterator handler;
2013-02-16 16:14:39 -08:00
TiXmlDocument doc( file );
if(!doc.LoadFile()) {
printf("Unable to load '%s': %s\n", file, doc.ErrorDesc());
return(false);
}
2013-05-06 13:07:41 -04:00
2013-02-16 16:14:39 -08:00
TiXmlElement *root = doc.FirstChildElement( root_ele );
2013-05-04 18:06:58 -07:00
if(root == nullptr) {
2013-02-16 16:14:39 -08:00
printf("Unable to find root '%s' in %s\n",root_ele, file);
return(false);
}
ParseOkay=true;
2013-05-06 13:07:41 -04:00
2013-05-04 18:06:58 -07:00
TiXmlNode *main_element = nullptr;
2013-02-16 16:14:39 -08:00
while( (main_element = root->IterateChildren( main_element )) ) {
if(main_element->Type() != TiXmlNode::ELEMENT)
continue; //skip crap we dont care about
TiXmlElement *ele = (TiXmlElement *) main_element;
handler=Handlers.find(ele->Value());
if (handler!=Handlers.end() && handler->second) {
ElementHandler h=handler->second;
2013-05-06 13:07:41 -04:00
2013-02-16 16:14:39 -08:00
/*
2013-05-09 10:44:08 -04:00
*
* This is kinda a sketchy operation here, since all of these
* element handler methods will be functions in child classes.
* This essentially causes us to do an un-checkable (and hence
* un-handle-properly-able) cast down to the child class. This
* WILL BREAK if any children classes do multiple inheritance.
*
*
*/
2013-05-06 13:07:41 -04:00
2013-02-16 16:14:39 -08:00
(this->*h)(ele);
} else {
//unhandled element.... do nothing for now
}
2013-05-06 13:07:41 -04:00
2013-02-16 16:14:39 -08:00
}
2013-05-06 13:07:41 -04:00
2013-02-16 16:14:39 -08:00
return(ParseOkay);
}
const char *XMLParser::ParseTextBlock(TiXmlNode *within, const char *name, bool optional) {
TiXmlElement * txt = within->FirstChildElement(name);
2013-05-04 18:06:58 -07:00
if(txt == nullptr) {
2013-02-16 16:14:39 -08:00
if(!optional) {
printf("Unable to find a '%s' element on %s element at line %d\n", name, within->Value(), within->Row());
ParseOkay=false;
}
2013-05-04 18:06:58 -07:00
return(nullptr);
2013-02-16 16:14:39 -08:00
}
TiXmlNode *contents = txt->FirstChild();
2013-05-04 18:06:58 -07:00
if(contents == nullptr || contents->Type() != TiXmlNode::TEXT) {
2013-02-16 16:14:39 -08:00
if(!optional)
printf("Node '%s' was expected to be a text element in %s element at line %d\n", name, txt->Value(), txt->Row());
2013-05-04 18:06:58 -07:00
return(nullptr);
2013-02-16 16:14:39 -08:00
}
return(contents->Value());
}
const char *XMLParser::GetText(TiXmlNode *within, bool optional) {
TiXmlNode *contents = within->FirstChild();
2013-05-04 18:06:58 -07:00
if(contents == nullptr || contents->Type() != TiXmlNode::TEXT) {
2013-02-16 16:14:39 -08:00
if(!optional) {
printf("Node was expected to be a text element in %s element at line %d\n", within->Value(), within->Row());
ParseOkay=false;
}
2013-05-04 18:06:58 -07:00
return(nullptr);
2013-02-16 16:14:39 -08:00
}
return(contents->Value());
}