Path#

File path retrieval and manipulation functions.

Info

To use declarations from this page, include the <essence/path.hpp> header.

std::string temp()#

Get the path to the systems’ canonical temporary directory.

Reads the environment for the value of TMPDIR if set, otherwise falling back to /tmp if it exists. If neither work, an exception is thrown.

std::optional<std::string> path::which(std::string_view name)#

Search the PATH environment variable directories for executable name.

Example

auto result_a = path::which("gcc");
auto result_b = path::which("some-non-existent-executable");

// *result_a holds: "/usr/bin/gcc"
// result_b holds: std::nullopt
std::string path::base(std::string_view name)#

Return the basename of name.

Example

std::string compiler { "/usr/bin/gcc" };
auto result = path::base(compiler);
// result holds: "gcc"
std::string path::dir(std::string_view name)#

Return the directory name of name.

Example

std::string compiler { "/usr/bin/gcc" };
auto result = path::dir(compiler);
// result holds: "/usr/bin"
std::string path::home()#

Return the users’ home directory path.

std::string &path::expandhome(std::string &path)#

Expand tilde (~) to the users’ home directory (in-place).

Example

// In this example, the invoking user is 'wbr'
std::string screenshots_dir { "~/pictures/screenshots" };
path::expandhome(screenshots_dir);
// screenshots_dir now holds: "/home/wbr/pictures/screenshots"
std::string path::cexpandhome(std::string_view path)#

Expand tilde (~) to the users’ home directory (copying).

std::string &path::unexpandhome(std::string &path)#

Reduce the users’ home directory to tilde (~), in-place.

Example

std::string screenshots_dir { "/home/wbr/pictures/screenshots" };
path::unexpandhome(screenshots_dir);
// screenshots_dir now holds: "~/pictures/screenshots"
std::string path::cunexpandhome(std::string_view path)#

Reduce the users’ home directory to tilde (~), copying.