1#ifndef _LOGIT_PATH_UTILS_HPP_INCLUDED
2#define _LOGIT_PATH_UTILS_HPP_INCLUDED
8#if __cplusplus >= 201703L
20#elif defined(__APPLE__)
22#include <mach-o/dyld.h>
39# if __cplusplus >= 201703L
40 namespace fs = std::filesystem;
43#if defined(__EMSCRIPTEN__)
47 inline std::vector<std::string>
get_list_files(
const std::string&) =
delete;
49 inline std::string
get_file_name(
const std::string& file_path) {
50 size_t pos = file_path.find_last_of(
"/\\");
51 if (pos == std::string::npos)
return file_path;
52 return file_path.substr(pos + 1);
55 inline std::string
make_relative(
const std::string& file_path,
const std::string&) {
61 inline bool is_file(
const std::string& path) {
62 size_t dot_pos = path.find_last_of(
'.');
63 size_t slash_pos = path.find_last_of(
"/\\");
64 return (dot_pos != std::string::npos && (slash_pos == std::string::npos || dot_pos > slash_pos));
73 std::vector<wchar_t> buffer(MAX_PATH);
74 HMODULE hModule = GetModuleHandle(NULL);
77 std::size_t size =
static_cast<std::size_t
>(GetModuleFileNameW(hModule, buffer.data(),
static_cast<DWORD
>(buffer.size())));
80 while (size == buffer.size() && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
81 buffer.resize(buffer.size() * 2);
82 size =
static_cast<std::size_t
>(GetModuleFileNameW(hModule, buffer.data(),
static_cast<DWORD
>(buffer.size())));
86 throw std::runtime_error(
"Failed to get executable path.");
89 std::wstring exe_path(buffer.begin(), buffer.begin() + size);
92 size_t pos = exe_path.find_last_of(L
"\\/");
93 if (pos != std::wstring::npos) {
94 exe_path = exe_path.substr(0, pos);
98 return wstring_to_utf8(exe_path);
99# elif defined(__APPLE__)
101 _NSGetExecutablePath(
nullptr, &size);
102 std::vector<char> path(size);
103 if (_NSGetExecutablePath(path.data(), &size) != 0) {
104 throw std::runtime_error(
"Failed to get executable path.");
106 std::string exe_path(path.data());
107 char resolved[PATH_MAX];
108 if (realpath(exe_path.c_str(), resolved) ==
nullptr) {
109 throw std::runtime_error(
"Failed to resolve executable path.");
112 size_t pos = exe_path.find_last_of(
"\\/");
113 if (pos != std::string::npos) {
114 exe_path = exe_path.substr(0, pos);
118 char result[PATH_MAX];
119 ssize_t count = readlink(
"/proc/self/exe", result, PATH_MAX);
122 throw std::runtime_error(
"Failed to get executable path.");
125 std::string exe_path(result, count);
128 size_t pos = exe_path.find_last_of(
"\\/");
129 if (pos != std::string::npos) {
130 exe_path = exe_path.substr(0, pos);
141 std::vector<std::string> list_files;
144 std::wstring wsearch_path;
148 wchar_t buffer[MAX_PATH];
149 GetCurrentDirectoryW(MAX_PATH, buffer);
150 wsearch_path = buffer;
152 wsearch_path = utf8_to_wstring(path);
156 if (!wsearch_path.empty()) {
157 wchar_t last_char = wsearch_path.back();
158 if (last_char != L
'\\' && last_char != L
'/') {
159 wsearch_path.push_back(L
'\\');
164 std::wstring pattern = wsearch_path + L
"*";
166 HANDLE hFind = FindFirstFileW(pattern.c_str(), &fd);
167 if (hFind != INVALID_HANDLE_VALUE) {
169 if (wcscmp(fd.cFileName, L
".") == 0 || wcscmp(fd.cFileName, L
"..") == 0)
172 std::wstring wfull_path = wsearch_path + fd.cFileName;
174 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
176 std::vector<std::string> sub_files =
get_list_files(wstring_to_utf8(wfull_path));
177 list_files.insert(list_files.end(), sub_files.begin(), sub_files.end());
180 list_files.push_back(wstring_to_utf8(wfull_path));
182 }
while (FindNextFileW(hFind, &fd));
187 std::string search_path = path;
188 if (search_path.empty()) {
189 char buffer[PATH_MAX];
190 if (getcwd(buffer, PATH_MAX)) {
191 search_path = buffer;
195 if (search_path.back() !=
'/' && search_path.back() !=
'\\') {
196 search_path.push_back(
'/');
198 DIR* dir = opendir(search_path.c_str());
200 struct dirent* entry;
201 while ((entry = readdir(dir)) !=
nullptr) {
202 std::string file_name = entry->d_name;
203 if (file_name ==
"." || file_name ==
"..")
205 std::string full_path = search_path + file_name;
207 if (stat(full_path.c_str(), &statbuf) == 0) {
208 if (S_ISDIR(statbuf.st_mode)) {
210 list_files.insert(list_files.end(), sub_files.begin(), sub_files.end());
211 }
else if (S_ISREG(statbuf.st_mode)) {
212 list_files.push_back(full_path);
226# if __cplusplus >= 201703L
227 return fs::u8path(file_path).filename().u8string();
229 size_t pos = file_path.find_last_of(
"/\\");
230 if (pos == std::string::npos)
return file_path;
231 return file_path.substr(pos + 1);
235#if __cplusplus >= 201703L
241 inline std::string
make_relative(
const std::string& file_path,
const std::string& base_path) {
242 if (base_path.empty())
return file_path;
243 std::filesystem::path fileP = std::filesystem::u8path(file_path);
244 std::filesystem::path baseP = std::filesystem::u8path(base_path);
246 std::filesystem::path relativeP = std::filesystem::relative(fileP, baseP, ec);
251 return relativeP.u8string();
261 std::wstring wide_path = utf8_to_wstring(path);
262 std::filesystem::path dir(wide_path);
264 std::filesystem::path dir = std::filesystem::u8path(path);
266 if (!std::filesystem::exists(dir)) {
268 if (!std::filesystem::create_directories(dir, ec)) {
269 throw std::runtime_error(
"Failed to create directories for path: " + dir.u8string());
289 size_t n = path.size();
292 if (n >= 1 && (path[0] ==
'/' || path[0] ==
'\\')) {
296 }
else if (n >= 2 && std::isalpha(path[0]) && path[1] ==
':') {
298 result.
root = path.substr(0, 2);
300 if (n >= 3 && (path[2] ==
'/' || path[2] ==
'\\')) {
309 while (i < n && (path[i] ==
'/' || path[i] ==
'\\')) {
314 while (j < n && path[j] !=
'/' && path[j] !=
'\\') {
318 result.
components.push_back(path.substr(i, j - i));
330 std::string
make_relative(
const std::string& file_path,
const std::string& base_path) {
331 if (base_path.empty())
return file_path;
341 size_t common_size = 0;
342 while (common_size < file_pc.
components.size() &&
349 std::vector<std::string> relative_components;
352 for (
size_t i = common_size; i < base_pc.
components.size(); ++i) {
353 relative_components.push_back(
"..");
357 for (
size_t i = common_size; i < file_pc.
components.size(); ++i) {
358 relative_components.push_back(file_pc.
components[i]);
362 std::string relative_path;
363 if (relative_components.empty()) {
366 for (
size_t i = 0; i < relative_components.size(); ++i) {
369 relative_path +=
'\\';
371 relative_path +=
'/';
374 relative_path += relative_components[i];
378 return relative_path;
384 inline bool is_file(
const std::string& path) {
385 size_t dot_pos = path.find_last_of(
'.');
386 size_t slash_pos = path.find_last_of(
"/\\");
387 return (dot_pos != std::string::npos && (slash_pos == std::string::npos || dot_pos > slash_pos));
394 if (path.empty())
return;
397 size_t components_size = components.size();
405 std::string current_path = path_pc.
root;
406 for (
size_t i = 0; i < components_size; ++i) {
407 if (!current_path.empty() && current_path.back() !=
'/' && current_path.back() !=
'\\') {
410 current_path += components[i];
413 if (components[i] ==
".." ||
414 components[i] ==
"/" ||
415 components[i] ==
"~/")
continue;
417 int ret = _mkdir(utf8_to_ansi(current_path).c_str());
419 int ret = mkdir(current_path.c_str(), 0755);
422 if (ret != 0 && errnum != EEXIST) {
423 throw std::runtime_error(
"Failed to create directory: " + current_path);
The primary namespace for the LogIt++ library.
std::vector< std::string > get_list_files(const std::string &path)
Recursively retrieves a list of all files in a directory.
std::string make_relative(const std::string &file_path, const std::string &base_path)
Computes the relative path from base_path to file_path.
bool is_file(const std::string &path)
Checks if a path represents a file (by checking for an extension).
PathComponents split_path(const std::string &path)
Splits a path into its root and components.
void create_directories(const std::string &path)
Creates directories recursively for the given path.
std::string get_file_name(const std::string &file_path)
Extracts the file name from a full file path.
std::string get_exec_dir()
Retrieves the directory of the executable file.
Structure to hold the root and components of a path.
std::string root
The root part of the path (e.g., "/", "C:")
std::vector< std::string > components
The components of the path.