1#ifndef _LOGIT_PATH_UTILS_HPP_INCLUDED
2#define _LOGIT_PATH_UTILS_HPP_INCLUDED
8#if __cplusplus >= 201703L
32# if __cplusplus >= 201703L
33 namespace fs = std::filesystem;
36#if defined(__EMSCRIPTEN__)
40 inline std::vector<std::string>
get_list_files(
const std::string&) {
41 std::cerr <<
"get_list_files is not supported under Emscripten" << std::endl;
45 inline std::string
get_file_name(
const std::string& file_path) {
46 size_t pos = file_path.find_last_of(
"/\\");
47 if (pos == std::string::npos)
return file_path;
48 return file_path.substr(pos + 1);
51 inline std::string
make_relative(
const std::string& file_path,
const std::string&) {
56 std::cerr <<
"create_directories is not supported under Emscripten" << std::endl;
59 inline bool is_file(
const std::string& path) {
60 size_t dot_pos = path.find_last_of(
'.');
61 size_t slash_pos = path.find_last_of(
"/\\");
62 return (dot_pos != std::string::npos && (slash_pos == std::string::npos || dot_pos > slash_pos));
71 std::vector<wchar_t> buffer(MAX_PATH);
72 HMODULE hModule = GetModuleHandle(NULL);
75 std::size_t size =
static_cast<std::size_t
>(GetModuleFileNameW(hModule, buffer.data(),
static_cast<DWORD
>(buffer.size())));
78 while (size == buffer.size() && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
79 buffer.resize(buffer.size() * 2);
80 size =
static_cast<std::size_t
>(GetModuleFileNameW(hModule, buffer.data(),
static_cast<DWORD
>(buffer.size())));
84 throw std::runtime_error(
"Failed to get executable path.");
87 std::wstring exe_path(buffer.begin(), buffer.begin() + size);
90 size_t pos = exe_path.find_last_of(L
"\\/");
91 if (pos != std::wstring::npos) {
92 exe_path = exe_path.substr(0, pos);
96 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
97 return converter.to_bytes(exe_path);
99 char result[PATH_MAX];
100 ssize_t count = readlink(
"/proc/self/exe", result, PATH_MAX);
103 throw std::runtime_error(
"Failed to get executable path.");
106 std::string exe_path(result, count);
109 size_t pos = exe_path.find_last_of(
"\\/");
110 if (pos != std::string::npos) {
111 exe_path = exe_path.substr(0, pos);
122 std::vector<std::string> list_files;
125 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
126 std::wstring wsearch_path;
130 wchar_t buffer[MAX_PATH];
131 GetCurrentDirectoryW(MAX_PATH, buffer);
132 wsearch_path = buffer;
134 wsearch_path = converter.from_bytes(path);
138 if (!wsearch_path.empty()) {
139 wchar_t last_char = wsearch_path.back();
140 if (last_char != L
'\\' && last_char != L
'/') {
141 wsearch_path.push_back(L
'\\');
146 std::wstring pattern = wsearch_path + L
"*";
148 HANDLE hFind = FindFirstFileW(pattern.c_str(), &fd);
149 if (hFind != INVALID_HANDLE_VALUE) {
151 if (wcscmp(fd.cFileName, L
".") == 0 || wcscmp(fd.cFileName, L
"..") == 0)
154 std::wstring wfull_path = wsearch_path + fd.cFileName;
156 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
158 std::vector<std::string> sub_files =
get_list_files(converter.to_bytes(wfull_path));
159 list_files.insert(list_files.end(), sub_files.begin(), sub_files.end());
162 list_files.push_back(converter.to_bytes(wfull_path));
164 }
while (FindNextFileW(hFind, &fd));
169 std::string search_path = path;
170 if (search_path.empty()) {
171 char buffer[PATH_MAX];
172 if (getcwd(buffer, PATH_MAX)) {
173 search_path = buffer;
177 if (search_path.back() !=
'/' && search_path.back() !=
'\\') {
178 search_path.push_back(
'/');
180 DIR* dir = opendir(search_path.c_str());
182 struct dirent* entry;
183 while ((entry = readdir(dir)) !=
nullptr) {
184 std::string file_name = entry->d_name;
185 if (file_name ==
"." || file_name ==
"..")
187 std::string full_path = search_path + file_name;
189 if (stat(full_path.c_str(), &statbuf) == 0) {
190 if (S_ISDIR(statbuf.st_mode)) {
192 list_files.insert(list_files.end(), sub_files.begin(), sub_files.end());
193 }
else if (S_ISREG(statbuf.st_mode)) {
194 list_files.push_back(full_path);
208# if __cplusplus >= 201703L
209 return fs::u8path(file_path).filename().u8string();
211 size_t pos = file_path.find_last_of(
"/\\");
212 if (pos == std::string::npos)
return file_path;
213 return file_path.substr(pos + 1);
217#if __cplusplus >= 201703L
223 inline std::string
make_relative(
const std::string& file_path,
const std::string& base_path) {
224 if (base_path.empty())
return file_path;
225 std::filesystem::path fileP = std::filesystem::u8path(file_path);
226 std::filesystem::path baseP = std::filesystem::u8path(base_path);
228 std::filesystem::path relativeP = std::filesystem::relative(fileP, baseP, ec);
233 return relativeP.u8string();
243 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
244 std::wstring wide_path = converter.from_bytes(path);
245 std::filesystem::path dir(wide_path);
247 std::filesystem::path dir = std::filesystem::u8path(path);
249 if (!std::filesystem::exists(dir)) {
251 if (!std::filesystem::create_directories(dir, ec)) {
252 throw std::runtime_error(
"Failed to create directories for path: " + dir.u8string());
272 size_t n = path.size();
275 if (n >= 1 && (path[0] ==
'/' || path[0] ==
'\\')) {
279 }
else if (n >= 2 && std::isalpha(path[0]) && path[1] ==
':') {
281 result.
root = path.substr(0, 2);
283 if (n >= 3 && (path[2] ==
'/' || path[2] ==
'\\')) {
292 while (i < n && (path[i] ==
'/' || path[i] ==
'\\')) {
297 while (j < n && path[j] !=
'/' && path[j] !=
'\\') {
301 result.
components.push_back(path.substr(i, j - i));
313 std::string
make_relative(
const std::string& file_path,
const std::string& base_path) {
314 if (base_path.empty())
return file_path;
324 size_t common_size = 0;
325 while (common_size < file_pc.
components.size() &&
332 std::vector<std::string> relative_components;
335 for (
size_t i = common_size; i < base_pc.
components.size(); ++i) {
336 relative_components.push_back(
"..");
340 for (
size_t i = common_size; i < file_pc.
components.size(); ++i) {
341 relative_components.push_back(file_pc.
components[i]);
345 std::string relative_path;
346 if (relative_components.empty()) {
349 for (
size_t i = 0; i < relative_components.size(); ++i) {
352 relative_path +=
'\\';
354 relative_path +=
'/';
357 relative_path += relative_components[i];
361 return relative_path;
367 inline bool is_file(
const std::string& path) {
368 size_t dot_pos = path.find_last_of(
'.');
369 size_t slash_pos = path.find_last_of(
"/\\");
370 return (dot_pos != std::string::npos && (slash_pos == std::string::npos || dot_pos > slash_pos));
377 if (path.empty())
return;
380 size_t components_size = components.size();
388 std::string current_path = path_pc.
root;
389 for (
size_t i = 0; i < components_size; ++i) {
390 if (!current_path.empty() && current_path.back() !=
'/' && current_path.back() !=
'\\') {
393 current_path += components[i];
396 if (components[i] ==
".." ||
397 components[i] ==
"/" ||
398 components[i] ==
"~/")
continue;
400 int ret = _mkdir(utf8_to_ansi(current_path).c_str());
402 int ret = mkdir(current_path.c_str(), 0755);
405 if (ret != 0 && errnum != EEXIST) {
406 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.