rcpputils  master
C++ API providing common utilities and data structures.
Classes | Functions
rcpputils::fs Namespace Reference

Classes

class  path
 Drop-in replacement for std::filesystem::path. More...
 

Functions

bool is_regular_file (const path &p) noexcept
 Check if the path is a regular file. More...
 
bool is_directory (const path &p) noexcept
 Check if the path is a directory. More...
 
uint64_t file_size (const path &p)
 Get the file size of the path. More...
 
bool exists (const path &path_to_check)
 Check if a path exists. More...
 
path temp_directory_path ()
 Get a path to a location in the temporary directory, if it's available. More...
 
path create_temp_directory (const std::string &base_name, const path &parent_path=temp_directory_path())
 Construct a uniquely named temporary directory, in "parent", with format base_nameXXXXXX. More...
 
path current_path ()
 Return current working directory. More...
 
bool create_directories (const path &p)
 Create a directory with the given path p. More...
 
bool remove (const path &p)
 Remove the file or directory at the path p. More...
 
bool remove_all (const path &p)
 Remove the directory at the path p and its content. More...
 
path remove_extension (const path &file_path, int n_times=1)
 Remove extension(s) from a path. More...
 
bool operator== (const path &a, const path &b)
 Compare two paths for equality. More...
 
bool operator!= (const path &a, const path &b)
 
std::ostreamoperator<< (std::ostream &os, const path &p)
 Convert the path to a string for ostream usage, such as in logging or string formatting. More...
 

Function Documentation

◆ is_regular_file()

bool rcpputils::fs::is_regular_file ( const path p)
noexcept

Check if the path is a regular file.

Parameters
pThe path to check
Returns
True if the path exists, false otherwise.

◆ is_directory()

bool rcpputils::fs::is_directory ( const path p)
noexcept

Check if the path is a directory.

Parameters
pThe path to check
Returns
True if the path is an existing directory, false otherwise.

◆ file_size()

uint64_t rcpputils::fs::file_size ( const path p)

Get the file size of the path.

Parameters
pThe path to get the file size of.
Returns
The file size in bytes.
Exceptions
std::sytem_error

◆ exists()

bool rcpputils::fs::exists ( const path path_to_check)

Check if a path exists.

Parameters
path_to_checkThe path to check.
Returns
True if the path exists, false otherwise.

◆ temp_directory_path()

path rcpputils::fs::temp_directory_path ( )

Get a path to a location in the temporary directory, if it's available.

This does not create any directories. On Windows, this uses "GetTempPathA" On non-Windows, this prefers the environment variable TMPDIR, falling back to /tmp

Returns
A path to a directory for storing temporary files and directories.

◆ create_temp_directory()

path rcpputils::fs::create_temp_directory ( const std::string base_name,
const path parent_path = temp_directory_path() 
)

Construct a uniquely named temporary directory, in "parent", with format base_nameXXXXXX.

The output, if successful, is guaranteed to be a newly-created directory. The underlying implementation keeps generating paths until one that does not exist is found. This guarantees that there will be no existing files in the returned directory.

Parameters
base_nameUser-specified portion of the created directory
parentThe parent path of the directory that will be created
Returns
A path to a newly-created directory with base_name and a 6-character unique suffix
Exceptions
std::system_errorIf any OS APIs do not succeed.

◆ current_path()

path rcpputils::fs::current_path ( )

Return current working directory.

Returns
The current working directory.
Exceptions
std::system_error

◆ create_directories()

bool rcpputils::fs::create_directories ( const path p)

Create a directory with the given path p.

This builds directories recursively and will skip directories if they are already created.

Returns
Return true if the directory already exists or is created, false otherwise.

◆ remove()

bool rcpputils::fs::remove ( const path p)

Remove the file or directory at the path p.

Parameters
pThe path of the object to remove.
Returns
true if the file exists and it was successfully removed, false otherwise.

◆ remove_all()

bool rcpputils::fs::remove_all ( const path p)

Remove the directory at the path p and its content.

Additionally to

See also
remove, remove_all removes a directory and its containing files.
Parameters
Thepath of the directory to remove.
Returns
true if the directory exists and it was successfully removed, false otherwise.

◆ remove_extension()

path rcpputils::fs::remove_extension ( const path file_path,
int  n_times = 1 
)

Remove extension(s) from a path.

An extension is defined as text starting from the end of a path to the first period (.) character.

Parameters
file_pathThe file path string.
n_timesThe number of extensions to remove if there are multiple extensions.
Returns
The path object.

◆ operator==()

bool rcpputils::fs::operator== ( const path a,
const path b 
)

Compare two paths for equality.

Returns
True if both paths are equal as strings.

◆ operator!=()

bool rcpputils::fs::operator!= ( const path a,
const path b 
)

◆ operator<<()

std::ostream& rcpputils::fs::operator<< ( std::ostream os,
const path p 
)

Convert the path to a string for ostream usage, such as in logging or string formatting.

Parameters
osThe stream to send the path string to
pThe path to stringify
Returns
The ostream, for chaining