This commit is contained in:
Glenn Maynard
2006-02-15 00:47:18 +00:00
parent 6fc982d681
commit 831cc0cd91
+51 -47
View File
@@ -28,7 +28,9 @@ RageFileObj::RageFileObj( const RageFileObj &cpy ):
m_pBuf = m_pBuffer + iOffsetIntoBuffer; m_pBuf = m_pBuffer + iOffsetIntoBuffer;
} }
else else
{
m_pBuffer = NULL; m_pBuffer = NULL;
}
m_iBufAvail = cpy.m_iBufAvail; m_iBufAvail = cpy.m_iBufAvail;
m_bEOF = cpy.m_bEOF; m_bEOF = cpy.m_bEOF;
@@ -76,7 +78,7 @@ int RageFileObj::Seek( int offset, int whence )
int RageFileObj::Read( void *pBuffer, size_t iBytes ) int RageFileObj::Read( void *pBuffer, size_t iBytes )
{ {
int ret = 0; int iRet = 0;
while( !m_bEOF && iBytes > 0 ) while( !m_bEOF && iBytes > 0 )
{ {
@@ -88,7 +90,7 @@ int RageFileObj::Read( void *pBuffer, size_t iBytes )
if( m_bCRC32Enabled ) if( m_bCRC32Enabled )
CRC32( m_iCRC32, pBuffer, iFromBuffer ); CRC32( m_iCRC32, pBuffer, iFromBuffer );
ret += iFromBuffer; iRet += iFromBuffer;
m_iFilePos += iFromBuffer; m_iFilePos += iFromBuffer;
iBytes -= iFromBuffer; iBytes -= iFromBuffer;
m_iBufAvail -= iFromBuffer; m_iBufAvail -= iFromBuffer;
@@ -117,21 +119,21 @@ int RageFileObj::Read( void *pBuffer, size_t iBytes )
if( m_bCRC32Enabled ) if( m_bCRC32Enabled )
CRC32( m_iCRC32, pBuffer, iFromFile ); CRC32( m_iCRC32, pBuffer, iFromFile );
ret += iFromFile; iRet += iFromFile;
m_iFilePos += iFromFile; m_iFilePos += iFromFile;
return ret; return iRet;
} }
/* 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_pBuf = m_pBuffer;
int got = FillBuf(); int iGot = FillBuf();
if( got == -1 ) if( iGot == -1 )
return got; return iGot;
if( got == 0 ) if( iGot == 0 )
m_bEOF = true; m_bEOF = true;
} }
return ret; return iRet;
} }
int RageFileObj::Read( RString &sBuffer, int iBytes ) int RageFileObj::Read( RString &sBuffer, int iBytes )
@@ -161,19 +163,19 @@ int RageFileObj::Read( RString &sBuffer, int iBytes )
return iRet; return iRet;
} }
int RageFileObj::Read( void *buffer, size_t bytes, int nmemb ) int RageFileObj::Read( void *pBuffer, size_t iBytes, int iNmemb )
{ {
const int iRet = Read( buffer, bytes*nmemb ); const int iRet = Read( pBuffer, iBytes*iNmemb );
if( iRet == -1 ) if( iRet == -1 )
return -1; return -1;
/* If we're reading 10-byte blocks, and we got 27 bytes, we have 7 extra bytes. /* If we're reading 10-byte blocks, and we got 27 bytes, we have 7 extra bytes.
* Seek back. XXX: seeking is very slow for eg. deflated ZIPs. If the block is * Seek back. XXX: seeking is very slow for eg. deflated ZIPs. If the block is
* small enough, we may be able to stuff the extra data into the buffer. */ * small enough, we may be able to stuff the extra data into the buffer. */
const int iExtra = iRet % bytes; const int iExtra = iRet % iBytes;
Seek( Tell()-iExtra ); Seek( Tell()-iExtra );
return iRet/bytes; return iRet/iBytes;
} }
int RageFileObj::Write( const void *pBuffer, size_t iBytes ) int RageFileObj::Write( const void *pBuffer, size_t iBytes )
@@ -188,13 +190,13 @@ int RageFileObj::Write( const void *pBuffer, size_t iBytes )
return iRet; return iRet;
} }
int RageFileObj::Write( const void *buffer, size_t bytes, int nmemb ) int RageFileObj::Write( const void *pBuffer, size_t iBytes, int iNmemb )
{ {
/* Simple write. We never return partial writes. */ /* Simple write. We never return partial writes. */
int ret = Write( buffer, bytes*nmemb ) / bytes; int iRet = Write( pBuffer, iBytes*iNmemb ) / iBytes;
if( ret == -1 ) if( iRet == -1 )
return -1; return -1;
return ret / bytes; return iRet / iBytes;
} }
int RageFileObj::Flush() int RageFileObj::Flush()
@@ -209,9 +211,9 @@ void RageFileObj::EnableBuffering()
m_pBuffer = new char[BSIZE]; m_pBuffer = new char[BSIZE];
} }
void RageFileObj::EnableCRC32( bool on ) void RageFileObj::EnableCRC32( bool bOn )
{ {
if( !on ) if( !bOn )
{ {
m_bCRC32Enabled = false; m_bCRC32Enabled = false;
return; return;
@@ -232,23 +234,23 @@ bool RageFileObj::GetCRC32( uint32_t *iRet )
/* Read up to the next \n, and return it in out. Strip the \n. If the \n is /* 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. */ * preceded by a \r (DOS newline), strip that, too. */
int RageFileObj::GetLine( RString &out ) int RageFileObj::GetLine( RString &sOut )
{ {
out = ""; sOut = "";
if( m_bEOF ) if( m_bEOF )
return 0; return 0;
EnableBuffering(); EnableBuffering();
bool GotData = false; bool bGotData = false;
while( 1 ) while( 1 )
{ {
bool done = 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_pBuf, '\n', m_iBufAvail );
bool ReAddCR = 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
@@ -256,36 +258,38 @@ int RageFileObj::GetLine( RString &out )
* \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_iBufAvail && m_pBuf[m_iBufAvail-1] == '\r' )
{ {
ReAddCR = true; bReAddCR = true;
--m_iBufAvail; --m_iBufAvail;
} }
p = m_pBuf+m_iBufAvail; /* everything */ p = m_pBuf+m_iBufAvail; /* everything */
} }
else else
done = true; {
bDone = true;
}
if( p >= m_pBuf ) if( p >= m_pBuf )
{ {
char *RealEnd = p; char *RealEnd = p;
if( done && p > m_pBuf && p[-1] == '\r' ) if( bDone && p > m_pBuf && p[-1] == '\r' )
--RealEnd; /* not including \r */ --RealEnd; /* not including \r */
out.append( m_pBuf, RealEnd ); sOut.append( m_pBuf, RealEnd );
if( done ) if( bDone )
++p; /* skip \n */ ++p; /* skip \n */
const int used = p-m_pBuf; const int iUsed = p-m_pBuf;
if( used ) if( iUsed )
{ {
m_iBufAvail -= used; m_iBufAvail -= iUsed;
m_iFilePos += used; m_iFilePos += iUsed;
GotData = true; bGotData = true;
m_pBuf = p; m_pBuf = p;
} }
} }
if( ReAddCR ) if( bReAddCR )
{ {
ASSERT( m_iBufAvail == 0 ); ASSERT( m_iBufAvail == 0 );
m_pBuf = m_pBuffer; m_pBuf = m_pBuffer;
@@ -293,27 +297,27 @@ int RageFileObj::GetLine( RString &out )
++m_iBufAvail; ++m_iBufAvail;
} }
if( done ) if( bDone )
break; break;
/* We need more data. */ /* We need more data. */
m_pBuf = m_pBuffer; m_pBuf = m_pBuffer;
const int size = FillBuf(); const int iSize = FillBuf();
/* If we've read data already, then don't mark EOF yet. Wait until the /* If we've read data already, then don't mark EOF yet. Wait until the
* next time we're called. */ * next time we're called. */
if( size == 0 && !GotData ) if( iSize == 0 && !bGotData )
{ {
m_bEOF = true; m_bEOF = true;
return 0; return 0;
} }
if( size == -1 ) if( iSize == -1 )
return -1; // error return -1; // error
if( size == 0 ) if( iSize == 0 )
break; // EOF or error break; // EOF or error
} }
return GotData? 1:0; return bGotData? 1:0;
} }
// Always use "\r\n". Even though the program may be running on Unix, the // Always use "\r\n". Even though the program may be running on Unix, the
@@ -324,9 +328,9 @@ int RageFileObj::GetLine( RString &out )
//#define NEWLINE "\n" //#define NEWLINE "\n"
//#endif //#endif
int RageFileObj::PutLine( const RString &str ) int RageFileObj::PutLine( const RString &sStr )
{ {
if( Write(str) == -1 ) if( Write(sStr) == -1 )
return -1; return -1;
return Write( RString(NEWLINE) ); return Write( RString(NEWLINE) );
} }
@@ -344,12 +348,12 @@ int RageFileObj::FillBuf()
* for seeking backwards.) */ * for seeking backwards.) */
const int iBufAvail = BSIZE - (m_pBuf-m_pBuffer) - m_iBufAvail; const int iBufAvail = BSIZE - (m_pBuf-m_pBuffer) - m_iBufAvail;
ASSERT_M( iBufAvail >= 0, ssprintf("%p, %p, %i", m_pBuf, m_pBuffer, (int) BSIZE ) ); ASSERT_M( iBufAvail >= 0, ssprintf("%p, %p, %i", m_pBuf, m_pBuffer, (int) BSIZE ) );
const int size = this->ReadInternal( m_pBuf+m_iBufAvail, iBufAvail ); const int iSize = this->ReadInternal( m_pBuf+m_iBufAvail, iBufAvail );
if( size > 0 ) if( iSize > 0 )
m_iBufAvail += size; m_iBufAvail += iSize;
return size; return iSize;
} }
void RageFileObj::ResetBuf() void RageFileObj::ResetBuf()