2024-06-29 23:38:27 -07:00
|
|
|
#include "ehs/io/Directory_W32.h"
|
|
|
|
#include "ehs/UTF.h"
|
|
|
|
#include "ehs/Log.h"
|
|
|
|
|
|
|
|
namespace ehs
|
|
|
|
{
|
2024-07-24 01:36:20 -07:00
|
|
|
Array<Str_8> Directory::GetAllFiles(const Str_8 &dir)
|
2024-06-29 23:38:27 -07:00
|
|
|
{
|
|
|
|
Array<Str_8> 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;
|
|
|
|
}
|
|
|
|
|
2024-07-24 01:36:20 -07:00
|
|
|
void Directory::CreateRecursive(Str_8 dir)
|
2024-06-29 23:38:27 -07:00
|
|
|
{
|
|
|
|
dir = dir.ReplaceAll("\\", "/");
|
|
|
|
|
|
|
|
const Vector<Str_8> 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();
|
|
|
|
}
|
|
|
|
|
2024-07-24 01:36:20 -07:00
|
|
|
void Directory::Create(const Str_8 &dir)
|
2024-06-29 23:38:27 -07:00
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|