2#ifndef _CONSOLIX_PATH_UTILS_HPP_INCLUDED
3#define _CONSOLIX_PATH_UTILS_HPP_INCLUDED
17 namespace fs = compat::filesystem;
23 std::vector<wchar_t> buffer(MAX_PATH);
24 HMODULE hModule = GetModuleHandle(
nullptr);
25 DWORD size = GetModuleFileNameW(hModule, buffer.data(),
static_cast<DWORD
>(buffer.size()));
27 while (size == buffer.size() && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
28 buffer.resize(buffer.size() * 2);
29 size = GetModuleFileNameW(hModule, buffer.data(),
static_cast<DWORD
>(buffer.size()));
33 throw std::runtime_error(
"Failed to get executable path.");
36 const std::wstring exe_path(buffer.begin(), buffer.begin() +
static_cast<std::ptrdiff_t
>(size));
37 return utf16_to_utf8(exe_path.c_str());
39 char result[PATH_MAX];
40 const ssize_t count = readlink(
"/proc/self/exe", result, PATH_MAX);
43 throw std::runtime_error(
"Failed to get executable path.");
46 return std::string(result,
static_cast<std::size_t
>(count));
60 return fs::u8path(file_path).filename().u8string();
67 return (fs::u8path(
get_exec_dir()) / fs::u8path(relative_path)).u8string();
74 inline std::string
make_relative(
const std::string& file_path,
const std::string& base_path) {
75 if (base_path.empty())
return file_path;
77 const fs::path file_path_native = fs::u8path(file_path);
78 const fs::path base_path_native = fs::u8path(base_path);
80 const fs::path relative_path =
compat::relative(file_path_native, base_path_native, ec);
81 return ec ? file_path : relative_path.u8string();
88 const fs::path dir = fs::u8path(path);
89 if (!fs::exists(dir)) {
91 if (!fs::create_directories(dir, ec)) {
92 throw std::runtime_error(
"Failed to create directories for path: " + dir.u8string());
Utilities for working with character encodings and string transformations.
filesystem::path relative(const filesystem::path &path, const filesystem::path &base, std::error_code &ec)
< Utility modules and helpers.
std::string get_exec_dir()
Retrieves the directory of the executable file.
std::string resolve_exec_path(const std::string &relative_path)
Resolves a relative path to absolute, based on executable location.
std::string get_exec_path()
Retrieves the full path of the executable.
std::string get_file_name(const std::string &file_path)
Extracts the file name from a full file path.
std::string make_relative(const std::string &file_path, const std::string &base_path)
Computes the relative path from base_path to file_path.
void create_directories(const std::string &path)
Creates directories recursively for the given path.
Compatibility helpers for standard library features used across Consolix.