//----------------------------------------------------------------------------- bool datamap_c::map_field_element( xml_element_ptr element, unsigned& column, std::string parent_key ) { xml_attribute_ptr attribute; std::string key = ""; unsigned size = 1; // This method needs more flexibility. Parsing the field here makes it easy // to know size, but the hardcoding of field names limits the capability for( unsigned j = 0; j < element->attributes(); j++ ) { attribute = element->attribute( j ); if( attribute->get_name() == "name" ) { key += attribute->get_value(); if( key == "position" ) { size = 3; } else if( key == "rotation" ) { size = 4; } else if( key == "linear-velocity" ) { size = 3; } else if( key == "angular-velocity" ) { size = 3; } else if( key == "control" ) { size = 6; } if( parent_key != "" ) { key = parent_key + "::" + key; } _map.insert( std::pair<std::string,unsigned>( key, ++column ) ); column += (size - 1); break; } } return true; }
//----------------------------------------------------------------------------- bool datamap_c::map_model_element( xml_element_ptr top, unsigned& column ) { xml_element_ptr element; xml_attribute_ptr attribute; std::string key = ""; for( unsigned i = 0; i < top->attributes(); i++ ) { attribute = top->attribute( i ); if( attribute->get_name() == "id" ) { key = attribute->get_value(); break; } } for( unsigned i = 0; i < top->elements(); i++ ) { element = top->element( i ); if( element->get_name() == "Link" ) map_link_element( element, column, key ); else if( element->get_name() == "Joint" ) map_joint_element( element, column, key ); } return true; }
//----------------------------------------------------------------------------- bool xml_c::build_tixml_element( void* tixml_element, xml_element_ptr element ) { // TODO: robust error checking TiXmlElement* tixml_e = (TiXmlElement*) tixml_element; std::string value = element->get_value(); if( value != "" ) tixml_e->LinkEndChild( new TiXmlText( value ) ); for( unsigned i = 0; i < element->attributes(); i++ ) { xml_attribute_ptr attrib = element->attribute( i ); tixml_e->SetAttribute( attrib->get_name(), attrib->get_value() ); } for( unsigned i = 0; i < element->elements(); i++ ) { xml_element_ptr child = element->element( i ); TiXmlElement* tixml_child = new TiXmlElement( child->get_name() ); tixml_e->LinkEndChild( tixml_child ); build_tixml_element( tixml_child, child ); } return true; }
//----------------------------------------------------------------------------- bool datamap_c::map_link_element( xml_element_ptr top, unsigned& column, std::string parent_key ) { xml_element_ptr element; xml_attribute_ptr attribute; std::string key = ""; for( unsigned i = 0; i < top->attributes(); i++ ) { attribute = top->attribute( i ); if( attribute->get_name() == "id" ) { key = attribute->get_value(); break; } } if( parent_key != "" ) { key = parent_key + "::" + key; } for( unsigned i = 0; i < top->elements(); i++ ) { element = top->element( i ); if( element->get_name() == "Field" ) map_field_element( element, column, key ); } return true; }