//----------------------------------------------------------------------------- // NOTE: This is an exact copy of code in BaseFileSystem.cpp which // has to be here because they want to call // the implementation of Open/Size/Read/Write in CBaseVMPIFileSystem //----------------------------------------------------------------------------- bool CBaseVMPIFileSystem::ReadFile( const char *pFileName, const char *pPath, CUtlBuffer &buf, int nMaxBytes, int nStartingByte, FSAllocFunc_t pfnAlloc ) { const char *pReadFlags = "rb"; if ( buf.IsText() && !buf.ContainsCRLF() ) { pReadFlags = "rt"; } FileHandle_t fp = Open( pFileName, buf.IsText() ? "rt" : "rb", pPath ); if ( !fp ) return false; int nBytesToRead = Size( fp ); if ( nMaxBytes > 0 ) { nBytesToRead = min( nMaxBytes, nBytesToRead ); } buf.EnsureCapacity( nBytesToRead + buf.TellPut() ); if ( nStartingByte != 0 ) { Seek( fp, nStartingByte, FILESYSTEM_SEEK_HEAD ); } int nBytesRead = Read( buf.PeekPut(), nBytesToRead, fp ); buf.SeekPut( CUtlBuffer::SEEK_CURRENT, nBytesRead ); Close( fp ); return (nBytesRead != 0); }
bool CBaseVMPIFileSystem::WriteFile( const char *pFileName, const char *pPath, CUtlBuffer &buf ) { const char *pWriteFlags = "wb"; if ( buf.IsText() && !buf.ContainsCRLF() ) { pWriteFlags = "wt"; } FileHandle_t fp = Open( pFileName, buf.IsText() ? "wt" : "wb", pPath ); if ( !fp ) return false; int nBytesWritten = Write( buf.Base(), buf.TellPut(), fp ); Close( fp ); return (nBytesWritten != 0); }
//----------------------------------------------------------------------------- // Converts a buffer from a CRLF buffer to a CR buffer (and back) // Returns false if no conversion was necessary (and outBuf is left untouched) // If the conversion occurs, outBuf will be cleared. //----------------------------------------------------------------------------- bool CUtlBuffer::ConvertCRLF( CUtlBuffer &outBuf ) { if ( !IsText() || !outBuf.IsText() ) return false; if ( ContainsCRLF() == outBuf.ContainsCRLF() ) return false; int nInCount = TellMaxPut(); outBuf.Purge(); outBuf.EnsureCapacity( nInCount ); bool bFromCRLF = ContainsCRLF(); // Start reading from the beginning int nGet = TellGet(); int nPut = TellPut(); int nGetDelta = 0; int nPutDelta = 0; const char *pBase = (const char*)Base(); int nCurrGet = 0; while ( nCurrGet < nInCount ) { const char *pCurr = &pBase[nCurrGet]; if ( bFromCRLF ) { const char *pNext = Q_strnistr( pCurr, "\r\n", nInCount - nCurrGet ); if ( !pNext ) { outBuf.Put( pCurr, nInCount - nCurrGet ); break; } int nBytes = (size_t)pNext - (size_t)pCurr; outBuf.Put( pCurr, nBytes ); outBuf.PutChar( '\n' ); nCurrGet += nBytes + 2; if ( nGet >= nCurrGet - 1 ) { --nGetDelta; } if ( nPut >= nCurrGet - 1 ) { --nPutDelta; } } else { const char *pNext = Q_strnchr( pCurr, '\n', nInCount - nCurrGet ); if ( !pNext ) { outBuf.Put( pCurr, nInCount - nCurrGet ); break; } int nBytes = (size_t)pNext - (size_t)pCurr; outBuf.Put( pCurr, nBytes ); outBuf.PutChar( '\r' ); outBuf.PutChar( '\n' ); nCurrGet += nBytes + 1; if ( nGet >= nCurrGet ) { ++nGetDelta; } if ( nPut >= nCurrGet ) { ++nPutDelta; } } } Assert( nPut + nPutDelta <= outBuf.TellMaxPut() ); outBuf.SeekGet( SEEK_HEAD, nGet + nGetDelta ); outBuf.SeekPut( SEEK_HEAD, nPut + nPutDelta ); return true; }