2#ifndef _CONSOLIX_PATH_UTILS_HPP_INCLUDED
3#define _CONSOLIX_PATH_UTILS_HPP_INCLUDED
13 namespace fs = std::filesystem;
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);
58 std::vector<wchar_t> buffer(MAX_PATH);
59 HMODULE hModule = GetModuleHandle(NULL);
62 DWORD size = GetModuleFileNameW(hModule, buffer.data(), buffer.size());
65 while (size == buffer.size() && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
66 buffer.resize(buffer.size() * 2);
67 size = GetModuleFileNameW(hModule, buffer.data(), buffer.size());
71 throw std::runtime_error(
"Failed to get executable path.");
74 std::wstring exe_path(buffer.begin(), buffer.begin() + size);
77 size_t pos = exe_path.find_last_of(L
"\\/");
78 if (pos != std::wstring::npos) {
79 exe_path = exe_path.substr(0, pos);
83 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
84 return converter.to_bytes(exe_path);
86 char result[PATH_MAX];
87 ssize_t count = readlink(
"/proc/self/exe", result, PATH_MAX);
90 throw std::runtime_error(
"Failed to get executable path.");
93 std::string exe_path(result, count);
96 size_t pos = exe_path.find_last_of(
"\\/");
97 if (pos != std::string::npos) {
98 exe_path = exe_path.substr(0, pos);
108 std::string get_exec_path() {
110 std::vector<wchar_t> buffer(MAX_PATH);
111 HMODULE hModule = GetModuleHandle(NULL);
113 DWORD size = GetModuleFileNameW(hModule, buffer.data(), (DWORD)buffer.size());
115 while (size == buffer.size() && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
116 buffer.resize(buffer.size() * 2);
117 size = GetModuleFileNameW(hModule, buffer.data(), (DWORD)buffer.size());
121 throw std::runtime_error("Failed to get executable path.");
124 std::wstring exe_path(buffer.begin(), buffer.begin() + size);
125 return std::filesystem::path(exe_path).string();
127 char result[PATH_MAX];
128 ssize_t count = readlink("/proc/self/exe", result, PATH_MAX);
130 throw std::runtime_error("Failed to get executable path.");
132 std::string exe_path(result, count);
139 std::string get_exec_dir() {
140 return std::filesystem::path(get_exec_path()).parent_path().string();
148 std::size_t pos = file_path.find_last_of(
"/\\");
149 return (pos == std::string::npos) ? file_path : file_path.substr(pos + 1);
156 inline std::string
make_relative(
const std::string& file_path,
const std::string& base_path) {
157 if (base_path.empty())
return file_path;
158 std::filesystem::path fileP = std::filesystem::u8path(file_path);
159 std::filesystem::path baseP = std::filesystem::u8path(base_path);
161 std::filesystem::path relativeP = std::filesystem::relative(fileP, baseP, ec);
162 return ec ? file_path : relativeP.u8string();
169 std::string resolve_exec_path(const std::string& relative_path) {
170 std::cout << "dir1 " << std::filesystem::absolute(get_exec_dir() + "\\" + relative_path).string() << std::endl;
171 std::cout << "dir2 " << (get_exec_dir() + "\\" + relative_path) << std::endl;
172# if defined(_WIN32) || defined(_WIN64)
173 //return std::filesystem::absolute(get_exec_dir() + "\\" + relative_path).string();
175 //return std::filesystem::absolute(get_exec_dir() + "/" + relative_path).string();
177 return get_exec_dir() + "\\" + relative_path;
186 std::filesystem::path dir(path);
187 if (!std::filesystem::exists(dir)) {
189 if (!std::filesystem::create_directories(dir, ec)) {
190 throw std::runtime_error(
"Failed to create directories for path: " + path);
< Utility modules and helpers.
std::string get_exec_dir()
Retrieves the directory of the executable file.
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.