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
10#include "encoding_utils.hpp"
11
12#include <string>
13#include <stdexcept>
14#include <vector>
15
16namespace consolix {
17 namespace fs = compat::filesystem;
18
21 inline std::string get_exec_path() {
22# ifdef _WIN32
23 std::vector<wchar_t> buffer(MAX_PATH);
24 HMODULE hModule = GetModuleHandle(nullptr);
25 DWORD size = GetModuleFileNameW(hModule, buffer.data(), static_cast<DWORD>(buffer.size()));
26
27 while (size == buffer.size() && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
28 buffer.resize(buffer.size() * 2);
29 size = GetModuleFileNameW(hModule, buffer.data(), static_cast<DWORD>(buffer.size()));
30 }
31
32 if (size == 0) {
33 throw std::runtime_error("Failed to get executable path.");
34 }
35
36 const std::wstring exe_path(buffer.begin(), buffer.begin() + static_cast<std::ptrdiff_t>(size));
37 return utf16_to_utf8(exe_path.c_str());
38# else
39 char result[PATH_MAX];
40 const ssize_t count = readlink("/proc/self/exe", result, PATH_MAX);
41
42 if (count == -1) {
43 throw std::runtime_error("Failed to get executable path.");
44 }
45
46 return std::string(result, static_cast<std::size_t>(count));
47# endif
48 }
49
52 inline std::string get_exec_dir() {
53 return fs::u8path(get_exec_path()).parent_path().u8string();
54 }
55
59 inline std::string get_file_name(const std::string& file_path) {
60 return fs::u8path(file_path).filename().u8string();
61 }
62
66 inline std::string resolve_exec_path(const std::string& relative_path) {
67 return (fs::u8path(get_exec_dir()) / fs::u8path(relative_path)).u8string();
68 }
69
74 inline std::string make_relative(const std::string& file_path, const std::string& base_path) {
75 if (base_path.empty()) return file_path;
76
77 const fs::path file_path_native = fs::u8path(file_path);
78 const fs::path base_path_native = fs::u8path(base_path);
79 std::error_code ec;
80 const fs::path relative_path = compat::relative(file_path_native, base_path_native, ec);
81 return ec ? file_path : relative_path.u8string();
82 }
83
87 inline void create_directories(const std::string& path) {
88 const fs::path dir = fs::u8path(path);
89 if (!fs::exists(dir)) {
90 std::error_code ec;
91 if (!fs::create_directories(dir, ec)) {
92 throw std::runtime_error("Failed to create directories for path: " + dir.u8string());
93 }
94 }
95 }
96
97}; // namespace consolix
98
99#endif // _CONSOLIX_PATH_UTILS_HPP_INCLUDED
Utilities for working with character encodings and string transformations.
filesystem::path relative(const filesystem::path &path, const filesystem::path &base, std::error_code &ec)
< 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.
Contains platform-specific API includes and definitions.
Compatibility helpers for standard library features used across Consolix.