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.

482 lines
12 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: 29/11/2001
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: FileGroup.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of FileGroup.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "..\..\LgLib\Version.h"
  16. #include "FileGroup.h"
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char THIS_FILE[]=__FILE__;
  20. #define new DEBUG_NEW
  21. #endif
  22. CFileGroup::CFileGroup()
  23. {
  24. m_nTempIndex = 0UL;
  25. m_FileName = _T( "groupfile.tmp" );
  26. }
  27. CFileGroup::~CFileGroup()
  28. {
  29. /*
  30. while( m_nTempIndex )
  31. {
  32. TCHAR pName[MAX_PATH];
  33. _sntprintf( (LPTSTR) pName, MAX_PATH, _T( "%08X.TMP" ), m_nTempIndex-- );
  34. ::DeleteFile( pName );
  35. }
  36. */
  37. }
  38. BOOL CFileGroup::Add( LPCTSTR pFileName )
  39. {
  40. Remove( pFileName );
  41. DWORD dwLenght = 0;
  42. LPBYTE pBuffer = NULL;
  43. BOOL bResult = FALSE;
  44. CFile file;
  45. if( file.Open( pFileName, CFile::modeRead ) )
  46. {
  47. dwLenght = file.GetLength();
  48. pBuffer = new BYTE[ dwLenght ];
  49. if( pBuffer )
  50. {
  51. file.Read( pBuffer, dwLenght );
  52. bResult = TRUE;
  53. }
  54. file.Close();
  55. }
  56. if( bResult )
  57. {
  58. if( file.Open( m_FileName, CFile::modeReadWrite | CFile::modeCreate | CFile::modeNoTruncate ) )
  59. {
  60. DWORD dwCurrentLenght = file.GetLength();
  61. if( dwCurrentLenght == 0 )
  62. {
  63. // prepare file header
  64. FILE_HEADER fh;
  65. _tcscpy( fh.pSignature, FG_SIGNATURE );
  66. fh.nOffset.QuadPart = sizeof( FILE_HEADER ) + dwLenght;
  67. fh.nCount.QuadPart = 1;
  68. // prepare file descriptor entry
  69. FILE_DESCRIPTOR fd;
  70. _tcscpy( fd.pFileName, pFileName );
  71. fd.nOffset.QuadPart = sizeof( FILE_HEADER );
  72. fd.nSize.QuadPart = dwLenght;
  73. // write file header
  74. file.Write( &fh, sizeof( FILE_HEADER ) );
  75. // write file contents
  76. file.Write( pBuffer, dwLenght );
  77. //write file descriptor
  78. file.Write( &fd, sizeof( FILE_DESCRIPTOR ) );
  79. }
  80. else
  81. {
  82. FILE_HEADER fh;
  83. // read file header
  84. file.Read( &fh, sizeof( FILE_HEADER ) );
  85. // check signature
  86. if( !_tcsncmp( fh.pSignature, FG_SIGNATURE, FG_SIGNATURE_SIZE ) )
  87. {
  88. DWORD dwTableSize = (DWORD)fh.nCount.QuadPart * sizeof( FILE_DESCRIPTOR );
  89. FILE_DESCRIPTOR *pTable = (FILE_DESCRIPTOR*) new BYTE[ dwTableSize ];
  90. if( pTable )
  91. {
  92. try
  93. {
  94. // seek to table of contents
  95. file.Seek( (DWORD)fh.nOffset.QuadPart, CFile::begin );
  96. // load table of contents
  97. if( file.Read( pTable, dwTableSize ) == dwTableSize )
  98. {
  99. try
  100. {
  101. // prepare new file descriptor
  102. FILE_DESCRIPTOR fd;
  103. fd.nOffset = fh.nOffset;
  104. fd.nSize.QuadPart = dwLenght;
  105. _tcscpy( fd.pFileName, pFileName );
  106. // seek to table of contents again
  107. file.Seek( (DWORD)fh.nOffset.QuadPart, CFile::begin );
  108. // write new file on this place
  109. file.Write( pBuffer, dwLenght );
  110. // update file header offset and size
  111. fh.nOffset.QuadPart = file.GetPosition();
  112. fh.nCount.QuadPart ++;
  113. // write table after this new file
  114. file.Write( pTable, dwTableSize );
  115. // write new table entry
  116. file.Write( &fd, sizeof( FILE_DESCRIPTOR ) );
  117. try
  118. {
  119. // seek to begin of file
  120. file.SeekToBegin();
  121. // write updated file header
  122. file.Write( &fh, sizeof( FILE_HEADER ) );
  123. }
  124. catch( CFileException *e )
  125. {
  126. e;
  127. }
  128. }
  129. catch( CFileException *e )
  130. {
  131. e;
  132. }
  133. }
  134. }
  135. catch( CFileException *e )
  136. {
  137. e;
  138. }
  139. delete pTable;
  140. }
  141. }
  142. }
  143. file.Close();
  144. }
  145. delete [] pBuffer;
  146. }
  147. return bResult;
  148. }
  149. BOOL CFileGroup::Remove( LPCTSTR pFileName )
  150. {
  151. CFile file;
  152. if( file.Open( m_FileName, CFile::modeReadWrite ) )
  153. {
  154. FILE_HEADER fh;
  155. // read file header
  156. file.Read( &fh, sizeof( FILE_HEADER ) );
  157. // check signature
  158. if( !_tcsncmp( fh.pSignature, FG_SIGNATURE, FG_SIGNATURE_SIZE ) )
  159. {
  160. DWORD dwTableSize = (DWORD)fh.nCount.QuadPart * sizeof( FILE_DESCRIPTOR );
  161. FILE_DESCRIPTOR *pTable = (FILE_DESCRIPTOR*) new BYTE[ dwTableSize ];
  162. if( pTable )
  163. {
  164. try
  165. {
  166. // seek to table of contents
  167. file.Seek( (DWORD)fh.nOffset.QuadPart, CFile::begin );
  168. // load table of contents
  169. if( file.Read( pTable, dwTableSize ) == dwTableSize )
  170. {
  171. try
  172. {
  173. // placeholder for deleting entry
  174. FILE_DESCRIPTOR fd;
  175. // searching entry with such name and then shift remainig
  176. BOOL bFound = FALSE;
  177. LARGE_INTEGER nSize = {0};
  178. for( DWORD nIndex = 0; nIndex < fh.nCount.QuadPart; nIndex++ )
  179. {
  180. if( !_tcscmp( pTable[nIndex].pFileName, pFileName ) )
  181. {
  182. // store deleting entry
  183. fd = pTable[nIndex];
  184. bFound = TRUE;
  185. }
  186. // shifting remaining entries
  187. if( bFound && ( nIndex + 1 ) < fh.nCount.QuadPart )
  188. {
  189. pTable[ nIndex ] = pTable[ nIndex + 1 ];
  190. // shifting offset of entry
  191. pTable[ nIndex ].nOffset.QuadPart -= fd.nSize.QuadPart;
  192. // calculating shifting block size
  193. nSize.QuadPart += pTable[ nIndex ].nSize.QuadPart;
  194. }
  195. }
  196. if( bFound )
  197. {
  198. // update header
  199. fh.nCount.QuadPart --;
  200. fh.nOffset.QuadPart -= fd.nSize.QuadPart;
  201. // shift remainig files
  202. const nBlockSize = (1<<16);
  203. LPBYTE pBuffer = (LPBYTE) new BYTE [ nBlockSize ];
  204. if( pBuffer )
  205. {
  206. // prepare offsets
  207. DWORD dwOffsetDest = (DWORD)fd.nOffset.QuadPart;
  208. DWORD dwOffsetSrc = (DWORD)(fd.nOffset.QuadPart + fd.nSize.QuadPart);
  209. // number of round blocks
  210. DWORD nBlockCount = (DWORD)( nSize.QuadPart / nBlockSize );
  211. if( nBlockCount )
  212. {
  213. // shifting round blocks
  214. for( nIndex = 0; nIndex < nBlockCount; nIndex ++ )
  215. {
  216. try
  217. {
  218. file.Seek( dwOffsetSrc, CFile::begin );
  219. file.Read( pBuffer, nBlockSize );
  220. file.Seek( dwOffsetDest, CFile::begin );
  221. file.Write( pBuffer, nBlockSize );
  222. dwOffsetSrc += nBlockSize;
  223. dwOffsetDest += nBlockSize;
  224. }
  225. catch( CFileException *e )
  226. {
  227. e;
  228. }
  229. }
  230. }
  231. // shifting last block
  232. DWORD nLastBlockSize = (DWORD)( nSize.QuadPart % nBlockSize );
  233. if( nLastBlockSize )
  234. {
  235. try
  236. {
  237. file.Seek( dwOffsetSrc, CFile::begin );
  238. file.Read( pBuffer, nLastBlockSize );
  239. file.Seek( dwOffsetDest, CFile::begin );
  240. file.Write( pBuffer, nLastBlockSize );
  241. }
  242. catch( CFileException *e )
  243. {
  244. e;
  245. }
  246. }
  247. delete [] pBuffer;
  248. // write table of contents
  249. file.Write( pTable, (DWORD)( fh.nCount.QuadPart * sizeof( FILE_DESCRIPTOR ) ) );
  250. try
  251. {
  252. // seek to begin of file
  253. file.SeekToBegin();
  254. // write updated file header
  255. file.Write( &fh, sizeof( FILE_HEADER ) );
  256. }
  257. catch( CFileException *e )
  258. {
  259. e;
  260. }
  261. file.SetLength( (DWORD)( nSize.QuadPart + sizeof( FILE_HEADER ) + sizeof( FILE_DESCRIPTOR ) * fh.nCount.QuadPart ) );
  262. }
  263. }
  264. }
  265. catch( CFileException *e )
  266. {
  267. e;
  268. }
  269. }
  270. }
  271. catch( CFileException *e )
  272. {
  273. e;
  274. }
  275. delete pTable;
  276. }
  277. }
  278. file.Close();
  279. }
  280. return TRUE;
  281. }
  282. BOOL CFileGroup::GetTemp( LPCTSTR pFileName, LPCTSTR pNewFileName, INT nNewFileNameSize )
  283. {
  284. BOOL bResult = FALSE;
  285. CFile file;
  286. if( file.Open( m_FileName, CFile::modeReadWrite ) )
  287. {
  288. FILE_HEADER fh;
  289. // read file header
  290. file.Read( &fh, sizeof( FILE_HEADER ) );
  291. // check signature
  292. if( !_tcsncmp( fh.pSignature, FG_SIGNATURE, FG_SIGNATURE_SIZE ) )
  293. {
  294. DWORD dwTableSize = (DWORD)fh.nCount.QuadPart * sizeof( FILE_DESCRIPTOR );
  295. FILE_DESCRIPTOR *pTable = (FILE_DESCRIPTOR*) new BYTE[ dwTableSize ];
  296. if( pTable )
  297. {
  298. try
  299. {
  300. // seek to table of contents
  301. file.Seek( (DWORD)fh.nOffset.QuadPart, CFile::begin );
  302. // load table of contents
  303. if( file.Read( pTable, dwTableSize ) == dwTableSize )
  304. {
  305. try
  306. {
  307. // placeholder for deleting entry
  308. FILE_DESCRIPTOR fd;
  309. // searching entry with such name and then shift remainig
  310. for( DWORD nIndex = 0; nIndex < fh.nCount.QuadPart; nIndex++ )
  311. {
  312. if( !_tcscmp( pTable[nIndex].pFileName, pFileName ) )
  313. {
  314. // store deleting entry
  315. fd = pTable[nIndex];
  316. bResult = TRUE;
  317. }
  318. }
  319. if( bResult )
  320. {
  321. // prepare temp filename
  322. _sntprintf( (LPTSTR) pNewFileName, nNewFileNameSize, _T( "%08X.TMP" ), m_nTempIndex++ );
  323. // create new file
  324. CFile temp;
  325. if( temp.Open( pNewFileName, CFile::modeCreate | CFile::modeWrite ) )
  326. {
  327. const nBlockSize = (1<<16);
  328. LPBYTE pBuffer = (LPBYTE) new BYTE [ nBlockSize ];
  329. if( pBuffer )
  330. {
  331. // prepare offset
  332. DWORD dwOffsetSrc = (DWORD)fd.nOffset.QuadPart;
  333. try
  334. {
  335. // seek to position of file
  336. file.Seek( dwOffsetSrc, CFile::begin );
  337. // number of round blocks
  338. DWORD nBlockCount = (DWORD)( fd.nSize.QuadPart / nBlockSize );
  339. if( nBlockCount )
  340. {
  341. // copying round blocks
  342. for( nIndex = 0; nIndex < nBlockCount; nIndex ++ )
  343. {
  344. file.Read( pBuffer, nBlockSize );
  345. temp.Write( pBuffer, nBlockSize );
  346. dwOffsetSrc += nBlockSize;
  347. }
  348. }
  349. // copying last block
  350. DWORD nLastBlockSize = (DWORD)( fd.nSize.QuadPart % nBlockSize );
  351. if( nLastBlockSize )
  352. {
  353. file.Read( pBuffer, nLastBlockSize );
  354. temp.Write( pBuffer, nLastBlockSize );
  355. }
  356. }
  357. catch( CFileException *e )
  358. {
  359. e;
  360. }
  361. delete [] pBuffer;
  362. }
  363. temp.Close();
  364. }
  365. }
  366. }
  367. catch( CFileException *e )
  368. {
  369. e;
  370. }
  371. }
  372. }
  373. catch( CFileException *e )
  374. {
  375. e;
  376. }
  377. delete pTable;
  378. }
  379. }
  380. file.Close();
  381. }
  382. if( !bResult )
  383. {
  384. _tcsncpy( (LPTSTR)pNewFileName, (LPTSTR)pFileName, nNewFileNameSize );
  385. }
  386. return bResult;
  387. }
  388. void CFileGroup::SetFileName( LPCTSTR pFileName, bool bCopy )
  389. {
  390. if( bCopy )
  391. ::CopyFile( m_FileName, pFileName, FALSE );
  392. m_FileName = pFileName;
  393. }
  394. HANDLE CFileGroup::LoadImage( LPCTSTR pFileName, UINT uType, int cxDesired, int cyDesired, UINT fuLoad )
  395. {
  396. HANDLE hResult = NULL;
  397. TCHAR pBuffer[MAX_PATH];
  398. BOOL bIsTemp = GetTemp( pFileName, pBuffer, MAX_PATH );
  399. hResult = ::LoadImage( NULL, pBuffer, uType, cxDesired, cyDesired, fuLoad );
  400. if( bIsTemp )
  401. DeleteFile( pBuffer );
  402. return hResult;
  403. }