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 return (fs::u8path(get_exec_path())).parent_path().u8string();
58 }
59
63 std::string get_file_name(const std::string& file_path) {
64 //std::size_t pos = file_path.find_last_of("/\\");
65 //return (pos == std::string::npos) ? file_path : file_path.substr(pos + 1);
66 return (fs::u8path(file_path)).filename().u8string();
67 }
68
72 std::string resolve_exec_path(const std::string& relative_path) {
73 return (fs::u8path(get_exec_dir()) / fs::u8path(relative_path)).u8string();
74 }
75
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);
84 std::error_code ec;
85 fs::path relativeP = fs::relative(fileP, baseP, ec);
86 return ec ? file_path : relativeP.u8string();
87 }
88
92 void create_directories(const std::string& path) {
93 fs::path dir = fs::u8path(path);
94 if (!std::filesystem::exists(dir)) {
95 std::error_code ec;
96 if (!std::filesystem::create_directories(dir, ec)) {
97 throw std::runtime_error("Failed to create directories for path: " + dir.u8string());
98 }
99 }
100 }
101
102}; // namespace consolix
103
104#endif // _CONSOLIX_PATH_UTILS_HPP_INCLUDED
< 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.