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
108 std::string get_file_name(const std::string& file_path) {
109 std::size_t pos = file_path.find_last_of("/\\");
110 return (pos == std::string::npos) ? file_path : file_path.substr(pos + 1);
111 }
112
117 inline std::string make_relative(const std::string& file_path, const std::string& base_path) {
118 if (base_path.empty()) return file_path;
119 std::filesystem::path fileP = std::filesystem::u8path(file_path);
120 std::filesystem::path baseP = std::filesystem::u8path(base_path);
121 std::error_code ec;
122 std::filesystem::path relativeP = std::filesystem::relative(fileP, baseP, ec);
123 return ec ? file_path : relativeP.u8string();
124 }
125
129 void create_directories(const std::string& path) {
130 std::filesystem::path dir(path);
131 if (!std::filesystem::exists(dir)) {
132 std::error_code ec;
133 if (!std::filesystem::create_directories(dir, ec)) {
134 throw std::runtime_error("Failed to create directories for path: " + path);
135 }
136 }
137 }
138
139}; // namespace consolix
140
141#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.