RageFile::READ, RageFile::WRITE

This commit is contained in:
Glenn Maynard
2003-12-03 23:50:53 +00:00
parent 35d1eadc3e
commit 51b65b039b
3 changed files with 119 additions and 51 deletions
+100 -43
View File
@@ -46,63 +46,120 @@ void CollapsePath( CString &sPath )
sPath = join( SLASH, as );
}
RageFile::RageFile(const CString& path, const char *mode)
RageFile::RageFile( const CString& path, RageFile::OpenMode mode )
{
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();
mPath = path;
FixSlashesInPlace(mPath);
mFP = fopen(mPath, mode);
Close();
mPath = path;
FixSlashesInPlace(mPath);
mFP = fopen( mPath, mode == READ? "r":"w" );
ResetBuf();
return mFP == NULL;
}
void RageFile::Close()
{
if (IsOpen())
fclose(mFP);
if (IsOpen())
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()
{
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;
CString ret;
GetLine( ret );
return ret;
}
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);
}