Consolix
Loading...
Searching...
No Matches
path_utils.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef _CONSOLIX_PATH_UTILS_HPP_INCLUDED
3#define _CONSOLIX_PATH_UTILS_HPP_INCLUDED
4
7
8#include <string>
9#include <vector>
10#include <filesystem>
11
12namespace consolix {
13 namespace fs = std::filesystem;
14
17 std::string get_exec_path() {
18# ifdef _WIN32
19 std::vector<wchar_t> buffer(MAX_PATH);
20 HMODULE hModule = GetModuleHandle(NULL);
21
22 // Пробуем получить путь
23 DWORD size = GetModuleFileNameW(hModule, buffer.data(), buffer.size());
24
25 // Если путь слишком длинный, увеличиваем буфер
26 while (size == buffer.size() && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
27 buffer.resize(buffer.size() * 2); // Увеличиваем буфер в два раза
28 size = GetModuleFileNameW(hModule, buffer.data(), buffer.size());
29 }
30
31 if (size == 0) {
32 throw std::runtime_error("Failed to get executable path.");
33 }
34
35 std::wstring exe_path(buffer.begin(), buffer.begin() + size);
36
37 // Преобразуем из std::wstring (UTF-16) в std::string (UTF-8)
38 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
39 return converter.to_bytes(exe_path);
40# else
41 char result[PATH_MAX];
42 ssize_t count = readlink("/proc/self/exe", result, PATH_MAX);
43
44 if (count == -1) {
45 throw std::runtime_error("Failed to get executable path.");
46 }
47
48 std::string exe_path(result, count);
49
50 return exe_path;
51# endif
52 }
53
56 std::string get_exec_dir() {
57# ifdef _WIN32
58 std::vector<wchar_t> buffer(MAX_PATH);
59 HMODULE hModule = GetModuleHandle(NULL);
60
61 // Пробуем получить путь
62 DWORD size = GetModuleFileNameW(hModule, buffer.data(), buffer.size());
63
64 // Если путь слишком длинный, увеличиваем буфер
65 while (size == buffer.size() && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
66 buffer.resize(buffer.size() * 2); // Увеличиваем буфер в два раза
67 size = GetModuleFileNameW(hModule, buffer.data(), buffer.size());
68 }
69
70 if (size == 0) {
71 throw std::runtime_error("Failed to get executable path.");
72 }
73
74 std::wstring exe_path(buffer.begin(), buffer.begin() + size);
75
76 // Обрезаем путь до директории (удаляем имя файла, оставляем только путь к папке)
77 size_t pos = exe_path.find_last_of(L"\\/");
78 if (pos != std::wstring::npos) {
79 exe_path = exe_path.substr(0, pos);
80 }
81
82 // Преобразуем из std::wstring (UTF-16) в std::string (UTF-8)
83 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
84 return converter.to_bytes(exe_path);
85# else
86 char result[PATH_MAX];
87 ssize_t count = readlink("/proc/self/exe", result, PATH_MAX);
88
89 if (count == -1) {
90 throw std::runtime_error("Failed to get executable path.");
91 }
92
93 std::string exe_path(result, count);
94
95 // Обрезаем путь до директории (удаляем имя файла, оставляем только путь к папке)
96 size_t pos = exe_path.find_last_of("\\/");
97 if (pos != std::string::npos) {
98 exe_path = exe_path.substr(0, pos);
99 }
100
101 return exe_path;
102# endif
103 }
104
105 /*
108 std::string get_exec_path() {
109# ifdef _WIN32
110 std::vector<wchar_t> buffer(MAX_PATH);
111 HMODULE hModule = GetModuleHandle(NULL);
112
113 DWORD size = GetModuleFileNameW(hModule, buffer.data(), (DWORD)buffer.size());
114
115 while (size == buffer.size() && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
116 buffer.resize(buffer.size() * 2);
117 size = GetModuleFileNameW(hModule, buffer.data(), (DWORD)buffer.size());
118 }
119
120 if (size == 0) {
121 throw std::runtime_error("Failed to get executable path.");
122 }
123
124 std::wstring exe_path(buffer.begin(), buffer.begin() + size);
125 return std::filesystem::path(exe_path).string();
126# else
127 char result[PATH_MAX];
128 ssize_t count = readlink("/proc/self/exe", result, PATH_MAX);
129 if (count == -1) {
130 throw std::runtime_error("Failed to get executable path.");
131 }
132 std::string exe_path(result, count);
133 return exe_path;
134# endif
135 }
136
139 std::string get_exec_dir() {
140 return std::filesystem::path(get_exec_path()).parent_path().string();
141 }
142 */
143
147 std::string get_file_name(const std::string& file_path) {
148 std::size_t pos = file_path.find_last_of("/\\");
149 return (pos == std::string::npos) ? file_path : file_path.substr(pos + 1);
150 }
151
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);
160 std::error_code ec;
161 std::filesystem::path relativeP = std::filesystem::relative(fileP, baseP, ec);
162 return ec ? file_path : relativeP.u8string();
163 }
164
165 /*
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();
174# else
175 //return std::filesystem::absolute(get_exec_dir() + "/" + relative_path).string();
176# endif
177 return get_exec_dir() + "\\" + relative_path;
178 }
179
180 */
181
185 void create_directories(const std::string& path) {
186 std::filesystem::path dir(path);
187 if (!std::filesystem::exists(dir)) {
188 std::error_code ec;
189 if (!std::filesystem::create_directories(dir, ec)) {
190 throw std::runtime_error("Failed to create directories for path: " + path);
191 }
192 }
193 }
194
195}; // namespace consolix
196
197#endif // _CONSOLIX_PATH_UTILS_HPP_INCLUDED
< 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.