#include "path.h" #include #include #include #include #include template static CharT to_lower_char(CharT character) { if constexpr (std::is_same_v) return static_cast(std::towlower(static_cast(character))); else return static_cast(std::tolower(static_cast(character))); } std::filesystem::path path_to_lower(const std::filesystem::path& path) { auto native = path.native(); for (auto& character : native) character = to_lower_char(character); return std::filesystem::path(native); } std::filesystem::path path_backslash_replace(const std::filesystem::path& path) { auto native = path.native(); constexpr auto backslash = static_cast('\\'); constexpr auto slash = static_cast('/'); for (auto& character : native) if (character == backslash) character = slash; return std::filesystem::path(native); } std::string path_to_utf8(const std::filesystem::path& path) { auto u8 = path.u8string(); return std::string(u8.begin(), u8.end()); } std::filesystem::path path_from_utf8(const std::string& utf8) { return std::filesystem::path(std::u8string(utf8.begin(), utf8.end())); } bool is_path_exist(const std::filesystem::path& path) { if (path.empty()) return false; std::error_code errorCode; return std::filesystem::exists(path, errorCode) && ((void)std::filesystem::status(path, errorCode), !errorCode); } bool is_path_extension(const std::filesystem::path& path, const std::string& extension) { if (path.empty()) return false; auto extensionUtf8 = path_to_utf8(std::filesystem::path(path).extension()); std::transform(extensionUtf8.begin(), extensionUtf8.end(), extensionUtf8.begin(), ::tolower); return extensionUtf8 == ("." + extension); } bool is_path_executable(const std::filesystem::path& path) { if (path.empty()) return false; std::error_code ec; auto status = std::filesystem::status(path, ec); if (ec) return false; if (!std::filesystem::exists(status)) return false; if (!std::filesystem::is_regular_file(status)) return false; #ifdef WIN32 auto extension = path_to_lower(path.extension()); if (extension != ".exe" && extension != ".bat" && extension != ".cmd") return false; #else auto permissions = status.permissions(); auto executableMask = std::filesystem::perms::owner_exec | std::filesystem::perms::group_exec | std::filesystem::perms::others_exec; if ((permissions & executableMask) == std::filesystem::perms::none) return false; #endif return true; } bool path_ensure_directory(const std::filesystem::path& path) { if (path.empty()) return false; std::error_code ec; if (std::filesystem::create_directories(path, ec)) return true; return is_path_exist(path.parent_path()); } std::filesystem::path path_make_relative(const std::filesystem::path& path) { if (path.empty()) return path; if (!path.is_absolute()) return path; std::error_code ec; auto relative = std::filesystem::relative(path, ec); if (!ec) return relative; return path; } std::filesystem::path path_make_relative(const std::filesystem::path& path, const std::filesystem::path& base) { if (path.empty()) return path; if (base.empty() || !path.is_absolute()) return path; std::error_code ec; auto relative = std::filesystem::relative(path, base, ec); if (!ec) return relative; return path; } std::filesystem::path path_backslash_handle(const std::filesystem::path& path) { auto newPath = path; if (is_path_exist(newPath)) return newPath; newPath = path_backslash_replace(newPath); return newPath; } std::filesystem::path path_case_insensitive_find(const std::filesystem::path& path) { auto handledPath = path_backslash_handle(path); if (is_path_exist(handledPath)) return handledPath; auto resolvedPath = handledPath.root_path(); for (auto& part : handledPath.relative_path()) { auto candidate = resolvedPath / part; if (is_path_exist(candidate)) { resolvedPath = candidate; continue; } auto parent = resolvedPath.empty() ? std::filesystem::path(".") : resolvedPath; std::error_code ec; bool isFound = false; if (std::filesystem::is_directory(parent, ec)) { auto partLower = path_to_lower(part); std::filesystem::directory_iterator it(parent, ec); std::filesystem::directory_iterator end = {}; for (; !ec && it != end; it.increment(ec)) if (path_to_lower(it->path().filename()) == partLower) { resolvedPath /= it->path().filename(); isFound = true; break; } } if (!isFound) resolvedPath = candidate; } if (is_path_exist(resolvedPath)) return resolvedPath; auto lowerPath = path_to_lower(handledPath); if (is_path_exist(lowerPath)) return lowerPath; return handledPath; }