Exemplo n.º 1
0
//-----------------------------------------------------------------------------
// <Options::ParseOptionsXML>
// Parse an XML file containing program options
//-----------------------------------------------------------------------------
bool Options::ParseOptionsXML
(
	string const& _filename
)
{
	TiXmlDocument doc;
	if( !doc.LoadFile( _filename.c_str(), TIXML_ENCODING_UTF8 ) )
	{
		Log::Write(LogLevel_Warning, "Failed to Parse %s: %s", _filename.c_str(), doc.ErrorDesc());
		return false;
	}
	Log::Write(LogLevel_Info, "Reading %s for Options", _filename.c_str());

	TiXmlElement const* optionsElement = doc.RootElement();

	// Read the options
	TiXmlElement const* optionElement = optionsElement->FirstChildElement();
	while( optionElement )
	{
		char const* str = optionElement->Value();
		if( str && !strcmp( str, "Option" ) )
		{
			char const* name = optionElement->Attribute( "name" );
			if( name )
			{
				Option* option = Find( name );
				if( option )
				{
					char const* value = optionElement->Attribute( "value" );
					if( value )
					{
						// Set the value
						option->SetValueFromString( value );
					}
				}
			}
		}

		optionElement = optionElement->NextSiblingElement();
	}

	return true;
}
Exemplo n.º 2
0
//-----------------------------------------------------------------------------
// <Options::ParseOptionsXML>
// Parse an XML file containing program options
//-----------------------------------------------------------------------------
bool Options::ParseOptionsXML
(
	string const& _filename
)
{
	TiXmlDocument doc;
	if( !doc.LoadFile( _filename.c_str(), TIXML_ENCODING_UTF8 ) )
	{
		return false;
	}

	TiXmlElement const* optionsElement = doc.RootElement();

	// Read the options
	TiXmlElement const* optionElement = optionsElement->FirstChildElement();
	while( optionElement )
	{
		char const* str = optionElement->Value();
		if( str && !strcmp( str, "Option" ) )
		{
			char const* name = optionElement->Attribute( "name" );
			if( name )
			{
				Option* option = Find( name );
				if( option )
				{
					char const* value = optionElement->Attribute( "value" );
					if( value )
					{	
						// Set the value
						option->SetValueFromString( value );
					}
				}
			}
		}

		optionElement = optionElement->NextSiblingElement();
	}

	return true;
}
Exemplo n.º 3
0
//-----------------------------------------------------------------------------
// <Options::ParseOptionsString>
// Parse a string containing program options, such as a command line
//-----------------------------------------------------------------------------
bool Options::ParseOptionsString
(
	string const& _commandLine
)
{
	bool res = true;

	int pos = 0;
	int start = 0;
	while( 1 )
	{
		pos = _commandLine.find_first_of( "--", start );
		if( string::npos == pos )
		{
			break;
		}
		start = pos + 2;

		// found an option.  Get the name.
		string optionName;
		pos = _commandLine.find( " ", start );
		if( string::npos == pos )
		{
			optionName = _commandLine.substr( start );
			start = pos;
		}
		else
		{
			optionName = _commandLine.substr( start, pos-start );
			start = pos + 1;
		}

		// Find the matching option object
		Option* option = Find( optionName );
		if( option )
		{
			// Read the values
			int numValues = 0;
			bool parsing = true;
			while( parsing )
			{
				string value;
				pos = _commandLine.find( " ", start );
				if( string::npos == pos )
				{
					// Last value in string
					value = _commandLine.substr( start );
					parsing = false;
					start = pos;
				}
				else
				{
					value = _commandLine.substr( start, pos-start );
					start = pos+1;
				}

				if( !value.compare( 0, 2, "--" ) )
				{
					// Value is actually the next option.
					if( !numValues )
					{
						// No values were read for this option
						// This is ok only for bool options, where we assume no value means "true".
						if( OptionType_Bool == option->m_type )
						{
							option->m_valueBool = true;
						}
						else
						{
							res = false;
						}
					}
				}
				else if( value.size() > 0 )
				{
					// Set the value
					option->SetValueFromString( value );
				}
			}
		}
	}

	return res;
}