|
|
//----------------------------------------------------------------------------
// Laser Guard Ltd.
// Copyright � 2000-2002. All Rights Reserved.
// Version 1.0 01/01/2000
// Last modification: 08/10/2003
// PROJECT:
// SUBSYSTEM:
// FILE: LgBitCodeFile.cpp
// AUTHOR: Sergey Chizhevskiy
// OVERVIEW
// ~~~~~~~~
// Source file of LgBitCodeFile.cpp
//----------------------------------------------------------------------------
#include "stdafx.h"
#include "..\..\LgLib\Version.h"
#include "LgBitCodeFile.h"
CLgBitCodeFile::CLgBitCodeFile( INT nBase ) : m_nOpenFlags(0), m_nBase( nBase ), CFile(){ LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgBitCodeFile::CLgBitCodeFile(INT)" );
InitBitCoder( nBase );}
CLgBitCodeFile::CLgBitCodeFile( HANDLE hFile, INT nBase ) : m_nOpenFlags( 0 ), m_nBase( nBase ),#if _MFC_VER > 0x0600
CFile( hFile ) #else
CFile( (int)hFile )#endif
{ LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgBitCodeFile::CLgBitCodeFile(HANDLE,INT)" );
InitBitCoder( nBase ); }
CLgBitCodeFile::CLgBitCodeFile( LPCTSTR lpszFileName, UINT nOpenFlags, INT nBase ) : m_nOpenFlags( 0 ), m_nBase( nBase ), CFile( lpszFileName, nOpenFlags ) { LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgBitCodeFile::CLgBitCodeFile(LPCTSTR,UINT,INT)" );
InitBitCoder( nBase ); }
CLgBitCodeFile::~CLgBitCodeFile(){ LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgBitCodeFile::~CLgBitCodeFile" );}
void CLgBitCodeFile::InitBitCoder( INT nBase ) { m_nBase = nBase; m_bPortion = TRUE; m_BitCoder.SetBuffer( m_pBuffer );}
UINT CLgBitCodeFile::Read( void* lpBuf, UINT nCount ){ LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgBitCodeFile::Read" ); PBYTE pBuffer = (PBYTE)lpBuf; INT nTempCount = nCount;
__try { while( nTempCount ) { // get new portion
if( m_bPortion ) {#if _MFC_VER > 0x0600
ULONGLONG dwRemain = CFile::GetLength() - CFile::GetPosition();#else
INT dwRemain = (INT)( CFile::GetLength() - CFile::GetPosition() );#endif
INT nToRead = (INT)( ( m_nBase <= dwRemain ) ? m_nBase : dwRemain ); if( nToRead < 0 || nToRead > sizeof(m_pBuffer) ) break;
// read new portion
if( !CFile::Read( m_pBuffer, nToRead ) ) break; // restart encoder
m_BitCoder.SetBuffer( m_pBuffer ); }
// decode portion
DWORD dwValue = 0; m_bPortion = m_BitCoder.Decode24( &dwValue, m_nBase );
// place into read buffer
*pBuffer++ = (BYTE)dwValue;
// update counter
nTempCount--; } } __except( EXCEPTION_EXECUTE_HANDLER ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgBitCodeFile::Read"), _T("Exception 0x%08X"), _exception_code() ); }
return nCount - nTempCount;}
void CLgBitCodeFile::Write( const void* lpBuf, UINT nCount ){ LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgBitCodeFile::Write" );
PBYTE pBuffer = (PBYTE)lpBuf; INT nTempCount = nCount; __try { while( nTempCount ) { // encode portion
DWORD dwValue = *pBuffer++; m_bPortion = m_BitCoder.Encode24( dwValue, m_nBase );
// update counter
nTempCount--;
// write new portion
if( m_bPortion ) { // write coded data
CFile::Write( m_pBuffer, m_nBase ); // restart encoder
m_BitCoder.SetBuffer( m_pBuffer ); } } } __except( EXCEPTION_EXECUTE_HANDLER ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgBitCodeFile::Write"), _T("Exception 0x%08X"), _exception_code() ); }}
ULONGLONG CLgBitCodeFile::Seek( LONGLONG lOff, UINT nFrom ){ LPCTSTR pFrom = _T("unknown"); switch( nFrom ) { case CFile::begin: pFrom = _T("begin"); break; case CFile::end: pFrom = _T("end"); break; case CFile::current: pFrom = _T("current"); break; }
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_WARNING, this, _T("CLgBitCodeFile::Seek"), _T("Offset %I64d, From %s, At %I64d"), lOff, pFrom, CFile::GetPosition() ); return CFile::GetPosition();//CFile::Seek( lOff, nFrom );
}
BOOL CLgBitCodeFile::Open( LPCTSTR lpszFileName, UINT nOpenFlags, CFileException* pError ){ LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgBitCodeFile::Open" );
if( !CFile::Open( lpszFileName, nOpenFlags, pError ) ) return FALSE;
// reinit bit coder
InitBitCoder( m_nBase );
// store flags for future use
m_nOpenFlags = nOpenFlags; return TRUE;}
void CLgBitCodeFile::Close(){ LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgBitCodeFile::Close" );
// write coded data
if( ( m_nOpenFlags & (CFile::modeWrite | CFile::modeCreate) ) && m_BitCoder.GetCount() ) CFile::Write( m_pBuffer, m_BitCoder.GetCount() );
// clear flags
m_nOpenFlags = 0;
CFile::Close();}
|