//--------------------------------------------------------- void ofXMLSettings::removeTag(string tag, int which){ vector<string> tokens = tokenize(tag,":"); //no tags so we return if( tokens.size() == 0 ) return; //grab the handle from the level we are at //normally this is the doc but could be a pushed node TiXmlHandle tagHandle = *storedHandle; if(which < 0) which = 0; for(int x=0;x<tokens.size();x++){ //we only support multi tags //with same name at root level if(x > 0) which = 0; TiXmlHandle isRealHandle = tagHandle.ChildElement( tokens.at(x), which); if ( !isRealHandle.Node() ) break; else{ if (x == tokens.size()-1){ //if we are at the last tag and it exists //we use its parent to remove it - haha tagHandle.ToNode()->RemoveChild( isRealHandle.ToNode() ); } tagHandle = isRealHandle; } } tokens.clear(); }
//--------------------------------------------------------- bool ofXMLSettings::readTag(string tag, char * valueString, int which){ vector<string> tokens = tokenize(tag,":"); TiXmlHandle tagHandle = *storedHandle; for(int x=0;x<tokens.size();x++){ if(x == 0)tagHandle = tagHandle.ChildElement(tokens.at(x), which); else tagHandle = tagHandle.FirstChildElement( tokens.at(x) ); } // once we've walked, let's get that value... TiXmlHandle valHandle = tagHandle.Child( 0 ); //now, clear that vector! tokens.clear(); // if that value is really text, let's get the value out of it ! if (valHandle.Text()){ int maxLen = MIN(MAX_TAG_VALUE_LENGTH_IN_CHARS, strlen(valHandle.Text()->Value())); memcpy(valueString, valHandle.Text()->Value(), maxLen); return true; } else { return false; } }
//--------------------------------------------------------- int ofxXmlSettings::writeAttribute(const string& tag, const string& attribute, const string& valueString, int which){ vector<string> tokens = tokenize(tag,":"); TiXmlHandle tagHandle = storedHandle; for (int x = 0; x < (int)tokens.size(); x++) { if (x == 0) tagHandle = tagHandle.ChildElement(tokens.at(x), which); else tagHandle = tagHandle.FirstChildElement(tokens.at(x)); } int ret = 0; if (tagHandle.ToElement()) { TiXmlElement* elem = tagHandle.ToElement(); elem->SetAttribute(attribute, valueString); // Do we really need this? We could just ignore this and remove the 'addAttribute' functions... // Now, just get the ID. int numSameTags; TiXmlElement* child = ( storedHandle.FirstChildElement( tokens.at(0) ) ).ToElement(); for (numSameTags = 0; child; child = child->NextSiblingElement( tokens.at(0) ), ++numSameTags) { // nothing } ret = numSameTags; } return ret; }
//--------------------------------------------------------- bool ofxXmlSettings::tagExists(const string& tag, int which){ vector<string> tokens = tokenize(tag,":"); bool found = false; //grab the handle from the level we are at //normally this is the doc but could be a pushed node TiXmlHandle tagHandle = storedHandle; if(which < 0) which = 0; for(int x=0;x<(int)tokens.size();x++){ //we only support multi tags //with same name at root level if(x > 0) which = 0; TiXmlHandle isRealHandle = tagHandle.ChildElement( tokens.at(x), which); //as soon as we find a tag that doesn't exist //we return false; if ( !isRealHandle.ToNode() ){ found = false; break; } else{ found = true; tagHandle = isRealHandle; } } return found; }
void SyncXmlParser::parseXmlFile() { printDebugMessage("parsing xml file", STATUS); TiXmlDocument doc(xml_file_name.c_str()); bool load_ok = doc.LoadFile(); if(load_ok) { printDebugMessage("file loaded: " + xml_file_name, INFO); } else { printDebugMessage("file load error: " + xml_file_name, INFO); exit(1); } TiXmlHandle doc_handle(&doc); TiXmlHandle sync = doc_handle.ChildElement("sync", 0); if(sync.Element()) { TiXmlHandle streams = sync.ChildElement("streams", 0); TiXmlHandle general = sync.ChildElement("general", 0); if(streams.Element()) { TiXmlHandle dvb = streams.ChildElement("dvb", 0); cout << dvb.ChildElement("active", 0).Element()->Value() << ": " << dvb.ChildElement("active", 0).Element()->GetText() << endl; int number_of_p2ps = 0; for(int i=0; i<30; i++) { TiXmlHandle p2p = streams.ChildElement("p2p", i); if(p2p.Element()) { number_of_p2ps++; } else { break; } } cout << "number of p2p streams: " << number_of_p2ps << endl; } if(general.Element()) { } } }
//--------------------------------------------------------- TiXmlElement* ofxXmlSettings::getElementForAttribute(const string& tag, int which){ vector<string> tokens = tokenize(tag,":"); TiXmlHandle tagHandle = storedHandle; for (int x = 0; x < (int)tokens.size(); x++) { if (x == 0) tagHandle = tagHandle.ChildElement(tokens.at(x), which); else tagHandle = tagHandle.FirstChildElement(tokens.at(x)); } return tagHandle.ToElement(); }
//--------------------------------------------------------- bool ofxXmlSettings::readTag(const string& tag, TiXmlHandle& valHandle, int which){ vector<string> tokens = tokenize(tag,":"); TiXmlHandle tagHandle = storedHandle; for(int x=0;x<(int)tokens.size();x++){ if(x == 0)tagHandle = tagHandle.ChildElement(tokens.at(x), which); else tagHandle = tagHandle.FirstChildElement( tokens.at(x) ); } // once we've walked, let's get that value... valHandle = tagHandle.Child( 0 ); return (valHandle.ToText() != NULL); }
//--------------------------------------------------------- void ofxXmlSettings::removeAttribute(const string& tag, const string& attribute, int which){ vector<string> tokens = tokenize(tag,":"); TiXmlHandle tagHandle = storedHandle; for (int x = 0; x < (int)tokens.size(); x++) { if (x == 0) tagHandle = tagHandle.ChildElement(tokens.at(x), which); else tagHandle = tagHandle.FirstChildElement(tokens.at(x)); } if (tagHandle.ToElement()) { TiXmlElement* elem = tagHandle.ToElement(); elem->RemoveAttribute(attribute); } }
//--------------------------------------------------------- bool ofxXmlSettings::getAttributeNames(const string& tag, vector<string>& outNames, int which){ vector<string> tokens = tokenize(tag,":"); TiXmlHandle tagHandle = storedHandle; for (int x = 0; x < (int)tokens.size(); x++) { if (x == 0) tagHandle = tagHandle.ChildElement(tokens.at(x), which); else tagHandle = tagHandle.FirstChildElement(tokens.at(x)); } if (tagHandle.ToElement()) { TiXmlElement* elem = tagHandle.ToElement(); // Do stuff with the element here for (TiXmlAttribute* a = elem->FirstAttribute(); a; a = a->Next()) outNames.push_back( string(a->Name()) ); } return !outNames.empty(); }
//--------------------------------------------------------- bool ofxXmlSettings::attributeExists(const string& tag, const string& attribute, int which){ vector<string> tokens = tokenize(tag,":"); TiXmlHandle tagHandle = storedHandle; for (int x = 0; x < (int)tokens.size(); x++) { if (x == 0) tagHandle = tagHandle.ChildElement(tokens.at(x), which); else tagHandle = tagHandle.FirstChildElement(tokens.at(x)); } if (tagHandle.ToElement()) { TiXmlElement* elem = tagHandle.ToElement(); // Do stuff with the element here for (TiXmlAttribute* a = elem->FirstAttribute(); a; a = a->Next()) { if (a->Name() == attribute) return true; } } return false; }
//--------------------------------------------------------- int ofxXmlSettings::getNumAttributes(const string& tag, int which){ vector<string> tokens = tokenize(tag,":"); TiXmlHandle tagHandle = storedHandle; for (int x = 0; x < (int)tokens.size(); x++) { if (x == 0) tagHandle = tagHandle.ChildElement(tokens.at(x), which); else tagHandle = tagHandle.FirstChildElement(tokens.at(x)); } if (tagHandle.ToElement()) { TiXmlElement* elem = tagHandle.ToElement(); // Do stuff with the element here TiXmlAttribute* first = elem->FirstAttribute(); if (first) { int count = 1; for (TiXmlAttribute* curr = first; curr != elem->LastAttribute(); curr = curr->Next()) count++; return count; } } return 0; }
//--------------------------------------------------------- int ofXMLSettings::writeTag(string tag, char * valueStr, int which){ vector<string> tokens = tokenize(tag,":"); // allocate then clean up : TiXmlElement ** elements = new TiXmlElement*[tokens.size()]; for(int x=0;x<tokens.size();x++){ elements[x] = new TiXmlElement(tokens.at(x)); } TiXmlText Value(valueStr); // search our way up - do these tags exist? // find the first that DOESNT exist, then move backwards... TiXmlHandle tagHandle = *storedHandle; bool addNewTag = false; if(which == -1)addNewTag = true; for(int x=0;x<tokens.size();x++){ if( x > 0 ){ //multi tags of same name //only for the root level which = 0; addNewTag = false; } TiXmlHandle isRealHandle = tagHandle.ChildElement( tokens.at(x), which); if ( !isRealHandle.Node() || addNewTag){ for(int i=tokens.size()-1;i>=x;i--){ if (i == tokens.size()-1){ elements[i]->InsertEndChild(Value); } else { elements[i]->InsertEndChild(*(elements[i+1])); } } tagHandle.ToNode()->InsertEndChild(*(elements[x])); break; } else { tagHandle = isRealHandle; if (x == tokens.size()-1){ // what we want to change : TiXmlHandle valHandle = tagHandle.Child( 0 ); tagHandle.ToNode()->Clear(); tagHandle.ToNode()->InsertEndChild(Value); } } } //lets find how many of the same tags exist //then we can return the numTags; int numSameTags = 0; while( (storedHandle->ChildElement( tokens.at(0), numSameTags )).Node() ){ numSameTags++; } //now, clear that vector! tokens.clear(); return numSameTags; }
shared_ptr<Entity> EntityFactory::createEntity(const string& path) { shared_ptr<Entity> entity(new Entity); shared_ptr<RenderJob> renderjob(new RenderJob()); entity->setRenderJob(renderjob); TiXmlDocument entityfile(path); if (!entityfile.LoadFile()) { // If error when loading file. LOG(logERROR) << "Error when loading entity file " << path << "\nError at line " << entityfile.ErrorRow() << "\nError description: " << entityfile.ErrorDesc(); throw EntityCreationError(); } TiXmlHandle dochandle = TiXmlHandle(&entityfile).FirstChild("entity"); // Set name and description. TiXmlElement* name_element = dochandle.FirstChild("name").ToElement(); if (name_element) entity->setName(name_element->GetText()); else LOG(logWARNING) << "No name element in entity file " << path; TiXmlElement* desc_element = dochandle.FirstChild("desc").ToElement(); if (desc_element) { entity->setDesc(desc_element->GetText()); } else { LOG(logWARNING) << "No desc element in entity file " << path; } /* * Load gfx. */ // Textures are loaded to an array in the order they are declared. My shader // code assumes that the albedo texture is index 0 and the normal map in // index 1. TiXmlHandle texhandle = dochandle.FirstChild("gfx").FirstChild("textures"); if (texhandle.ToNode()) { // If texture tag exists. vector<GLuint> created_textures; TiXmlElement* texture_element; int i = 0; while (texture_element = texhandle.ChildElement(i++).ToElement()) { string texname(texture_element->GetText()); created_textures.push_back(Locator::getFileService().makeTexture(texname)); } int num_textures = created_textures.size(); if (num_textures > 0) { renderjob->m_textures = new GLuint[num_textures]; } renderjob->m_num_textures = num_textures;; for (int i = 0; i < num_textures; i++) { renderjob->m_textures[i] = created_textures[i]; } } // Load model and check number of materials. TiXmlElement* model_element = dochandle.FirstChild("gfx").FirstChild("model").ToElement(); if (!model_element) { LOG(logERROR) << "No model element in entity file " << path; throw EntityCreationError(); } string modelpath = "assets/models/" + string(model_element->GetText()) + ".obj"; string absolute_path(Locator::getFileService().getRealPath(modelpath)); ObjFile model(absolute_path); // Load shaders. bool materials_defined = false; // Needed to determine whether uniform blocks are created. { TiXmlElement* shader_defines_element = dochandle.FirstChild("gfx").FirstChild("shader_defines").ToElement(); if (!shader_defines_element) { LOG(logERROR) << "No shader_defines element in entity file " << path; throw EntityCreationError(); } set<string> defines; string shader_defines(shader_defines_element->GetText()); // Tokenize shader_defines; typedef boost::tokenizer<boost::char_separator<char> > tokenizer; boost::char_separator<char> comma(", "); tokenizer tokens(shader_defines, comma); foreach(string define, tokens) { defines.insert(define); } GLuint num_materials = model.getNumMaterials(); string materials_define("MATERIALS"); if (defines.find(materials_define) != defines.end()) { // If materials define found. materials_defined = true; defines.erase(materials_define); stringstream materials_define_stream; materials_define_stream << materials_define << " " << num_materials; defines.insert(materials_define_stream.str()); } // Insert vertex attrib indices. int i = 0; foreach(const char* enum_name, renderjob_enums::vertex_strings) { defines.insert(makeDefineFromEnum(enum_name, i++)); }
//--------------------------------------------------------- int ofxXmlSettings::writeTag(const string& tag, const string& valueStr, int which){ vector<string> tokens = tokenize(tag,":"); // allocate on the stack vector<TiXmlElement> elements; elements.reserve(tokens.size()); for(int x=0;x<(int)tokens.size();x++) elements.push_back(tokens.at(x)); TiXmlText Value(valueStr); // search our way up - do these tags exist? // find the first that DOESNT exist, then move backwards... TiXmlHandle tagHandle = storedHandle; bool addNewTag = false; if(which == -1)addNewTag = true; for(int x=0;x<(int)tokens.size();x++){ if( x > 0 ){ //multi tags of same name //only for the root level which = 0; addNewTag = false; } TiXmlHandle isRealHandle = tagHandle.ChildElement( tokens.at(x), which); if ( !isRealHandle.ToNode() || addNewTag){ for(int i=(int)tokens.size()-1;i>=x;i--){ if (i == (int)tokens.size()-1){ elements[i].InsertEndChild(Value); } else { elements[i].InsertEndChild(elements[i+1]); } } tagHandle.ToNode()->InsertEndChild(elements[x]); break; } else { tagHandle = isRealHandle; if (x == (int)tokens.size()-1){ // what we want to change : TiXmlHandle valHandle = tagHandle.Child( 0 ); tagHandle.ToNode()->Clear(); tagHandle.ToNode()->InsertEndChild(Value); } } } //lets count how many tags with our name exist so we can return an index //ripped from tinyXML as doing this ourselves once is a LOT! faster //than having this called n number of times in a while loop - we go from n*n iterations to n iterations int numSameTags; TiXmlElement* child = ( storedHandle.FirstChildElement( tokens.at(0) ) ).ToElement(); for (numSameTags = 0; child; child = child->NextSiblingElement( tokens.at(0) ), ++numSameTags){ //nothing } return numSameTags; }