Пример #1
0
static void RemoveLeadingWhitespace( vsString &string )
{
	bool done = false;

	while(!done)
	{
		done = true;

		if ( !string.empty() && IsWhitespace(string[0]) )
		{
			string.erase(0,1);
			done = false;
		}
	}
}
Пример #2
0
static vsString ExtractStringToken( vsString &string )
{
	vsAssert(string[0] == '\"', "Tried to extract a string that didn't start with \"!");

	vsString result;

	//result.append( 1, string[0] );	// get first '"'
	string.erase(0,1);

	bool escaped = false;
	while( !string.empty() && string[0] ){
		if ( escaped )
		{
			if ( string[0] == 'n' )
			{
				result.append( 1, '\n' );
			}
			else
			{
				result.append( 1, string[0] );
			}
			escaped = false;
		}
		else
		{
			if ( string[0] == '\"' )
			{
				break; // end of string!
			}
			else if ( string[0] == '\\' )
			{
				escaped = true;
			}
			else
			{
				result.append( 1, string[0] );
			}
		}
		string.erase(0,1);
	}
	//result.append( 1, string[0] );	// get last '"'
	string.erase(0,1);

	return result;
}
Пример #3
0
bool
vsToken::ExtractFrom( vsString &string )
{
	m_type = Type_None;

	RemoveLeadingWhitespace(string);

	if ( !string.empty() )
	{
		if ( string[0] == '\"' )
		{
			m_string = ExtractStringToken(string);
			m_type = Type_String;
			return true;
		}
		else if ( string[0] == '{' )
		{
			m_type = Type_OpenBrace;
			string.erase(0,1);
			return true;
		}
		else if ( string[0] == '}' )
		{
			m_type = Type_CloseBrace;
			string.erase(0,1);
			return true;
		}
		else if ( string[0] == '\n' )
		{
			m_type = Type_NewLine;
			string.erase(0,1);
			return true;
		}
		else if ( ::IsAlpha(string[0]) )
		{
			m_string = ExtractLabelToken(string);
			m_type = Type_Label;
			return true;
		}
		else if ( ::IsNumeric( string[0]) )
		{
			vsString token = ExtractNumberToken(string);

			if ( token == "-" ) // Ugly:  handle negative nans as zeroes.
			{
				m_int = 0;
				m_type = Type_Integer;
				return false;
			}

			bool isAFloat = ( token.find('.') != token.npos );
			if ( isAFloat )
			{
				bool success = (sscanf( token.c_str(), "%f", &m_float )!=0);
				vsAssert(success, "Couldn't find a floating value where we expected one?");
				m_type = Type_Float;
				return true;
			}
			else
			{
				bool success = sscanf( token.c_str(), "%d", &m_int) != 0;
				vsAssert(success, "Couldn't find an integer value?" );
				m_type = Type_Integer;
				return true;
			}
		}
		else if ( string[0] == '#' )
		{
			// comment!
			string = vsEmptyString;
		}
		else if ( string[0] == '=' )
		{
			m_type = Type_Equals;
			string.erase(0,1);
			return true;
		}
		else
		{
			// no clue what it was!  Just treat it as a string, breaking at the next whitespace

			m_string = ExtractWhitespaceStringToken(string);
			m_type = Type_String;
			return true;
		}
	}

	return false;
}