2014-08-13 20:30:35 +00:00
|
|
|
/*
|
|
|
|
AppleWin : An Apple //e emulator for Windows
|
|
|
|
|
|
|
|
Copyright (C) 1994-1996, Michael O'Brien
|
|
|
|
Copyright (C) 1999-2001, Oliver Schmidt
|
|
|
|
Copyright (C) 2002-2005, Tom Charlesworth
|
|
|
|
Copyright (C) 2006-2014, Tom Charlesworth, Michael Pohoreski
|
|
|
|
|
|
|
|
AppleWin is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
AppleWin is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with AppleWin; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2006-02-26 06:27:51 +00:00
|
|
|
#include "StdAfx.h"
|
2010-02-14 21:11:26 +00:00
|
|
|
|
2014-08-13 21:03:33 +00:00
|
|
|
#include "Util_Text.h"
|
|
|
|
#include "Util_MemoryTextFile.h"
|
2022-03-13 16:37:25 +00:00
|
|
|
#include "StrFormat.h"
|
2006-02-26 06:27:51 +00:00
|
|
|
|
|
|
|
// MemoryTextFile _________________________________________________________________________________
|
|
|
|
|
2006-02-27 00:31:46 +00:00
|
|
|
const int EOL_NULL = 0;
|
2006-02-26 06:27:51 +00:00
|
|
|
|
|
|
|
//===========================================================================
|
2019-09-06 16:34:25 +00:00
|
|
|
bool MemoryTextFile_t::Read( const std::string & pFileName )
|
2006-02-26 06:27:51 +00:00
|
|
|
{
|
|
|
|
bool bStatus = false;
|
2019-09-06 16:34:25 +00:00
|
|
|
FILE *hFile = fopen( pFileName.c_str(), "rb" );
|
2006-02-26 06:27:51 +00:00
|
|
|
|
|
|
|
if (hFile)
|
|
|
|
{
|
|
|
|
fseek( hFile, 0, SEEK_END );
|
|
|
|
long nSize = ftell( hFile );
|
|
|
|
fseek( hFile, 0, SEEK_SET );
|
|
|
|
|
|
|
|
m_vBuffer.reserve( nSize + 1 );
|
2017-08-25 01:43:54 +00:00
|
|
|
m_vBuffer.insert( m_vBuffer.begin(), nSize+1, 0 ); // NOTE: Can NOT m_vBuffer.clear(); MUST insert() _before_ using at()
|
2006-02-26 06:27:51 +00:00
|
|
|
|
|
|
|
char *pBuffer = & m_vBuffer.at(0);
|
|
|
|
fread( (void*)pBuffer, nSize, 1, hFile );
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2020-12-10 21:08:15 +00:00
|
|
|
memset( pLine, 0, nMaxLineChars );
|
2006-02-26 06:27:51 +00:00
|
|
|
strncpy( pLine, m_vLines[ iLine ], nMaxLineChars-1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// cr/new lines are converted into null, string terminators
|
|
|
|
//===========================================================================
|
|
|
|
void MemoryTextFile_t::GetLinePointers()
|
|
|
|
{
|
|
|
|
if (! m_bDirty)
|
|
|
|
return;
|
|
|
|
|
2022-03-12 11:19:34 +00:00
|
|
|
m_vLines.clear();
|
2010-01-17 18:43:06 +00:00
|
|
|
char *pBegin = & m_vBuffer[ 0 ];
|
|
|
|
char *pLast = & m_vBuffer[ m_vBuffer.size()-1 ];
|
2006-02-26 06:27:51 +00:00
|
|
|
|
|
|
|
char *pEnd = NULL;
|
|
|
|
char *pStartNextLine;
|
|
|
|
|
2010-01-17 18:43:06 +00:00
|
|
|
while (pBegin <= pLast)
|
2006-02-26 06:27:51 +00:00
|
|
|
{
|
2010-01-17 18:43:06 +00:00
|
|
|
if ( *pBegin ) // Only keep non-empty lines
|
|
|
|
m_vLines.push_back( pBegin );
|
2006-02-26 06:27:51 +00:00
|
|
|
|
|
|
|
pEnd = const_cast<char*>( SkipUntilEOL( pBegin ));
|
|
|
|
|
2006-02-27 00:31:46 +00:00
|
|
|
if (*pEnd == EOL_NULL)
|
2006-02-26 06:27:51 +00:00
|
|
|
{
|
|
|
|
// Found EOL via null
|
|
|
|
pStartNextLine = pEnd + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pStartNextLine = const_cast<char*>( 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 );
|
2006-02-27 00:31:46 +00:00
|
|
|
*pEnd = EOL_NULL;
|
2006-02-26 06:27:51 +00:00
|
|
|
}
|
|
|
|
pBegin = pStartNextLine;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_bDirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//===========================================================================
|
2022-03-13 16:37:25 +00:00
|
|
|
void MemoryTextFile_t::PushLine( const char *pLine )
|
2006-02-26 06:27:51 +00:00
|
|
|
{
|
2022-03-13 16:37:25 +00:00
|
|
|
const char *pSrc = pLine;
|
2006-02-26 06:27:51 +00:00
|
|
|
while (pSrc && *pSrc)
|
|
|
|
{
|
|
|
|
if (*pSrc == CHAR_CR)
|
2006-02-27 00:31:46 +00:00
|
|
|
m_vBuffer.push_back( EOL_NULL );
|
2006-02-26 06:27:51 +00:00
|
|
|
else
|
|
|
|
if (*pSrc == CHAR_LF)
|
2006-02-27 00:31:46 +00:00
|
|
|
m_vBuffer.push_back( EOL_NULL );
|
2006-02-26 06:27:51 +00:00
|
|
|
else
|
|
|
|
m_vBuffer.push_back( *pSrc );
|
|
|
|
|
|
|
|
pSrc++;
|
|
|
|
}
|
2006-02-27 00:31:46 +00:00
|
|
|
m_vBuffer.push_back( EOL_NULL );
|
2006-02-26 06:27:51 +00:00
|
|
|
|
|
|
|
m_bDirty = true;
|
|
|
|
}
|
|
|
|
|
2022-03-13 16:37:25 +00:00
|
|
|
void MemoryTextFile_t::PushLineFormat( const char *pFormat, ... )
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
va_start(va, pFormat);
|
|
|
|
PushLine(StrFormatV(pFormat, va).c_str());
|
|
|
|
va_end(va);
|
|
|
|
}
|