1
0
mirror of https://github.com/winscp/winscp.git synced 2026-10-24 02:28:55 +03:00
Files
winscp/libs/mfc/source/filecore.cpp
Martin Prikryl 693f5da37c Removing CFileException MFC class
Originally because it seemed that it was causing problems with 64-bit build, which wasn't true in the end. But it's a good move anyway.

Source commit: 2d2d341d8e98f31692c1beca8445899e289ee0cb
2026-04-18 01:11:47 +02:00

156 lines
3.6 KiB
C++

// This is a part of the Microsoft Foundation Classes C++ library.
// Copyright (C) 1992-1998 Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Foundation Classes Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding the
// Microsoft Foundation Classes product.
#include "stdafx.h"
#include <SysUtils.hpp>
////////////////////////////////////////////////////////////////////////////
// CFile implementation
CFile::CFile()
{
m_hFile = (HANDLE) hFileNull;
}
CFile::~CFile()
{
if (m_hFile != (HANDLE)hFileNull)
Close();
}
BOOL CFile::Open(const wchar_t * lpszFileName, UINT nOpenFlags)
{
m_hFile = (HANDLE)hFileNull;
m_AdditionalInfo = L"\r\n" + ExpandFileName(lpszFileName);
ASSERT(sizeof(HANDLE) == sizeof(UINT));
ASSERT(shareCompat == 0);
// map read/write mode
ASSERT((modeRead|modeWrite|modeReadWrite) == 3);
DWORD dwAccess = 0;
switch (nOpenFlags & 3)
{
case modeRead:
dwAccess = GENERIC_READ;
break;
case modeWrite:
dwAccess = GENERIC_WRITE;
break;
case modeReadWrite:
dwAccess = GENERIC_READ|GENERIC_WRITE;
break;
default:
ASSERT(FALSE); // invalid share mode
}
// map share mode
DWORD dwShareMode = 0;
switch (nOpenFlags & 0x70) // map compatibility mode to exclusive
{
default:
ASSERT(FALSE); // invalid share mode?
case shareCompat:
case shareExclusive:
dwShareMode = 0;
break;
case shareDenyWrite:
dwShareMode = FILE_SHARE_READ;
break;
case shareDenyRead:
dwShareMode = FILE_SHARE_WRITE;
break;
case shareDenyNone:
dwShareMode = FILE_SHARE_WRITE|FILE_SHARE_READ;
break;
}
// map modeNoInherit flag
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(sa);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = (nOpenFlags & modeNoInherit) == 0;
// map creation flags
DWORD dwCreateFlag;
if (nOpenFlags & modeCreate)
{
if (nOpenFlags & modeNoTruncate)
dwCreateFlag = OPEN_ALWAYS;
else
dwCreateFlag = CREATE_ALWAYS;
}
else
dwCreateFlag = OPEN_EXISTING;
// attempt file creation
HANDLE hFile = ::CreateFile(lpszFileName, dwAccess, dwShareMode, &sa,
dwCreateFlag, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
return FALSE;
}
m_hFile = hFile;
return TRUE;
}
UINT CFile::Read(void* lpBuf, UINT nCount)
{
ASSERT(m_hFile != (HANDLE)hFileNull);
if (nCount == 0)
return 0; // avoid Win32 "null-read"
ASSERT(lpBuf != NULL);
DWORD dwRead;
if (!::ReadFile(m_hFile, lpBuf, nCount, &dwRead, NULL))
RaiseLastOSError(GetLastError(), m_AdditionalInfo);
return (UINT)dwRead;
}
void CFile::Write(const void* lpBuf, UINT nCount)
{
ASSERT(m_hFile != (HANDLE)hFileNull);
if (nCount == 0)
return; // avoid Win32 "null-write" option
ASSERT(lpBuf != NULL);
DWORD nWritten;
if (!::WriteFile(m_hFile, lpBuf, nCount, &nWritten, NULL))
RaiseLastOSError(GetLastError(), m_AdditionalInfo);
// Win32s will not return an error all the time (usually DISK_FULL)
if (nWritten != nCount)
RaiseLastOSError(ERROR_DISK_FULL, m_AdditionalInfo);
}
void CFile::Close()
{
ASSERT(m_hFile != (HANDLE)hFileNull);
BOOL bError = FALSE;
if (m_hFile != (HANDLE)hFileNull)
bError = !::CloseHandle(m_hFile);
m_hFile = (HANDLE) hFileNull;
UnicodeString AdditionalInfo = m_AdditionalInfo;
m_AdditionalInfo = EmptyStr;
if (bError)
RaiseLastOSError(GetLastError(), AdditionalInfo);
}
/////////////////////////////////////////////////////////////////////////////