AuroraRuntime/Source/IO/FS/FS.hpp
J Reece Wilson 3defb1bb14 [+] Linux Watcher
[*] Expand watcher API -> Breaking NT
[*] Reexpand loop queue API -> Breaking NT
[*] Linux CPUInfo clean up
[*] Bug fix: mkdir should set execute flag... because directories are special
[*] Refactor: Cleanup base64
[*] Bug fix: UNIX path normalization
[*] Bug fix: missing O_CREAT flag (au auto-creates)
[*] Normalize line endings
2022-04-10 16:40:49 +01:00

155 lines
4.0 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: FS.hpp
Date: 2021-6-16
Author: Reece
***/
#pragma once
#if !defined(NO_FSTREAM) && (!defined(__has_include) || __has_include(<fstream>))
#include <fstream>
#if !defined(HAS_FSTREAM)
#define HAS_FSTREAM
#endif
#endif
#if !defined(NO_STD_FS) && (!defined(__has_include) || __has_include(<filesystem>))
#include <filesystem>
#define HAS_STD_FS
#if !defined(_HAS_CXX17) && defined(AURORA_COMPILER_MSVC)
// map experimental::filesystem to filesystem::
// as in newer compilers it is not experimental anymore
namespace std
{
namespace filesystem = experimental::filesystem;
}
#endif
#endif
namespace Aurora::IO::FS
{
#if defined(VENDOR_GENERIC_MICROSOFT) || defined(VENDOR_CONSOLE_MICROSOFT)
static const unsigned char kPathSplitter = '\\';
static const AuString kDoublePathSplitter = "\\\\";
#else
static const unsigned char kPathSplitter = '/';
static const AuString kDoublePathSplitter = "//";
#endif
void _NormalizePath(AuString &str);
static void NormalizePath(AuString &str)
{
try
{
_NormalizePath(str);
}
catch (...)
{
str = {};
}
}
static auline AuString NormalizePathRet(const AuString &str)
{
try
{
AuString ret = str;
_NormalizePath(ret);
return ret;
}
catch (...)
{
return {};
}
}
bool _MkDir(const AuString &str);
static bool CreateDirectories(const AuString &cpath, bool isFile)
{
try
{
// This gives us a significant performance boost
AuString path;
if (isFile && GetDirectoryFromPath(path, cpath))
{
if (DirExists(path))
{
return true;
}
}
for (int i = 0; i < cpath.size(); i++)
{
bool end = i == cpath.size() - 1;
if ((cpath[i] == kPathSplitter) ||
((!isFile) && (end)))
{
auto subpath = end ? cpath : AuString(cpath.begin(), cpath.begin() + i);
if (subpath.empty())
{
continue;
}
if (DirExists(subpath))
{
continue;
}
if (!_MkDir(subpath))
{
if (!DirExists(subpath)) // copium. im worried :(
{
return false; // ...aw shit
}
}
}
}
return true;
}
catch (...)
{
return false;
}
}
static bool IterateDirEntriesSTL(const AuString &path, bool filesOnly, AuList<AuString> &patches)
{
#if defined(HAS_STD_FS)
auto normalizedPath = NormalizePathRet(path);
try
{
for (const auto &entry : std::filesystem::directory_iterator(normalizedPath))
{
auto cpath = entry.path();
if (filesOnly && !std::filesystem::is_regular_file(cpath))
continue;
if (!filesOnly && !std::filesystem::is_directory(cpath))
continue;
AuString path = cpath.string();
patches.push_back(path.substr(path.find_last_of(kPathSplitter) + 1));
}
return true;
}
catch (...)
{
Debug::PrintError();
Logging::LogWarn("IO Error, couldn't iterate path: {}", normalizedPath);
return false;
}
#endif
return false;
}
void InitResources();
}