RageFile class

This commit is contained in:
Steve Checkoway
2003-11-27 07:46:36 +00:00
parent 0c71022d84
commit b716ee6680
2 changed files with 108 additions and 21 deletions
+76 -21
View File
@@ -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);
}
+32
View File
@@ -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
@@ -22,4 +24,34 @@ 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