2008-09-18 03:15:49 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
|
2015-05-03 11:05:53 +00:00
|
|
|
#include <TChar.H>
|
|
|
|
|
2008-09-18 03:15:49 +00:00
|
|
|
CFile::CFile() :
|
|
|
|
m_hFile(INVALID_HANDLE_VALUE),
|
|
|
|
m_bCloseOnDelete(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CFile::CFile(HANDLE hFile) :
|
|
|
|
m_hFile(hFile),
|
|
|
|
m_bCloseOnDelete(true)
|
|
|
|
{
|
|
|
|
if (hFile == 0)
|
|
|
|
{
|
2015-01-27 05:07:44 +00:00
|
|
|
_ASSERTE(hFile != 0);
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CFile::~CFile()
|
|
|
|
{
|
|
|
|
if (m_hFile != INVALID_HANDLE_VALUE && m_bCloseOnDelete)
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-14 23:28:15 +00:00
|
|
|
CFile::CFile(LPCTSTR lpszFileName, ULONG nOpenFlags) :
|
2008-09-18 03:15:49 +00:00
|
|
|
m_hFile(INVALID_HANDLE_VALUE),
|
|
|
|
m_bCloseOnDelete(true)
|
|
|
|
{
|
|
|
|
Open(lpszFileName, nOpenFlags);
|
|
|
|
}
|
|
|
|
|
2009-12-28 22:22:50 +00:00
|
|
|
bool CFile::Open(LPCTSTR lpszFileName, ULONG nOpenFlags)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
|
|
|
if (!Close())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lpszFileName == NULL || _tcslen(lpszFileName) == 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_bCloseOnDelete = true;
|
|
|
|
m_hFile = INVALID_HANDLE_VALUE;
|
|
|
|
|
2009-12-28 22:22:50 +00:00
|
|
|
ULONG dwAccess = 0;
|
2008-09-18 03:15:49 +00:00
|
|
|
switch (nOpenFlags & 3)
|
|
|
|
{
|
|
|
|
case modeRead:
|
|
|
|
dwAccess = GENERIC_READ;
|
|
|
|
break;
|
|
|
|
case modeWrite:
|
|
|
|
dwAccess = GENERIC_WRITE;
|
|
|
|
break;
|
|
|
|
case modeReadWrite:
|
|
|
|
dwAccess = GENERIC_READ|GENERIC_WRITE;
|
|
|
|
break;
|
|
|
|
default:
|
2015-01-27 05:07:44 +00:00
|
|
|
_ASSERTE(false);
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// map share mode
|
2009-12-28 22:22:50 +00:00
|
|
|
ULONG dwShareMode = 0;
|
|
|
|
|
|
|
|
dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
|
2008-09-18 03:15:49 +00:00
|
|
|
if ((nOpenFlags & shareDenyWrite) == shareDenyWrite) { dwShareMode &= ~FILE_SHARE_WRITE; }
|
|
|
|
if ((nOpenFlags & shareDenyRead) == shareDenyRead) { dwShareMode &= ~FILE_SHARE_READ; }
|
|
|
|
if ((nOpenFlags & shareExclusive) == shareExclusive) { dwShareMode = 0; }
|
|
|
|
|
|
|
|
// map modeNoInherit flag
|
|
|
|
SECURITY_ATTRIBUTES sa;
|
|
|
|
sa.nLength = sizeof(sa);
|
|
|
|
sa.lpSecurityDescriptor = NULL;
|
|
|
|
sa.bInheritHandle = (nOpenFlags & modeNoInherit) == 0;
|
|
|
|
|
|
|
|
// map creation flags
|
2009-12-28 22:22:50 +00:00
|
|
|
ULONG dwCreateFlag = 0;
|
2008-09-18 03:15:49 +00:00
|
|
|
if (nOpenFlags & modeCreate)
|
|
|
|
{
|
|
|
|
if (nOpenFlags & modeNoTruncate)
|
|
|
|
dwCreateFlag = OPEN_ALWAYS;
|
|
|
|
else
|
|
|
|
dwCreateFlag = CREATE_ALWAYS;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
dwCreateFlag = OPEN_EXISTING;
|
|
|
|
|
|
|
|
// attempt file creation
|
2015-02-16 06:19:11 +00:00
|
|
|
HANDLE hFile = ::CreateFile(lpszFileName, dwAccess, dwShareMode, &sa,
|
2008-09-18 03:15:49 +00:00
|
|
|
dwCreateFlag, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
|
|
|
{ //#define ERROR_PATH_NOT_FOUND 3L
|
2009-12-28 22:22:50 +00:00
|
|
|
//ULONG err = GetLastError();
|
2008-09-18 03:15:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
m_hFile = hFile;
|
|
|
|
m_bCloseOnDelete = TRUE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CFile::Close()
|
|
|
|
{
|
|
|
|
bool bError = true;
|
|
|
|
if (m_hFile != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
bError = !::CloseHandle(m_hFile);
|
|
|
|
}
|
|
|
|
m_hFile = INVALID_HANDLE_VALUE;
|
|
|
|
m_bCloseOnDelete = false;
|
|
|
|
return bError;
|
|
|
|
}
|
|
|
|
|
2009-12-28 22:22:50 +00:00
|
|
|
ULONG CFile::SeekToEnd ( void )
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
|
|
|
return Seek(0, CFile::end);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CFile::SeekToBegin ( void )
|
|
|
|
{
|
|
|
|
Seek(0, CFile::begin);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CFile::IsOpen( void ) const
|
|
|
|
{
|
|
|
|
return m_hFile != INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CFile::Flush()
|
|
|
|
{
|
|
|
|
if (m_hFile == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ::FlushFileBuffers(m_hFile) != 0;
|
|
|
|
}
|
|
|
|
|
2009-12-28 22:22:50 +00:00
|
|
|
bool CFile::Write(const void* lpBuf, ULONG nCount)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
|
|
|
if (nCount == 0)
|
|
|
|
{
|
|
|
|
return true; // avoid Win32 "null-write" option
|
|
|
|
}
|
|
|
|
|
2009-12-28 22:22:50 +00:00
|
|
|
ULONG nWritten = 0;
|
2008-09-18 03:15:49 +00:00
|
|
|
if (!::WriteFile(m_hFile, lpBuf, nCount, &nWritten, NULL))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nWritten != nCount)
|
|
|
|
{
|
|
|
|
// Win32s will not return an error all the time (usually DISK_FULL)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-12-28 22:22:50 +00:00
|
|
|
ULONG CFile::Read(void* lpBuf, ULONG nCount)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
|
|
|
if (nCount == 0)
|
|
|
|
{
|
|
|
|
return 0; // avoid Win32 "null-read"
|
|
|
|
}
|
|
|
|
|
2009-12-28 22:22:50 +00:00
|
|
|
ULONG dwRead = 0;
|
2008-09-18 03:15:49 +00:00
|
|
|
if (!::ReadFile(m_hFile, lpBuf, nCount, &dwRead, NULL))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return (UINT)dwRead;
|
|
|
|
}
|
|
|
|
|
|
|
|
long CFile::Seek(long lOff, SeekPosition nFrom)
|
|
|
|
{
|
2009-12-28 22:22:50 +00:00
|
|
|
ULONG dwNew = ::SetFilePointer(m_hFile, lOff, NULL, (ULONG)nFrom);
|
|
|
|
if (dwNew == (ULONG)-1)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dwNew;
|
|
|
|
}
|
|
|
|
|
2009-12-28 22:22:50 +00:00
|
|
|
ULONG CFile::GetPosition() const
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
|
|
|
return ::SetFilePointer(m_hFile, 0, NULL, FILE_CURRENT);
|
|
|
|
}
|
|
|
|
|
2009-12-28 22:22:50 +00:00
|
|
|
bool CFile::SetLength(ULONG dwNewLen)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
|
|
|
Seek((LONG)dwNewLen, begin);
|
|
|
|
|
|
|
|
return ::SetEndOfFile(m_hFile) != 0;
|
|
|
|
}
|
|
|
|
|
2009-12-28 22:22:50 +00:00
|
|
|
ULONG CFile::GetLength() const
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
|
|
|
return GetFileSize(m_hFile,0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CFile::SetEndOfFile()
|
|
|
|
{
|
|
|
|
return ::SetEndOfFile(m_hFile) != 0;
|
|
|
|
}
|
2009-12-28 22:22:50 +00:00
|
|
|
|