XMLAttribute* XMLElement::FindOrCreateAttribute( const char* name ) { XMLAttribute* last = 0; XMLAttribute* attrib = 0; for( attrib = rootAttribute; attrib; last = attrib, attrib = attrib->next ) { if ( XMLUtil::StringEqual( attrib->Name(), name ) ) { break; } } if ( !attrib ) { attrib = new (document->attributePool.Alloc() ) XMLAttribute(); attrib->memPool = &document->attributePool; if ( last ) { last->next = attrib; } else { rootAttribute = attrib; } attrib->SetName( name ); } return attrib; }
XMLAttribute* XMLElement::FindOrCreateAttribute( const char* name ) { XMLAttribute* last = 0; XMLAttribute* attrib = 0; for( attrib = _rootAttribute; attrib; last = attrib, attrib = attrib->_next ) { if ( XMLUtil::StringEqual( attrib->Name(), name ) ) { break; } } if ( !attrib ) { attrib = new (_document->_attributePool.Alloc() ) XMLAttribute(); attrib->_memPool = &_document->_attributePool; if ( last ) { last->_next = attrib; } else { _rootAttribute = attrib; } attrib->SetName( name ); attrib->_memPool->SetTracked(); // always created and linked. } return attrib; }
void ConfigSingleton::configure(std::string xml_file_path) { //Open XML config file and try to load it XMLDocument doc; if(doc.LoadFile(xml_file_path.c_str()) != XML_SUCCESS) { std::cerr << "Cannot reach configuration file: " << xml_file_path << "!" << std::endl; return; } //Look for <config> element XMLElement* pConfig = doc.FirstChildElement("root")->FirstChildElement("config"); if(pConfig==nullptr) { std::cerr << "Invalid configuration file: " << xml_file_path << "!" << std::endl; return; } //Iterate attribute list and set parameter values //Version 2 of TinyXML won't let me iterate over Attribute list this way by returning //const XMLAttribute*. I'm forced to const_cast before I find an elegant way of doing this. XMLAttribute* pAttrib = const_cast<XMLAttribute*>(pConfig->FirstAttribute()); while(pAttrib != nullptr) { set_parameter(pAttrib->Name(),pAttrib->Value()); pAttrib = const_cast<XMLAttribute*>(pAttrib->Next()); } }
const XMLAttribute* XMLElement::FindAttribute( const char* name ) const { XMLAttribute* a = 0; for( a=rootAttribute; a; a = a->next ) { if ( XMLUtil::StringEqual( a->Name(), name ) ) return a; } return 0; }
char* XMLElement::ParseAttributes( char* p ) { const char* start = p; XMLAttribute* prevAttribute = 0; // Read the attributes. while( p ) { p = XMLUtil::SkipWhiteSpace( p ); if ( !p || !(*p) ) { _document->SetError( XML_ERROR_PARSING_ELEMENT, start, Name() ); return 0; } // attribute. if (XMLUtil::IsNameStartChar( *p ) ) { XMLAttribute* attrib = new (_document->_attributePool.Alloc() ) XMLAttribute(); attrib->_memPool = &_document->_attributePool; attrib->_memPool->SetTracked(); p = attrib->ParseDeep( p, _document->ProcessEntities() ); if ( !p || Attribute( attrib->Name() ) ) { DELETE_ATTRIBUTE( attrib ); _document->SetError( XML_ERROR_PARSING_ATTRIBUTE, start, p ); return 0; } // There is a minor bug here: if the attribute in the source xml // document is duplicated, it will not be detected and the // attribute will be doubly added. However, tracking the 'prevAttribute' // avoids re-scanning the attribute list. Preferring performance for // now, may reconsider in the future. if ( prevAttribute ) { prevAttribute->_next = attrib; } else { _rootAttribute = attrib; } prevAttribute = attrib; } // end of the tag else if ( *p == '/' && *(p+1) == '>' ) { _closingType = CLOSED; return p+2; // done; sealed element. } // end of the tag else if ( *p == '>' ) { ++p; break; } else { _document->SetError( XML_ERROR_PARSING_ELEMENT, start, p ); return 0; } } return p; }
const XMLAttribute* XMLElement::FindAttribute(const char* name) const { for (XMLAttribute* a = _rootAttribute; a; a = a->_next) { if (XMLUtil::StringEqual(a->Name(), name)) { return a; } } return 0; }
char* XMLElement::ParseAttributes( char* p ) { const char* start = p; // Read the attributes. while( p ) { p = XMLUtil::SkipWhiteSpace( p ); if ( !p || !(*p) ) { document->SetError( XML_ERROR_PARSING_ELEMENT, start, Name() ); return 0; } // attribute. if ( XMLUtil::IsAlpha( *p ) ) { XMLAttribute* attrib = new (document->attributePool.Alloc() ) XMLAttribute(); attrib->memPool = &document->attributePool; p = attrib->ParseDeep( p, document->ProcessEntities() ); if ( !p || Attribute( attrib->Name() ) ) { DELETE_ATTRIBUTE( attrib ); document->SetError( XML_ERROR_PARSING_ATTRIBUTE, start, p ); return 0; } LinkAttribute( attrib ); } // end of the tag else if ( *p == '/' && *(p+1) == '>' ) { closingType = CLOSED; return p+2; // done; sealed element. } // end of the tag else if ( *p == '>' ) { ++p; break; } else { document->SetError( XML_ERROR_PARSING_ELEMENT, start, p ); return 0; } } return p; }
void XMLElement::DeleteAttribute(const char* name) { XMLAttribute* prev = 0; for (XMLAttribute* a = _rootAttribute; a; a = a->_next) { if (XMLUtil::StringEqual(name, a->Name())) { if (prev) { prev->_next = a->_next; } else { _rootAttribute = a->_next; } DeleteAttribute(a); break; } prev = a; } }
void cSDL2DSceneManager::addLayerObjects(c2DLayer *Layer, XMLElement *Element) { cSceneObject* Object = new cSceneObject(); unsigned int r=0; unsigned int g=0; unsigned int b=0; if(!Object) return; for(XMLAttribute* ElementAttrib = const_cast<XMLAttribute*>(Element->FirstAttribute()); ElementAttrib; ElementAttrib = const_cast<XMLAttribute*>(ElementAttrib->Next())) { std::string AttribName = ElementAttrib->Name(); std::string AttribValue = ElementAttrib->Value(); if(AttribName=="resourceID") { cResourceManager* ResourceManager = cResourceManager::GetResourceManager(); Object->setResourceObject((cRenderResource*) ResourceManager->findResourcebyID(atoi(AttribValue.c_str()))); } if(AttribName=="posx") { Object->m_PosX = atof(AttribValue.c_str()); } if(AttribName=="posy") { Object->m_PosY = atof(AttribValue.c_str()); } if(AttribName=="colorkey") { if(AttribValue=="true") Object->m_bColorKeyEnabled=true; else Object->m_bColorKeyEnabled=false; } if(AttribName=="r") { r = atoi(AttribValue.c_str()); } if(AttribName=="g") { g = atoi(AttribValue.c_str()); } if(AttribName=="b") { b = atoi(AttribValue.c_str()); } } if(Object->m_bColorKeyEnabled) Object->setColorKey(r,g,b); Layer->m_SceneObjects.push_back(Object); }
bool cSDL2DSceneManager::loadFromXMLFile(std::string Filename) { XMLDocument doc(Filename.c_str()); std::list<c2DLayer*> List; if(doc.LoadFile(Filename.c_str()) == XML_NO_ERROR) { //Find resources node XMLNode* ResourceTree = doc.RootElement(); if(ResourceTree) { //Enumerate resource objects for(XMLNode* child = ResourceTree->FirstChild(); child; child = child->NextSibling()) { XMLElement *Element = child->ToElement(); if(Element) { c2DLayer *Layer = new c2DLayer(); Layer->m_ZOrder = m_Layers.size(); for(XMLAttribute* ElementAttrib = const_cast<XMLAttribute*>(Element->FirstAttribute()); ElementAttrib; ElementAttrib = const_cast<XMLAttribute*>(ElementAttrib->Next())) { //Examine layers std::string AttribName = ElementAttrib->Name(); std::string AttribValue = ElementAttrib->Value(); //Detect resource type. Graphic? Audio? Text? if(AttribName=="name") { Layer->m_Name=AttribValue; continue; } if(AttribName=="posx") { Layer->m_PosX = atof(AttribValue.c_str()); } if(AttribName=="posy") { Layer->m_PosY = atof(AttribValue.c_str()); } if(AttribName=="visible") { if(AttribValue=="true") Layer->m_bVisible=true; else Layer->m_bVisible=false; } } m_Layers.push_back(Layer); //Cycle through layer objects for(XMLNode* objs = child->FirstChild(); objs; objs = objs->NextSibling()) { if(std::string(objs->Value())=="objects") { for(XMLNode* obj = objs->FirstChild(); obj; obj = obj->NextSibling()) { XMLElement *ObjElement = obj->ToElement(); addLayerObjects(Layer, ObjElement); } } } } } sortLayers(); return true; } } return false; }