Skip to content

Commit

Permalink
FHU: Switch over win32 file operations to std::filesystem
Browse files Browse the repository at this point in the history
These were broken to varying degrees across the board on win32,
just switch to their std::filesystem as windows doesn't have the
same allocator issues that require their reimplementation.
  • Loading branch information
bylaws committed Apr 9, 2024
1 parent 1a8b61b commit 1893ea0
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions FEXHeaderUtils/FEXHeaderUtils/Filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
#include <unistd.h>

namespace FHU::Filesystem {
#ifdef _WIN32
inline fextl::string PathToString(const std::filesystem::path &path) {
return path.string<char, std::char_traits<char>, fextl::FEXAlloc<char>>();
}
#endif

/**
* @brief Check if a filepath exists.
*
Expand Down Expand Up @@ -93,6 +99,7 @@ namespace FHU::Filesystem {
*
* @return True if the directory tree was created or already exists.
*/
#ifndef _WIN32
inline bool CreateDirectories(const fextl::string &Path) {
// Try to create the directory initially.
if (CreateDirectory(Path) != CreateDirectoryResult::ERROR) {
Expand All @@ -106,6 +113,12 @@ namespace FHU::Filesystem {
}
return false;
}
#else
inline bool CreateDirectories(const fextl::string &Path) {
std::error_code ec;
return std::filesystem::exists(Path, ec) || std::filesystem::create_directories(Path, ec);
}
#endif

/**
* @brief Extracts the filename component from a file path.
Expand All @@ -114,6 +127,7 @@ namespace FHU::Filesystem {
*
* @return The filename component of the path.
*/
#ifndef _WIN32
inline fextl::string GetFilename(const fextl::string &Path) {
auto LastSeparator = Path.rfind('/');
if (LastSeparator == fextl::string::npos) {
Expand All @@ -123,7 +137,13 @@ namespace FHU::Filesystem {

return Path.substr(LastSeparator + 1);
}
#else
inline fextl::string GetFilename(const fextl::string &Path) {
return PathToString(std::filesystem::path(Path).filename());
}
#endif

#ifndef _WIN32
inline fextl::string ParentPath(const fextl::string &Path) {
auto LastSeparator = Path.rfind('/');

Expand Down Expand Up @@ -151,14 +171,29 @@ namespace FHU::Filesystem {

return SubString;
}
#else
inline fextl::string ParentPath(const fextl::string &Path) {
return PathToString(std::filesystem::path(Path).parent_path());
}
#endif

#ifndef _WIN32
inline bool IsRelative(const std::string_view Path) {
return !Path.starts_with('/');
}

inline bool IsAbsolute(const std::string_view Path) {
return Path.starts_with('/');
}
#else
inline bool IsRelative(const std::string_view Path) {
return std::filesystem::path(Path).is_relative();
}

inline bool IsAbsolute(const std::string_view Path) {
return std::filesystem::path(Path).is_absolute();
}
#endif

enum class CopyOptions {
NONE,
Expand Down Expand Up @@ -250,6 +285,7 @@ namespace FHU::Filesystem {
return rename(From.c_str(), To.c_str()) == 0 ? std::error_code{} : std::make_error_code(std::errc::io_error);
}

#ifndef _WIN32
inline fextl::string LexicallyNormal(const fextl::string &Path) {
const auto PathSize = Path.size();

Expand Down Expand Up @@ -352,6 +388,11 @@ namespace FHU::Filesystem {
fmt::join(Parts, "/"),
NeedsFinalSeparator ? "/" : "");
}
#else
inline fextl::string LexicallyNormal(const fextl::string &Path) {
return PathToString(std::filesystem::path(Path).lexically_normal());
}
#endif

#ifndef _WIN32
inline char *Absolute(const char *Path, char Fill[PATH_MAX]) {
Expand Down

0 comments on commit 1893ea0

Please sign in to comment.