2#ifndef LOGIT_CPP_HEADER_LOGIT_CPP_LOGIT_UTILS_PATH_UTILS_HPP_INCLUDED
3#define LOGIT_CPP_HEADER_LOGIT_CPP_LOGIT_UTILS_PATH_UTILS_HPP_INCLUDED
9#if __cplusplus >= 201703L
21#elif defined(__APPLE__)
23#include <mach-o/dyld.h>
40# if __cplusplus >= 201703L
41 namespace fs = std::filesystem;
44#if defined(__EMSCRIPTEN__)
48 inline std::vector<std::string>
get_list_files(
const std::string&) =
delete;
50 inline std::string
get_file_name(
const std::string& file_path) {
51 size_t pos = file_path.find_last_of(
"/\\");
52 if (pos == std::string::npos)
return file_path;
53 return file_path.substr(pos + 1);
56 inline std::string
make_relative(
const std::string& file_path,
const std::string&) {
62 inline bool is_file(
const std::string& path) {
63 size_t dot_pos = path.find_last_of(
'.');
64 size_t slash_pos = path.find_last_of(
"/\\");
65 return (dot_pos != std::string::npos && (slash_pos == std::string::npos || dot_pos > slash_pos));
74 std::vector<wchar_t> buffer(MAX_PATH);
75 HMODULE hModule = GetModuleHandle(NULL);
78 std::size_t size =
static_cast<std::size_t
>(GetModuleFileNameW(hModule, buffer.data(),
static_cast<DWORD
>(buffer.size())));
81 while (size == buffer.size() && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
82 buffer.resize(buffer.size() * 2);
83 size =
static_cast<std::size_t
>(GetModuleFileNameW(hModule, buffer.data(),
static_cast<DWORD
>(buffer.size())));
87 throw std::runtime_error(
"Failed to get executable path.");
90 std::wstring exe_path(buffer.begin(), buffer.begin() + size);
93 size_t pos = exe_path.find_last_of(L
"\\/");
94 if (pos != std::wstring::npos) {
95 exe_path = exe_path.substr(0, pos);
99 return wstring_to_utf8(exe_path);
100# elif defined(__APPLE__)
102 _NSGetExecutablePath(
nullptr, &size);
103 std::vector<char> path(size);
104 if (_NSGetExecutablePath(path.data(), &size) != 0) {
105 throw std::runtime_error(
"Failed to get executable path.");
107 std::string exe_path(path.data());
108 char resolved[PATH_MAX];
109 if (realpath(exe_path.c_str(), resolved) ==
nullptr) {
110 throw std::runtime_error(
"Failed to resolve executable path.");
113 size_t pos = exe_path.find_last_of(
"\\/");
114 if (pos != std::string::npos) {
115 exe_path = exe_path.substr(0, pos);
119 char result[PATH_MAX];
120 ssize_t count = readlink(
"/proc/self/exe", result, PATH_MAX);
123 throw std::runtime_error(
"Failed to get executable path.");
126 std::string exe_path(result, count);
129 size_t pos = exe_path.find_last_of(
"\\/");
130 if (pos != std::string::npos) {
131 exe_path = exe_path.substr(0, pos);
142 std::vector<std::string> list_files;
145 std::wstring wsearch_path;
149 wchar_t buffer[MAX_PATH];
150 GetCurrentDirectoryW(MAX_PATH, buffer);
151 wsearch_path = buffer;
153 wsearch_path = utf8_to_wstring(path);
157 if (!wsearch_path.empty()) {
158 wchar_t last_char = wsearch_path.back();
159 if (last_char != L
'\\' && last_char != L
'/') {
160 wsearch_path.push_back(L
'\\');
165 std::wstring pattern = wsearch_path + L
"*";
167 HANDLE hFind = FindFirstFileW(pattern.c_str(), &fd);
168 if (hFind != INVALID_HANDLE_VALUE) {
170 if (wcscmp(fd.cFileName, L
".") == 0 || wcscmp(fd.cFileName, L
"..") == 0)
173 std::wstring wfull_path = wsearch_path + fd.cFileName;
175 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
177 std::vector<std::string> sub_files =
get_list_files(wstring_to_utf8(wfull_path));
178 list_files.insert(list_files.end(), sub_files.begin(), sub_files.end());
181 list_files.push_back(wstring_to_utf8(wfull_path));
183 }
while (FindNextFileW(hFind, &fd));
188 std::string search_path = path;
189 if (search_path.empty()) {
190 char buffer[PATH_MAX];
191 if (getcwd(buffer, PATH_MAX)) {
192 search_path = buffer;
196 if (search_path.back() !=
'/' && search_path.back() !=
'\\') {
197 search_path.push_back(
'/');
199 DIR* dir = opendir(search_path.c_str());
201 struct dirent* entry;
202 while ((entry = readdir(dir)) !=
nullptr) {
203 std::string file_name = entry->d_name;
204 if (file_name ==
"." || file_name ==
"..")
206 std::string full_path = search_path + file_name;
208 if (stat(full_path.c_str(), &statbuf) == 0) {
209 if (S_ISDIR(statbuf.st_mode)) {
211 list_files.insert(list_files.end(), sub_files.begin(), sub_files.end());
212 }
else if (S_ISREG(statbuf.st_mode)) {
213 list_files.push_back(full_path);
227# if __cplusplus >= 201703L
228 return fs::u8path(file_path).filename().u8string();
230 size_t pos = file_path.find_last_of(
"/\\");
231 if (pos == std::string::npos)
return file_path;
232 return file_path.substr(pos + 1);
236#if __cplusplus >= 201703L
242 inline std::string
make_relative(
const std::string& file_path,
const std::string& base_path) {
243 if (base_path.empty())
return file_path;
244 std::filesystem::path fileP = std::filesystem::u8path(file_path);
245 std::filesystem::path baseP = std::filesystem::u8path(base_path);
247 std::filesystem::path relativeP = std::filesystem::relative(fileP, baseP, ec);
252 return relativeP.u8string();
262 std::wstring wide_path = utf8_to_wstring(path);
263 std::filesystem::path dir(wide_path);
265 std::filesystem::path dir = std::filesystem::u8path(path);
267 if (!std::filesystem::exists(dir)) {
269 if (!std::filesystem::create_directories(dir, ec)) {
270 throw std::runtime_error(
"Failed to create directories for path: " + dir.u8string());
290 size_t n = path.size();
293 if (n >= 1 && (path[0] ==
'/' || path[0] ==
'\\')) {
297 }
else if (n >= 2 && std::isalpha(path[0]) && path[1] ==
':') {
299 result.
root = path.substr(0, 2);
301 if (n >= 3 && (path[2] ==
'/' || path[2] ==
'\\')) {
310 while (i < n && (path[i] ==
'/' || path[i] ==
'\\')) {
315 while (j < n && path[j] !=
'/' && path[j] !=
'\\') {
319 result.
components.push_back(path.substr(i, j - i));
331 inline std::string
make_relative(
const std::string& file_path,
const std::string& base_path) {
332 if (base_path.empty())
return file_path;
342 size_t common_size = 0;
343 while (common_size < file_pc.
components.size() &&
350 std::vector<std::string> relative_components;
353 for (
size_t i = common_size; i < base_pc.
components.size(); ++i) {
354 relative_components.push_back(
"..");
358 for (
size_t i = common_size; i < file_pc.
components.size(); ++i) {
359 relative_components.push_back(file_pc.
components[i]);
363 std::string relative_path;
364 if (relative_components.empty()) {
367 for (
size_t i = 0; i < relative_components.size(); ++i) {
370 relative_path +=
'\\';
372 relative_path +=
'/';
375 relative_path += relative_components[i];
379 return relative_path;
385 inline bool is_file(
const std::string& path) {
386 size_t dot_pos = path.find_last_of(
'.');
387 size_t slash_pos = path.find_last_of(
"/\\");
388 return (dot_pos != std::string::npos && (slash_pos == std::string::npos || dot_pos > slash_pos));
395 if (path.empty())
return;
398 size_t components_size = components.size();
406 std::string current_path = path_pc.
root;
407 for (
size_t i = 0; i < components_size; ++i) {
408 if (!current_path.empty() && current_path.back() !=
'/' && current_path.back() !=
'\\') {
411 current_path += components[i];
414 if (components[i] ==
".." ||
415 components[i] ==
"/" ||
416 components[i] ==
"~/")
continue;
418 int ret = _mkdir(utf8_to_ansi(current_path).c_str());
420 int ret = mkdir(current_path.c_str(), 0755);
423 if (ret != 0 && errnum != EEXIST) {
424 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.