1
0

filecore.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10. #include "stdafx.h"
  11. #include <SysUtils.hpp>
  12. ////////////////////////////////////////////////////////////////////////////
  13. // CFile implementation
  14. CFile::CFile()
  15. {
  16. m_hFile = (HANDLE) hFileNull;
  17. }
  18. CFile::~CFile()
  19. {
  20. if (m_hFile != (HANDLE)hFileNull)
  21. Close();
  22. }
  23. BOOL CFile::Open(const wchar_t * lpszFileName, UINT nOpenFlags)
  24. {
  25. m_hFile = (HANDLE)hFileNull;
  26. m_strFileName = ExpandFileName(lpszFileName);
  27. ASSERT(sizeof(HANDLE) == sizeof(UINT));
  28. ASSERT(shareCompat == 0);
  29. // map read/write mode
  30. ASSERT((modeRead|modeWrite|modeReadWrite) == 3);
  31. DWORD dwAccess = 0;
  32. switch (nOpenFlags & 3)
  33. {
  34. case modeRead:
  35. dwAccess = GENERIC_READ;
  36. break;
  37. case modeWrite:
  38. dwAccess = GENERIC_WRITE;
  39. break;
  40. case modeReadWrite:
  41. dwAccess = GENERIC_READ|GENERIC_WRITE;
  42. break;
  43. default:
  44. ASSERT(FALSE); // invalid share mode
  45. }
  46. // map share mode
  47. DWORD dwShareMode = 0;
  48. switch (nOpenFlags & 0x70) // map compatibility mode to exclusive
  49. {
  50. default:
  51. ASSERT(FALSE); // invalid share mode?
  52. case shareCompat:
  53. case shareExclusive:
  54. dwShareMode = 0;
  55. break;
  56. case shareDenyWrite:
  57. dwShareMode = FILE_SHARE_READ;
  58. break;
  59. case shareDenyRead:
  60. dwShareMode = FILE_SHARE_WRITE;
  61. break;
  62. case shareDenyNone:
  63. dwShareMode = FILE_SHARE_WRITE|FILE_SHARE_READ;
  64. break;
  65. }
  66. // map modeNoInherit flag
  67. SECURITY_ATTRIBUTES sa;
  68. sa.nLength = sizeof(sa);
  69. sa.lpSecurityDescriptor = NULL;
  70. sa.bInheritHandle = (nOpenFlags & modeNoInherit) == 0;
  71. // map creation flags
  72. DWORD dwCreateFlag;
  73. if (nOpenFlags & modeCreate)
  74. {
  75. if (nOpenFlags & modeNoTruncate)
  76. dwCreateFlag = OPEN_ALWAYS;
  77. else
  78. dwCreateFlag = CREATE_ALWAYS;
  79. }
  80. else
  81. dwCreateFlag = OPEN_EXISTING;
  82. // attempt file creation
  83. HANDLE hFile = ::CreateFile(lpszFileName, dwAccess, dwShareMode, &sa,
  84. dwCreateFlag, FILE_ATTRIBUTE_NORMAL, NULL);
  85. if (hFile == INVALID_HANDLE_VALUE)
  86. {
  87. return FALSE;
  88. }
  89. m_hFile = hFile;
  90. return TRUE;
  91. }
  92. UINT CFile::Read(void* lpBuf, UINT nCount)
  93. {
  94. ASSERT(m_hFile != (HANDLE)hFileNull);
  95. if (nCount == 0)
  96. return 0; // avoid Win32 "null-read"
  97. ASSERT(lpBuf != NULL);
  98. DWORD dwRead;
  99. if (!::ReadFile(m_hFile, lpBuf, nCount, &dwRead, NULL))
  100. CFileException::ThrowOsError((LONG)::GetLastError(), m_strFileName.c_str());
  101. return (UINT)dwRead;
  102. }
  103. void CFile::Write(const void* lpBuf, UINT nCount)
  104. {
  105. ASSERT(m_hFile != (HANDLE)hFileNull);
  106. if (nCount == 0)
  107. return; // avoid Win32 "null-write" option
  108. ASSERT(lpBuf != NULL);
  109. DWORD nWritten;
  110. if (!::WriteFile(m_hFile, lpBuf, nCount, &nWritten, NULL))
  111. CFileException::ThrowOsError((LONG)::GetLastError(), m_strFileName.c_str());
  112. // Win32s will not return an error all the time (usually DISK_FULL)
  113. if (nWritten != nCount)
  114. AfxThrowFileException(CFileException::diskFull, -1, m_strFileName.c_str());
  115. }
  116. void CFile::Close()
  117. {
  118. ASSERT(m_hFile != (HANDLE)hFileNull);
  119. BOOL bError = FALSE;
  120. if (m_hFile != (HANDLE)hFileNull)
  121. bError = !::CloseHandle(m_hFile);
  122. m_hFile = (HANDLE) hFileNull;
  123. m_strFileName = EmptyStr;
  124. if (bError)
  125. CFileException::ThrowOsError((LONG)::GetLastError());
  126. }
  127. /////////////////////////////////////////////////////////////////////////////