You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
482 lines
12 KiB
482 lines
12 KiB
//----------------------------------------------------------------------------
|
|
// Laser Guard Ltd.
|
|
// Copyright © 2000-2002. All Rights Reserved.
|
|
// Version 1.0 01/01/2000
|
|
// Last modification: 08/10/2003
|
|
// PROJECT:
|
|
// SUBSYSTEM:
|
|
// FILE: FileGroup.cpp
|
|
// AUTHOR: Sergey Chizhevskiy
|
|
// OVERVIEW
|
|
// ~~~~~~~~
|
|
// Source file of FileGroup.cpp
|
|
//----------------------------------------------------------------------------
|
|
#include "stdafx.h"
|
|
#include "..\..\LgLib\Version.h"
|
|
#include "FileGroup.h"
|
|
|
|
#ifdef _DEBUG
|
|
#undef THIS_FILE
|
|
static char THIS_FILE[]=__FILE__;
|
|
#define new DEBUG_NEW
|
|
#endif
|
|
|
|
CLgFileGroup::CLgFileGroup()
|
|
{
|
|
m_nTempIndex = 0UL;
|
|
m_FileName = _T( "groupfile.tmp" );
|
|
}
|
|
|
|
CLgFileGroup::~CLgFileGroup()
|
|
{
|
|
/*
|
|
while( m_nTempIndex )
|
|
{
|
|
TCHAR pName[MAX_PATH];
|
|
_sntprintf( (LPTSTR) pName, MAX_PATH, _T( "%08X.TMP" ), m_nTempIndex-- );
|
|
::DeleteFile( pName );
|
|
}
|
|
*/
|
|
}
|
|
|
|
BOOL CLgFileGroup::Add( LPCTSTR pFileName )
|
|
{
|
|
Remove( pFileName );
|
|
|
|
DWORD dwLenght = 0;
|
|
LPBYTE pBuffer = NULL;
|
|
BOOL bResult = FALSE;
|
|
CFile file;
|
|
if( file.Open( pFileName, CFile::modeRead ) )
|
|
{
|
|
dwLenght = file.GetLength();
|
|
pBuffer = (PBYTE)malloc(dwLenght);
|
|
if( pBuffer )
|
|
{
|
|
file.Read( pBuffer, dwLenght );
|
|
bResult = TRUE;
|
|
}
|
|
file.Close();
|
|
}
|
|
|
|
if( bResult )
|
|
{
|
|
if( file.Open( m_FileName, CFile::modeReadWrite | CFile::modeCreate | CFile::modeNoTruncate ) )
|
|
{
|
|
DWORD dwCurrentLenght = file.GetLength();
|
|
|
|
if( dwCurrentLenght == 0 )
|
|
{
|
|
// prepare file header
|
|
FILE_HEADER fh;
|
|
_tcscpy( fh.pSignature, FG_SIGNATURE );
|
|
fh.nOffset.QuadPart = sizeof( FILE_HEADER ) + dwLenght;
|
|
fh.nCount.QuadPart = 1;
|
|
|
|
// prepare file descriptor entry
|
|
FILE_DESCRIPTOR fd;
|
|
_tcscpy( fd.pFileName, pFileName );
|
|
fd.nOffset.QuadPart = sizeof( FILE_HEADER );
|
|
fd.nSize.QuadPart = dwLenght;
|
|
|
|
// write file header
|
|
file.Write( &fh, sizeof( FILE_HEADER ) );
|
|
|
|
// write file contents
|
|
file.Write( pBuffer, dwLenght );
|
|
|
|
//write file descriptor
|
|
file.Write( &fd, sizeof( FILE_DESCRIPTOR ) );
|
|
}
|
|
else
|
|
{
|
|
FILE_HEADER fh;
|
|
|
|
// read file header
|
|
file.Read( &fh, sizeof( FILE_HEADER ) );
|
|
|
|
// check signature
|
|
if( !_tcsncmp( fh.pSignature, FG_SIGNATURE, FG_SIGNATURE_SIZE ) )
|
|
{
|
|
DWORD dwTableSize = (DWORD)fh.nCount.QuadPart * sizeof( FILE_DESCRIPTOR );
|
|
|
|
FILE_DESCRIPTOR *pTable = (FILE_DESCRIPTOR*) (PBYTE)malloc( dwTableSize );
|
|
|
|
if( pTable )
|
|
{
|
|
try
|
|
{
|
|
// seek to table of contents
|
|
file.Seek( (DWORD)fh.nOffset.QuadPart, CFile::begin );
|
|
|
|
// load table of contents
|
|
if( file.Read( pTable, dwTableSize ) == dwTableSize )
|
|
{
|
|
try
|
|
{
|
|
// prepare new file descriptor
|
|
FILE_DESCRIPTOR fd;
|
|
fd.nOffset = fh.nOffset;
|
|
fd.nSize.QuadPart = dwLenght;
|
|
_tcscpy( fd.pFileName, pFileName );
|
|
|
|
// seek to table of contents again
|
|
file.Seek( (DWORD)fh.nOffset.QuadPart, CFile::begin );
|
|
|
|
// write new file on this place
|
|
file.Write( pBuffer, dwLenght );
|
|
|
|
// update file header offset and size
|
|
fh.nOffset.QuadPart = file.GetPosition();
|
|
fh.nCount.QuadPart ++;
|
|
|
|
// write table after this new file
|
|
file.Write( pTable, dwTableSize );
|
|
|
|
// write new table entry
|
|
file.Write( &fd, sizeof( FILE_DESCRIPTOR ) );
|
|
|
|
try
|
|
{
|
|
// seek to begin of file
|
|
file.SeekToBegin();
|
|
|
|
// write updated file header
|
|
file.Write( &fh, sizeof( FILE_HEADER ) );
|
|
}
|
|
catch( CFileException *e )
|
|
{
|
|
e;
|
|
}
|
|
}
|
|
catch( CFileException *e )
|
|
{
|
|
e;
|
|
}
|
|
|
|
}
|
|
}
|
|
catch( CFileException *e )
|
|
{
|
|
e;
|
|
}
|
|
|
|
delete pTable;
|
|
}
|
|
}
|
|
}
|
|
|
|
file.Close();
|
|
}
|
|
|
|
free( pBuffer );
|
|
}
|
|
|
|
return bResult;
|
|
}
|
|
|
|
BOOL CLgFileGroup::Remove( LPCTSTR pFileName )
|
|
{
|
|
CFile file;
|
|
if( file.Open( m_FileName, CFile::modeReadWrite ) )
|
|
{
|
|
FILE_HEADER fh;
|
|
|
|
// read file header
|
|
file.Read( &fh, sizeof( FILE_HEADER ) );
|
|
|
|
// check signature
|
|
if( !_tcsncmp( fh.pSignature, FG_SIGNATURE, FG_SIGNATURE_SIZE ) )
|
|
{
|
|
DWORD dwTableSize = (DWORD)fh.nCount.QuadPart * sizeof( FILE_DESCRIPTOR );
|
|
|
|
FILE_DESCRIPTOR *pTable = (FILE_DESCRIPTOR*) (PBYTE)malloc( dwTableSize );
|
|
|
|
if( pTable )
|
|
{
|
|
try
|
|
{
|
|
// seek to table of contents
|
|
file.Seek( (DWORD)fh.nOffset.QuadPart, CFile::begin );
|
|
|
|
// load table of contents
|
|
if( file.Read( pTable, dwTableSize ) == dwTableSize )
|
|
{
|
|
try
|
|
{
|
|
// placeholder for deleting entry
|
|
FILE_DESCRIPTOR fd;
|
|
|
|
// searching entry with such name and then shift remainig
|
|
BOOL bFound = FALSE;
|
|
LARGE_INTEGER nSize = {0};
|
|
for( DWORD nIndex = 0; nIndex < fh.nCount.QuadPart; nIndex++ )
|
|
{
|
|
if( !_tcscmp( pTable[nIndex].pFileName, pFileName ) )
|
|
{
|
|
// store deleting entry
|
|
fd = pTable[nIndex];
|
|
bFound = TRUE;
|
|
}
|
|
|
|
// shifting remaining entries
|
|
if( bFound && ( nIndex + 1 ) < fh.nCount.QuadPart )
|
|
{
|
|
pTable[ nIndex ] = pTable[ nIndex + 1 ];
|
|
|
|
// shifting offset of entry
|
|
pTable[ nIndex ].nOffset.QuadPart -= fd.nSize.QuadPart;
|
|
|
|
// calculating shifting block size
|
|
nSize.QuadPart += pTable[ nIndex ].nSize.QuadPart;
|
|
}
|
|
}
|
|
|
|
if( bFound )
|
|
{
|
|
// update header
|
|
fh.nCount.QuadPart --;
|
|
fh.nOffset.QuadPart -= fd.nSize.QuadPart;
|
|
|
|
// shift remainig files
|
|
const INT nBlockSize = (1<<16);
|
|
|
|
LPBYTE pBuffer = (LPBYTE) malloc( nBlockSize );
|
|
if( pBuffer )
|
|
{
|
|
// prepare offsets
|
|
DWORD dwOffsetDest = (DWORD)fd.nOffset.QuadPart;
|
|
DWORD dwOffsetSrc = (DWORD)(fd.nOffset.QuadPart + fd.nSize.QuadPart);
|
|
|
|
// number of round blocks
|
|
DWORD nBlockCount = (DWORD)( nSize.QuadPart / nBlockSize );
|
|
if( nBlockCount )
|
|
{
|
|
// shifting round blocks
|
|
for( DWORD nIndex = 0; nIndex < nBlockCount; nIndex ++ )
|
|
{
|
|
try
|
|
{
|
|
file.Seek( dwOffsetSrc, CFile::begin );
|
|
file.Read( pBuffer, nBlockSize );
|
|
file.Seek( dwOffsetDest, CFile::begin );
|
|
file.Write( pBuffer, nBlockSize );
|
|
dwOffsetSrc += nBlockSize;
|
|
dwOffsetDest += nBlockSize;
|
|
}
|
|
catch( CFileException *e )
|
|
{
|
|
e;
|
|
}
|
|
}
|
|
}
|
|
|
|
// shifting last block
|
|
DWORD nLastBlockSize = (DWORD)( nSize.QuadPart % nBlockSize );
|
|
if( nLastBlockSize )
|
|
{
|
|
try
|
|
{
|
|
file.Seek( dwOffsetSrc, CFile::begin );
|
|
file.Read( pBuffer, nLastBlockSize );
|
|
file.Seek( dwOffsetDest, CFile::begin );
|
|
file.Write( pBuffer, nLastBlockSize );
|
|
}
|
|
catch( CFileException *e )
|
|
{
|
|
e;
|
|
}
|
|
}
|
|
|
|
free( pBuffer );
|
|
|
|
// write table of contents
|
|
file.Write( pTable, (DWORD)( fh.nCount.QuadPart * sizeof( FILE_DESCRIPTOR ) ) );
|
|
|
|
try
|
|
{
|
|
// seek to begin of file
|
|
file.SeekToBegin();
|
|
|
|
// write updated file header
|
|
file.Write( &fh, sizeof( FILE_HEADER ) );
|
|
}
|
|
catch( CFileException *e )
|
|
{
|
|
e;
|
|
}
|
|
|
|
file.SetLength( (DWORD)( nSize.QuadPart + sizeof( FILE_HEADER ) + sizeof( FILE_DESCRIPTOR ) * fh.nCount.QuadPart ) );
|
|
}
|
|
}
|
|
}
|
|
catch( CFileException *e )
|
|
{
|
|
e;
|
|
}
|
|
|
|
}
|
|
}
|
|
catch( CFileException *e )
|
|
{
|
|
e;
|
|
}
|
|
|
|
delete pTable;
|
|
}
|
|
}
|
|
|
|
file.Close();
|
|
}
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
BOOL CLgFileGroup::GetTemp( LPCTSTR pFileName, LPCTSTR pNewFileName, INT nNewFileNameSize )
|
|
{
|
|
BOOL bResult = FALSE;
|
|
|
|
CFile file;
|
|
if( file.Open( m_FileName, CFile::modeReadWrite ) )
|
|
{
|
|
FILE_HEADER fh;
|
|
|
|
// read file header
|
|
file.Read( &fh, sizeof( FILE_HEADER ) );
|
|
|
|
// check signature
|
|
if( !_tcsncmp( fh.pSignature, FG_SIGNATURE, FG_SIGNATURE_SIZE ) )
|
|
{
|
|
DWORD dwTableSize = (DWORD)fh.nCount.QuadPart * sizeof( FILE_DESCRIPTOR );
|
|
|
|
FILE_DESCRIPTOR *pTable = (FILE_DESCRIPTOR*) malloc( dwTableSize );
|
|
|
|
if( pTable )
|
|
{
|
|
try
|
|
{
|
|
// seek to table of contents
|
|
file.Seek( (DWORD)fh.nOffset.QuadPart, CFile::begin );
|
|
|
|
// load table of contents
|
|
if( file.Read( pTable, dwTableSize ) == dwTableSize )
|
|
{
|
|
try
|
|
{
|
|
// placeholder for deleting entry
|
|
FILE_DESCRIPTOR fd;
|
|
|
|
// searching entry with such name and then shift remainig
|
|
for( DWORD nIndex = 0; nIndex < fh.nCount.QuadPart; nIndex++ )
|
|
{
|
|
if( !_tcscmp( pTable[nIndex].pFileName, pFileName ) )
|
|
{
|
|
// store deleting entry
|
|
fd = pTable[nIndex];
|
|
bResult = TRUE;
|
|
}
|
|
}
|
|
|
|
if( bResult )
|
|
{
|
|
// prepare temp filename
|
|
_sntprintf( (LPTSTR) pNewFileName, nNewFileNameSize, _T( "%08X.TMP" ), m_nTempIndex++ );
|
|
|
|
// create new file
|
|
CFile temp;
|
|
if( temp.Open( pNewFileName, CFile::modeCreate | CFile::modeWrite ) )
|
|
{
|
|
const INT nBlockSize = (1<<16);
|
|
|
|
LPBYTE pBuffer = (LPBYTE)malloc( nBlockSize );
|
|
if( pBuffer )
|
|
{
|
|
// prepare offset
|
|
DWORD dwOffsetSrc = (DWORD)fd.nOffset.QuadPart;
|
|
try
|
|
{
|
|
// seek to position of file
|
|
file.Seek( dwOffsetSrc, CFile::begin );
|
|
|
|
// number of round blocks
|
|
DWORD nBlockCount = (DWORD)( fd.nSize.QuadPart / nBlockSize );
|
|
if( nBlockCount )
|
|
{
|
|
// copying round blocks
|
|
for( DWORD nIndex = 0; nIndex < nBlockCount; nIndex ++ )
|
|
{
|
|
file.Read( pBuffer, nBlockSize );
|
|
temp.Write( pBuffer, nBlockSize );
|
|
dwOffsetSrc += nBlockSize;
|
|
}
|
|
}
|
|
|
|
// copying last block
|
|
DWORD nLastBlockSize = (DWORD)( fd.nSize.QuadPart % nBlockSize );
|
|
if( nLastBlockSize )
|
|
{
|
|
file.Read( pBuffer, nLastBlockSize );
|
|
temp.Write( pBuffer, nLastBlockSize );
|
|
}
|
|
}
|
|
catch( CFileException *e )
|
|
{
|
|
e;
|
|
}
|
|
|
|
free( pBuffer );
|
|
}
|
|
|
|
temp.Close();
|
|
}
|
|
}
|
|
}
|
|
catch( CFileException *e )
|
|
{
|
|
e;
|
|
}
|
|
}
|
|
}
|
|
catch( CFileException *e )
|
|
{
|
|
e;
|
|
}
|
|
|
|
delete pTable;
|
|
}
|
|
}
|
|
|
|
file.Close();
|
|
}
|
|
|
|
if( !bResult )
|
|
{
|
|
_tcsncpy( (LPTSTR)pNewFileName, (LPTSTR)pFileName, nNewFileNameSize );
|
|
}
|
|
|
|
return bResult;
|
|
}
|
|
|
|
void CLgFileGroup::SetFileName( LPCTSTR pFileName, bool bCopy )
|
|
{
|
|
if( bCopy )
|
|
::CopyFile( m_FileName, pFileName, FALSE );
|
|
|
|
m_FileName = pFileName;
|
|
}
|
|
|
|
HANDLE CLgFileGroup::LoadImage( LPCTSTR pFileName, UINT uType, int cxDesired, int cyDesired, UINT fuLoad )
|
|
{
|
|
HANDLE hResult = NULL;
|
|
|
|
TCHAR pBuffer[MAX_PATH];
|
|
|
|
BOOL bIsTemp = GetTemp( pFileName, pBuffer, MAX_PATH );
|
|
|
|
hResult = ::LoadImage( NULL, pBuffer, uType, cxDesired, cyDesired, fuLoad );
|
|
|
|
if( bIsTemp )
|
|
DeleteFile( pBuffer );
|
|
|
|
return hResult;
|
|
}
|