39#include <system_error>
55 explicit path(
const std::filesystem::path&
path)
56 : _base_path(normalize(
path)) {}
67 operator std::filesystem::path()
86 std::list<std::filesystem::path>
get_subdirs(
const std::regex& filter = std::regex(
".*"))
88 std::list<std::filesystem::path> directories_list;
90 if (std::filesystem::exists(_base_path) && std::filesystem::is_directory(_base_path))
92 for (
auto it = std::filesystem::directory_iterator(_base_path); it != std::filesystem::directory_iterator(); ++it)
94 bool is_directory = std::filesystem::is_directory(*it);
95 std::string path_str = it->path().string();
97 if (is_directory && std::regex_search(path_str, filter))
99 directories_list.emplace_back(path_str);
104 return directories_list;
124 using namespace std::string_literals;
126 bool success =
false;
127 std::string msg =
"unknown error"s;
130 if (delete_prior_creating)
132 std::filesystem::remove_all(_base_path);
135 if (!std::filesystem::exists(_base_path))
140 std::filesystem::create_directories(_base_path);
142 success = std::filesystem::exists(_base_path);
144 catch (std::exception& ex)
151 std::string err_msg(
"cbeam::filesystem::path::create(\"" + _base_path.string() +
"\"): " + msg);
166 void copy_to(
const std::filesystem::path& target)
const
168 if (!std::filesystem::exists(_base_path) || !std::filesystem::is_directory(_base_path))
170 throw cbeam::error::runtime_error(
"cbeam::filesystem::path::copy_to: source directory " + _base_path.string() +
" does not exist or is not a directory");
172 if (std::filesystem::exists(target))
174 path(target).remove();
177 std::filesystem::copy(_base_path, target, std::filesystem::copy_options::recursive);
195 std::error_code
error;
203 std::filesystem::rename(_base_path, temp_path,
error);
208 std::filesystem::remove_all(temp_path,
error);
211 std::filesystem::rename(temp_path, _base_path,
error);
229 return _base_path == other._base_path;
245 return _base_path != other._base_path;
261 return _base_path == other;
277 return _base_path != other;
293 std::string s = p.string();
296 while (!s.empty() && (s.back() == std::filesystem::path::preferred_separator || s.back() ==
'/' || s.back() ==
'\\'))
300 return std::filesystem::path(s);
319 static std::filesystem::path normalize(
const std::filesystem::path& p)
321 using namespace std::string_literals;
322 std::filesystem::path result;
324 std::size_t numParent = 0;
327 while (it != p.begin())
330 if (it->string() !=
"/" && it->string() !=
"\\" && !it->empty())
332 if (it->string() ==
"..")
336 else if (numParent > 0)
342 std::string colon =
":"s;
343 if (std::equal(colon.rbegin(), colon.rend(), it->string().rbegin()))
346 result = (it->wstring() + std::filesystem::path::preferred_separator) / result;
348 result = (it->string() + std::filesystem::path::preferred_separator) / result;
353 result = *it / result;
359 if (*p.begin() ==
"/"s)
361 result = *p.begin() / result;
367 std::filesystem::path _base_path;
A Cbeam-specific runtime error that also acts like std::runtime_error.
Definition runtime_error.hpp:46
void create_directory(bool delete_prior_creating=false) const
Creates the directory specified by _base_path.
Definition path.hpp:122
void copy_to(const std::filesystem::path &target) const
Copies the directory specified by _base_path to a target location.
Definition path.hpp:166
bool operator!=(const path &other) const
Inequality comparison operator for cbeam::filesystem::path.
Definition path.hpp:243
std::list< std::filesystem::path > get_subdirs(const std::regex &filter=std::regex(".*"))
Retrieves a list of subdirectories matching a given regular expression filter.
Definition path.hpp:86
operator std::filesystem::path()
Cast operator to std::filesystem::path.
Definition path.hpp:67
bool operator==(const path &other) const
Equality comparison operator for cbeam::filesystem::path.
Definition path.hpp:227
bool operator==(const std::filesystem::path &other) const
Equality comparison operator for cbeam::filesystem::path.
Definition path.hpp:259
path(const std::filesystem::path &path)
Explicit constructor for cbeam::filesystem::path.
Definition path.hpp:55
bool operator!=(const std::filesystem::path &other) const
Inequality comparison operator for cbeam::filesystem::path.
Definition path.hpp:275
void remove() const
Removes the directory specified by _base_path.
Definition path.hpp:193
static std::filesystem::path remove_trailing_directory_separators(const std::filesystem::path &p)
Removes trailing directory separators from a given path.
Definition path.hpp:291
Defines Cbeam-specific exception types that behave like their standard counterparts....
Definition base_error.hpp:31
Facilitates file I/O, path normalization, and directory operations in a cross-platform manner....
Definition io.hpp:36
std::string random_string(std::string::size_type length, std::mt19937 &gen=default_generator())
Generates a random string of specified length.
Definition generators.hpp:67