#include "StdAfx.h"
#include "DiskFile32.h"
#include "MemoryFile32.h"
CMemoryFile32::CMemoryFile32(UINT32 unInitialAllocSize)
{
unLastErrorCode = MEMFILE32_ERRCODE_OK;
unPointer = 0;
unFileSize = 0;
unAllocFileSize = 0;
bEndOfFile = false;
pFileBuffer = NULL;
pFileBuffer = new UBYTE [unInitialAllocSize];
if ( ! pFileBuffer )
{
unLastErrorCode = MEMFILE32_ERRCODE_NEWBUFFER;
}
unAllocFileSize = unInitialAllocSize;
FillMemory(pFileBuffer, unAllocFileSize, 0);
}
CMemoryFile32::~CMemoryFile32()
{
if( pFileBuffer )
delete [] pFileBuffer;
}
UINT32 CMemoryFile32::GetLastErrorCode(void)
{
return unLastErrorCode;
}
void CMemoryFile32::ReAllocateMem(UINT32 unNewSize)
{
UINT32 unNewAllocFileSize;
UBYTE *pNewFileBuffer;
unLastErrorCode = MEMFILE32_ERRCODE_UNKNOWNERROR;
// new size
unNewAllocFileSize = unNewSize + FILE_DEF_MEM_ALL_SIZE;
try
{
// create new buffer
pNewFileBuffer = new UBYTE [unNewAllocFileSize];
if( ! pNewFileBuffer )
{
unLastErrorCode = MEMFILE32_ERRCODE_NEWBUFFERREALLOC;
throw;
}
// init new buffer
FillMemory(pNewFileBuffer, unNewAllocFileSize, 0);
// move previous content
MoveMemory(pNewFileBuffer, pFileBuffer, unFileSize);
// delete previous buffer
delete [] pFileBuffer;
// set new pointer
pFileBuffer = pNewFileBuffer;
// set new allocated size
unAllocFileSize = unNewAllocFileSize;
}
catch (...)
{
}
unLastErrorCode = MEMFILE32_ERRCODE_OK;
}
void CMemoryFile32::LoadFromDisk(TCHAR *psFilePath)
{
unLastErrorCode = MEMFILE32_ERRCODE_UNKNOWNERROR;
CDiskFile32* pDiskFile;
UINT32 unBytesTo, unBytesDone;
try
{
pDiskFile = new CDiskFile32;
if ( !pDiskFile )
{
unLastErrorCode = MEMFILE32_ERRCODE_LOADFROMDISK;
throw;
}
pDiskFile->SetFilePath(psFilePath);
// allocate buffer
unBytesTo = pDiskFile->GetSize();
ReAllocateMem(unBytesTo);
pDiskFile->Read(pFileBuffer, unBytesTo, &unBytesDone);
if ( unBytesDone != unBytesTo )
{
unLastErrorCode = MEMFILE32_ERRCODE_LOADFROMDISKREAD;
throw;
}
unFileSize = unBytesDone;
if(pDiskFile)
delete pDiskFile;
}
catch(...)
{
if(pDiskFile)
delete pDiskFile;
return;
}
unLastErrorCode = MEMFILE32_ERRCODE_OK;
}
void CMemoryFile32::SaveToDisk(TCHAR *psFilePath, bool bAppend)
{
unLastErrorCode = MEMFILE32_ERRCODE_UNKNOWNERROR;
CDiskFile32* pDiskFile;
UINT32 unBytesTo, unBytesDone;
try
{
pDiskFile = new CDiskFile32;
if ( !pDiskFile )
{
unLastErrorCode = MEMFILE32_ERRCODE_LOADFROMDISK;
throw;
}
pDiskFile->SetFilePath(psFilePath);
unBytesTo = unFileSize;
if ( bAppend )
pDiskFile->Append(pFileBuffer, unBytesTo);
else
pDiskFile->Write(pFileBuffer, unBytesTo, &unBytesDone);
if ( pDiskFile )
delete pDiskFile;
}
catch(...)
{
if ( pDiskFile )
delete pDiskFile;
return;
}
unLastErrorCode = MEMFILE32_ERRCODE_OK;
}
bool CMemoryFile32::CopyToClipbboard(void)
{
HANDLE hCFTextData;
void *pCFTextData;
bool bOk = false;
if ( OpenClipboard(NULL) )
{
EmptyClipboard();
// CF_TEXT format
hCFTextData = GlobalAlloc(GMEM_DDESHARE, unFileSize+100);
pCFTextData = (void*) GlobalLock(hCFTextData);
if ( !pCFTextData )
{
unLastErrorCode = MEMFILE32_ERRCODE_NOCLPBRDMEM;
return bOk;
}
MoveMemory(pCFTextData, (void*) pFileBuffer, unFileSize);
GlobalUnlock(hCFTextData);
SetClipboardData(CF_TEXT, hCFTextData);
CloseClipboard();
bOk = true;
}
return bOk;
}
bool CMemoryFile32::PasteFromClipbboard(void)
{
HANDLE hClipboardData;
void* pClipboardData;
bool bOk = false;
if ( ! IsClipboardFormatAvailable(CF_TEXT) )
return false;
if ( OpenClipboard(NULL) )
{
hClipboardData = GetClipboardData(CF_TEXT);
if ( hClipboardData)
{
pClipboardData = GlobalLock(hClipboardData);
if ( pClipboardData )
{
GlobalUnlock(hClipboardData);
Erase();
UINT32 unBytesTo, unBytesDone;
unBytesTo = (UINT32) GlobalSize(hClipboardData);
Write((void*) pClipboardData, unBytesTo, &unBytesDone);
bOk = true;
}
}
CloseClipboard();
}
return bOk;
}
void CMemoryFile32::Erase()
{
unLastErrorCode = MEMFILE32_ERRCODE_UNKNOWNERROR;
FillMemory(pFileBuffer, unFileSize, 0);
unPointer = 0;
unFileSize = 0;
unLastErrorCode = MEMFILE32_ERRCODE_OK;
}
UINT32 CMemoryFile32::GetSize(void)
{
return unFileSize;
}
bool CMemoryFile32::GetEndOfFile(void)
{
return bEndOfFile;
}
void CMemoryFile32::SetPointer(UINT32 unOffset, UINT32 unCode)
{
switch(unCode)
{
case CODE_FILE_MOVETO:
unPointer = unOffset;
break;
case CODE_FILE_RESETP:
unPointer = 0;
break;
case CODE_FILE_END:
unPointer = unFileSize;
break;
}
bEndOfFile = false;
}
UINT32 CMemoryFile32::GetPointer(void)
{
return unPointer;
}
// binary read and writes
void CMemoryFile32::Read(void *pMemBuffer, UINT32 unBytesTo, UINT32 *punBytesDone)
{
UINT32 unBytesToRead, unBytesLeft;
unLastErrorCode = MEMFILE32_ERRCODE_UNKNOWNERROR;
if ( NULL == pMemBuffer )
{
unLastErrorCode = MEMFILE32_ERRCODE_NOBUFFER;
return;
}
bEndOfFile = false;
unBytesToRead = unBytesTo;
unBytesLeft = unFileSize - unPointer;
if (unBytesToRead > unBytesLeft)
{
unBytesToRead = unBytesLeft;
bEndOfFile = true;
}
MoveMemory(pMemBuffer, pFileBuffer+unPointer, unBytesToRead);
unPointer += unBytesToRead;
unLastErrorCode = MEMFILE32_ERRCODE_OK;
if(punBytesDone)
*punBytesDone = unBytesToRead;
}
void CMemoryFile32::Write(void *pMemBuffer, UINT32 unBytesTo, UINT32 *punBytesDone)
{
UINT32 unBytesToWrite, unTotalBytesNeeded;
unLastErrorCode = MEMFILE32_ERRCODE_UNKNOWNERROR;
if ( NULL == pMemBuffer )
{
unLastErrorCode = MEMFILE32_ERRCODE_NOBUFFER;
return;
}
unBytesToWrite = unBytesTo;
unTotalBytesNeeded = unPointer + unBytesToWrite;
if(unTotalBytesNeeded > unAllocFileSize) // re-allocate
ReAllocateMem(unTotalBytesNeeded);
MoveMemory(pFileBuffer+unPointer, pMemBuffer, unBytesToWrite);
unPointer += unBytesToWrite;
if (unPointer > unFileSize)
unFileSize = unPointer;
unLastErrorCode = MEMFILE32_ERRCODE_OK;
if (punBytesDone)
*punBytesDone = unBytesToWrite;
}
void CMemoryFile32::Append(void *pMemBuffer, UINT32 unBytesTo, UINT32 *punBytesDone)
{
SetPointer(0, CODE_FILE_END);
Write(pMemBuffer, unBytesTo, punBytesDone);
}
void CMemoryFile32::STR_ReadString(TCHAR *pMemBuffer, UINT32 unMaxChar)
{
TCHAR *pOutPtr, *pInPtr;
UINT32 unCharCount;
unLastErrorCode = MEMFILE32_ERRCODE_UNKNOWNERROR;
bEndOfFile = false;
pOutPtr = pMemBuffer;
unCharCount = 0;
while(true)
{
if(unPointer >= unFileSize)
{
*pOutPtr = L'\0';
bEndOfFile = true;
return;
}
pInPtr = (TCHAR*) (pFileBuffer+unPointer);
// EOL Windows
if( (*pInPtr == L'\r') && (*(pInPtr+1) == L'\n') )
{
*pOutPtr = L'\0';
#if defined (UNICODE)
// skip 4 bytes
unPointer++; unPointer++; unPointer++; unPointer++;
#else
// skip 2 bytes
unPointer++; unPointer++;
#endif
break;
}
// EOL Unix
if( *pInPtr == L'\n' )
{
*pOutPtr = L'\0';
#if defined (UNICODE)
// skip 2 bytes
unPointer++; unPointer++;
#else
// skip 1 bytes
unPointer++;
#endif
break;
}
*pOutPtr = *pInPtr;
pOutPtr++;
#if defined (UNICODE)
// skip 2 bytes
unPointer++; unPointer++;
#else
// skip 1 bytes
unPointer++;
#endif
unCharCount++;
if ( unCharCount >= (unMaxChar-1) )
{
*pOutPtr = L'\0';
break;
}
}
unLastErrorCode = MEMFILE32_ERRCODE_OK;
}
void CMemoryFile32::STR_AppendString(TCHAR *pString, bool bIsUnix)
{
UINT32 unBytesDone, unBytesTo;
TCHAR cbEOLwin[] = _T("\r\n");
TCHAR cbEOLunix[] = _T("\n");
unLastErrorCode = MEMFILE32_ERRCODE_UNKNOWNERROR;
// Write string
SetPointer(0, CODE_FILE_END);
unBytesTo = (UINT32) _tcslen(pString);
#if defined (UNICODE)
unBytesTo = 2*unBytesTo;
#endif
Write((void*) pString, unBytesTo, &unBytesDone);
// Write EOL
if (bIsUnix)
{
#if defined (UNICODE)
unBytesTo = 2;
#else
unBytesTo = 1;
#endif
Write((void*) cbEOLunix, unBytesTo, &unBytesDone);
}
else
{
#if defined (UNICODE)
unBytesTo = 4;
#else
unBytesTo = 2;
#endif
Write((void*) cbEOLwin, unBytesTo, &unBytesDone);
}
unLastErrorCode = MEMFILE32_ERRCODE_OK;
}
void CMemoryFile32::STR_LineFeed(UINT32 unLineCount, bool bIsUnix)
{
UINT32 unBytesDone, unBytesTo, unAuxCount;
TCHAR cbEOLwin[] = _T("\r\n");
TCHAR cbEOLunix[] = _T("\n");
unLastErrorCode = MEMFILE32_ERRCODE_UNKNOWNERROR;
for(unAuxCount = 0 ; unAuxCount < unLineCount ; unAuxCount++)
{
SetPointer(0, CODE_FILE_END);
// Write EOL
if (bIsUnix)
{
#if defined (UNICODE)
unBytesTo = 2;
#else
unBytesTo = 1;
#endif
Write((void*) cbEOLunix, unBytesTo, &unBytesDone);
}
else
{
#if defined (UNICODE)
unBytesTo = 4;
#else
unBytesTo = 2;
#endif
Write((void*) cbEOLwin, unBytesTo, &unBytesDone);
}
}
unLastErrorCode = MEMFILE32_ERRCODE_OK;
}
void CMemoryFile32::SkipLineTerminators(void)
{
TCHAR *pInPtr;
unLastErrorCode = MEMFILE32_ERRCODE_UNKNOWNERROR;
bEndOfFile = false;
while(true)
{
if(unPointer >= unFileSize)
{
bEndOfFile = true;
return;
}
pInPtr = (TCHAR*) (pFileBuffer+unPointer);
if( ( *pInPtr != L'\r' ) || ( *pInPtr != L'\n' ) )
break;
// EOL Windows
if( (*pInPtr == L'\r') && (*(pInPtr+1) == L'\n') )
{
#if defined (UNICODE)
// skip 4 bytes
unPointer++; unPointer++; unPointer++; unPointer++;
#else
// skip 2 bytes
unPointer++; unPointer++;
#endif
break;
}
// EOL Unix
if( *pInPtr == L'\n' )
{
#if defined (UNICODE)
// skip 2 bytes
unPointer++; unPointer++;
#else
// skip 1 bytes
unPointer++;
#endif
break;
}
}
unLastErrorCode = MEMFILE32_ERRCODE_OK;
}