void vsFailedAssert( const vsString &conditionStr, const vsString &msg, const char *file, int line ) { if ( !bAsserted ) { // failed assertion.. trace out some information and then crash. bAsserted = true; vsString trimmedFile(file); size_t pos = trimmedFile.rfind('/'); if ( pos ) { trimmedFile = trimmedFile.substr(pos+1); } vsLog("Failed assertion: %s", msg.c_str()); vsLog("Failed condition: (%s)", conditionStr.c_str()); vsLog("at %s:%d", trimmedFile.c_str(), line); vsLog_End(); { #if defined(_DEBUG) DEBUG_BREAK; #else vsString mbString = vsFormatString("Failed assertion: %s\nFailed condition: (%s)\nat %s:%d", msg.c_str(), conditionStr.c_str(), trimmedFile.c_str(), line); SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Failed assertion", mbString.c_str(), NULL); #endif CRASH; } } else { vsLog("Error: Asserted while handling assertion!"); } }
void vsFile::EnsureWriteDirectoryExists( const vsString &writeDirectoryName ) // static method { if ( !DirectoryExists(writeDirectoryName) ) { int mkdirResult = PHYSFS_mkdir( writeDirectoryName.c_str() ); vsAssert( mkdirResult != 0, vsFormatString("Failed to create directory '%s%s%s': %s", PHYSFS_getWriteDir(), PHYSFS_getDirSeparator(), writeDirectoryName.c_str(), PHYSFS_getLastError()) ); } }
void vsErrorLog(const vsString &str) { fprintf(stderr, "%s\n", str.c_str() ); if ( s_log ) { PHYSFS_write( s_log, str.c_str(), 1, str.size() ); #ifdef _WIN32 PHYSFS_write( s_log, "\r\n", 1, 2 ); #else PHYSFS_write( s_log, "\n", 1, 1 ); #endif } }
static void RemoveLeadingWhitespace( vsString &string ) { bool done = false; while(!done) { done = true; if ( !string.empty() && IsWhitespace(string[0]) ) { string.erase(0,1); done = false; } } }
void vsLocalisationTable::Init(const vsString &language) { s_localisationTable = new vsHashTable<vsString>( 128 ); vsString filename = vsFormatString("%s.xlat", language.c_str()); if ( vsFile::Exists(filename) ) { vsFile table(filename); vsString str; vsRecord r; while( table.Record(&r) ) { if ( r.GetTokenCount() > 0 ) { vsString label = r.GetLabel().AsString(); vsString string = r.GetToken(0).AsString(); s_localisationTable->AddItemWithKey(string, label); } } } }
float vsBuiltInFont::GetStringWidth( const vsString &string, float size, float capSize ) { if ( capSize == 0.f ) capSize = size; float width = 0.f; size_t len = strlen(string.c_str()); for ( size_t i = 0; i < len; i++ ) { char thisChar = string[i]; char upperChar = toupper(thisChar); float thisSize = size; if ( thisChar == ' ' ) { // half width spaces, because that looks better. thisSize *= c_spaceKerning; } else if ( upperChar >= 'A' && upperChar <= 'Z' && thisChar == upperChar && capSize > 0.f ) { thisSize = capSize; } width += 2.0f * c_kerningFactor * thisSize; } return width; }
bool vsFile::DeleteDirectory( const vsString &filename ) { if ( DirectoryExists(filename) ) { vsArray<vsString> files; DirectoryContents(&files, filename); for ( int i = 0; i < files.ItemCount(); i++ ) { vsString ff = vsFormatString("%s/%s", filename.c_str(), files[i].c_str()); if ( vsFile::DirectoryExists( ff ) ) { // it's a directory; remove it! DeleteDirectory( ff ); } else { // it's a file, delete it. Delete( ff ); } } // I should now be empty, so delete me. return DeleteEmptyDirectory( filename ); } return false; }
bool vsFile::Delete( const vsString &filename ) // static method { if ( DirectoryExists(filename) ) // This file is a directory, don't delete it! return false; return PHYSFS_delete(filename.c_str()) != 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; }
vsFile::vsFile( const vsString &filename, vsFile::Mode mode ): m_mode(mode), m_length(0) { vsAssert( !DirectoryExists(filename), vsFormatString("Attempted to open directory '%s' as a plain file", filename.c_str()) ); if ( mode == MODE_Read ) m_file = PHYSFS_openRead( filename.c_str() ); else m_file = PHYSFS_openWrite( filename.c_str() ); if ( m_file ) { m_length = (size_t)PHYSFS_fileLength(m_file); } vsAssert( m_file != NULL, STR("Error opening file '%s': %s", filename.c_str(), PHYSFS_getLastError()) ); }
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; }
void vsBuiltInFont::CreateStringInDisplayList(vsDisplayList *list, const vsString &string, float size, float capSize, JustificationType j, float maxWidth) { if ( maxWidth > 0.f ) { WrapLine(string, size, capSize, j, maxWidth); float lineHeight = capSize; float lineMargin = capSize * c_lineMarginFactor; // float baseOffsetDown = lineHeight * (m_wrappedLineCount * 0.5f); float totalHeight = (lineHeight * s_wrappedLineCount) + (lineMargin * (s_wrappedLineCount-1)); float baseOffsetDown = totalHeight * 0.5f; float topLinePosition = baseOffsetDown - totalHeight + lineHeight; // float halfTotalHeight = totalHeight * 0.5f; list->Clear(); vsVector2D offset(0.f,topLinePosition); for ( int i = 0; i < s_wrappedLineCount; i++ ) { offset.y = topLinePosition + (i*(lineHeight+lineMargin)); s_tempFontList.Clear(); BuildDisplayListFromString( &s_tempFontList, s_wrappedLine[i].c_str(), size, capSize, j, offset ); list->Append(s_tempFontList); } vsVector2D tl,br; list->GetBoundingBox(tl,br); /* float height = br.y-tl.y; list->Clear(); for ( int i = 0; i < m_wrappedLineCount; i++ ) { offset.Set(0.f,lineHeight*i - 0.5f*height); s_tempFontList.Clear(); BuildDisplayListFromString( &s_tempFontList, m_wrappedLine[i].c_str(), size, capSize, j, offset ); list->Append(s_tempFontList); }*/ } else { float lineHeight = capSize; float totalHeight = lineHeight; float baseOffsetDown = totalHeight * 0.5f; float topLinePosition = baseOffsetDown - totalHeight + lineHeight; list->Clear(); vsVector2D offset(0.f,topLinePosition); list->Clear(); BuildDisplayListFromString( list, string.c_str(), size, capSize, j, offset ); } }
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; }
bool vsFile::DeleteEmptyDirectory( const vsString &filename ) { // If it's not a directory, don't delete it! // // Note that PHYSFS_delete will return an error if we // try to delete a non-empty directory. // if ( DirectoryExists(filename) ) return PHYSFS_delete(filename.c_str()) != 0; return false; }
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; }
vsString vsLocalisationTable::GetTranslation( const vsString &key ) { vsString *str = s_localisationTable->FindItem(key); if ( str ) { return *str; } else { return vsFormatString("<<%s>>", key.c_str()); } }
vsNetClient::vsNetClient(const vsString &address, uint16_t port) { hostent *h = gethostbyname( address.c_str() ); if (h == NULL) { #if !defined(_WIN32) // todo: Network errors under WinSock! herror("gethostbyname"); #endif vsAssert( h != NULL, vsFormatString("Gethostbyname error: See console output for details" ) ); } m_privateIP = ((struct in_addr *)h->h_addr)->s_addr; m_privatePort = port; }
int vsFile::DirectoryContents( vsArray<vsString>* result, const vsString &dirName ) // static method { result->Clear(); char **files = PHYSFS_enumerateFiles(dirName.c_str()); char **i; std::vector<char*> s; for (i = files; *i != NULL; i++) s.push_back(*i); std::sort(s.begin(), s.end(), sortFilesByModificationDate(dirName)); for (size_t i = 0; i < s.size(); i++) result->AddItem( s[i] ); PHYSFS_freeList(files); return result->ItemCount(); }
vsPreferences::vsPreferences(const vsString &filename_in) { vsString filename = vsFormatString("%s.prefs",filename_in.c_str()); m_filename = filename; m_preferenceList = new vsPreferenceObject; if ( fsFile::Exists(filename) ) // if the file exists, then read the current pref values out of it. { fsRecord record; fsFile prefsFile(filename); while( prefsFile.Record(&record) ) { vsAssert( record.GetArgCount() == 3, vsFormatString("%s preference has wrong number of arguments!", record.GetLabel().c_str() )); AddPreference( record.GetLabel(), record.GetArgAsInt(0), record.GetArgAsInt(1), record.GetArgAsInt(2)); } } }
void vsLog(const vsString &str) { printf("%s\n", str.c_str() ); }
bool vsFile::DirectoryExists( const vsString &filename ) // static method { return PHYSFS_exists(filename.c_str()) && PHYSFS_isDirectory(filename.c_str()); }
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; }