diff --git a/source/glest_game/global/config.cpp b/source/glest_game/global/config.cpp index 125680fc0..956c5ffb9 100644 --- a/source/glest_game/global/config.cpp +++ b/source/glest_game/global/config.cpp @@ -57,29 +57,29 @@ Config::Config(std::pair type, std::pair f fileName.second = getGameReadWritePath() + fileName.second; } - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] cfgFile.first = [%s]\n",__FILE__,__FUNCTION__,__LINE__,fileName.first.c_str()); + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] cfgFile.first = [%s]\n",__FILE__,__FUNCTION__,__LINE__,fileName.first.c_str()); if(fileMustExist.first == true || (fileMustExist.first == false && fileExists(fileName.first) == true)) { properties.first.load(fileName.first); fileLoaded.first = true; - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] cfgFile.first = [%s]\n",__FILE__,__FUNCTION__,__LINE__,fileName.first.c_str()); + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] cfgFile.first = [%s]\n",__FILE__,__FUNCTION__,__LINE__,fileName.first.c_str()); } - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] cfgFile.first = [%s]\n",__FILE__,__FUNCTION__,__LINE__,fileName.first.c_str()); + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] cfgFile.first = [%s]\n",__FILE__,__FUNCTION__,__LINE__,fileName.first.c_str()); if(properties.first.getString("UserOverrideFile", defaultNotFoundValue.c_str()) != defaultNotFoundValue) { fileName.second = properties.first.getString("UserOverrideFile"); } - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] cfgFile.second = [%s]\n",__FILE__,__FUNCTION__,__LINE__,fileName.second.c_str()); + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] cfgFile.second = [%s]\n",__FILE__,__FUNCTION__,__LINE__,fileName.second.c_str()); if(fileMustExist.second == true || (fileMustExist.second == false && fileExists(fileName.second) == true)) { properties.second.load(fileName.second); fileLoaded.second = true; - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] cfgFile.second = [%s]\n",__FILE__,__FUNCTION__,__LINE__,fileName.second.c_str()); + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] cfgFile.second = [%s]\n",__FILE__,__FUNCTION__,__LINE__,fileName.second.c_str()); } try { @@ -95,8 +95,7 @@ Config::Config(std::pair type, std::pair f SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] ERROR trying to auto-create cfgFile.second = [%s]\n",__FILE__,__FUNCTION__,__LINE__,fileName.second.c_str()); } - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] cfgFile.second = [%s]\n",__FILE__,__FUNCTION__,__LINE__,fileName.second.c_str()); - + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] cfgFile.second = [%s]\n",__FILE__,__FUNCTION__,__LINE__,fileName.second.c_str()); } Config &Config::getInstance(std::pair type, std::pair file, std::pair fileMustExist) { diff --git a/source/glest_game/graphics/renderer.cpp b/source/glest_game/graphics/renderer.cpp index ddd0ef8fe..94a4ec67b 100644 --- a/source/glest_game/graphics/renderer.cpp +++ b/source/glest_game/graphics/renderer.cpp @@ -182,6 +182,15 @@ Renderer::Renderer(){ particleManager[i]= graphicsFactory->newParticleManager(); fontManager[i]= graphicsFactory->newFontManager(); } + + glHint(GL_FOG_HINT, GL_FASTEST); + //glHint(GL_GENERATE_MIPMAP_HINT, GL_FASTEST); + glHint(GL_LINE_SMOOTH_HINT, GL_FASTEST); + //glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST); + //glHint(GL_POINT_SMOOTH_HINT, GL_FASTEST); + + //glHint(GL_POLYGON_SMOOTH_HINT, GL_FASTEST); + glHint(GL_TEXTURE_COMPRESSION_HINT, GL_FASTEST); } Renderer::~Renderer(){ @@ -1184,6 +1193,7 @@ void Renderer::renderSurface(){ glEnable(GL_BLEND); glEnable(GL_COLOR_MATERIAL); glDisable(GL_ALPHA_TEST); + glEnable(GL_CULL_FACE); //fog of war tex unit glActiveTexture(fowTexUnit); @@ -2391,11 +2401,11 @@ string Renderer::getGlInfo(){ infoStr+= lang.get("OpenGlInfo")+":\n"; infoStr+= " "+lang.get("OpenGlVersion")+": "; - infoStr+= string(getGlVersion())+"\n"; + infoStr+= string((getGlVersion() != NULL ? getGlVersion() : "?"))+"\n"; infoStr+= " "+lang.get("OpenGlRenderer")+": "; - infoStr+= string(getGlRenderer())+"\n"; + infoStr+= string((getGlVersion() != NULL ? getGlVersion() : "?"))+"\n"; infoStr+= " "+lang.get("OpenGlVendor")+": "; - infoStr+= string(getGlVendor())+"\n"; + infoStr+= string((getGlVendor() != NULL ? getGlVendor() : "?"))+"\n"; infoStr+= " "+lang.get("OpenGlMaxLights")+": "; infoStr+= intToStr(getGlMaxLights())+"\n"; infoStr+= " "+lang.get("OpenGlMaxTextureSize")+": "; diff --git a/source/glest_game/main/main.cpp b/source/glest_game/main/main.cpp index 33bc30986..38d492984 100644 --- a/source/glest_game/main/main.cpp +++ b/source/glest_game/main/main.cpp @@ -378,18 +378,28 @@ int glestMain(int argc, char** argv){ AllocRegistry memoryLeaks = AllocRegistry::getInstance(); #endif + bool haveSpecialOutputCommandLineOption = false; + if( hasCommandArgument(argc, argv,"--opengl-info") == true || + hasCommandArgument(argc, argv,"--version") == true) { + haveSpecialOutputCommandLineOption = true; + } + + if( haveSpecialOutputCommandLineOption == false || + hasCommandArgument(argc, argv,"--version") == true) { #ifdef USE_STREFLOP streflop_init(); printf("%s, STREFLOP enabled.\n",getNetworkVersionString().c_str()); #else printf("%s, STREFLOP NOT enabled.\n",getNetworkVersionString().c_str()); #endif + } - if(hasCommandArgument(argc, argv,"--version") == true) { + if( hasCommandArgument(argc, argv,"--version") == true && + hasCommandArgument(argc, argv,"--opengl-info") == false) { return -1; } - SystemFlags::init(); + SystemFlags::init(haveSpecialOutputCommandLineOption); SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled = true; SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled = true; @@ -437,12 +447,14 @@ int glestMain(int argc, char** argv){ SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).debugLogFileName = debugWorldSynchLogFile; SystemFlags::getSystemSettingType(SystemFlags::debugUnitCommands).debugLogFileName = debugUnitCommandsLogFile; - printf("Startup settings are: debugSystem [%d], debugNetwork [%d], debugPerformance [%d], debugWorldSynch [%d], debugUnitCommands[%d]\n", - SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled, - SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled, - SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled, - SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled, - SystemFlags::getSystemSettingType(SystemFlags::debugUnitCommands).enabled); + if(haveSpecialOutputCommandLineOption == false) { + printf("Startup settings are: debugSystem [%d], debugNetwork [%d], debugPerformance [%d], debugWorldSynch [%d], debugUnitCommands[%d]\n", + SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled, + SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled, + SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled, + SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled, + SystemFlags::getSystemSettingType(SystemFlags::debugUnitCommands).enabled); + } NetworkInterface::setDisplayMessageFunction(ExceptionHandler::DisplayMessage); MenuStateMasterserver::setDisplayMessageFunction(ExceptionHandler::DisplayMessage); @@ -538,6 +550,13 @@ int glestMain(int argc, char** argv){ Renderer &renderer= Renderer::getInstance(); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] OpenGL Info:\n%s\n",__FILE__,__FUNCTION__,__LINE__,renderer.getGlInfo().c_str()); + if(hasCommandArgument(argc, argv,"--opengl-info") == true) { + Renderer &renderer= Renderer::getInstance(); + printf("%s",renderer.getGlInfo().c_str()); + + return -1; + } + //main loop while(Window::handleEvent()){ program->loop(); diff --git a/source/shared_lib/include/util/util.h b/source/shared_lib/include/util/util.h index e4859aa2d..227ca463c 100644 --- a/source/shared_lib/include/util/util.h +++ b/source/shared_lib/include/util/util.h @@ -107,6 +107,8 @@ protected: static std::map debugLogFileList; + static bool haveSpecialOutputCommandLineOption; + public: static CURL *curl_handle; @@ -115,7 +117,7 @@ public: SystemFlags(); ~SystemFlags(); - static void init(); + static void init(bool haveSpecialOutputCommandLineOption); static SystemFlagsType & getSystemSettingType(DebugType type) { return debugLogFileList[type]; } static size_t httpWriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data); static std::string getHTTP(std::string URL,CURL *handle=NULL, int timeOut=-1); diff --git a/source/shared_lib/sources/platform/sdl/gl_wrap.cpp b/source/shared_lib/sources/platform/sdl/gl_wrap.cpp index c8cce720a..fef5ad6dc 100644 --- a/source/shared_lib/sources/platform/sdl/gl_wrap.cpp +++ b/source/shared_lib/sources/platform/sdl/gl_wrap.cpp @@ -72,7 +72,7 @@ void PlatformContextGl::init(int colorBits, int depthBits, int stencilBits,bool // SDL_Surface* icon= SDL_CreateRGBSurfaceFrom((void*)logo,32,32,32,128,0x000000ff,0x0000ff00,0x00ff0000,0xff000000); //#endif - printf("In [%s::%s Line: %d] icon = %p\n",__FILE__,__FUNCTION__,__LINE__,icon); + //printf("In [%s::%s Line: %d] icon = %p\n",__FILE__,__FUNCTION__,__LINE__,icon); if(icon == NULL) { printf("Error: %s\n", SDL_GetError()); } diff --git a/source/shared_lib/sources/util/properties.cpp b/source/shared_lib/sources/util/properties.cpp index 55f7ee68c..120a82036 100644 --- a/source/shared_lib/sources/util/properties.cpp +++ b/source/shared_lib/sources/util/properties.cpp @@ -1,7 +1,7 @@ // ============================================================== // This file is part of Glest Shared Library (www.glest.org) // -// Copyright (C) 2001-2007 Martiño Figueroa +// Copyright (C) 2001-2007 Martio Figueroa // // You can redistribute this code and/or modify it under // the terms of the GNU General Public License as published @@ -36,7 +36,7 @@ void Properties::load(const string &path){ this->path= path; - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] path = [%s]\n",__FILE__,__FUNCTION__,__LINE__,path.c_str()); + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] path = [%s]\n",__FILE__,__FUNCTION__,__LINE__,path.c_str()); fileStream.open(path.c_str(), ios_base::in); if(fileStream.fail()){ @@ -44,7 +44,7 @@ void Properties::load(const string &path){ throw runtime_error("Can't open propertyMap file: " + path); } - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] path = [%s]\n",__FILE__,__FUNCTION__,__LINE__,path.c_str()); + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] path = [%s]\n",__FILE__,__FUNCTION__,__LINE__,path.c_str()); propertyMap.clear(); while(!fileStream.eof()){ diff --git a/source/shared_lib/sources/util/util.cpp b/source/shared_lib/sources/util/util.cpp index 854f0e07b..5ec3c6d2f 100644 --- a/source/shared_lib/sources/util/util.cpp +++ b/source/shared_lib/sources/util/util.cpp @@ -42,6 +42,7 @@ std::map SystemFlags::debug int SystemFlags::lockFile = -1; int SystemFlags::lockFileCountIndex = -1; string SystemFlags::lockfilename = ""; +bool SystemFlags::haveSpecialOutputCommandLineOption = false; CURL *SystemFlags::curl_handle = NULL; int SystemFlags::DEFAULT_HTTP_TIMEOUT = 10; // @@ -147,7 +148,8 @@ CURL *SystemFlags::initHTTP() { return handle; } -void SystemFlags::init() { +void SystemFlags::init(bool haveSpecialOutputCommandLineOption) { + SystemFlags::haveSpecialOutputCommandLineOption = haveSpecialOutputCommandLineOption; if(SystemFlags::debugLogFileList.size() == 0) { SystemFlags::debugLogFileList[SystemFlags::debugSystem] = SystemFlags::SystemFlagsType(SystemFlags::debugSystem); SystemFlags::debugLogFileList[SystemFlags::debugNetwork] = SystemFlags::SystemFlagsType(SystemFlags::debugNetwork); @@ -206,7 +208,10 @@ SystemFlags::~SystemFlags() { void SystemFlags::Close() { if(SystemFlags::debugLogFileList.size() > 0) { - printf("START Closing logfiles\n"); + if(SystemFlags::haveSpecialOutputCommandLineOption == false) { + printf("START Closing logfiles\n"); + } + for(std::map::iterator iterMap = SystemFlags::debugLogFileList.begin(); iterMap != SystemFlags::debugLogFileList.end(); iterMap++) { SystemFlags::SystemFlagsType ¤tDebugLog = iterMap->second; @@ -229,13 +234,15 @@ void SystemFlags::Close() { } if(SystemFlags::debugLogFileList.size() > 0) { - printf("END Closing logfiles\n"); + if(SystemFlags::haveSpecialOutputCommandLineOption == false) { + printf("END Closing logfiles\n"); + } } } void SystemFlags::handleDebug(DebugType type, const char *fmt, ...) { if(SystemFlags::debugLogFileList.size() == 0) { - SystemFlags::init(); + SystemFlags::init(false); } SystemFlags::SystemFlagsType ¤tDebugLog = SystemFlags::debugLogFileList[type]; if(currentDebugLog.enabled == false) { @@ -312,13 +319,17 @@ void SystemFlags::handleDebug(DebugType type, const char *fmt, ...) { SystemFlags::lockfilename = newlockfile; debugLog += intToStr(idx); - printf("Opening additional logfile [%s]\n",debugLog.c_str()); + if(SystemFlags::haveSpecialOutputCommandLineOption == false) { + printf("Opening additional logfile [%s]\n",debugLog.c_str()); + } } } else if(SystemFlags::lockFileCountIndex > 0) { debugLog += intToStr(SystemFlags::lockFileCountIndex); - printf("Opening additional logfile [%s]\n",debugLog.c_str()); + if(SystemFlags::haveSpecialOutputCommandLineOption == false) { + printf("Opening additional logfile [%s]\n",debugLog.c_str()); + } } if(currentDebugLog.fileStream == NULL) { @@ -328,7 +339,9 @@ void SystemFlags::handleDebug(DebugType type, const char *fmt, ...) { currentDebugLog.mutex = new Mutex(); } - printf("Opening logfile [%s] type = %d, currentDebugLog.fileStreamOwner = %d\n",debugLog.c_str(),type, currentDebugLog.fileStreamOwner); + if(SystemFlags::haveSpecialOutputCommandLineOption == false) { + printf("Opening logfile [%s] type = %d, currentDebugLog.fileStreamOwner = %d\n",debugLog.c_str(),type, currentDebugLog.fileStreamOwner); + } MutexSafeWrapper safeMutex(currentDebugLog.mutex); diff --git a/source/shared_lib/sources/xml/xml_parser.cpp b/source/shared_lib/sources/xml/xml_parser.cpp new file mode 100644 index 000000000..727f17059 --- /dev/null +++ b/source/shared_lib/sources/xml/xml_parser.cpp @@ -0,0 +1,428 @@ +// ============================================================== +// This file is part of Glest Shared Library (www.glest.org) +// +// Copyright (C) 2001-2008 Marti�o Figueroa +// +// You can redistribute this code and/or modify it under +// the terms of the GNU General Public License as published +// by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version +// ============================================================== + +#include "xml_parser.h" + +#include +#include + +#include "conversion.h" + +#include +#include +#include + +#include "leak_dumper.h" + + +XERCES_CPP_NAMESPACE_USE + +using namespace std; + +namespace Shared{ namespace Xml{ + +using namespace Util; + +// ===================================================== +// class ErrorHandler +// ===================================================== + +class ErrorHandler: public DOMErrorHandler{ +public: + virtual bool handleError (const DOMError &domError){ + if(domError.getSeverity()== DOMError::DOM_SEVERITY_FATAL_ERROR){ + char msgStr[strSize], fileStr[strSize]; + XMLString::transcode(domError.getMessage(), msgStr, strSize-1); + XMLString::transcode(domError.getLocation()->getURI(), fileStr, strSize-1); + int lineNumber= domError.getLocation()->getLineNumber(); + throw runtime_error("Error parsing XML, file: " + string(fileStr) + ", line: " + intToStr(lineNumber) + ": " + string(msgStr)); + } + return true; + } +}; + +// ===================================================== +// class XmlIo +// ===================================================== + +bool XmlIo::initialized= false; + +XmlIo::XmlIo(){ + try{ + XMLPlatformUtils::Initialize(); + } + catch(const XMLException&){ + throw runtime_error("Error initializing XML system"); + } + + try{ + XMLCh str[strSize]; + XMLString::transcode("LS", str, strSize-1); + + implementation = DOMImplementationRegistry::getDOMImplementation(str); + } + catch(const DOMException){ + throw runtime_error("Exception while creating XML parser"); + } +} + +XmlIo &XmlIo::getInstance(){ + static XmlIo XmlIo; + return XmlIo; +} + +XmlIo::~XmlIo(){ + XMLPlatformUtils::Terminate(); +} + +XmlNode *XmlIo::load(const string &path){ + + try{ + ErrorHandler errorHandler; +#if XERCES_VERSION_MAJOR < 3 + DOMBuilder *parser= (static_cast(implementation))->createDOMBuilder(DOMImplementationLS::MODE_SYNCHRONOUS, 0); + parser->setErrorHandler(&errorHandler); + parser->setFeature(XMLUni::fgXercesSchema, true); + parser->setFeature(XMLUni::fgXercesSchemaFullChecking, true); + parser->setFeature(XMLUni::fgDOMValidation, true); +#else + DOMLSParser *parser = (static_cast(implementation))->createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS, 0); + DOMConfiguration *config = parser->getDomConfig(); + config->setParameter(XMLUni::fgXercesSchema, true); + config->setParameter(XMLUni::fgXercesSchemaFullChecking, true); + config->setParameter(XMLUni::fgDOMValidate, true); +#endif + DOMDocument *document= parser->parseURI(path.c_str()); + + if(document==NULL){ + throw runtime_error("Can not parse URL: " + path); + } + + XmlNode *rootNode= new XmlNode(document->getDocumentElement()); + parser->release(); + return rootNode; + } + catch(const DOMException &e){ + throw runtime_error("Exception while loading: " + path + ": " + XMLString::transcode(e.msg)); + } +} + +void XmlIo::save(const string &path, const XmlNode *node){ + try{ + XMLCh str[strSize]; + XMLString::transcode(node->getName().c_str(), str, strSize-1); + + DOMDocument *document= implementation->createDocument(0, str, 0); + DOMElement *documentElement= document->getDocumentElement(); + + for(int i=0; igetChildCount(); ++i){ + documentElement->appendChild(node->getChild(i)->buildElement(document)); + } + + LocalFileFormatTarget file(path.c_str()); +#if XERCES_VERSION_MAJOR < 3 + DOMWriter* writer = implementation->createDOMWriter(); + writer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true); + writer->writeNode(&file, *document); +#else + DOMLSSerializer *serializer = implementation->createLSSerializer(); + DOMLSOutput* output=implementation->createLSOutput(); + DOMConfiguration* config=serializer->getDomConfig(); + config->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint,true); + output->setByteStream(&file); + serializer->write(document,output); + output->release(); + serializer->release(); +#endif + document->release(); + } + catch(const DOMException &e){ + throw runtime_error("Exception while saving: " + path + ": " + XMLString::transcode(e.msg)); + } +} +// ===================================================== +// class XmlTree +// ===================================================== + +XmlTree::XmlTree(){ + rootNode= NULL; +} + +void XmlTree::init(const string &name){ + this->rootNode= new XmlNode(name); +} + +void XmlTree::load(const string &path){ + this->rootNode= XmlIo::getInstance().load(path); +} + +void XmlTree::save(const string &path){ + XmlIo::getInstance().save(path, rootNode); +} + +XmlTree::~XmlTree(){ + delete rootNode; +} + +// ===================================================== +// class XmlNode +// ===================================================== + +XmlNode::XmlNode(DOMNode *node){ + + //get name + char str[strSize]; + XMLString::transcode(node->getNodeName(), str, strSize-1); + name= str; + + //check document + if(node->getNodeType()==DOMNode::DOCUMENT_NODE){ + name="document"; + } + + //check children + for(int i=0; igetChildNodes()->getLength(); ++i){ + DOMNode *currentNode= node->getChildNodes()->item(i); + if(currentNode->getNodeType()==DOMNode::ELEMENT_NODE){ + XmlNode *xmlNode= new XmlNode(currentNode); + children.push_back(xmlNode); + } + } + + //check attributes + DOMNamedNodeMap *domAttributes= node->getAttributes(); + if(domAttributes!=NULL){ + for(int i=0; igetLength(); ++i){ + DOMNode *currentNode= domAttributes->item(i); + if(currentNode->getNodeType()==DOMNode::ATTRIBUTE_NODE){ + XmlAttribute *xmlAttribute= new XmlAttribute(domAttributes->item(i)); + attributes.push_back(xmlAttribute); + } + } + } + + //get value + if(node->getNodeType()==DOMNode::ELEMENT_NODE && children.size()==0){ + char *textStr= XMLString::transcode(node->getTextContent()); + text= textStr; + XMLString::release(&textStr); + } +} + +XmlNode::XmlNode(const string &name){ + this->name= name; +} + +XmlNode::~XmlNode(){ + for(int i=0; i=attributes.size()){ + throw runtime_error(getName()+" node doesn't have "+intToStr(i)+" attributes"); + } + return attributes[i]; +} + +XmlAttribute *XmlNode::getAttribute(const string &name) const{ + for(int i=0; igetName()==name){ + return attributes[i]; + } + } + throw runtime_error("\"" + getName() + "\" node doesn't have a attribute named \"" + name + "\""); +} + +XmlNode *XmlNode::getChild(int i) const { + if(i>=children.size()) + throw runtime_error("\"" + getName()+"\" node doesn't have "+intToStr(i+1)+" children"); + return children[i]; +} + + +XmlNode *XmlNode::getChild(const string &childName, int i) const{ + if(i>=children.size()){ + throw runtime_error("\"" + name + "\" node doesn't have "+intToStr(i+1)+" children named \"" + childName + "\"\n\nTree: "+getTreeString()); + } + + int count= 0; + for(int j=0; jgetName()==childName){ + if(count==i){ + return children[j]; + } + count++; + } + } + + throw runtime_error("Node \""+getName()+"\" doesn't have "+intToStr(i+1)+" children named \""+childName+"\"\n\nTree: "+getTreeString()); +} + +bool XmlNode::hasChildAtIndex(const string &childName, int i) const +{ + int count= 0; + for(int j = 0; j < children.size(); ++j) + { + if(children[j]->getName()==childName) + { + if(count==i){ + return true; + } + count++; + } + } + + return false; +} + + +bool XmlNode::hasChild(const string &childName) const +{ + int count= 0; + for(int j = 0; j < children.size(); ++j) + { + if(children[j]->getName()==childName) + { + return true; + } + } + + return false; +} + +XmlNode *XmlNode::addChild(const string &name){ + XmlNode *node= new XmlNode(name); + children.push_back(node); + return node; +} + +XmlAttribute *XmlNode::addAttribute(const string &name, const string &value){ + XmlAttribute *attr= new XmlAttribute(name, value); + attributes.push_back(attr); + return attr; +} + +DOMElement *XmlNode::buildElement(DOMDocument *document) const{ + XMLCh str[strSize]; + XMLString::transcode(name.c_str(), str, strSize-1); + + DOMElement *node= document->createElement(str); + + for(int i=0; igetName().c_str(), str, strSize-1); + DOMAttr *attr= document->createAttribute(str); + + XMLString::transcode(attributes[i]->getValue().c_str(), str, strSize-1); + attr->setValue(str); + + node->setAttributeNode(attr); + } + + for(int i=0; iappendChild(children[i]->buildElement(document)); + } + + return node; +} + +string XmlNode::getTreeString() const{ + string str; + + str+= getName(); + + if(!children.empty()){ + str+= " ("; + for(int i=0; igetTreeString(); + str+= " "; + } + str+=") "; + } + + return str; +} + +// ===================================================== +// class XmlAttribute +// ===================================================== + +XmlAttribute::XmlAttribute(DOMNode *attribute){ + char str[strSize]; + + XMLString::transcode(attribute->getNodeValue(), str, strSize-1); + value= str; + + XMLString::transcode(attribute->getNodeName(), str, strSize-1); + name= str; +} + +XmlAttribute::XmlAttribute(const string &name, const string &value){ + this->name= name; + this->value= value; +} + +bool XmlAttribute::getBoolValue() const{ + if(value=="true"){ + return true; + } + else if(value=="false"){ + return false; + } + else{ + throw runtime_error("Not a valid bool value (true or false): " +getName()+": "+ value); + } +} + +int XmlAttribute::getIntValue() const{ + return strToInt(value); +} + +int XmlAttribute::getIntValue(int min, int max) const{ + int i= strToInt(value); + if(imax){ + throw runtime_error("Xml Attribute int out of range: " + getName() + ": " + value); + } + return i; +} + +float XmlAttribute::getFloatValue() const{ + return strToFloat(value); +} + +float XmlAttribute::getFloatValue(float min, float max) const{ + float f= strToFloat(value); + if(fmax){ + throw runtime_error("Xml attribute float out of range: " + getName() + ": " + value); + } + return f; +} + +const string &XmlAttribute::getRestrictedValue() const +{ + const string allowedCharacters = "abcdefghijklmnopqrstuvwxyz1234567890._-/"; + + for(int i= 0; i