2021-06-27 21:25:29 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: FS.hpp
|
|
|
|
Date: 2021-6-16
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#pragma once
|
|
|
|
|
2021-09-06 10:58:08 +00:00
|
|
|
#if !defined(NO_FSTREAM) && (!defined(__has_include) || __has_include(<fstream>))
|
2021-06-27 21:25:29 +00:00
|
|
|
#include <fstream>
|
|
|
|
#if !defined(HAS_FSTREAM)
|
|
|
|
#define HAS_FSTREAM
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2021-09-06 10:58:08 +00:00
|
|
|
#if !defined(NO_STD_FS) && (!defined(__has_include) || __has_include(<filesystem>))
|
2021-06-27 21:25:29 +00:00
|
|
|
#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
|
2021-09-06 10:58:08 +00:00
|
|
|
namespace std
|
|
|
|
{
|
2021-06-27 21:25:29 +00:00
|
|
|
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)
|
|
|
|
{
|
2022-01-24 18:37:06 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
_NormalizePath(str);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2023-09-12 19:48:35 +00:00
|
|
|
AuResetMember(str);
|
2022-01-24 18:37:06 +00:00
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 18:37:06 +00:00
|
|
|
static auline AuString NormalizePathRet(const AuString &str)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2022-01-24 18:37:06 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
AuString ret = str;
|
|
|
|
_NormalizePath(ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool _MkDir(const AuString &str);
|
|
|
|
|
|
|
|
static bool CreateDirectories(const AuString &cpath, bool isFile)
|
|
|
|
{
|
2022-01-24 18:37:06 +00:00
|
|
|
try
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2022-03-30 14:17:15 +00:00
|
|
|
// This gives us a significant performance boost
|
|
|
|
AuString path;
|
|
|
|
if (isFile && GetDirectoryFromPath(path, cpath))
|
|
|
|
{
|
|
|
|
if (DirExists(path))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 18:37:06 +00:00
|
|
|
for (int i = 0; i < cpath.size(); i++)
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2022-01-24 18:37:06 +00:00
|
|
|
bool end = i == cpath.size() - 1;
|
|
|
|
if ((cpath[i] == kPathSplitter) ||
|
|
|
|
((!isFile) && (end)))
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2022-01-24 18:37:06 +00:00
|
|
|
auto subpath = end ? cpath : AuString(cpath.begin(), cpath.begin() + i);
|
2022-04-10 15:40:49 +00:00
|
|
|
if (subpath.empty())
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-01-24 18:37:06 +00:00
|
|
|
if (DirExists(subpath))
|
2021-06-27 21:25:29 +00:00
|
|
|
{
|
2022-01-24 18:37:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_MkDir(subpath))
|
|
|
|
{
|
|
|
|
if (!DirExists(subpath)) // copium. im worried :(
|
|
|
|
{
|
|
|
|
return false; // ...aw shit
|
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 18:37:06 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2021-06-27 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InitResources();
|
|
|
|
}
|