class CInputFile
{
public:
CInputFile() : m_Handle( INVALID_HANDLE_VALUE ) {}
~CInputFile() { Close(); }
bool Open( const char * pszFilename )
{
m_Handle = CreateFile( pszFilename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL );
if( !IsValid() )return false;
GetFileSizeEx( m_Handle, &m_nSize );
LARGE_INTEGER li = { 0 };
SetFilePointerEx( m_Handle, li, &m_nPosition, FILE_CURRENT );
return true;
}
void Close()
{
if( IsValid() )
{
CloseHandle( m_Handle );
m_Handle = INVALID_HANDLE_VALUE;
}
}
LONGLONG GetSize64() const { return m_nSize.QuadPart;}
LONGLONG GetPosition64() const { return m_nPosition.QuadPart;}
LONGLONG SetPosition64( LONGLONG llNewPosition )
{
LONGLONG llOldPosition = m_nPosition.QuadPart;
LARGE_INTEGER liNewPosition = { 0 };
liNewPosition.QuadPart = llNewPosition;
SetFilePointerEx( m_Handle, liNewPosition, NULL, FILE_BEGIN );
m_nPosition = liNewPosition;
return llOldPosition;
}
DWORD GetSize() const { return m_nSize.LowPart;}
DWORD GetPosition() const { return m_nPosition.LowPart; }
DWORD SetPosition( DWORD dwNewPosition )
{
DWORD dwPos = m_nPosition.LowPart;
if( dwNewPosition > 0x7fffffff )
{
SetFilePointer( m_Handle, 0x7fffffff, NULL, FILE_BEGIN );
SetFilePointer( m_Handle, dwNewPosition - 0x7fffffff, NULL, FILE_BEGIN );
}
else
SetFilePointer( m_Handle, dwNewPosition, NULL, FILE_BEGIN );
m_nPosition.HighPart = 0;
m_nPosition.LowPart = dwNewPosition;
return dwPos;
}
bool IsValid() const { return m_Handle != INVALID_HANDLE_VALUE;}
HANDLE GetHandle() const { return m_Handle;}
protected:
LARGE_INTEGER m_nSize;
LARGE_INTEGER m_nPosition;
HANDLE m_Handle;
};
class CInputFileMapping
{
public:
CInputFileMapping() : m_Handle(NULL) {}
~CInputFileMapping() {
if( IsValid() )CloseHandle( m_Handle );
}
bool Attach( CInputFile & file )
{
m_Handle = CreateFileMapping( file.GetHandle(), NULL, PAGE_READONLY, 0, 0, NULL );
if( !IsValid() )return false;
return true;
}
bool IsValid() const { return m_Handle != NULL;}
HANDLE GetHandle() const { return m_Handle;}
protected:
HANDLE m_Handle;
};