diff --git a/AppleWin/source/Util_MemoryTextFile.cpp b/AppleWin/source/Util_MemoryTextFile.cpp new file mode 100644 index 00000000..3addf07a --- /dev/null +++ b/AppleWin/source/Util_MemoryTextFile.cpp @@ -0,0 +1,121 @@ +#include "StdAfx.h" +#pragma hdrstop + +// MemoryTextFile _________________________________________________________________________________ + +const int EOL_TERM = 0; + +//=========================================================================== +bool MemoryTextFile_t::Read( char *pFileName ) +{ + bool bStatus = false; + FILE *hFile = fopen( pFileName, "rt" ); + + if (hFile) + { + fseek( hFile, 0, SEEK_END ); + long nSize = ftell( hFile ); + fseek( hFile, 0, SEEK_SET ); + + m_vBuffer.reserve( nSize + 1 ); + m_vBuffer.insert( m_vBuffer.begin(), nSize+1, 0 ); + + char *pBuffer = & m_vBuffer.at(0); + fread( (void*)pBuffer, nSize, 1, hFile ); + + m_vBuffer.push_back( EOL_TERM ); + + fclose(hFile); + + m_bDirty = true; + GetLinePointers(); + + bStatus = true; + } + + return bStatus; +} + + +//=========================================================================== +void MemoryTextFile_t::GetLine( const int iLine, char *pLine, const int nMaxLineChars ) +{ + if (m_bDirty) + { + GetLinePointers(); + } + + ZeroMemory( pLine, nMaxLineChars ); + strncpy( pLine, m_vLines[ iLine ], nMaxLineChars-1 ); +} + + +// cr/new lines are converted into null, string terminators +//=========================================================================== +void MemoryTextFile_t::GetLinePointers() +{ + if (! m_bDirty) + return; + + m_vLines.erase( m_vLines.begin(), m_vLines.end() ); + char *pBegin = & m_vBuffer.at( 0 ); + char *pLast = & m_vBuffer[ m_vBuffer.size() ]; + + char *pEnd = NULL; + char *pStartNextLine; + + while (pBegin < pLast) + { + m_vLines.push_back( pBegin ); + + pEnd = const_cast( SkipUntilEOL( pBegin )); + + if (*pEnd != EOL_TERM) + { + // Found EOL via null + pStartNextLine = pEnd + 1; + } + else + { + pStartNextLine = const_cast( EatEOL( pEnd )); + + // DOS/Win "Text" mode converts LF CR (0D 0A) to CR (0D) + // but just in case, the file is read in binary. + int nEOL = pStartNextLine - pEnd; + while (nEOL-- > 1) + { + *pEnd++ = ' '; + } + + // assert( pEnd != NULL ); + *pEnd = EOL_TERM; + } + pBegin = pStartNextLine; + } + + m_bDirty = false; +} + + +//=========================================================================== +void MemoryTextFile_t::PushLine( char *pLine ) +{ + char *pSrc = pLine; + while (pSrc && *pSrc) + { + if (*pSrc == CHAR_CR) + m_vBuffer.push_back( EOL_TERM ); + else + if (*pSrc == CHAR_LF) + m_vBuffer.push_back( EOL_TERM ); + else + m_vBuffer.push_back( *pSrc ); + + pSrc++; + } + m_vBuffer.push_back( EOL_TERM ); + + m_bDirty = true; +} + + diff --git a/AppleWin/source/Util_MemoryTextFile.h b/AppleWin/source/Util_MemoryTextFile.h new file mode 100644 index 00000000..118718ee --- /dev/null +++ b/AppleWin/source/Util_MemoryTextFile.h @@ -0,0 +1,49 @@ +#pragma once + + +// Memory Text File _________________________________________________________ + + class MemoryTextFile_t + { + vector m_vBuffer; + vector m_vLines ; // array of pointers to start of lines + bool m_bDirty ; // line pointers not up-to-date + + void GetLinePointers(); + + public: + MemoryTextFile_t() +// : m_nSize( 0 ) +// , m_pBuffer( 0 ) +// , m_nLines( 0 ) + : m_bDirty( false ) + { + m_vBuffer.reserve( 2048 ); + m_vLines.reserve( 128 ); + } + + bool Read( TCHAR *pFileName ); + void Reset() + { + m_vBuffer.erase( m_vBuffer.begin(), m_vBuffer.end() ); + m_vLines.erase( m_vLines.begin(), m_vLines.end() ); + } + +inline int GetNumLines() + { + if (m_bDirty) + GetLinePointers(); + + return m_vLines.size(); + } + +inline char *GetLine( const int iLine ) const + { + return m_vLines.at( iLine ); + } + + void GetLine( const int iLine, char *pLine, const int n ); + + void PushLine( char *pLine ); + }; +