diff --git a/FEXHeaderUtils/FEXHeaderUtils/Filesystem.h b/FEXHeaderUtils/FEXHeaderUtils/Filesystem.h index 26a0e1f319..58c0579839 100644 --- a/FEXHeaderUtils/FEXHeaderUtils/Filesystem.h +++ b/FEXHeaderUtils/FEXHeaderUtils/Filesystem.h @@ -19,6 +19,12 @@ #include namespace FHU::Filesystem { +#ifdef _WIN32 + inline fextl::string PathToString(const std::filesystem::path &path) { + return path.string, fextl::FEXAlloc>(); + } +#endif + /** * @brief Check if a filepath exists. * @@ -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) { @@ -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. @@ -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) { @@ -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('/'); @@ -151,7 +171,13 @@ 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('/'); } @@ -159,6 +185,15 @@ namespace FHU::Filesystem { 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, @@ -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(); @@ -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]) {