From b125e3d98096080bec169a43026459484e14c75b Mon Sep 17 00:00:00 2001 From: Steve Checkoway Date: Sun, 30 Nov 2003 06:20:25 +0000 Subject: [PATCH] Fix RageFile, use RageFile. Const fix in LyricsLoader. --- stepmania/src/Bookkeeper.cpp | 37 +++--- stepmania/src/GameState.cpp | 25 ++-- stepmania/src/LyricsLoader.cpp | 43 ++++--- stepmania/src/LyricsLoader.h | 2 +- stepmania/src/NotesLoaderBMS.cpp | 67 ++++------ stepmania/src/RageFile.cpp | 6 + stepmania/src/RageFile.h | 3 +- stepmania/src/RageUtil.cpp | 10 +- stepmania/src/SongManager.cpp | 119 ++++++++++++------ stepmania/src/TitleSubstitution.cpp | 22 ++-- .../src/arch/MovieTexture/MovieTexture.cpp | 55 ++++---- 11 files changed, 216 insertions(+), 173 deletions(-) diff --git a/stepmania/src/Bookkeeper.cpp b/stepmania/src/Bookkeeper.cpp index 76e62d8e20..ca81423cd4 100644 --- a/stepmania/src/Bookkeeper.cpp +++ b/stepmania/src/Bookkeeper.cpp @@ -18,8 +18,8 @@ #include "IniFile.h" #include "GameConstantsAndTypes.h" #include "SongManager.h" +#include "RageFile.h" #include -#include Bookkeeper* BOOKKEEPER = NULL; // global and accessable from anywhere in our program @@ -92,14 +92,15 @@ void Bookkeeper::ReadFromDisk() ini.GetValue( "MachineStatistics", "TotalPlays", m_iTotalPlays ); // read dat - FILE* fp = fopen( COINS_DAT, "r" ); - if( fp ) - { - for( int i=0; iGetMetric( "Common","DefaultModifiers" ) @@ -1007,18 +1008,20 @@ void GameState::StoreRankingName( PlayerNumber pn, CString name ) // Filter swear words from name // name.MakeUpper(); - ifstream f; - f.open( NAMES_BLACKLIST_FILE ); - if( f.good() ) - { - CString sLine; - while( getline(f,sLine) ) + RageFile file(NAMES_BLACKLIST_FILE); + + if (file.IsOpen()) { - sLine.MakeUpper(); - if( name.Find(sLine) != -1 ) - name = ""; + CString line; + + while (!file.AtEOF()) + { + line = file.GetLine(); + line.MakeUpper(); + if (name.Find(line) != -1) + name = ""; + } } - } vector aFeats; GetRankingFeats( pn, aFeats ); diff --git a/stepmania/src/LyricsLoader.cpp b/stepmania/src/LyricsLoader.cpp index 211ef9d579..14fddc71bd 100644 --- a/stepmania/src/LyricsLoader.cpp +++ b/stepmania/src/LyricsLoader.cpp @@ -8,7 +8,6 @@ #include "RageUtil.h" #include "LyricsLoader.h" #include "ThemeManager.h" - #include "RageFile.h" #include using namespace std; @@ -20,40 +19,40 @@ static int CompareLyricSegments(const LyricSegment &seg1, const LyricSegment &se return seg1.m_fStartTime < seg2.m_fStartTime; } -bool LyricsLoader::LoadFromLRCFile( CString sPath, Song &out ) +bool LyricsLoader::LoadFromLRCFile(const CString& sPath, Song& out) { LOG->Trace( "LyricsLoader::LoadFromLRCFile(%s)", sPath.c_str() ); - ifstream input(sPath); - if(input.bad()) + RageFile input(sPath, "r"); + + if (!input.IsOpen()) { - LOG->Warn( "Error opening file '%s' for reading.", sPath.c_str() ); + LOG->Warn("Error opening file '%s' for reading.", sPath.c_str()); return false; } - - string line; - + RageColor CurrentColor = LYRICS_DEFAULT_COLOR; - + out.m_LyricSegments.clear(); - - while(input.good() && getline(input, line)) + + while(input.GetError() == 0 && !input.AtEOF()) { + CString line = input.GetLine(); if(!line.compare(0, 2, "//")) continue; - + /* "[data1] data2". Ignore whitespace at the beginning of the line. */ static Regex x("^ *\\[([^]]+)\\] *(.*)$"); - + vector matches; if(!x.Compare(line, matches)) continue; ASSERT( matches.size() == 2 ); - + CString &sValueName = matches[0]; CString &sValueData = matches[1]; StripCrnl(sValueData); - + // handle the data if( 0==stricmp(sValueName,"COLOUR") || 0==stricmp(sValueName,"COLOR") ) { @@ -63,17 +62,17 @@ bool LyricsLoader::LoadFromLRCFile( CString sPath, Song &out ) if(result != 3) { LOG->Trace( "The color value '%s' in '%s' is invalid.", - sValueData.c_str(), sPath.c_str() ); + sValueData.c_str(), sPath.c_str() ); continue; } - + CurrentColor = RageColor(r / 256.0f, g / 256.0f, b / 256.0f, 1); continue; } - + { /* If we've gotten this far, and no other statement caught - * this value before this does, assume it's a time value. */ + * this value before this does, assume it's a time value. */ LyricSegment seg; seg.m_Color = CurrentColor; @@ -84,13 +83,13 @@ bool LyricsLoader::LoadFromLRCFile( CString sPath, Song &out ) out.AddLyricSegment( seg ); } } - + sort( out.m_LyricSegments.begin(), out.m_LyricSegments.end(), CompareLyricSegments ); LOG->Trace( "LyricsLoader::LoadFromLRCFile done" ); - + return true; } - + /* ----------------------------------------------------------------------------- Copyright (c) 2003 by the person(s) listed below. All rights reserved. diff --git a/stepmania/src/LyricsLoader.h b/stepmania/src/LyricsLoader.h index 53b8a559a8..27ede5ed96 100644 --- a/stepmania/src/LyricsLoader.h +++ b/stepmania/src/LyricsLoader.h @@ -8,7 +8,7 @@ class LyricsLoader { public: - bool LoadFromLRCFile( CString sPath, Song &out ); + bool LoadFromLRCFile( const CString& sPath, Song &out ); }; #endif diff --git a/stepmania/src/NotesLoaderBMS.cpp b/stepmania/src/NotesLoaderBMS.cpp index 2ef28e7d15..ca38c346e0 100644 --- a/stepmania/src/NotesLoaderBMS.cpp +++ b/stepmania/src/NotesLoaderBMS.cpp @@ -155,13 +155,13 @@ bool BMSLoader::LoadFromBMSFile( const CString &sPath, Steps &out ) pNoteData->SetNumTracks( MAX_NOTE_TRACKS ); ResetTracksMagic(); - ifstream file(sPath); - if( file.bad() ) - RageException::Throw( "Failed to open %s for reading.", sPath.c_str() ); - - CString line; - while( getline(file, line) ) // foreach line + RageFile file(sPath); + + if (!file.IsOpen()) + RageException::Throw("Failed to open %s for reading.", sPath.c_str()); + while (!file.AtEOF()) { + CString line = file.GetLine(); StripCrnl(line); CString value_name; // fill these in CString value_data; @@ -454,15 +454,14 @@ bool BMSLoader::LoadFromDir( CString sDir, Song &out ) CString sPath = out.GetSongDir() + arrayBMSFileNames[0]; - ifstream file(sPath); - if( file.bad() ) + RageFile file(sPath); + + if (!file.IsOpen()) RageException::Throw( "Failed to open %s for reading.", sPath.c_str() ); - - CString line; - while( getline(file, line) ) // foreach line + while (!file.AtEOF()) { + CString line = file.GetLine(); StripCrnl(line); - CString value_name; // fill these in CString value_data; @@ -577,20 +576,6 @@ bool BMSLoader::LoadFromDir( CString sDir, Song &out ) break; } - // Let me just take a moment to express how frustrated I am with the new, - // poorly-designed changes to the BMS format. - // - // - // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaahhhhhhhhhhhhhhhhhhhhhhhh!!!!!!!!!!!!!!! - // - // Thank you. - - // MD 10/26/03 - And allow me to add to that my opinion of the BMS format in general. - // - // ()*&@^*&^@$(*&^!@(*&^($*&^!#(*&@!&*#*#*@#*@#(*@(%@(*! - // - // end MD 10/26/03 - case 8: { // indirect bpm // This is a very inefficient way to parse, but it doesn't matter much // because this is only parsed on the first run after the song is installed. @@ -599,13 +584,17 @@ bool BMSLoader::LoadFromDir( CString sDir, Song &out ) // open the song file again and and look for this tag's value - ifstream file(sPath); - if( file.bad() ) + /* I don't like this. I think we should just seek back to the beginning + * rather than open the file again. However, I'm not changing the logic, + * only the implementation. -- Steve + */ + RageFile file(sPath);//Why doesn't VC6 bitch here but it does with int?? + + if (!file.IsOpen()) RageException::Throw( "Failed to open %s for reading.", sPath.c_str() ); - - CString line; - while( getline(file, line) ) // foreach line + while (!file.AtEOF()) { + CString line = file.GetLine(); StripCrnl(line); CString value_name; // fill these in CString value_data; @@ -628,9 +617,7 @@ bool BMSLoader::LoadFromDir( CString sDir, Song &out ) value_data.erase(0,iIndexOfSeparator+1); } else // no separator - { value_name = line; - } if( 0==stricmp(value_name, sTagToLookFor) ) { @@ -640,9 +627,7 @@ bool BMSLoader::LoadFromDir( CString sDir, Song &out ) } if( fBPM == -1 ) // we didn't find the line we were looking for - { LOG->Trace( "WARNING: Couldn't find tag '%s' in '%s'.", sTagToLookFor.c_str(), sPath.c_str() ); - } else { BPMSegment newSeg( NoteRowToBeat(iStepIndex), fBPM ); @@ -661,13 +646,13 @@ bool BMSLoader::LoadFromDir( CString sDir, Song &out ) // open the song file again and and look for this tag's value - ifstream file(sPath); - if( file.bad() ) - RageException::Throw( "Failed to open %s for reading.", sPath.c_str() ); - - CString line; - while( getline(file, line) ) // foreach line + RageFile file(sPath);//Why doesn't VC6 bitch here but it does with int?? + + if (!file.IsOpen()) + RageException::Throw( "Failed to open %s for reading.", sPath.c_str() ); + while (!file.AtEOF()) { + CString line = file.GetLine(); StripCrnl(line); CString value_name; // fill these in CString value_data; diff --git a/stepmania/src/RageFile.cpp b/stepmania/src/RageFile.cpp index 3d2f03985f..b8f47bce00 100644 --- a/stepmania/src/RageFile.cpp +++ b/stepmania/src/RageFile.cpp @@ -46,6 +46,12 @@ void CollapsePath( CString &sPath ) sPath = join( SLASH, as ); } +RageFile::RageFile(const CString& path, const char *mode) +{ + mFP = NULL; + Open(path, mode); +} + bool RageFile::Open(const CString& path, const char *mode) { Close(); diff --git a/stepmania/src/RageFile.h b/stepmania/src/RageFile.h index bea8f4fc05..8d27a723df 100644 --- a/stepmania/src/RageFile.h +++ b/stepmania/src/RageFile.h @@ -14,7 +14,6 @@ */ #include -#include // call FixSlashes on any path that came from the user void FixSlashesInPlace( CString &sPath ); @@ -31,7 +30,7 @@ private: public: RageFile() : mPath("") { mFP = NULL; } - RageFile(const CString& path, const char *mode = "r") { Open(path, mode); } + RageFile(const CString& path, const char *mode = "r"); ~RageFile() { Close(); } bool Open(const CString& path, const char *mode = "r"); diff --git a/stepmania/src/RageUtil.cpp b/stepmania/src/RageUtil.cpp index 6089169da2..db6b1c790c 100644 --- a/stepmania/src/RageUtil.cpp +++ b/stepmania/src/RageUtil.cpp @@ -508,14 +508,10 @@ CString DerefRedir(const CString &path) CString GetRedirContents(const CString &path) { - CString sNewFileName; - { - ifstream file(path); - getline(file, sNewFileName); - } - + RageFile file(path); + CString sNewFileName = file.GetLine(); + StripCrnl(sNewFileName); - return sNewFileName; } diff --git a/stepmania/src/SongManager.cpp b/stepmania/src/SongManager.cpp index 7d805243f8..681e4a56a1 100644 --- a/stepmania/src/SongManager.cpp +++ b/stepmania/src/SongManager.cpp @@ -38,6 +38,7 @@ SongManager* SONGMAN = NULL; // global and accessable from anywhere in our progr #define SONGS_DIR BASE_PATH "Songs" SLASH #define COURSES_DIR BASE_PATH "Courses" SLASH #define STATS_PATH BASE_PATH "stats.html" +#define GROUP_SORT_COLOR_FILE BASE_PATH "Data" SLASH "GroupSortColor.ini" const CString CATEGORY_RANKING_FILE = BASE_PATH "Data" SLASH "CategoryRanking.dat"; const CString MACHINE_STEPS_MEM_CARD_DATA = BASE_PATH "Data" SLASH "MachineStepsMemCardData.dat"; const CString MACHINE_COURSE_MEM_CARD_DATA = BASE_PATH "Data" SLASH "MachineCourseMemCardData.dat"; @@ -85,9 +86,39 @@ SongManager::SongManager( LoadingWindow *ld ) SONGMAN = NULL; throw; } - + g_LastMetricUpdate.SetZero(); UpdateMetrics(); + + IniFile gsc(GROUP_SORT_COLOR_FILE); + const CString sortKey("Sort"); + const CString colorsKey("Colors"); + const IniFile::key *key; + int i = 0; + CString value; + + if (!gsc.ReadFile()) + return; + + gsc.GetValue(sortKey, "UnsortedAtEnd", m_bUnsortedAtEnd); + while (gsc.GetValue(sortKey, ssprintf("Group%d", ++i), value)) + m_sSortedGroupNames.push_back(value); + + key = gsc.GetKey(colorsKey); + + if (!key) + return; + + IniFile::key::const_iterator iter = key->begin(); + + while (iter != key->end()) + { + RageColor c(1,1,1,1); + sscanf((*iter).second, "%f,%f,%f,%f", &c.r, &c.g, &c.b, &c.a); + m_mMappedGroupColors[(*iter).first] = c; + LOG->Trace("Found color: %f.%f.%f.%f", c.r, c.g, c.b, c.a); + ++iter; + } } SongManager::~SongManager() @@ -346,60 +377,67 @@ void SongManager::FreeSongs() // // Helper function for reading/writing scores // -bool FileRead( ifstream& f, CString& sOut ) +bool FileRead(RageFile& f, CString& sOut) { - if( f.eof() ) + if (f.AtEOF()) return false; - getline(f, sOut); + sOut = f.GetLine(); return true; } -bool FileRead( ifstream& f, int& iOut ) + +bool FileRead(RageFile& f, int& iOut) { CString s; - if( !FileRead( f, s ) ) + if (!FileRead(f, s)) return false; - iOut = atoi( s ); + iOut = atoi(s); return true; } -bool FileRead( ifstream& f, unsigned& uOut ) + +bool FileRead(RageFile& f, unsigned& uOut) { CString s; - if( !FileRead( f, s ) ) + if (!FileRead(f, s)) return false; - uOut = atoi( s ); + uOut = atoi(s); return true; } -bool FileRead( ifstream& f, float& fOut ) + +bool FileRead(RageFile& f, float& fOut) { CString s; - if( !FileRead( f, s ) ) + if (!FileRead(f, s)) return false; - fOut = (float) atof( s ); + fOut = (float)atof(s); return true; } -void FileWrite( ofstream& f, const CString& sWrite ) + +void FileWrite(RageFile& f, const CString& sWrite) { - f << sWrite << "\n"; + f.PutString(sWrite + "\n"); } -void FileWrite( ofstream& f, int iWrite ) + +void FileWrite(RageFile& f, int iWrite) { - f << iWrite << "\n"; + f.PutString(ssprintf("%d\n", iWrite)); } -void FileWrite( ofstream& f, size_t uWrite ) + +void FileWrite(RageFile& f, size_t uWrite) { - f << uWrite << "\n"; + f.PutString(ssprintf("%lu\n", uWrite)); } -void FileWrite( ofstream& f, float fWrite ) + +void FileWrite(RageFile& f, float fWrite) { - f << fWrite << "\n"; + f.PutString(ssprintf("%f\n", fWrite)); } #define WARN_AND_RETURN { LOG->Warn("Error parsing file '%s' at %s:%d",fn.c_str(),__FILE__,__LINE__); return; } void SongManager::ReadStepsMemCardDataFromFile( CString fn, int mc ) { - ifstream f(fn); - if( !f.good() ) + RageFile f(fn); + if (!f.IsOpen() || f.GetError() != 0) WARN_AND_RETURN; int version; @@ -493,10 +531,10 @@ void SongManager::ReadStepsMemCardDataFromFile( CString fn, int mc ) void SongManager::ReadCategoryRankingsFromFile( CString fn ) { - ifstream f(fn); - if( !f.good() ) + RageFile f(fn); + if (!f.IsOpen() || f.GetError() != 0) WARN_AND_RETURN; - + int version; if( !FileRead(f, version) ) WARN_AND_RETURN; @@ -525,10 +563,10 @@ void SongManager::ReadCategoryRankingsFromFile( CString fn ) void SongManager::ReadCourseMemCardDataFromFile( CString fn, int mc ) { - ifstream f(fn); - if( !f.good() ) + RageFile f(fn); + if (!f.IsOpen() || f.GetError() != 0) WARN_AND_RETURN; - + int version; if( !FileRead(f, version) ) WARN_AND_RETURN; @@ -680,8 +718,8 @@ void SongManager::SaveCategoryRankingsToFile( CString fn ) { LOG->Trace("SongManager::SaveCategoryRankingsToFile"); - ofstream f(fn); - if( !f.good() ) + RageFile f(fn); + if (!f.IsOpen() || f.GetError() != 0) return; FileWrite( f, CATEGORY_RANKING_VERSION ); @@ -708,10 +746,10 @@ void SongManager::SaveCourseMemCardDataToFile( CString fn, int mc ) { LOG->Trace("SongManager::SaveCourseMemCardDataToFile"); - ofstream f(fn); - if( !f.good() ) + RageFile f(fn); + if (!f.IsOpen() || f.GetError() != 0) return; - + FileWrite( f, COURSE_MEM_CARD_DATA_VERSION ); FileWrite( f, m_pCourses.size() ); @@ -761,12 +799,12 @@ void SongManager::SaveStepsMemCardDataToFile( CString fn, int mc ) { LOG->Trace("SongManager::SaveStepsMemCardDataToFile %s", fn.c_str()); - ofstream f(fn); - if( !f.good() ) + RageFile f(fn); + if (!f.IsOpen() || f.GetError() != 0) return; - + FileWrite( f, STEPS_MEM_CARD_DATA_VERSION ); - + FileWrite( f, m_pSongs.size() ); for( unsigned s=0; s::const_iterator iter = m_mMappedGroupColors.find(sGroupName); + + if (iter != m_mMappedGroupColors.end()) + return (*iter).second; + UpdateMetrics(); // search for the group index diff --git a/stepmania/src/TitleSubstitution.cpp b/stepmania/src/TitleSubstitution.cpp index 0400890e5b..8bac49f15a 100644 --- a/stepmania/src/TitleSubstitution.cpp +++ b/stepmania/src/TitleSubstitution.cpp @@ -101,18 +101,22 @@ TitleSubst::TitleSubst(const CString §ion) void TitleSubst::Load(const CString &filename, const CString §ion) { - ifstream f; - f.open(filename); - if(!f.good()) return; - + RageFile f(filename); + + if (!f.IsOpen() || f.GetError() != 0) + return; + CString CurrentSection; TitleTrans tr; - - while(!f.eof()) + + while (!f.AtEOF()) { - CString line; - if(!getline(f, line)) continue; - + CString line = f.GetLine(); + if (f.GetError() != 0) + { + clearerr(f.GetFilePointer()); + continue; + } if(line.size() > 0 && utf8_get_char(line.c_str()) == 0xFEFF) { /* Annoying header that Windows puts on UTF-8 plaintext diff --git a/stepmania/src/arch/MovieTexture/MovieTexture.cpp b/stepmania/src/arch/MovieTexture/MovieTexture.cpp index 4946e93652..b5a352daaf 100644 --- a/stepmania/src/arch/MovieTexture/MovieTexture.cpp +++ b/stepmania/src/arch/MovieTexture/MovieTexture.cpp @@ -4,6 +4,7 @@ #include "RageLog.h" #include "MovieTexture_Null.h" #include "PrefsManager.h" +#include "RageFile.h" /* _WINDOWS is Windows only, where _WIN32 is Windows and Xbox, I think. Does this * work on the Xbox? -glenn */ @@ -21,7 +22,6 @@ #include "MovieTexture_FFMpeg.h" #endif -#include "RageFile.h" bool RageMovieTexture::GetFourCC( CString fn, CString &handler, CString &type ) { CString ignore, ext; @@ -35,32 +35,37 @@ bool RageMovieTexture::GetFourCC( CString fn, CString &handler, CString &type ) return true; } - ifstream f; - f.exceptions( ifstream::eofbit | ifstream::failbit | ifstream::badbit ); - - try { - f.open(fn); - - f.seekg( 0x70, ios_base::beg ); - type = " "; - f.read((char *) type.c_str(), 4); - int i; - for( i = 0; i < 4; ++i) - if(type[i] < 0x20 || type[i] > 0x7E) type[i] = '?'; - - f.seekg( 0xBC, ios_base::beg ); - - handler = " "; - f.read((char *) handler.c_str(), 4); - for(i = 0; i < 4; ++i) - if(handler[i] < 0x20 || handler[i] > 0x7E) handler[i] = '?'; - } catch(ifstream::failure e) { - LOG->Warn("error on %s: %s", fn.c_str(), e.what() ); - handler = type = ""; - return false; - } + //Not very pretty but should do all the same error checking without iostream +#define HANDLE_ERROR(x) {error = x; goto errorLabel;} + RageFile file(fn); + CString error(""); + int i; + + if (!file.IsOpen()) + HANDLE_ERROR("Could not open file."); + if (!file.Seek(0x70, SEEK_SET)) + HANDLE_ERROR("Could not seek."); + type = " "; + if (file.Read((char *)type.c_str(), 4) != 4) + HANDLE_ERROR("Could not read."); + for (i=0; i<4; ++i) + if (type[i] < 0x20 || type[i] > 0x7E) type[i] = '?'; + + if (!file.Seek(0xBC, SEEK_SET)) + HANDLE_ERROR("Could not seek."); + handler = " "; + if (file.Read((char *)handler.c_str(), 4) != 4) + HANDLE_ERROR("Could not read."); + for (i=0; i<4; ++i) + if (handler[i] < 0x20 || handler[i] > 0x7E) handler[i] = '?'; return true; + +errorLabel: + LOG->Warn("Error on %s: %s.", fn.c_str(), error.c_str()); + handler = type = ""; + return false; +#undef HANDLE_ERROR } static void DumpAVIDebugInfo( CString fn )