Cbeam
Loading...
Searching...
No Matches
cbeam::filesystem::path Class Reference

#include <path.hpp>

Public Member Functions

 path (const std::filesystem::path &path)
 Explicit constructor for cbeam::filesystem::path.
 
 operator std::filesystem::path ()
 Cast operator to std::filesystem::path.
 
std::list< std::filesystem::path > get_subdirs (const std::regex &filter=std::regex(".*"))
 Retrieves a list of subdirectories matching a given regular expression filter.
 
void create_directory (bool delete_prior_creating=false) const
 Creates the directory specified by _base_path.
 
void copy_to (const std::filesystem::path &target) const
 Copies the directory specified by _base_path to a target location.
 
void remove () const
 Removes the directory specified by _base_path.
 
bool operator== (const path &other) const
 Equality comparison operator for cbeam::filesystem::path.
 
bool operator!= (const path &other) const
 Inequality comparison operator for cbeam::filesystem::path.
 
bool operator== (const std::filesystem::path &other) const
 Equality comparison operator for cbeam::filesystem::path.
 
bool operator!= (const std::filesystem::path &other) const
 Inequality comparison operator for cbeam::filesystem::path.
 

Static Public Member Functions

static std::filesystem::path remove_trailing_directory_separators (const std::filesystem::path &p)
 Removes trailing directory separators from a given path.
 

Constructor & Destructor Documentation

◆ path()

cbeam::filesystem::path::path ( const std::filesystem::path & path)
inlineexplicit

Explicit constructor for cbeam::filesystem::path.

Constructs a new path object by normalizing the provided std::filesystem::path. The normalization ensures that the path is in a canonical and consistent format, which is useful for reliable path comparisons and operations.

Parameters
pathThe std::filesystem::path object to be normalized and used as the base path.

Member Function Documentation

◆ copy_to()

void cbeam::filesystem::path::copy_to ( const std::filesystem::path & target) const
inline

Copies the directory specified by _base_path to a target location.

This function copies the entire directory pointed to by _base_path to the specified target path. If the target directory already exists, it will be removed before copying. The copy operation is recursive, including all subdirectories and files.

Parameters
targetThe std::filesystem::path object representing the target directory to copy to.
Exceptions
cbeam::error::runtime_errorThrows an exception if the source directory does not exist, is not a directory, or an existing target directory cannot be removed.

◆ create_directory()

void cbeam::filesystem::path::create_directory ( bool delete_prior_creating = false) const
inline

Creates the directory specified by _base_path.

This function attempts to create a new directory based on the _base_path member. If 'delete_prior_creating' is set to true, any existing directory at this path will be deleted before creating the new one. Exception handling is implemented to catch and throw errors during the creation process.

Parameters
delete_prior_creatingIf true, deletes the existing directory at _base_path before creating a new one. Default is false.
Exceptions
cbeam::error::runtime_errorThrows an exception if the directory cannot be created. This corresponds to the return code of std::filesystem::create_directories with one exception: Under Windows, it is regarded as an error if the path ends with a '\' separator, even if the directory has been created successfully. In contrast, this method only regards it is an error if the directory has not been created (being consist with Linux and macOS, where directory paths with trailing '/' are common).

◆ get_subdirs()

std::list< std::filesystem::path > cbeam::filesystem::path::get_subdirs ( const std::regex & filter = std::regex(".*"))
inline

Retrieves a list of subdirectories matching a given regular expression filter.

This function scans the directory specified by the _base_path member and collects all subdirectories that match the provided regular expression filter. By default, it matches all subdirectories. Under Windows, if a subdirectory is represented by a shortcut (*.LNK file), it will be resolved and included in its resolved form in the resulting list. This ensures that the list reflects the actual directories rather than the shortcut files.

Parameters
filterA std::regex object representing the filter to apply. The default value ".*" matches all subdirectories. The filter is applied to the resolved path names.
Returns
A std::list of std::filesystem::path objects representing the filtered subdirectories.

◆ operator std::filesystem::path()

cbeam::filesystem::path::operator std::filesystem::path ( )
inline

Cast operator to std::filesystem::path.

This operator allows the cbeam::filesystem::path object to be implicitly converted to std::filesystem::path. It returns the underlying _base_path member, facilitating seamless integration and use with standard filesystem functions.

Returns
A std::filesystem::path object representing the _base_path member.

◆ operator!=() [1/2]

bool cbeam::filesystem::path::operator!= ( const path & other) const
inline

Inequality comparison operator for cbeam::filesystem::path.

Compares this normalized path object with another for inequality. Two path objects are considered unequal if their normalized _base_path members are different. This ensures that paths in different formats representing different directories or files are correctly identified as unequal.

Parameters
otherAnother cbeam::filesystem::path object to compare with.
Returns
true if the path objects represent different normalized paths, false otherwise.

◆ operator!=() [2/2]

bool cbeam::filesystem::path::operator!= ( const std::filesystem::path & other) const
inline

Inequality comparison operator for cbeam::filesystem::path.

Compares this normalized path object with a std::filesystem::path object for inequality. The comparison is based on the normalized _base_path member of this object and the std::filesystem::path object.

Parameters
otherA std::filesystem::path object to compare with this object.
Returns
true if this path object and the other std::filesystem::path object represent different normalized paths, false otherwise.

◆ operator==() [1/2]

bool cbeam::filesystem::path::operator== ( const path & other) const
inline

Equality comparison operator for cbeam::filesystem::path.

Compares this normalized path object with another for equality. Two path objects are considered equal if their normalized _base_path members are the same. This ensures that paths in different formats representing the same directory or file are correctly identified as equal.

Parameters
otherAnother cbeam::filesystem::path object to compare with.
Returns
true if both path objects represent the same normalized path, false otherwise.

◆ operator==() [2/2]

bool cbeam::filesystem::path::operator== ( const std::filesystem::path & other) const
inline

Equality comparison operator for cbeam::filesystem::path.

Compares this normalized path object with a std::filesystem::path object for equality. The comparison is based on the normalized _base_path member of this object and the std::filesystem::path object.

Parameters
otherA std::filesystem::path object to compare with this object.
Returns
true if this path object and the other std::filesystem::path object represent the same normalized path, false otherwise.

◆ remove()

void cbeam::filesystem::path::remove ( ) const
inline

Removes the directory specified by _base_path.

This function attempts to securely remove the directory pointed to by _base_path. It first renames the directory to avoid partial deletion issues, especially on Windows. The renaming involves appending a random string to ensure uniqueness. If the rename operation fails, an exception is thrown. After renaming, the directory is deleted. If the removal fails, the directory is renamed back to its original name and an exception is thrown. This approach aims to mitigate risks associated with partial deletions and improve the reliability of the removal process.

Exceptions
cbeam::error::runtime_errorThrows an exception if renaming or removal fails.

◆ remove_trailing_directory_separators()

static std::filesystem::path cbeam::filesystem::path::remove_trailing_directory_separators ( const std::filesystem::path & p)
inlinestatic

Removes trailing directory separators from a given path.

This static function cleans the input path by removing any trailing directory separators. It accounts for different types of separators (forward slash, backslash, and the platform's preferred separator) to ensure compatibility across various platforms. This function is useful for normalizing paths before processing them further.

Parameters
pThe std::filesystem::path object representing the path to be processed.
Returns
A new std::filesystem::path object with trailing directory separators removed.

The documentation for this class was generated from the following file: