From d2affdf34f0e10f62f16e116ed35b2dfed196cfb Mon Sep 17 00:00:00 2001 From: assiduous Date: Thu, 23 Jan 2020 18:25:34 -0800 Subject: Added WindowsFileSystem::DeleteDirectory, BasicFileSystem::IsPathAbsolute functions --- Platforms/Basic/interface/BasicFileSystem.h | 2 ++ Platforms/Basic/src/BasicFileSystem.cpp | 14 +++++++++++++ Platforms/Win32/interface/Win32FileSystem.h | 3 ++- Platforms/Win32/src/Win32FileSystem.cpp | 31 ++++++++++++++++++++++++++--- 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/Platforms/Basic/interface/BasicFileSystem.h b/Platforms/Basic/interface/BasicFileSystem.h index 6e0172df..f3ebecd9 100644 --- a/Platforms/Basic/interface/BasicFileSystem.h +++ b/Platforms/Basic/interface/BasicFileSystem.h @@ -101,6 +101,8 @@ public: Diligent::String* Path, Diligent::String* Name); + static bool IsPathAbsolute(const Diligent::Char* strPath); + protected: static Diligent::String m_strWorkingDirectory; }; diff --git a/Platforms/Basic/src/BasicFileSystem.cpp b/Platforms/Basic/src/BasicFileSystem.cpp index f77dd454..2f5b303e 100644 --- a/Platforms/Basic/src/BasicFileSystem.cpp +++ b/Platforms/Basic/src/BasicFileSystem.cpp @@ -127,3 +127,17 @@ void BasicFileSystem::SplitFilePath(const Diligent::String& FullName, *Name = FullName; } } + +bool BasicFileSystem::IsPathAbsolute(const Diligent::Char* strPath) +{ + if (strPath == nullptr || strPath[0] == 0) + return false; + +#if PLATFORM_WIN32 || PLATFORM_UNIVERSAL_WINDOWS + return strPath[1] == ':' && (strPath[2] == '\\' || strPath[2] == '/'); +#elif PLATFORM_LINUX || PLATFORM_MACOS || PLATFORM_IOS || PLATFORM_ANDROID + return strPath[0] == '/'; +#else +# error Unknown platform. +#endif +} diff --git a/Platforms/Win32/interface/Win32FileSystem.h b/Platforms/Win32/interface/Win32FileSystem.h index 425eae27..95218860 100644 --- a/Platforms/Win32/interface/Win32FileSystem.h +++ b/Platforms/Win32/interface/Win32FileSystem.h @@ -49,8 +49,9 @@ public: static bool PathExists(const Diligent::Char* strPath); static bool CreateDirectory(const Diligent::Char* strPath); - static void ClearDirectory(const Diligent::Char* strPath); + static void ClearDirectory(const Diligent::Char* strPath, bool Recursive = false); static void DeleteFile(const Diligent::Char* strPath); + static void DeleteDirectory(const Diligent::Char* strPath); static std::vector> Search(const Diligent::Char* SearchPattern); diff --git a/Platforms/Win32/src/Win32FileSystem.cpp b/Platforms/Win32/src/Win32FileSystem.cpp index fc826693..61245fbb 100644 --- a/Platforms/Win32/src/Win32FileSystem.cpp +++ b/Platforms/Win32/src/Win32FileSystem.cpp @@ -131,9 +131,12 @@ static bool CreateDirectoryImpl(const Char* strPath) // Test all parent directories std::string DirectoryPath = strPath; std::string::size_type SlashPos = std::wstring::npos; + const auto SlashSym = WindowsFileSystem::GetSlashSymbol(); + WindowsFileSystem::CorrectSlashes(DirectoryPath, SlashSym); + do { - SlashPos = DirectoryPath.find('\\', (SlashPos != std::string::npos) ? SlashPos + 1 : 0); + SlashPos = DirectoryPath.find(SlashSym, (SlashPos != std::string::npos) ? SlashPos + 1 : 0); std::string ParentDir = (SlashPos != std::wstring::npos) ? DirectoryPath.substr(0, SlashPos) : DirectoryPath; if (!WindowsFileSystem::PathExists(ParentDir.c_str())) @@ -147,7 +150,7 @@ static bool CreateDirectoryImpl(const Char* strPath) return true; } -void WindowsFileSystem::ClearDirectory(const Char* strPath) +void WindowsFileSystem::ClearDirectory(const Char* strPath, bool Recursive) { WIN32_FIND_DATAA ffd; HANDLE hFind = INVALID_HANDLE_VALUE; @@ -168,12 +171,27 @@ void WindowsFileSystem::ClearDirectory(const Char* strPath) // List all the files in the directory do { - if (!(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) + if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) + { + if (Recursive) + { + // Skip '.' and anything that begins with '..' + if (!((ffd.cFileName[0] == '.' && ffd.cFileName[1] == 0) || (ffd.cFileName[0] == '.' && ffd.cFileName[1] == '.'))) + { + auto SubDirName = Directory + ffd.cFileName; + ClearDirectory(SubDirName.c_str()); + RemoveDirectoryA(SubDirName.c_str()); + } + } + } + else { auto FileName = Directory + ffd.cFileName; DeleteFileImpl(FileName.c_str()); } } while (FindNextFileA(hFind, &ffd) != 0); + + FindClose(hFind); } @@ -182,6 +200,13 @@ static void DeleteFileImpl(const Char* strPath) DeleteFileA(strPath); } +void WindowsFileSystem::DeleteDirectory(const Diligent::Char* strPath) +{ + ClearDirectory(strPath, true); + RemoveDirectoryA(strPath); +} + + bool WindowsFileSystem::PathExists(const Char* strPath) { return PathFileExistsA(strPath) != FALSE; -- cgit v1.2.3