📄 file_win32.cpp
字号:
//
// File_WIN32.cpp
//
// $Id: //poco/Main/Foundation/src/File_WIN32.cpp#5 $
//
// Copyright (c) 2004, Guenter Obiltschnig/Applied Informatics.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Redistributions in any form must be accompanied by information on
// how to obtain complete source code for this software and any
// accompanying software that uses this software. The source code
// must either be included in the distribution or be available for no
// more than the cost of distribution plus a nominal fee, and must be
// freely redistributable under reasonable conditions. For an
// executable file, complete source code means the source code for all
// modules it contains. It does not include source code for modules or
// files that typically accompany the major components of the operating
// system on which the executable file runs.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
#include "Foundation/File_WIN32.h"
#include "Foundation/Exception.h"
#include <windows.h>
Foundation_BEGIN
class FileHandle
{
public:
FileHandle(const std::string& path, DWORD access, DWORD share, DWORD disp)
{
_h = CreateFile(path.c_str(), access, share, 0, disp, 0, 0);
if (!_h) FileImp::handleError(path);
}
~FileHandle()
{
if (_h) CloseHandle(_h);
}
HANDLE get() const
{
return _h;
}
private:
HANDLE _h;
};
FileImp::FileImp()
{
}
FileImp::FileImp(const std::string& path): _path(path)
{
std::string::size_type n = _path.size();
if (n > 0 && (_path[n - 1] == '\\' || _path[n - 1] == '/'))
_path.resize(n - 1);
}
FileImp::~FileImp()
{
}
void FileImp::setPathImp(const std::string& path)
{
_path = path;
}
bool FileImp::existsImp() const
{
poco_assert (!_path.empty());
DWORD attr = GetFileAttributes(_path.c_str());
if (attr == 0xFFFFFFFF)
{
switch (GetLastError())
{
case ERROR_FILE_NOT_FOUND:
case ERROR_PATH_NOT_FOUND:
case ERROR_NOT_READY:
case ERROR_INVALID_DRIVE:
return false;
default:
handleError(_path);
}
}
return true;
}
bool FileImp::canReadImp() const
{
poco_assert (!_path.empty());
DWORD attr = GetFileAttributes(_path.c_str());
if (attr == 0xFFFFFFFF)
{
switch (GetLastError())
{
case ERROR_ACCESS_DENIED:
return false;
default:
handleError(_path);
}
}
return true;
}
bool FileImp::canWriteImp() const
{
poco_assert (!_path.empty());
DWORD attr = GetFileAttributes(_path.c_str());
if (attr == 0xFFFFFFFF)
handleError(_path);
return (attr & FILE_ATTRIBUTE_READONLY) == 0;
}
bool FileImp::isFileImp() const
{
poco_assert (!_path.empty());
DWORD attr = GetFileAttributes(_path.c_str());
if (attr == 0xFFFFFFFF)
handleError(_path);
return (attr & FILE_ATTRIBUTE_DIRECTORY) == 0;
}
bool FileImp::isDirectoryImp() const
{
poco_assert (!_path.empty());
DWORD attr = GetFileAttributes(_path.c_str());
if (attr == 0xFFFFFFFF)
handleError(_path);
return (attr & FILE_ATTRIBUTE_DIRECTORY) != 0;
}
Timestamp FileImp::createdImp() const
{
poco_assert (!_path.empty());
WIN32_FILE_ATTRIBUTE_DATA fad;
if (GetFileAttributesEx(_path.c_str(), GetFileExInfoStandard, &fad) == 0)
handleError(_path);
return Timestamp::fromFileTimeNP(fad.ftCreationTime.dwLowDateTime, fad.ftCreationTime.dwHighDateTime);
}
Timestamp FileImp::getLastModifiedImp() const
{
poco_assert (!_path.empty());
WIN32_FILE_ATTRIBUTE_DATA fad;
if (GetFileAttributesEx(_path.c_str(), GetFileExInfoStandard, &fad) == 0)
handleError(_path);
return Timestamp::fromFileTimeNP(fad.ftLastWriteTime.dwLowDateTime, fad.ftLastWriteTime.dwHighDateTime);
}
void FileImp::setLastModifiedImp(const Timestamp& ts)
{
poco_assert (!_path.empty());
UInt32 low;
UInt32 high;
ts.toFileTimeNP(low, high);
FILETIME ft;
ft.dwLowDateTime = low;
ft.dwHighDateTime = high;
FileHandle fh(_path, FILE_ALL_ACCESS, FILE_SHARE_WRITE, OPEN_EXISTING);
if (SetFileTime(fh.get(), 0, &ft, &ft) == 0)
handleError(_path);
}
FileImp::FileSizeImp FileImp::getSizeImp() const
{
poco_assert (!_path.empty());
WIN32_FILE_ATTRIBUTE_DATA fad;
if (GetFileAttributesEx(_path.c_str(), GetFileExInfoStandard, &fad) == 0)
handleError(_path);
LARGE_INTEGER li;
li.LowPart = fad.nFileSizeLow;
li.HighPart = fad.nFileSizeHigh;
return li.QuadPart;
}
void FileImp::setSizeImp(FileSizeImp size)
{
poco_assert (!_path.empty());
FileHandle fh(_path, GENERIC_WRITE, FILE_SHARE_WRITE, OPEN_EXISTING);
LARGE_INTEGER li;
li.QuadPart = size;
if (SetFilePointer(fh.get(), li.LowPart, &li.HighPart, FILE_BEGIN) == -1)
handleError(_path);
if (SetEndOfFile(fh.get()) == 0)
handleError(_path);
}
void FileImp::setWriteableImp(bool flag)
{
poco_assert (!_path.empty());
DWORD attr = GetFileAttributes(_path.c_str());
if (attr == -1)
handleError(_path);
if (flag)
attr &= ~FILE_ATTRIBUTE_READONLY;
else
attr |= FILE_ATTRIBUTE_READONLY;
if (SetFileAttributes(_path.c_str(), attr) == 0)
handleError(_path);
}
void FileImp::copyToImp(const std::string& path) const
{
poco_assert (!_path.empty());
if (CopyFile(_path.c_str(), path.c_str(), FALSE) == 0)
handleError(_path);
}
void FileImp::renameToImp(const std::string& path)
{
poco_assert (!_path.empty());
if (MoveFile(_path.c_str(), path.c_str()) == 0)
handleError(_path);
}
void FileImp::removeImp()
{
poco_assert (!_path.empty());
if (isDirectoryImp())
{
if (RemoveDirectory(_path.c_str()) == 0)
handleError(_path);
}
else
{
if (DeleteFile(_path.c_str()) == 0)
handleError(_path);
}
}
bool FileImp::createFileImp()
{
poco_assert (!_path.empty());
HANDLE hFile = CreateFile(_path.c_str(), GENERIC_WRITE, 0, 0, CREATE_NEW, 0, 0);
if (hFile)
{
CloseHandle(hFile);
return true;
}
else if (GetLastError() == ERROR_ALREADY_EXISTS)
return false;
else
handleError(_path);
return false;
}
bool FileImp::createDirectoryImp()
{
poco_assert (!_path.empty());
if (existsImp() && isDirectoryImp())
return false;
if (CreateDirectory(_path.c_str(), 0) == 0)
handleError(_path);
return true;
}
void FileImp::handleError(const std::string& path)
{
switch (GetLastError())
{
case ERROR_FILE_NOT_FOUND:
throw FileNotFoundException(path);
case ERROR_PATH_NOT_FOUND:
case ERROR_BAD_NETPATH:
case ERROR_CANT_RESOLVE_FILENAME:
case ERROR_INVALID_DRIVE:
throw PathNotFoundException(path);
case ERROR_ACCESS_DENIED:
throw FileAccessDeniedException(path);
case ERROR_ALREADY_EXISTS:
case ERROR_FILE_EXISTS:
throw FileExistsException(path);
case ERROR_INVALID_NAME:
case ERROR_DIRECTORY:
case ERROR_FILENAME_EXCED_RANGE:
case ERROR_BAD_PATHNAME:
throw PathSyntaxException(path);
case ERROR_FILE_READ_ONLY:
throw FileReadOnlyException(path);
case ERROR_CANNOT_MAKE:
throw CreateFileException(path);
case ERROR_DIR_NOT_EMPTY:
throw FileException("directory not empty", path);
default:
throw FileException(path);
}
}
Foundation_END
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -