Leaked source code of windows server 2003
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.

230 lines
5.7 KiB

  1. //------------------------------------------------------------------------------------------
  2. // CFileManager.cpp
  3. //
  4. // A managing map files.
  5. //
  6. // Created By: aarayas
  7. //
  8. // History: 01/12/2001
  9. //
  10. //------------------------------------------------------------------------------------------
  11. #include "CFileManager.h"
  12. //------------------------------------------------------------------------------------------
  13. // CFileManager::CFileManager
  14. //
  15. // Initialize a CFileManager
  16. //
  17. //--------------------------------------------------------------------------- aarayas ------
  18. CFileManager::CFileManager()
  19. {
  20. m_fFileOpen = false;
  21. m_hFile = NULL;
  22. m_hFileMap = NULL;
  23. m_dwFileSize1 = 0;
  24. m_dwFileSize2 = 0;
  25. m_pMem = NULL;
  26. }
  27. //------------------------------------------------------------------------------------------
  28. // CFileManager::CFileManager
  29. //
  30. // Initialize a CFileManager
  31. //
  32. //--------------------------------------------------------------------------- aarayas ------
  33. CFileManager::~CFileManager()
  34. {
  35. if (m_fFileOpen)
  36. {
  37. Close();
  38. }
  39. }
  40. //------------------------------------------------------------------------------------------
  41. // CFileManager::Load
  42. //
  43. // Load file.
  44. //
  45. //--------------------------------------------------------------------------- aarayas ------
  46. bool CFileManager::Load(const WCHAR* pwszFileName, void** pMem, unsigned int* size)
  47. {
  48. // Open files.
  49. m_hFile = CMN_CreateFileW(pwszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  50. if (m_hFile != INVALID_HANDLE_VALUE)
  51. {
  52. m_dwFileSize1 = CMN_GetFileSize(m_hFile, &m_dwFileSize2);
  53. if (m_dwFileSize1 != 0 && m_dwFileSize1 != 0xFFFFFFFF)
  54. {
  55. m_hFileMap = CMN_CreateFileMapping(m_hFile, NULL, PAGE_READONLY, 0, 0, NULL);
  56. if (m_hFileMap != NULL)
  57. {
  58. m_pMem = CMN_MapViewOfFile(m_hFileMap, FILE_MAP_READ, 0, 0, 0);
  59. if (m_pMem != NULL)
  60. {
  61. // Create new size.
  62. *size = m_dwFileSize1;
  63. *pMem = m_pMem;
  64. m_fFileOpen = true;
  65. return true;
  66. }
  67. CMN_CloseHandle(m_hFileMap);
  68. }
  69. }
  70. CMN_CloseHandle(m_hFile);
  71. }
  72. m_hFileMap = NULL;
  73. m_hFile = NULL;
  74. m_dwFileSize1 = 0;
  75. m_dwFileSize2 = 0;
  76. return false;
  77. }
  78. //------------------------------------------------------------------------------------------
  79. // CFileManager::Close
  80. //
  81. // Close file.
  82. //
  83. //--------------------------------------------------------------------------- aarayas ------
  84. bool CFileManager::Close()
  85. {
  86. if (m_fFileOpen)
  87. {
  88. m_fFileOpen = false;
  89. if (m_pMem && !CMN_UnmapViewOfFile(m_pMem))
  90. {
  91. return false;
  92. }
  93. m_pMem = 0;
  94. if (m_hFileMap && !CMN_CloseHandle(m_hFileMap))
  95. {
  96. return false;
  97. }
  98. m_hFileMap = 0;
  99. if (!CMN_CloseHandle(m_hFile))
  100. {
  101. return false;
  102. }
  103. m_hFile = 0;
  104. m_dwFileSize1 = 0;
  105. m_dwFileSize2 = 0;
  106. }
  107. return true;
  108. }
  109. //------------------------------------------------------------------------------------------
  110. // CFileManager::CreateFile
  111. //
  112. // Create Files
  113. //
  114. //--------------------------------------------------------------------------- aarayas ------
  115. bool CFileManager::CreateFile(const WCHAR* pwszFileName, bool fWrite)
  116. {
  117. bool fRet = true;
  118. if (fWrite)
  119. m_hFile = CMN_CreateFileW( pwszFileName, GENERIC_WRITE, 0, (LPSECURITY_ATTRIBUTES)0,CREATE_ALWAYS,
  120. FILE_ATTRIBUTE_NORMAL, (HANDLE)0);
  121. else
  122. m_hFile = CMN_CreateFileW( pwszFileName, GENERIC_READ, 0, (LPSECURITY_ATTRIBUTES)0,OPEN_EXISTING,
  123. FILE_ATTRIBUTE_NORMAL, (HANDLE)0);
  124. if (m_hFile == INVALID_HANDLE_VALUE || m_hFile == 0)
  125. {
  126. m_hFile = 0;
  127. m_fFileOpen = false;
  128. fRet = false;
  129. }
  130. else
  131. m_fFileOpen = true;
  132. return fRet;
  133. }
  134. //------------------------------------------------------------------------------------------
  135. // CFileManager::MovePointer
  136. //
  137. // Move file pointer
  138. // Parameter:
  139. // dwMoveMethod
  140. // FILE_BEGIN - The starting point is zero or the beginning of the file.
  141. // FILE_CURRENT - The starting point is the current value of the file pointer.
  142. // FILE_END - The starting point is the current end-of-file position.
  143. //
  144. //--------------------------------------------------------------------------- aarayas ------
  145. bool CFileManager::MovePointer(DWORD dwMoveMethod)
  146. {
  147. bool fRet = false;
  148. if (m_fFileOpen && m_hFile)
  149. {
  150. if (SetFilePointer( m_hFile, // handle of file
  151. 0, // number of bytes to move file pointer
  152. 0, // address of high-order word of distance to move
  153. dwMoveMethod // how to move
  154. ) == 0)
  155. {
  156. fRet = true;
  157. }
  158. }
  159. return fRet;
  160. }
  161. //------------------------------------------------------------------------------------------
  162. // CFileManager::Write
  163. //
  164. // Write to files.
  165. //
  166. //--------------------------------------------------------------------------- aarayas ------
  167. bool CFileManager::Write(const void* lpBuffer,DWORD nNumberOfBytesToWrite)
  168. {
  169. bool fRet = false;
  170. if (m_fFileOpen && m_hFile)
  171. {
  172. DWORD cb;
  173. if (WriteFile(m_hFile, (LPCVOID)lpBuffer, nNumberOfBytesToWrite, &cb, (LPOVERLAPPED)0))
  174. {
  175. if (cb == nNumberOfBytesToWrite)
  176. fRet = true;
  177. }
  178. }
  179. return fRet;
  180. }
  181. //------------------------------------------------------------------------------------------
  182. // CFileManager::Read
  183. //
  184. // Write to files.
  185. //
  186. //--------------------------------------------------------------------------- aarayas ------
  187. bool CFileManager::Read(void* lpBuffer,DWORD nNumberOfBytesToRead)
  188. {
  189. bool fRet = false;
  190. if (m_fFileOpen && m_hFile)
  191. {
  192. DWORD cb;
  193. if (ReadFile(m_hFile, (void*)lpBuffer, nNumberOfBytesToRead, &cb, (LPOVERLAPPED)0))
  194. {
  195. if (cb == nNumberOfBytesToRead)
  196. fRet = true;
  197. }
  198. }
  199. return fRet;
  200. }