mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-20 11:05:54 +00:00
* Remove binding, dispatch handling and exit hooks in favor of std alternatives; remove crash-handling infrastructure, error message framework, system-specific process handling and Objective-C interop helpers. * Simplify Beast function profiling * Simplify beast::Time interface * Simplify beast::String interface * Simplify beast::File interface
373 lines
12 KiB
C++
373 lines
12 KiB
C++
//------------------------------------------------------------------------------
|
|
/*
|
|
This file is part of Beast: https://github.com/vinniefalco/Beast
|
|
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
|
|
|
|
Portions of this file are from JUCE.
|
|
Copyright (c) 2013 - Raw Material Software Ltd.
|
|
Please visit http://www.juce.com
|
|
|
|
Permission to use, copy, modify, and/or distribute this software for any
|
|
purpose with or without fee is hereby granted, provided that the above
|
|
copyright notice and this permission notice appear in all copies.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
*/
|
|
//==============================================================================
|
|
|
|
namespace beast
|
|
{
|
|
|
|
#ifndef INVALID_FILE_ATTRIBUTES
|
|
#define INVALID_FILE_ATTRIBUTES ((DWORD) -1)
|
|
#endif
|
|
|
|
//==============================================================================
|
|
namespace WindowsFileHelpers
|
|
{
|
|
static_assert (sizeof (ULARGE_INTEGER) == sizeof (FILETIME),
|
|
"The FILETIME structure format has been modified.");
|
|
|
|
DWORD getAtts (const String& path)
|
|
{
|
|
return GetFileAttributes (path.toWideCharPointer());
|
|
}
|
|
|
|
std::int64_t fileTimeToTime (const FILETIME* const ft)
|
|
{
|
|
return (std::int64_t) ((reinterpret_cast<const ULARGE_INTEGER*> (ft)->QuadPart - 116444736000000000LL) / 10000);
|
|
}
|
|
|
|
FILETIME* timeToFileTime (const std::int64_t time, FILETIME* const ft) noexcept
|
|
{
|
|
if (time <= 0)
|
|
return nullptr;
|
|
|
|
reinterpret_cast<ULARGE_INTEGER*> (ft)->QuadPart = (ULONGLONG) (time * 10000 + 116444736000000000LL);
|
|
return ft;
|
|
}
|
|
|
|
String getDriveFromPath (String path)
|
|
{
|
|
if (path.isNotEmpty() && path[1] == ':' && path[2] == 0)
|
|
path << '\\';
|
|
|
|
const size_t numBytes = CharPointer_UTF16::getBytesRequiredFor (path.getCharPointer()) + 4;
|
|
HeapBlock<WCHAR> pathCopy;
|
|
pathCopy.calloc (numBytes, 1);
|
|
path.copyToUTF16 (pathCopy, numBytes);
|
|
|
|
if (PathStripToRoot (pathCopy))
|
|
path = static_cast <const WCHAR*> (pathCopy);
|
|
|
|
return path;
|
|
}
|
|
|
|
unsigned int getWindowsDriveType (const String& path)
|
|
{
|
|
return GetDriveType (getDriveFromPath (path).toWideCharPointer());
|
|
}
|
|
|
|
File getSpecialFolderPath (int type)
|
|
{
|
|
WCHAR path [MAX_PATH + 256];
|
|
|
|
if (SHGetSpecialFolderPath (0, path, type, FALSE))
|
|
return File (String (path));
|
|
|
|
return File::nonexistent ();
|
|
}
|
|
|
|
File getModuleFileName (HINSTANCE moduleHandle)
|
|
{
|
|
WCHAR dest [MAX_PATH + 256];
|
|
dest[0] = 0;
|
|
GetModuleFileName (moduleHandle, dest, (DWORD) numElementsInArray (dest));
|
|
return File (String (dest));
|
|
}
|
|
|
|
Result getResultForLastError()
|
|
{
|
|
TCHAR messageBuffer [256] = { 0 };
|
|
|
|
FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
nullptr, GetLastError(), MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
messageBuffer, (DWORD) numElementsInArray (messageBuffer) - 1, nullptr);
|
|
|
|
return Result::fail (String (messageBuffer));
|
|
}
|
|
}
|
|
|
|
//==============================================================================
|
|
const beast_wchar File::separator = '\\';
|
|
const String File::separatorString ("\\");
|
|
|
|
|
|
//==============================================================================
|
|
bool File::exists() const
|
|
{
|
|
return fullPath.isNotEmpty()
|
|
&& WindowsFileHelpers::getAtts (fullPath) != INVALID_FILE_ATTRIBUTES;
|
|
}
|
|
|
|
bool File::existsAsFile() const
|
|
{
|
|
return fullPath.isNotEmpty()
|
|
&& (WindowsFileHelpers::getAtts (fullPath) & FILE_ATTRIBUTE_DIRECTORY) == 0;
|
|
}
|
|
|
|
bool File::isDirectory() const
|
|
{
|
|
const DWORD attr = WindowsFileHelpers::getAtts (fullPath);
|
|
return ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) && (attr != INVALID_FILE_ATTRIBUTES);
|
|
}
|
|
|
|
//==============================================================================
|
|
bool File::deleteFile() const
|
|
{
|
|
if (! exists())
|
|
return true;
|
|
|
|
return isDirectory() ? RemoveDirectory (fullPath.toWideCharPointer()) != 0
|
|
: DeleteFile (fullPath.toWideCharPointer()) != 0;
|
|
}
|
|
|
|
Result File::createDirectoryInternal (const String& fileName) const
|
|
{
|
|
return CreateDirectory (fileName.toWideCharPointer(), 0) ? Result::ok()
|
|
: WindowsFileHelpers::getResultForLastError();
|
|
}
|
|
|
|
//==============================================================================
|
|
std::int64_t beast_fileSetPosition (void* handle, std::int64_t pos)
|
|
{
|
|
LARGE_INTEGER li;
|
|
li.QuadPart = pos;
|
|
li.LowPart = SetFilePointer ((HANDLE) handle, (LONG) li.LowPart, &li.HighPart, FILE_BEGIN); // (returns -1 if it fails)
|
|
return li.QuadPart;
|
|
}
|
|
|
|
void FileInputStream::openHandle()
|
|
{
|
|
HANDLE h = CreateFile (file.getFullPathName().toWideCharPointer(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
|
|
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, 0);
|
|
|
|
if (h != INVALID_HANDLE_VALUE)
|
|
fileHandle = (void*) h;
|
|
else
|
|
status = WindowsFileHelpers::getResultForLastError();
|
|
}
|
|
|
|
void FileInputStream::closeHandle()
|
|
{
|
|
CloseHandle ((HANDLE) fileHandle);
|
|
}
|
|
|
|
size_t FileInputStream::readInternal (void* buffer, size_t numBytes)
|
|
{
|
|
if (fileHandle != 0)
|
|
{
|
|
DWORD actualNum = 0;
|
|
if (! ReadFile ((HANDLE) fileHandle, buffer, (DWORD) numBytes, &actualNum, 0))
|
|
status = WindowsFileHelpers::getResultForLastError();
|
|
|
|
return (size_t) actualNum;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
//==============================================================================
|
|
void FileOutputStream::openHandle()
|
|
{
|
|
HANDLE h = CreateFile (file.getFullPathName().toWideCharPointer(), GENERIC_WRITE, FILE_SHARE_READ, 0,
|
|
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
|
|
|
|
if (h != INVALID_HANDLE_VALUE)
|
|
{
|
|
LARGE_INTEGER li;
|
|
li.QuadPart = 0;
|
|
li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_END);
|
|
|
|
if (li.LowPart != INVALID_SET_FILE_POINTER)
|
|
{
|
|
fileHandle = (void*) h;
|
|
currentPosition = li.QuadPart;
|
|
return;
|
|
}
|
|
}
|
|
|
|
status = WindowsFileHelpers::getResultForLastError();
|
|
}
|
|
|
|
void FileOutputStream::closeHandle()
|
|
{
|
|
CloseHandle ((HANDLE) fileHandle);
|
|
}
|
|
|
|
std::ptrdiff_t FileOutputStream::writeInternal (const void* buffer, size_t numBytes)
|
|
{
|
|
if (fileHandle != nullptr)
|
|
{
|
|
DWORD actualNum = 0;
|
|
if (! WriteFile ((HANDLE) fileHandle, buffer, (DWORD) numBytes, &actualNum, 0))
|
|
status = WindowsFileHelpers::getResultForLastError();
|
|
|
|
return (std::ptrdiff_t) actualNum;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
void FileOutputStream::flushInternal()
|
|
{
|
|
if (fileHandle != nullptr)
|
|
if (! FlushFileBuffers ((HANDLE) fileHandle))
|
|
status = WindowsFileHelpers::getResultForLastError();
|
|
}
|
|
|
|
Result FileOutputStream::truncate()
|
|
{
|
|
if (fileHandle == nullptr)
|
|
return status;
|
|
|
|
flush();
|
|
return SetEndOfFile ((HANDLE) fileHandle) ? Result::ok()
|
|
: WindowsFileHelpers::getResultForLastError();
|
|
}
|
|
|
|
//==============================================================================
|
|
|
|
std::int64_t File::getSize() const
|
|
{
|
|
WIN32_FILE_ATTRIBUTE_DATA attributes;
|
|
|
|
if (GetFileAttributesEx (fullPath.toWideCharPointer(), GetFileExInfoStandard, &attributes))
|
|
return (((std::int64_t) attributes.nFileSizeHigh) << 32) | attributes.nFileSizeLow;
|
|
|
|
return 0;
|
|
}
|
|
|
|
//==============================================================================
|
|
File File::getSpecialLocation (const SpecialLocationType type)
|
|
{
|
|
int csidlType = 0;
|
|
|
|
switch (type)
|
|
{
|
|
case userHomeDirectory: csidlType = CSIDL_PROFILE; break;
|
|
case userDocumentsDirectory: csidlType = CSIDL_PERSONAL; break;
|
|
case userDesktopDirectory: csidlType = CSIDL_DESKTOP; break;
|
|
case userApplicationDataDirectory: csidlType = CSIDL_APPDATA; break;
|
|
case commonApplicationDataDirectory: csidlType = CSIDL_COMMON_APPDATA; break;
|
|
case commonDocumentsDirectory: csidlType = CSIDL_COMMON_DOCUMENTS; break;
|
|
case globalApplicationsDirectory: csidlType = CSIDL_PROGRAM_FILES; break;
|
|
case userMusicDirectory: csidlType = 0x0d; /*CSIDL_MYMUSIC*/ break;
|
|
case userMoviesDirectory: csidlType = 0x0e; /*CSIDL_MYVIDEO*/ break;
|
|
case userPicturesDirectory: csidlType = 0x27; /*CSIDL_MYPICTURES*/ break;
|
|
|
|
case tempDirectory:
|
|
{
|
|
WCHAR dest [2048];
|
|
dest[0] = 0;
|
|
GetTempPath ((DWORD) numElementsInArray (dest), dest);
|
|
return File (String (dest));
|
|
}
|
|
|
|
default:
|
|
bassertfalse; // unknown type?
|
|
return File::nonexistent ();
|
|
}
|
|
|
|
return WindowsFileHelpers::getSpecialFolderPath (csidlType);
|
|
}
|
|
|
|
//==============================================================================
|
|
File File::getCurrentWorkingDirectory()
|
|
{
|
|
WCHAR dest [MAX_PATH + 256];
|
|
dest[0] = 0;
|
|
GetCurrentDirectory ((DWORD) numElementsInArray (dest), dest);
|
|
return File (String (dest));
|
|
}
|
|
|
|
//==============================================================================
|
|
class DirectoryIterator::NativeIterator::Pimpl
|
|
{
|
|
public:
|
|
Pimpl (const File& directory, const String& wildCard)
|
|
: directoryWithWildCard (File::addTrailingSeparator (directory.getFullPathName()) + wildCard),
|
|
handle (INVALID_HANDLE_VALUE)
|
|
{
|
|
}
|
|
|
|
Pimpl (Pimpl const&) = delete;
|
|
Pimpl& operator= (Pimpl const&) = delete;
|
|
|
|
~Pimpl()
|
|
{
|
|
if (handle != INVALID_HANDLE_VALUE)
|
|
FindClose (handle);
|
|
}
|
|
|
|
bool next (String& filenameFound,
|
|
bool* const isDir, bool* const isHidden, std::int64_t* const fileSize,
|
|
Time* const modTime, Time* const creationTime, bool* const isReadOnly)
|
|
{
|
|
using namespace WindowsFileHelpers;
|
|
WIN32_FIND_DATA findData;
|
|
|
|
if (handle == INVALID_HANDLE_VALUE)
|
|
{
|
|
handle = FindFirstFile (directoryWithWildCard.toWideCharPointer(), &findData);
|
|
|
|
if (handle == INVALID_HANDLE_VALUE)
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
if (FindNextFile (handle, &findData) == 0)
|
|
return false;
|
|
}
|
|
|
|
filenameFound = findData.cFileName;
|
|
|
|
if (isDir != nullptr) *isDir = ((findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
|
|
if (isHidden != nullptr) *isHidden = ((findData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) != 0);
|
|
if (isReadOnly != nullptr) *isReadOnly = ((findData.dwFileAttributes & FILE_ATTRIBUTE_READONLY) != 0);
|
|
if (fileSize != nullptr) *fileSize = findData.nFileSizeLow + (((std::int64_t) findData.nFileSizeHigh) << 32);
|
|
if (modTime != nullptr) *modTime = Time (fileTimeToTime (&findData.ftLastWriteTime));
|
|
if (creationTime != nullptr) *creationTime = Time (fileTimeToTime (&findData.ftCreationTime));
|
|
|
|
return true;
|
|
}
|
|
|
|
private:
|
|
const String directoryWithWildCard;
|
|
HANDLE handle;
|
|
};
|
|
|
|
DirectoryIterator::NativeIterator::NativeIterator (const File& directory, const String& wildCard)
|
|
: pimpl (new DirectoryIterator::NativeIterator::Pimpl (directory, wildCard))
|
|
{
|
|
}
|
|
|
|
DirectoryIterator::NativeIterator::~NativeIterator()
|
|
{
|
|
}
|
|
|
|
bool DirectoryIterator::NativeIterator::next (String& filenameFound,
|
|
bool* const isDir, bool* const isHidden, std::int64_t* const fileSize,
|
|
Time* const modTime, Time* const creationTime, bool* const isReadOnly)
|
|
{
|
|
return pimpl->next (filenameFound, isDir, isHidden, fileSize, modTime, creationTime, isReadOnly);
|
|
}
|
|
|
|
} // beast
|