#pragma region CPL License /* Nuclex Native Framework Copyright (C) 2002-2021 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #pragma endregion // CPL License #ifndef NUCLEX_STORAGE_FILESYSTEM_WINDOWS_WINDOWSFILEAPI_H #define NUCLEX_STORAGE_FILESYSTEM_WINDOWS_WINDOWSFILEAPI_H #include "Nuclex/Storage/Config.h" #if defined(NUCLEX_STORAGE_WIN32) #include "../../Helpers/WindowsApi.h" #include // std::uint8_t, std::size_t namespace Nuclex { namespace Storage { namespace FileSystem { namespace Windows { // ------------------------------------------------------------------------------------------- // /// Wraps the Windows file system API /// /// /// This is just a small helper class that reduces the amount of boilerplate code /// required when calling the file system API functions from Windows, such as /// checking result codes and transforming paths from UTF-8 to UTF-16 stored in /// wchar_ts of non-standard 2 byte size. /// /// /// It is not intended to hide operating system details or make this API platform /// neutral (the File and Container classes do that), so depending on the amount /// of noise required by the file system APIs, only some methods will be wrapped here. /// /// class WindowsFileApi { /// Creates the specified directory /// Path to the directory that will be created public: static void CreateDirectory(const std::string &path); /// Creates all directories down to the specified path /// Directory that will be created recursively public: static void CreateDirectoryRecursively(const std::string &directory); /// Deletes a directory and everything in it /// Path of the directory that will be deleted public: static void DeleteDirectory(const std::string &path); /// Deletes a file /// Path of the file that will be deleted /// True if the file was deleted, false if it didn't exist public: static bool DeleteFile(const std::string &path); /// Moves or renames a file /// Current path of the file that will be moved /// New path the file will become accessible under public: static void MoveFile(const std::string &path, const std::string &newPath); /// Determines the location of the system's temp directory /// The path to the system's temp directory public: static std::string GetTempPath(); /// Obtains the full path of the specified module /// /// Handle of the module whose path will be determined, nullptr for executable /// /// The full path to the specified module public: static std::string GetModuleFileName(HMODULE moduleHandle = nullptr); /// Returns the current working directory of the process /// The process' current working directory public: static std::string GetCurrentWorkingDirectory(); /// /// Retrieves the extended attributes of the file in the specified path /// /// Path of the file whose attributes will be retrieved /// Receives the attributes of the file /// /// True if the attributes were retrieved, false if the file doesn't exist /// public: static bool GetFileAttributesEx( const std::string &path, WIN32_FILE_ATTRIBUTE_DATA &fileAttributeData ); /// Finds the first file matching the specified search mask /// Search mask controlling which files will be found /// Receives the first found file or directory /// The handle of the file search or INVALID_HANDLE_VALUE if none found public: static HANDLE FindFirstFile( const std::string &searchMask, WIN32_FIND_DATAW &findData ); /// Advances to the next file in a file search /// Search handle returned by FindFirstFile() /// Receives the next found file or directory /// True if there was a next file, otherwise false public: static bool FindNextFile(HANDLE searchHandle, WIN32_FIND_DATAW &findData); /// Closes the specified search handle /// Handle of the search that will be closed /// /// Whether to throw an exception if the search handle cannot be closed /// public: static void CloseSearch(HANDLE searchHandle, bool throwOnError = true); /// Resolves the normalized, absolute path to the specified path /// Path that will be followed and normalized /// The absolute path of the specified path's ultimate target public: static std::string GetFullPathName(const std::string &path); /// Creates all directories down to the specified path /// Directory that will be created recursively /// /// The provided directory must not be terminated with a path separator. /// private: static void createDirectoryRecursively(const std::wstring &directory); /// Recursively deletes the specified directory and all its contents /// Absolute path of the directory that will be deleted /// /// The provided directory must not be terminated with a path separator. /// private: static void deleteDirectoryRecursively(const std::wstring &directory); }; // ------------------------------------------------------------------------------------------- // /// Closes a search handle upon destruction class SearchScope { /// Initializes the search handle closer /// Search handle that will be closed public: SearchScope(::HANDLE searchHandle) : searchHandle(searchHandle) {} /// Closes the search handle public: ~SearchScope() { const bool throwOnError = false; WindowsFileApi::CloseSearch(this->searchHandle, throwOnError); } /// Search handle that will be closed upon destruction private: ::HANDLE searchHandle; }; // ------------------------------------------------------------------------------------------- // }}}} // namespace Nuclex::Storage::FileSystem::Windows #endif // defined(NUCLEX_STORAGE_WIN32) #endif // NUCLEX_STORAGE_FILESYSTEM_WINDOWS_WINDOWSFILEAPI_H