Boost::property_tree at its finest (for parsing XML files in this case):
// initialises all resources and definitions for the specified scenario
bool ScenarioManager::initialiseScenario( std::string& scenarioName )
{
// boost::property_tree will throw an std::runtime_error if anything fails
try {
// resolve namespaces
using namespace boost::property_tree;
// find matching scenario node by iterating over them
ptree scenarioNode = m_ScenarioProperties->get_child( "root" );
for( ptree::iterator it = scenarioNode.begin();; )
{
// make sure we're looking at a scenario node
if( it->first.compare( "scenario" ) == 0 )
{
// compare attribute with specified scenario name and see if we've found it
if( it->second.get_child( "<xmlattr>" ).get<std::string>( "name" ).compare( scenarioName ) == 0 )
{
scenarioNode = it->second;
break;
}
}
// next iteration
++it;
// if we've reached the end and the scenario name wasn't found yet, it doesn't exist
if( it == scenarioNode.end() )
throw ptree_error( "Scenario name not defined in XML file" );
}
// catch errors
}catch( std::exception& e ){
std::cout << "FAGGOT!! " << e.what() << std::endl;
return false;
}
return true;
}
Especially things like this:
if( it->second.get_child( "<xmlattr>" ).get<std::string>( "name" ).compare( scenarioName ) == 0 )
The time it took me to figure out how it's done... My head hurts.
[EDIT] Oh and here's the xml file.
<root>
<scenario name="gay1"></scenario>
<scenario name="somethingelsegay></scenario>
</root>
TheComet