19 std::vector<wchar_t> buffer(MAX_PATH);
20 HMODULE hModule = GetModuleHandle(NULL);
23 DWORD size = GetModuleFileNameW(hModule, buffer.data(), buffer.size());
26 while (size == buffer.size() && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
27 buffer.resize(buffer.size() * 2);
28 size = GetModuleFileNameW(hModule, buffer.data(), buffer.size());
32 throw std::runtime_error(
"Failed to get executable path.");
35 std::wstring exe_path(buffer.begin(), buffer.begin() + size);
38 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
39 return converter.to_bytes(exe_path);
41 char result[PATH_MAX];
42 ssize_t count = readlink(
"/proc/self/exe", result, PATH_MAX);
45 throw std::runtime_error(
"Failed to get executable path.");
48 std::string exe_path(result, count);
80 inline std::string
make_relative(
const std::string& file_path,
const std::string& base_path) {
81 if (base_path.empty())
return file_path;
82 fs::path fileP = fs::u8path(file_path);
83 fs::path baseP = fs::u8path(base_path);
85 fs::path relativeP = fs::relative(fileP, baseP, ec);
86 return ec ? file_path : relativeP.u8string();