diff --git a/kernel/io.cc b/kernel/io.cc index dfdb56d16..45aa496b0 100644 --- a/kernel/io.cc +++ b/kernel/io.cc @@ -2,6 +2,7 @@ #include "kernel/log.h" #include #include +#include #if !defined(WIN32) #include @@ -398,16 +399,21 @@ const char* const OS_PATH_SEP = "/"; #endif std::string name_from_file_path(std::string path) { - size_t sep_pos = path.find_last_of(OS_PATH_SEP); - if (sep_pos != std::string::npos) - return path.substr(sep_pos + 1); - else - return path; + return std::filesystem::path(path).filename().string(); } // Includes OS_PATH_SEP at the end if present std::string parent_from_file_path(std::string path) { - return path.substr(0, path.find_last_of(OS_PATH_SEP)+1); + auto parent = std::filesystem::path(path).parent_path(); + if (parent.empty()) { + return ""; + } + // Add trailing separator to match original behavior + std::string result = parent.string(); + if (!result.empty() && result.back() != std::filesystem::path::preferred_separator) { + result += std::filesystem::path::preferred_separator; + } + return result; } void format_emit_unescaped(std::string &result, std::string_view fmt)