EHS/src/io/Directory_W32.cpp

87 lines
2.1 KiB
C++
Raw Normal View History

2024-06-29 23:38:27 -07:00
#include "ehs/io/Directory_W32.h"
#include "ehs/UTF.h"
#include "ehs/Log.h"
namespace ehs
{
Array<Str_8> GetAllFiles(const Str_8 &dir)
{
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;
}
void CreateRecursive(Str_8 dir)
{
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();
}
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();
}
}