From: Steve C. <ste...@us...> - 2003-11-27 07:46:40
|
Update of /cvsroot/stepmania/stepmania/src In directory sc8-pr-cvs1:/tmp/cvs-serv7198 Modified Files: RageFile.h RageFile.cpp Log Message: RageFile class Index: RageFile.h =================================================================== RCS file: /cvsroot/stepmania/stepmania/src/RageFile.h,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -p -r1.3 -r1.4 --- RageFile.h 13 Aug 2003 04:40:23 -0000 1.3 +++ RageFile.h 27 Nov 2003 07:46:36 -0000 1.4 @@ -9,10 +9,12 @@ Copyright (c) 2001-2002 by the person(s) listed below. All rights reserved. Chris Danford + Steve Checkoway ----------------------------------------------------------------------------- */ #include <fstream> +#include <cstdio> using namespace std; // using "std::ifstream" causes problems below in VC6. Why?!? // call FixSlashes on any path that came from the user @@ -21,5 +23,35 @@ CString FixSlashes( CString sPath ); void CollapsePath( CString &sPath ); + +class RageFile +{ +private: + FILE *mFP; + CString mPath; + +public: + RageFile() : mPath("") { mFP = NULL; } + RageFile(const CString& path, const char *mode = "r") { Open(path, mode); } + ~RageFile() { Close(); } + + bool Open(const CString& path, const char *mode = "r"); + void Close(); + + bool IsOpen() { return (mFP == NULL); } + bool AtEOF() { return feof(mFP); } + int GetError() { return ferror(mFP); } + FILE *GetFilePointer() { return mFP; } + + long Tell() { return ftell(mFP); } + bool Seek(long offset, int origin = SEEK_CUR) { return !fseek(mFP, offset, origin); } + void Rewind() { rewind(mFP); } + + // GetLine() strips new lines + CString GetLine(); + bool PutString(const CString& string) { return fputs(string, mFP) >= 0; } + size_t Read(void *buffer, size_t bytes); + size_t Write(const void *buffer, size_t bytes); +}; #endif Index: RageFile.cpp =================================================================== RCS file: /cvsroot/stepmania/stepmania/src/RageFile.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -p -r1.2 -r1.3 --- RageFile.cpp 13 Aug 2003 04:40:23 -0000 1.2 +++ RageFile.cpp 27 Nov 2003 07:46:36 -0000 1.3 @@ -1,46 +1,101 @@ #include "global.h" /* ------------------------------------------------------------------------------ + ----------------------------------------------------------------------------- Class: RageFile - + Desc: See header. - + Copyright (c) 2001-2003 by the person(s) listed below. All rights reserved. Chris Danford ------------------------------------------------------------------------------ -*/ + Steve Checkoway + ----------------------------------------------------------------------------- + */ +#include "global.h" #include "RageFile.h" #include "RageUtil.h" void FixSlashesInPlace( CString &sPath ) { - sPath.Replace( "/", SLASH ); - sPath.Replace( "\\", SLASH ); + sPath.Replace( "/", SLASH ); + sPath.Replace( "\\", SLASH ); } CString FixSlashes( CString sPath ) { - sPath.Replace( "/", SLASH ); - sPath.Replace( "\\", SLASH ); - return sPath; + sPath.Replace( "/", SLASH ); + sPath.Replace( "\\", SLASH ); + return sPath; } void CollapsePath( CString &sPath ) { - CStringArray as; - split( sPath, SLASH, as ); - - for( unsigned i=0; i<as.size(); i++ ) + CStringArray as; + split( sPath, SLASH, as ); + + for( unsigned i=0; i<as.size(); i++ ) + { + if( as[i] == ".." ) { - if( as[i] == ".." ) - { - as.erase( as.begin()+i-1 ); - as.erase( as.begin()+i-1 ); - i -= 2; - } + as.erase( as.begin()+i-1 ); + as.erase( as.begin()+i-1 ); + i -= 2; } - sPath = join( SLASH, as ); + } + sPath = join( SLASH, as ); } +bool RageFile::Open(const CString& path, const char *mode) +{ + mPath = path; + FixSlashesInPlace(mPath); + mFP = fopen(mPath, mode); + + return mFP == NULL; +} + +void RageFile::Close() +{ + if (IsOpen()) + fclose(mFP); + + mFP = NULL; +} + +CString RageFile::GetLine() +{ + if (!IsOpen()) + RageException::Throw("\"%s\" is not open.", mPath.c_str()); + + CString buf(""); + char buffer[256]; // long line! + + do + { + char *ret = fgets(buffer, 256, mFP); + + buf += buffer; + + if (ret == NULL) + break; // EOF or error + } while (strlen(buffer) == 255); + + return buf; +} + +size_t RageFile::Read(void *buffer, size_t bytes) +{ + if (!IsOpen()) + RageException::Throw("\"%s\" is not open.", mPath.c_str()); + + return fread(buffer, 1L, bytes, mFP); +} + +size_t RageFile::Write(const void *buffer, size_t bytes) +{ + if (!IsOpen()) + RageException::Throw("\"%s\" is not open.", mPath.c_str()); + + return fwrite(buffer, 1L, bytes, mFP); +} |