diff --git a/stepmania/src/RageFile.cpp b/stepmania/src/RageFile.cpp index 409896c6af..ae2b8faa75 100644 --- a/stepmania/src/RageFile.cpp +++ b/stepmania/src/RageFile.cpp @@ -343,13 +343,29 @@ void RageFile::Rewind() m_File->Rewind(); } -int RageFile::Read( CString &buffer, size_t bytes ) +int RageFile::Read( CString &buffer, int bytes ) { - char *buf = new char[bytes]; - int ret = Read( buf, bytes ); - if( ret != -1 ) - buffer.assign( buf, buf+ret ); - delete [] buf; + buffer.clear(); + buffer.reserve( bytes != -1? bytes: this->GetFileSize() ); + + int ret = 0; + char buf[4096]; + while( bytes == -1 || ret < bytes ) + { + int ToRead = sizeof(buf); + if( bytes != -1 ) + ToRead = min( ToRead, bytes-ret ); + + const int got = Read( buf, ToRead ); + if( got == 0 ) + break; + if( got == -1 ) + return -1; + + buffer.append( buf, got ); + ret += got; + } + return ret; } diff --git a/stepmania/src/RageFile.h b/stepmania/src/RageFile.h index f9d82ce8d4..04ac76e3e7 100644 --- a/stepmania/src/RageFile.h +++ b/stepmania/src/RageFile.h @@ -61,7 +61,7 @@ public: /* Raw I/O: */ int Read( void *buffer, size_t bytes ); - int Read( CString &buffer, size_t bytes ); + int Read( CString &buffer, int bytes = -1 ); int Write( const void *buffer, size_t bytes ); int Write( const CString& string ) { return Write( string.data(), string.size() ); } int Flush();