rename buffers (make read-buffer explicit)

This commit is contained in:
Glenn Maynard
2006-02-15 00:52:31 +00:00
parent 7272580311
commit e1d55d1361
2 changed files with 49 additions and 49 deletions
+43 -43
View File
@@ -4,11 +4,11 @@
RageFileObj::RageFileObj() RageFileObj::RageFileObj()
{ {
m_pBuffer = NULL; m_pReadBuffer = NULL;
ResetBuf(); ResetBuf();
m_iBufAvail = 0; m_iReadBufAvail = 0;
m_bEOF = false; m_bEOF = false;
m_iFilePos = 0; m_iFilePos = 0;
m_bCRC32Enabled = false; m_bCRC32Enabled = false;
@@ -19,20 +19,20 @@ RageFileObj::RageFileObj( const RageFileObj &cpy ):
RageFileBasic(cpy) RageFileBasic(cpy)
{ {
/* If the original file has a buffer, copy it. */ /* If the original file has a buffer, copy it. */
if( cpy.m_pBuffer != NULL ) if( cpy.m_pReadBuffer != NULL )
{ {
m_pBuffer = new char[BSIZE]; m_pReadBuffer = new char[BSIZE];
memcpy( m_pBuffer, cpy.m_pBuffer, BSIZE ); memcpy( m_pReadBuffer, cpy.m_pReadBuffer, BSIZE );
int iOffsetIntoBuffer = cpy.m_pBuf - cpy.m_pBuffer; int iOffsetIntoBuffer = cpy.m_pReadBuf - cpy.m_pReadBuffer;
m_pBuf = m_pBuffer + iOffsetIntoBuffer; m_pReadBuf = m_pReadBuffer + iOffsetIntoBuffer;
} }
else else
{ {
m_pBuffer = NULL; m_pReadBuffer = NULL;
} }
m_iBufAvail = cpy.m_iBufAvail; m_iReadBufAvail = cpy.m_iReadBufAvail;
m_bEOF = cpy.m_bEOF; m_bEOF = cpy.m_bEOF;
m_iFilePos = cpy.m_iFilePos; m_iFilePos = cpy.m_iFilePos;
m_bCRC32Enabled = cpy.m_bCRC32Enabled; m_bCRC32Enabled = cpy.m_bCRC32Enabled;
@@ -41,7 +41,7 @@ RageFileObj::RageFileObj( const RageFileObj &cpy ):
RageFileObj::~RageFileObj() RageFileObj::~RageFileObj()
{ {
delete [] m_pBuffer; delete [] m_pReadBuffer;
} }
int RageFileObj::Seek( int iOffset ) int RageFileObj::Seek( int iOffset )
@@ -82,19 +82,19 @@ int RageFileObj::Read( void *pBuffer, size_t iBytes )
while( !m_bEOF && iBytes > 0 ) while( !m_bEOF && iBytes > 0 )
{ {
if( m_pBuffer != NULL && m_iBufAvail ) if( m_pReadBuffer != NULL && m_iReadBufAvail )
{ {
/* Copy data out of the buffer first. */ /* Copy data out of the buffer first. */
int iFromBuffer = min( (int) iBytes, m_iBufAvail ); int iFromBuffer = min( (int) iBytes, m_iReadBufAvail );
memcpy( pBuffer, m_pBuf, iFromBuffer ); memcpy( pBuffer, m_pReadBuf, iFromBuffer );
if( m_bCRC32Enabled ) if( m_bCRC32Enabled )
CRC32( m_iCRC32, pBuffer, iFromBuffer ); CRC32( m_iCRC32, pBuffer, iFromBuffer );
iRet += iFromBuffer; iRet += iFromBuffer;
m_iFilePos += iFromBuffer; m_iFilePos += iFromBuffer;
iBytes -= iFromBuffer; iBytes -= iFromBuffer;
m_iBufAvail -= iFromBuffer; m_iReadBufAvail -= iFromBuffer;
m_pBuf += iFromBuffer; m_pReadBuf += iFromBuffer;
pBuffer = (char *) pBuffer + iFromBuffer; pBuffer = (char *) pBuffer + iFromBuffer;
} }
@@ -102,11 +102,11 @@ int RageFileObj::Read( void *pBuffer, size_t iBytes )
if( !iBytes ) if( !iBytes )
break; break;
ASSERT( m_iBufAvail == 0 ); ASSERT( m_iReadBufAvail == 0 );
/* If buffering is disabled, or the block is bigger than the buffer, /* If buffering is disabled, or the block is bigger than the buffer,
* read the remainder of the data directly into the desteination buffer. */ * read the remainder of the data directly into the desteination buffer. */
if( m_pBuffer == NULL || iBytes >= BSIZE ) if( m_pReadBuffer == NULL || iBytes >= BSIZE )
{ {
/* We have a lot more to read, so don't waste time copying it into the /* We have a lot more to read, so don't waste time copying it into the
* buffer. */ * buffer. */
@@ -125,7 +125,7 @@ int RageFileObj::Read( void *pBuffer, size_t iBytes )
} }
/* If buffering is enabled, and we need more data, fill the buffer. */ /* If buffering is enabled, and we need more data, fill the buffer. */
m_pBuf = m_pBuffer; m_pReadBuf = m_pReadBuffer;
int iGot = FillBuf(); int iGot = FillBuf();
if( iGot == -1 ) if( iGot == -1 )
return iGot; return iGot;
@@ -207,8 +207,8 @@ int RageFileObj::Flush()
void RageFileObj::EnableReadBuffering() void RageFileObj::EnableReadBuffering()
{ {
if( m_pBuffer == NULL ) if( m_pReadBuffer == NULL )
m_pBuffer = new char[BSIZE]; m_pReadBuffer = new char[BSIZE];
} }
void RageFileObj::EnableCRC32( bool bOn ) void RageFileObj::EnableCRC32( bool bOn )
@@ -249,59 +249,59 @@ int RageFileObj::GetLine( RString &sOut )
bool bDone = false; bool bDone = false;
/* Find the end of the block we'll move to out. */ /* Find the end of the block we'll move to out. */
char *p = (char *) memchr( m_pBuf, '\n', m_iBufAvail ); char *p = (char *) memchr( m_pReadBuf, '\n', m_iReadBufAvail );
bool bReAddCR = false; bool bReAddCR = false;
if( p == NULL ) if( p == NULL )
{ {
/* Hack: If the last character of the buffer is \r, then it's likely that an /* Hack: If the last character of the buffer is \r, then it's likely that an
* \r\n has been split across buffers. Move everything else, then move the * \r\n has been split across buffers. Move everything else, then move the
* \r to the beginning of the buffer and handle it the next time around the loop. */ * \r to the beginning of the buffer and handle it the next time around the loop. */
if( m_iBufAvail && m_pBuf[m_iBufAvail-1] == '\r' ) if( m_iReadBufAvail && m_pReadBuf[m_iReadBufAvail-1] == '\r' )
{ {
bReAddCR = true; bReAddCR = true;
--m_iBufAvail; --m_iReadBufAvail;
} }
p = m_pBuf+m_iBufAvail; /* everything */ p = m_pReadBuf+m_iReadBufAvail; /* everything */
} }
else else
{ {
bDone = true; bDone = true;
} }
if( p >= m_pBuf ) if( p >= m_pReadBuf )
{ {
char *RealEnd = p; char *RealEnd = p;
if( bDone && p > m_pBuf && p[-1] == '\r' ) if( bDone && p > m_pReadBuf && p[-1] == '\r' )
--RealEnd; /* not including \r */ --RealEnd; /* not including \r */
sOut.append( m_pBuf, RealEnd ); sOut.append( m_pReadBuf, RealEnd );
if( bDone ) if( bDone )
++p; /* skip \n */ ++p; /* skip \n */
const int iUsed = p-m_pBuf; const int iUsed = p-m_pReadBuf;
if( iUsed ) if( iUsed )
{ {
m_iBufAvail -= iUsed; m_iReadBufAvail -= iUsed;
m_iFilePos += iUsed; m_iFilePos += iUsed;
bGotData = true; bGotData = true;
m_pBuf = p; m_pReadBuf = p;
} }
} }
if( bReAddCR ) if( bReAddCR )
{ {
ASSERT( m_iBufAvail == 0 ); ASSERT( m_iReadBufAvail == 0 );
m_pBuf = m_pBuffer; m_pReadBuf = m_pReadBuffer;
m_pBuffer[m_iBufAvail] = '\r'; m_pReadBuffer[m_iReadBufAvail] = '\r';
++m_iBufAvail; ++m_iReadBufAvail;
} }
if( bDone ) if( bDone )
break; break;
/* We need more data. */ /* We need more data. */
m_pBuf = m_pBuffer; m_pReadBuf = m_pReadBuffer;
const int iSize = FillBuf(); const int iSize = FillBuf();
@@ -341,25 +341,25 @@ int RageFileObj::PutLine( const RString &sStr )
int RageFileObj::FillBuf() int RageFileObj::FillBuf()
{ {
/* Don't call this unless buffering is enabled. */ /* Don't call this unless buffering is enabled. */
ASSERT( m_pBuffer != NULL ); ASSERT( m_pReadBuffer != NULL );
/* The buffer starts at m_Buffer; any data in it starts at m_pBuf; space between /* The buffer starts at m_Buffer; any data in it starts at m_pReadBuf; space between
* the two is old data that we've read. (Don't mangle that data; we can use it * the two is old data that we've read. (Don't mangle that data; we can use it
* for seeking backwards.) */ * for seeking backwards.) */
const int iBufAvail = BSIZE - (m_pBuf-m_pBuffer) - m_iBufAvail; const int iBufAvail = BSIZE - (m_pReadBuf-m_pReadBuffer) - m_iReadBufAvail;
ASSERT_M( iBufAvail >= 0, ssprintf("%p, %p, %i", m_pBuf, m_pBuffer, (int) BSIZE ) ); ASSERT_M( iBufAvail >= 0, ssprintf("%p, %p, %i", m_pReadBuf, m_pReadBuffer, (int) BSIZE ) );
const int iSize = this->ReadInternal( m_pBuf+m_iBufAvail, iBufAvail ); const int iSize = this->ReadInternal( m_pReadBuf+m_iReadBufAvail, iBufAvail );
if( iSize > 0 ) if( iSize > 0 )
m_iBufAvail += iSize; m_iReadBufAvail += iSize;
return iSize; return iSize;
} }
void RageFileObj::ResetBuf() void RageFileObj::ResetBuf()
{ {
m_iBufAvail = 0; m_iReadBufAvail = 0;
m_pBuf = m_pBuffer; m_pReadBuf = m_pReadBuffer;
} }
/* /*
+6 -6
View File
@@ -110,9 +110,9 @@ private:
int m_iFilePos; int m_iFilePos;
/* /*
* If buffering is enabled, m_pBuffer is the buffer, m_pBuf is the current read * If read buffering is enabled, m_pReadBuffer is the buffer, m_pReadBuf is the
* position in the buffer and m_iBufAvail is the number of bytes at m_pBuf. Note * current read position in the buffer, and m_iReadBufAvail is the number of
* that buffering is only enabled if: * bytes at m_pReadBuf. Note that read buffering is only enabled if:
* *
* - GetLine() is called (which requires buffering to efficiently search for newlines); * - GetLine() is called (which requires buffering to efficiently search for newlines);
* - or EnableReadBuffering() is called * - or EnableReadBuffering() is called
@@ -126,9 +126,9 @@ private:
* memory. * memory.
*/ */
enum { BSIZE = 1024 }; enum { BSIZE = 1024 };
char *m_pBuffer; char *m_pReadBuffer;
char *m_pBuf; char *m_pReadBuf;
int m_iBufAvail; int m_iReadBufAvail;
/* If EnableCRC32() is called, a CRC32 will be calculated as the file is read. /* If EnableCRC32() is called, a CRC32 will be calculated as the file is read.
* This is only meaningful if EnableCRC32() is called at the very start of the * This is only meaningful if EnableCRC32() is called at the very start of the