From 2a2296685ec21b76e77ccd4d56de85ea24d211aa Mon Sep 17 00:00:00 2001 From: Karutoh Date: Sat, 29 Jun 2024 23:38:27 -0700 Subject: [PATCH] Fixed Directory for Windows. --- CMakeLists.txt | 7 ++- include/ehs/io/Directory.h | 4 +- include/ehs/io/Directory_W32.h | 16 +++++++ src/io/Directory_LNX.cpp | 6 +-- src/io/Directory_W32.cpp | 87 ++++++++++++++++++++++++++++++++++ src/io/UsbBase.cpp | 2 - 6 files changed, 111 insertions(+), 11 deletions(-) create mode 100644 include/ehs/io/Directory_W32.h create mode 100644 src/io/Directory_W32.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 1dd8539..ead05fa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -164,8 +164,6 @@ set(EHS_SOURCES include/ehs/io/mdl/MdlCodec.h include/ehs/io/UsbBase.h src/io/UsbBase.cpp - include/ehs/io/Usb_LNX.h - src/io/Usb_LNX.cpp include/ehs/db/DbTable.h include/ehs/db/DbObject.h include/ehs/db/DbVar.h @@ -177,8 +175,6 @@ set(EHS_SOURCES src/db/DbTable.cpp include/ehs/io/BaseDirectory.h src/io/BaseDirectory.cpp - include/ehs/io/Directory_LNX.h - src/io/Directory_LNX.cpp include/ehs/io/Directory.h ) @@ -196,6 +192,7 @@ if (IS_OS_WINDOWS) src/io/Window_W32.cpp include/ehs/io/Window_W32.h src/io/COM.cpp include/ehs/io/COM.h src/system/CPU_MSVC_AMD64.asm src/HRNG_MSVC.asm src/Math_MSVC_AMD64.asm + src/io/Directory_W32.cpp include/ehs/io/Directory_W32.h ) elseif (IS_OS_LINUX) list(APPEND EHS_SOURCES @@ -210,6 +207,8 @@ elseif (IS_OS_LINUX) src/io/audio/AudioDevice_ALSA.cpp include/ehs/io/audio/AudioDevice_ALSA.h src/system/FileSystem.cpp include/ehs/system/FileSystem.h src/system/User.cpp include/ehs/system/User.h + src/io/Directory_LNX.cpp include/ehs/io/Directory_LNX.h + src/io/Usb_LNX.cpp include/ehs/io/Usb_LNX.h ) set(LINUX_WINDOW_SYSTEM "Wayland" CACHE STRING "Linux Window System") diff --git a/include/ehs/io/Directory.h b/include/ehs/io/Directory.h index 9d48339..df34830 100644 --- a/include/ehs/io/Directory.h +++ b/include/ehs/io/Directory.h @@ -3,7 +3,7 @@ #include "ehs/system/OS.h" #if defined(EHS_OS_WINDOWS) -#include "Directory_W32.h" + #include "Directory_W32.h" #elif defined(EHS_OS_LINUX) -#include "Directory_LNX.h" + #include "Directory_LNX.h" #endif \ No newline at end of file diff --git a/include/ehs/io/Directory_W32.h b/include/ehs/io/Directory_W32.h new file mode 100644 index 0000000..ca0adba --- /dev/null +++ b/include/ehs/io/Directory_W32.h @@ -0,0 +1,16 @@ +#pragma once + +#include "BaseDirectory.h" + +namespace ehs +{ + class Directory : public BaseDirectory + { + public: + static Array GetAllFiles(const Str_8 &dir); + + static void CreateRecursive(Str_8 dir); + + static void Create(const Str_8 &dir); + }; +} \ No newline at end of file diff --git a/src/io/Directory_LNX.cpp b/src/io/Directory_LNX.cpp index f488b99..312923d 100644 --- a/src/io/Directory_LNX.cpp +++ b/src/io/Directory_LNX.cpp @@ -13,14 +13,14 @@ namespace ehs if (!dir.Size()) { - EHS_LOG_INT(LogType::WARN, 2, "The given directory was empty."); + EHS_LOG_INT(LogType::WARN, 1, "The given directory was empty."); return result; } DIR* hdl = opendir(dir); if (!hdl) { - EHS_LOG_INT(LogType::ERR, 3, "Failed to open directory, \"" + dir + "\"."); + EHS_LOG_INT(LogType::ERR, 2, "Failed to open directory, \"" + dir + "\"."); return result; } @@ -31,7 +31,7 @@ namespace ehs if (closedir(hdl) == -1) { - EHS_LOG_INT(LogType::ERR, 4, "Failed to close directory, \"" + dir + "\"."); + EHS_LOG_INT(LogType::ERR, 3, "Failed to close directory, \"" + dir + "\"."); return result; } diff --git a/src/io/Directory_W32.cpp b/src/io/Directory_W32.cpp new file mode 100644 index 0000000..dbaf2ad --- /dev/null +++ b/src/io/Directory_W32.cpp @@ -0,0 +1,87 @@ +#include "ehs/io/Directory_W32.h" +#include "ehs/UTF.h" +#include "ehs/Log.h" + +namespace ehs +{ + Array GetAllFiles(const Str_8 &dir) + { + Array result; + + if (!dir.Size()) + { + EHS_LOG_INT(LogType::WARN, 1, "The given directory was empty."); + return result; + } + + WIN32_FIND_DATAW fData; + HANDLE hFind = FindFirstFileW(UTF::To_16(dir) + L"\\*", &fData); + + if (hFind == INVALID_HANDLE_VALUE) + { + EHS_LOG_INT(LogType::ERR, 2, "Failed to open directory, \"" + dir + "\"."); + return result; + } + + do + { + Str_8 fName = UTF::To_8(fData.cFileName); + if (fName == "." || fName == "..") + continue; + + result.Push(fName); + } + while (FindNextFileW(hFind, &fData) != 0); + + if (!FindClose(hFind)) + { + EHS_LOG_INT(LogType::ERR, 3, "Failed to close directory, \"" + dir + "\"."); + return result; + } + + EHS_LOG_SUCCESS(); + + return result; + } + + void CreateRecursive(Str_8 dir) + { + dir = dir.ReplaceAll("\\", "/"); + + const Vector dirs = dir.Split("/"); + + for (UInt_64 i = 0; i < dirs.Size(); ++i) + { + const Str_8 final = (Str_8&&)dirs[i]; + for (UInt_64 x = 0; x < i; ++x) + dirs[i] += dirs[x] + "/"; + + dirs[i] += final; + + if (!CreateDirectoryW(UTF::To_16(dirs[i]), nullptr)) + { + if (const DWORD code = GetLastError(); code != ERROR_ALREADY_EXISTS) + { + EHS_LOG_INT(LogType::ERR, 1, "Failed to create directory, \"" + dirs[i] + "\" with error #" + Str_8::FromNum(code) + "."); + return; + } + } + } + + EHS_LOG_SUCCESS(); + } + + void Create(const Str_8 &dir) + { + if (!CreateDirectoryW(UTF::To_16(dir), nullptr)) + { + if (const DWORD code = GetLastError(); code != ERROR_ALREADY_EXISTS) + { + EHS_LOG_INT(LogType::ERR, 1, "Failed to create directory, \"" + dir + "\" with error #" + Str_8::FromNum(code) + "."); + return; + } + } + + EHS_LOG_SUCCESS(); + } +} \ No newline at end of file diff --git a/src/io/UsbBase.cpp b/src/io/UsbBase.cpp index 4eb5576..cad5d3a 100644 --- a/src/io/UsbBase.cpp +++ b/src/io/UsbBase.cpp @@ -1,8 +1,6 @@ #include "ehs/io/UsbBase.h" #include "ehs/Util.h" -#include - namespace ehs { UsbBase::UsbBase()