Old fashion design for PC-104
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.

187 lines
4.7 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 01/01/2000
  5. // Last modification: 08/10/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: LgBitCodeFile.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of LgBitCodeFile.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "..\..\LgLib\Version.h"
  16. #include "LgBitCodeFile.h"
  17. CLgBitCodeFile::CLgBitCodeFile( INT nBase ) :
  18. m_nOpenFlags(0),
  19. m_nBase( nBase ),
  20. CFile()
  21. {
  22. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgBitCodeFile::CLgBitCodeFile(INT)" );
  23. InitBitCoder( nBase );
  24. }
  25. CLgBitCodeFile::CLgBitCodeFile( HANDLE hFile, INT nBase ) :
  26. m_nOpenFlags( 0 ),
  27. m_nBase( nBase ),
  28. #if _MFC_VER > 0x0600
  29. CFile( hFile )
  30. #else
  31. CFile( (int)hFile )
  32. #endif
  33. {
  34. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgBitCodeFile::CLgBitCodeFile(HANDLE,INT)" );
  35. InitBitCoder( nBase );
  36. }
  37. CLgBitCodeFile::CLgBitCodeFile( LPCTSTR lpszFileName, UINT nOpenFlags, INT nBase ) :
  38. m_nOpenFlags( 0 ),
  39. m_nBase( nBase ),
  40. CFile( lpszFileName, nOpenFlags )
  41. {
  42. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgBitCodeFile::CLgBitCodeFile(LPCTSTR,UINT,INT)" );
  43. InitBitCoder( nBase );
  44. }
  45. CLgBitCodeFile::~CLgBitCodeFile()
  46. {
  47. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgBitCodeFile::~CLgBitCodeFile" );
  48. }
  49. void CLgBitCodeFile::InitBitCoder( INT nBase )
  50. {
  51. m_nBase = nBase;
  52. m_bPortion = TRUE;
  53. m_BitCoder.SetBuffer( m_pBuffer );
  54. }
  55. UINT CLgBitCodeFile::Read( void* lpBuf, UINT nCount )
  56. {
  57. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgBitCodeFile::Read" );
  58. PBYTE pBuffer = (PBYTE)lpBuf;
  59. INT nTempCount = nCount;
  60. __try
  61. {
  62. while( nTempCount )
  63. {
  64. // get new portion
  65. if( m_bPortion )
  66. {
  67. #if _MFC_VER > 0x0600
  68. ULONGLONG dwRemain = CFile::GetLength() - CFile::GetPosition();
  69. #else
  70. INT dwRemain = (INT)( CFile::GetLength() - CFile::GetPosition() );
  71. #endif
  72. INT nToRead = (INT)( ( m_nBase <= dwRemain ) ? m_nBase : dwRemain );
  73. if( nToRead < 0 || nToRead > sizeof(m_pBuffer) )
  74. break;
  75. // read new portion
  76. if( !CFile::Read( m_pBuffer, nToRead ) )
  77. break;
  78. // restart encoder
  79. m_BitCoder.SetBuffer( m_pBuffer );
  80. }
  81. // decode portion
  82. DWORD dwValue = 0;
  83. m_bPortion = m_BitCoder.Decode24( &dwValue, m_nBase );
  84. // place into read buffer
  85. *pBuffer++ = (BYTE)dwValue;
  86. // update counter
  87. nTempCount--;
  88. }
  89. }
  90. __except( EXCEPTION_EXECUTE_HANDLER )
  91. {
  92. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgBitCodeFile::Read"), _T("Exception 0x%08X"), _exception_code() );
  93. }
  94. return nCount - nTempCount;
  95. }
  96. void CLgBitCodeFile::Write( const void* lpBuf, UINT nCount )
  97. {
  98. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgBitCodeFile::Write" );
  99. PBYTE pBuffer = (PBYTE)lpBuf;
  100. INT nTempCount = nCount;
  101. __try
  102. {
  103. while( nTempCount )
  104. {
  105. // encode portion
  106. DWORD dwValue = *pBuffer++;
  107. m_bPortion = m_BitCoder.Encode24( dwValue, m_nBase );
  108. // update counter
  109. nTempCount--;
  110. // write new portion
  111. if( m_bPortion )
  112. {
  113. // write coded data
  114. CFile::Write( m_pBuffer, m_nBase );
  115. // restart encoder
  116. m_BitCoder.SetBuffer( m_pBuffer );
  117. }
  118. }
  119. }
  120. __except( EXCEPTION_EXECUTE_HANDLER )
  121. {
  122. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgBitCodeFile::Write"), _T("Exception 0x%08X"), _exception_code() );
  123. }
  124. }
  125. ULONGLONG CLgBitCodeFile::Seek( LONGLONG lOff, UINT nFrom )
  126. {
  127. LPCTSTR pFrom = _T("unknown");
  128. switch( nFrom )
  129. {
  130. case CFile::begin: pFrom = _T("begin"); break;
  131. case CFile::end: pFrom = _T("end"); break;
  132. case CFile::current: pFrom = _T("current"); break;
  133. }
  134. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_WARNING, this, _T("CLgBitCodeFile::Seek"), _T("Offset %I64d, From %s, At %I64d"), lOff, pFrom, CFile::GetPosition() );
  135. return CFile::GetPosition();//CFile::Seek( lOff, nFrom );
  136. }
  137. BOOL CLgBitCodeFile::Open( LPCTSTR lpszFileName, UINT nOpenFlags, CFileException* pError )
  138. {
  139. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgBitCodeFile::Open" );
  140. if( !CFile::Open( lpszFileName, nOpenFlags, pError ) )
  141. return FALSE;
  142. // reinit bit coder
  143. InitBitCoder( m_nBase );
  144. // store flags for future use
  145. m_nOpenFlags = nOpenFlags;
  146. return TRUE;
  147. }
  148. void CLgBitCodeFile::Close()
  149. {
  150. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgBitCodeFile::Close" );
  151. // write coded data
  152. if( ( m_nOpenFlags & (CFile::modeWrite | CFile::modeCreate) ) && m_BitCoder.GetCount() )
  153. CFile::Write( m_pBuffer, m_BitCoder.GetCount() );
  154. // clear flags
  155. m_nOpenFlags = 0;
  156. CFile::Close();
  157. }