1#ifndef _LOGIT_PATH_UTILS_HPP_INCLUDED
2#define _LOGIT_PATH_UTILS_HPP_INCLUDED
7#if __cplusplus >= 201703L
31# if __cplusplus >= 201703L
32 namespace fs = std::filesystem;
39 std::vector<wchar_t> buffer(MAX_PATH);
40 HMODULE hModule = GetModuleHandle(NULL);
43 DWORD size = GetModuleFileNameW(hModule, buffer.data(), buffer.size());
46 while (size == buffer.size() && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
47 buffer.resize(buffer.size() * 2);
48 size = GetModuleFileNameW(hModule, buffer.data(), buffer.size());
52 throw std::runtime_error(
"Failed to get executable path.");
55 std::wstring exe_path(buffer.begin(), buffer.begin() + size);
58 size_t pos = exe_path.find_last_of(L
"\\/");
59 if (pos != std::wstring::npos) {
60 exe_path = exe_path.substr(0, pos);
64 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
65 return converter.to_bytes(exe_path);
67 char result[PATH_MAX];
68 ssize_t count = readlink(
"/proc/self/exe", result, PATH_MAX);
71 throw std::runtime_error(
"Failed to get executable path.");
74 std::string exe_path(result, count);
77 size_t pos = exe_path.find_last_of(
"\\/");
78 if (pos != std::string::npos) {
79 exe_path = exe_path.substr(0, pos);
90 std::vector<std::string> list_files;
93 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
94 std::wstring wsearch_path;
98 wchar_t buffer[MAX_PATH];
99 GetCurrentDirectoryW(MAX_PATH, buffer);
100 wsearch_path = buffer;
102 wsearch_path = converter.from_bytes(path);
106 if (!wsearch_path.empty()) {
107 wchar_t last_char = wsearch_path.back();
108 if (last_char != L
'\\' && last_char != L
'/') {
109 wsearch_path.push_back(L
'\\');
114 std::wstring pattern = wsearch_path + L
"*";
116 HANDLE hFind = FindFirstFileW(pattern.c_str(), &fd);
117 if (hFind != INVALID_HANDLE_VALUE) {
119 if (wcscmp(fd.cFileName, L
".") == 0 || wcscmp(fd.cFileName, L
"..") == 0)
122 std::wstring wfull_path = wsearch_path + fd.cFileName;
124 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
126 std::vector<std::string> sub_files =
get_list_files(converter.to_bytes(wfull_path));
127 list_files.insert(list_files.end(), sub_files.begin(), sub_files.end());
130 list_files.push_back(converter.to_bytes(wfull_path));
132 }
while (FindNextFileW(hFind, &fd));
137 std::string search_path = path;
138 if (search_path.empty()) {
139 char buffer[PATH_MAX];
140 if (getcwd(buffer, PATH_MAX)) {
141 search_path = buffer;
145 if (search_path.back() !=
'/' && search_path.back() !=
'\\') {
146 search_path.push_back(
'/');
148 DIR* dir = opendir(search_path.c_str());
150 struct dirent* entry;
151 while ((entry = readdir(dir)) !=
nullptr) {
152 std::string file_name = entry->d_name;
153 if (file_name ==
"." || file_name ==
"..")
155 std::string full_path = search_path + file_name;
157 if (stat(full_path.c_str(), &statbuf) == 0) {
158 if (S_ISDIR(statbuf.st_mode)) {
160 list_files.insert(list_files.end(), sub_files.begin(), sub_files.end());
161 }
else if (S_ISREG(statbuf.st_mode)) {
162 list_files.push_back(full_path);
176 size_t pos = file_path.find_last_of(
"/\\");
177 if (pos == std::string::npos)
return file_path;
178 return file_path.substr(pos + 1);
181#if __cplusplus >= 201703L
187 inline std::string
make_relative(
const std::string& file_path,
const std::string& base_path) {
188 if (base_path.empty())
return file_path;
189 std::filesystem::path fileP = std::filesystem::u8path(file_path);
190 std::filesystem::path baseP = std::filesystem::u8path(base_path);
192 std::filesystem::path relativeP = std::filesystem::relative(fileP, baseP, ec);
197 return relativeP.u8string();
207 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
208 std::wstring wide_path = converter.from_bytes(path);
209 std::filesystem::path dir(wide_path);
211 std::filesystem::path dir(path);
213 if (!std::filesystem::exists(dir)) {
215 if (!std::filesystem::create_directories(dir, ec)) {
216 throw std::runtime_error(
"Failed to create directories for path: " + path);
236 size_t n = path.size();
239 if (n >= 1 && (path[0] ==
'/' || path[0] ==
'\\')) {
243 }
else if (n >= 2 && std::isalpha(path[0]) && path[1] ==
':') {
245 result.
root = path.substr(0, 2);
247 if (n >= 3 && (path[2] ==
'/' || path[2] ==
'\\')) {
256 while (i < n && (path[i] ==
'/' || path[i] ==
'\\')) {
261 while (j < n && path[j] !=
'/' && path[j] !=
'\\') {
265 result.
components.push_back(path.substr(i, j - i));
277 std::string
make_relative(
const std::string& file_path,
const std::string& base_path) {
278 if (base_path.empty())
return file_path;
279 PathComponents file_pc =
split_path(file_path);
280 PathComponents base_pc =
split_path(base_path);
283 if (file_pc.root != base_pc.root) {
288 size_t common_size = 0;
289 while (common_size < file_pc.components.size() &&
290 common_size < base_pc.components.size() &&
291 file_pc.components[common_size] == base_pc.components[common_size]) {
296 std::vector<std::string> relative_components;
299 for (
size_t i = common_size; i < base_pc.components.size(); ++i) {
300 relative_components.push_back(
"..");
304 for (
size_t i = common_size; i < file_pc.components.size(); ++i) {
305 relative_components.push_back(file_pc.components[i]);
309 std::string relative_path;
310 if (relative_components.empty()) {
313 for (
size_t i = 0; i < relative_components.size(); ++i) {
316 relative_path +=
'\\';
318 relative_path +=
'/';
321 relative_path += relative_components[i];
325 return relative_path;
331 inline bool is_file(
const std::string& path) {
332 size_t dot_pos = path.find_last_of(
'.');
333 size_t slash_pos = path.find_last_of(
"/\\");
334 return (dot_pos != std::string::npos && (slash_pos == std::string::npos || dot_pos > slash_pos));
341 if (path.empty())
return;
343 auto &components = path_pc.components;
344 size_t components_size = components.size();
352 std::string current_path = path_pc.root;
353 for (
size_t i = 0; i < components_size; ++i) {
354 if (!current_path.empty() && current_path.back() !=
'/' && current_path.back() !=
'\\') {
357 current_path += components[i];
360 if (components[i] ==
".." ||
361 components[i] ==
"/" ||
362 components[i] ==
"~/")
continue;
366 int ret = mkdir(current_path.c_str(), 0755);
369 if (ret != 0 && errnum != EEXIST) {
370 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 using C++17 std::filesystem.
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.
std::string utf8_to_ansi(const std::string &utf8)
Converts a UTF-8 string to an ANSI string (Windows-specific).
void create_directories(const std::string &path)
Creates directories recursively for the given path using C++17 std::filesystem.
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.