RageFile::READ, RageFile::WRITE
This commit is contained in:
@@ -92,7 +92,7 @@ void Bookkeeper::ReadFromDisk()
|
|||||||
ini.GetValue( "MachineStatistics", "TotalPlays", m_iTotalPlays );
|
ini.GetValue( "MachineStatistics", "TotalPlays", m_iTotalPlays );
|
||||||
|
|
||||||
// read dat
|
// read dat
|
||||||
RageFile file(COINS_DAT, "r");
|
RageFile file( COINS_DAT );
|
||||||
if (file.IsOpen())
|
if (file.IsOpen())
|
||||||
{
|
{
|
||||||
const CString line = file.GetLine();
|
const CString line = file.GetLine();
|
||||||
@@ -130,7 +130,7 @@ void Bookkeeper::WriteToDisk()
|
|||||||
ini.WriteFile();
|
ini.WriteFile();
|
||||||
|
|
||||||
// write dat
|
// write dat
|
||||||
RageFile file(COINS_DAT, "w");
|
RageFile file( COINS_DAT, RageFile::WRITE );
|
||||||
|
|
||||||
if (file.IsOpen())
|
if (file.IsOpen())
|
||||||
{
|
{
|
||||||
|
|||||||
+99
-42
@@ -46,63 +46,120 @@ void CollapsePath( CString &sPath )
|
|||||||
sPath = join( SLASH, as );
|
sPath = join( SLASH, as );
|
||||||
}
|
}
|
||||||
|
|
||||||
RageFile::RageFile(const CString& path, const char *mode)
|
RageFile::RageFile( const CString& path, RageFile::OpenMode mode )
|
||||||
{
|
{
|
||||||
mFP = NULL;
|
mFP = NULL;
|
||||||
Open(path, mode);
|
m_BufUsed = 0;
|
||||||
|
Open(path, mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool RageFile::Open(const CString& path, const char *mode)
|
bool RageFile::Open( const CString& path, RageFile::OpenMode mode )
|
||||||
{
|
{
|
||||||
Close();
|
Close();
|
||||||
mPath = path;
|
mPath = path;
|
||||||
FixSlashesInPlace(mPath);
|
FixSlashesInPlace(mPath);
|
||||||
mFP = fopen(mPath, mode);
|
mFP = fopen( mPath, mode == READ? "r":"w" );
|
||||||
|
ResetBuf();
|
||||||
|
|
||||||
return mFP == NULL;
|
return mFP == NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void RageFile::Close()
|
void RageFile::Close()
|
||||||
{
|
{
|
||||||
if (IsOpen())
|
if (IsOpen())
|
||||||
fclose(mFP);
|
fclose(mFP);
|
||||||
|
|
||||||
mFP = NULL;
|
mFP = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RageFile::ResetBuf()
|
||||||
|
{
|
||||||
|
m_BufUsed = 0;
|
||||||
|
m_pBuf = m_Buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Read up to the next \n, and return it in out. Strip the \n. If the \n is
|
||||||
|
* preceded by a \r (DOS newline), strip that, too. */
|
||||||
|
void RageFile::GetLine( CString &out )
|
||||||
|
{
|
||||||
|
out = "";
|
||||||
|
|
||||||
|
if (!IsOpen())
|
||||||
|
RageException::Throw("\"%s\" is not open.", mPath.c_str());
|
||||||
|
|
||||||
|
while( 1 )
|
||||||
|
{
|
||||||
|
bool done = false;
|
||||||
|
|
||||||
|
/* Find the end of the block we'll move to out. */
|
||||||
|
char *p = (char *) memchr( m_pBuf, '\n', m_BufUsed );
|
||||||
|
if( p == NULL )
|
||||||
|
p = m_pBuf+m_BufUsed; /* everything */
|
||||||
|
else
|
||||||
|
done = true;
|
||||||
|
|
||||||
|
if( p >= m_pBuf )
|
||||||
|
{
|
||||||
|
char *RealEnd = p;
|
||||||
|
if( done && p > m_pBuf && p[-1] == '\r' )
|
||||||
|
--RealEnd; /* not including \r */
|
||||||
|
out.append( m_pBuf, RealEnd );
|
||||||
|
|
||||||
|
if( done )
|
||||||
|
++p; /* skip \n */
|
||||||
|
m_BufUsed -= p-m_pBuf;
|
||||||
|
m_pBuf = p;
|
||||||
|
}
|
||||||
|
|
||||||
|
if( done )
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* We need more data. That implies that we moved everything. */
|
||||||
|
ASSERT( !m_BufUsed );
|
||||||
|
m_pBuf = m_Buffer;
|
||||||
|
|
||||||
|
size_t size = Read( m_pBuf, sizeof(m_Buffer) );
|
||||||
|
if( size <= 0 )
|
||||||
|
break; // EOF or error
|
||||||
|
m_BufUsed += size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int RageFile::Read( void *buffer, size_t bytes )
|
||||||
|
{
|
||||||
|
if( !IsOpen() )
|
||||||
|
RageException::Throw("\"%s\" is not open.", mPath.c_str());
|
||||||
|
|
||||||
|
int ret = 0;
|
||||||
|
|
||||||
|
int FromBuffer = min( (int) bytes, m_BufUsed );
|
||||||
|
memcpy( buffer, m_pBuf, FromBuffer );
|
||||||
|
ret += FromBuffer;
|
||||||
|
buffer = (char *) buffer + FromBuffer;
|
||||||
|
bytes -= FromBuffer;
|
||||||
|
|
||||||
|
if( bytes )
|
||||||
|
{
|
||||||
|
int FromFile = fread( buffer, 1L, bytes, mFP );
|
||||||
|
if( FromFile < 0 )
|
||||||
|
return -1;
|
||||||
|
ret += FromFile;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
CString RageFile::GetLine()
|
CString RageFile::GetLine()
|
||||||
{
|
{
|
||||||
if (!IsOpen())
|
CString ret;
|
||||||
RageException::Throw("\"%s\" is not open.", mPath.c_str());
|
GetLine( ret );
|
||||||
|
return ret;
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -28,12 +28,19 @@ private:
|
|||||||
FILE *mFP;
|
FILE *mFP;
|
||||||
CString mPath;
|
CString mPath;
|
||||||
|
|
||||||
|
enum { BSIZE = 256 };
|
||||||
|
char m_Buffer[BSIZE];
|
||||||
|
char *m_pBuf;
|
||||||
|
int m_BufUsed;
|
||||||
|
|
||||||
|
void ResetBuf();
|
||||||
public:
|
public:
|
||||||
|
enum OpenMode { READ, WRITE };
|
||||||
RageFile() : mPath("") { mFP = NULL; }
|
RageFile() : mPath("") { mFP = NULL; }
|
||||||
RageFile(const CString& path, const char *mode = "r");
|
RageFile( const CString& path, OpenMode mode = READ );
|
||||||
~RageFile() { Close(); }
|
~RageFile() { Close(); }
|
||||||
|
|
||||||
bool Open(const CString& path, const char *mode = "r");
|
bool Open( const CString& path, OpenMode mode = READ );
|
||||||
void Close();
|
void Close();
|
||||||
|
|
||||||
bool IsOpen() { return (mFP != NULL); }
|
bool IsOpen() { return (mFP != NULL); }
|
||||||
@@ -45,11 +52,15 @@ public:
|
|||||||
bool Seek(long offset, int origin = SEEK_CUR) { return !fseek(mFP, offset, origin); }
|
bool Seek(long offset, int origin = SEEK_CUR) { return !fseek(mFP, offset, origin); }
|
||||||
void Rewind() { rewind(mFP); }
|
void Rewind() { rewind(mFP); }
|
||||||
|
|
||||||
|
/* Raw I/O: */
|
||||||
// GetLine() strips new lines
|
// GetLine() strips new lines
|
||||||
|
bool PutString(const CString& string) { return fputs(string, mFP) >= 0; }
|
||||||
|
int Read(void *buffer, size_t bytes);
|
||||||
|
int Write(const void *buffer, size_t bytes);
|
||||||
|
|
||||||
|
/* Line-based I/O: */
|
||||||
CString GetLine();
|
CString GetLine();
|
||||||
bool PutString(const CString& string) { return fputs(string, mFP) >= 0; }
|
void GetLine( CString &out );
|
||||||
size_t Read(void *buffer, size_t bytes);
|
|
||||||
size_t Write(const void *buffer, size_t bytes);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user