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; }
static void RemoveLeadingWhitespace( vsString &string ) { bool done = false; while(!done) { done = true; if ( !string.empty() && IsWhitespace(string[0]) ) { string.erase(0,1); done = false; } } }
static vsString ExtractStringToken( vsString &string ) { vsString token; size_t index = string.find(' '); if ( index == vsString::npos ) { token = string; string = ""; } else { token = string.substr(0, index); string.erase(0,index+1); // string = string.substr(index, vsString::npos); } return token; }
static vsString ExtractWhitespaceStringToken( vsString &string ) // pull out a string defined by whitespace { vsString label; size_t len = string.length(); size_t index = 0; for ( index = 0; index < len; index++ ) { if ( IsWhitespace(string[index]) ) // this character isn't alphabetic, so isn't part of the label { index--; // back up one character break; // exit the loop } } label = string.substr(0, index+1); string.erase(0,index+1); return label; }
static vsString ExtractNumberToken( vsString &string ) { vsAssert(IsNumeric(string[0]), "Tried to extract a number from something that isn't a number!"); // okay. We need to find vsString numberString; size_t len = string.length(); size_t index = 0; for ( index = 0; index < len; index++ ) { if ( !IsNumeric(string[index]) ) // this character isn't alphabetic, so isn't part of the label { index--; // back up one character break; // exit the loop } } numberString = string.substr(0, index+1); string.erase(0,index+1); return numberString; }
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; }