ADTF
Loading...
Searching...
No Matches
a_util::filesystem::Path Class Reference

#include <path.h>

Public Types

enum  PathSeparator { PS_ForwardSlash = 0 , PS_BackwardSlash = 1 , PS_Native = 2 }
 Path separator types. More...
 

Public Member Functions

 Path ()
 Default CTOR for an empty path.
 
 Path (const Path &path)
 
Pathoperator= (const Path &path)
 
 Path (const std::string &path)
 
 Path (const char *path)
 
 ~Path ()
 DTOR.
 
 operator std::string () const
 
std::string toString (PathSeparator separator=PS_Native) const
 
bool setPath (const std::string &path)
 
Pathappend (const Path &path)
 
Path getRoot () const
 
Path getRootName () const
 
Path getRootPath () const
 
Path getParent () const
 
Path getLastElement () const
 
std::string getExtension () const
 
PathappendToBasename (const std::string &str)
 
PathreplaceExtension (const std::string &extension)
 
PathremoveLastElement ()
 
PathmakeCanonical ()
 
PathmakeAbsolute ()
 
PathmakeAbsolute (Path base_path)
 
PathmakeRelative ()
 
PathmakeRelative (Path input_path)
 
bool isRelative () const
 
bool isAbsolute () const
 
bool isBaseOf (const Path &path) const
 
bool isEmpty () const
 
PathClear ()
 

Friends

bool operator== (const Path &lhs, const Path &rhs)
 

Detailed Description

File/Directory path class

This class represents paths on the filesystem. Only syntactic aspects of paths are handled, the path may or may not exist and might not even be allowed to exist on the current platform.

Member Enumeration Documentation

◆ PathSeparator

Path separator types.

Enumerator
PS_ForwardSlash 

Unix slash.

PS_BackwardSlash 

Windows slash.

PS_Native 

Native slash, depending on the current platform.

Constructor & Destructor Documentation

◆ Path() [1/3]

a_util::filesystem::Path::Path ( const Path & path)

Copy CTOR

Parameters
[in]pathThe instance to copy from

◆ Path() [2/3]

a_util::filesystem::Path::Path ( const std::string & path)

Initializing CTOR

Note
If the path is invalid, the instance is initialized to the empty state
Parameters
[in]pathThe file or directory path to initialize the instance

◆ Path() [3/3]

a_util::filesystem::Path::Path ( const char * path)

Initializing CTOR

Note
If the path is invalid, the instance is initialized to the empty state
Parameters
[in]pathThe file or directory path to initialize the instance

Member Function Documentation

◆ append()

Path & a_util::filesystem::Path::append ( const Path & path)

Appends a relative file or directory path or another file extension to the current path

Note
This method will throw a PathException if path is absolute
Parameters
[in]pathThe (relative) (file-)path or file extension to append
Returns
*this
Exceptions
PathExceptionIf path is absolute.

◆ appendToBasename()

Path & a_util::filesystem::Path::appendToBasename ( const std::string & str)

Append a string to a basename

Note
If the path is a file path, the string is inserted before the extension.
If it's a directory path, the string is added.
Parameters
[in]strString to append to the basename
Return values
*thiswith str appended to the basename

◆ Clear()

Path & a_util::filesystem::Path::Clear ( )

Clears the path and resets it to the empty state

Returns
Returns *this, to be able to chain additional method calls

◆ getExtension()

std::string a_util::filesystem::Path::getExtension ( ) const

Get the extension of the file (without the '.')

e.g.: "/dir/subdir/file.txt" returns "txt", "/dir/subdir" returns ""

Returns
The extension or an empty string if the path is a directory or has no extension

◆ getLastElement()

Path a_util::filesystem::Path::getLastElement ( ) const

Get the name of the directory or file

e.g.: "/dir/subdir/file.txt" returns "file.txt", "/dir/subdir" returns "subdir"

Returns
The last element of the path

◆ getParent()

Path a_util::filesystem::Path::getParent ( ) const

Get the parent path of the directory or file

e.g.: "/dir/subdir/file.txt" returns "/dir/subdir", "/dir/subdir" returns "/dir"

Note
If called on a root path, *this is returned
Returns
A path instance pointing to the parent directory (or root)

◆ getRoot()

Path a_util::filesystem::Path::getRoot ( ) const

Get the root path of the directory/file

Note
: For network paths, the host is returned (e.g. "\\netdrive\test" returns "\\netdrive")

Usually "C:" or "D:" on Windows, on Unix-like systems usually "/". e.g.: "/dir/subdir/file.txt" returns "/", "C:/dir" returns "C:"

Deprecated
Use getRootName() instead.
Returns
A path instance pointing to the root directory

◆ getRootName()

Path a_util::filesystem::Path::getRootName ( ) const

Get the root name of the directory/file

Note
: For network paths, the host name is returned (e.g. "\\netdrive\test" returns "\\netdrive")

Usually "C:" or "D:" on Windows, on Unix-like systems usually "/". e.g.: "/dir/subdir/file.txt" returns "/", "C:/dir" returns "C:"

Returns
A path name instance pointing to the root directory

◆ getRootPath()

Path a_util::filesystem::Path::getRootPath ( ) const

Get the root path of the directory/file

Note
: For network paths, the host path is returned (e.g. "\\netdrive\test" returns "\\netdrive\")

Usually "C:\" or "D:" on Windows, on Unix-like systems usually "/". e.g.: "/dir/subdir/file.txt" returns "/", "C:/dir" returns "C:/"

Returns
A path instance pointing to the root directory

◆ isAbsolute()

bool a_util::filesystem::Path::isAbsolute ( ) const

Returns whether the current path is absolute

Note
This Method throws a PathException is the current path is empty
Return values
trueif the path is absolute, false otherwise

◆ isBaseOf()

bool a_util::filesystem::Path::isBaseOf ( const Path & path) const

Returns whether the current path is a child path of the target path

Note
This Method throws a PathException is the current path is empty
Return values
trueif the path is a child, false otherwise

◆ isEmpty()

bool a_util::filesystem::Path::isEmpty ( ) const

Returns whether the current path is empty

Return values
trueif the path is empty, false otherwise

◆ isRelative()

bool a_util::filesystem::Path::isRelative ( ) const

Returns whether the current path is relative

Return values
trueif the path is relative, false otherwise
Exceptions
PathExceptionif the current path is empty

◆ makeAbsolute() [1/2]

Path & a_util::filesystem::Path::makeAbsolute ( )

Convert the path to an absolute path relative to the current working directory

Returns
*this containing the absolute path

◆ makeAbsolute() [2/2]

Path & a_util::filesystem::Path::makeAbsolute ( Path base_path)

Convert the path to an absolute path relative to the given base_path

Parameters
[in]base_pathReference to which the absolute path is converted
Returns
*this containing the absolute path

◆ makeCanonical()

Path & a_util::filesystem::Path::makeCanonical ( )

Make the current path canonical.

Note
If the path is already canonical, no changes are made

This means that all occurences of '.' and '..' are evaluated syntactically. e.g.: "/./dir/../dir/file.txt" is replaced by "/file.txt".

Returns
*this

◆ makeRelative() [1/2]

Path & a_util::filesystem::Path::makeRelative ( )

Convert the path to a relative path relative to the current working directory

Returns
*this containing the relative path

◆ makeRelative() [2/2]

Path & a_util::filesystem::Path::makeRelative ( Path input_path)

Convert the path to a relative path relative to the input_path

Parameters
[in]input_pathReference to which the relative path is converted
Returns
*this containing the relative path
Exceptions
PathExceptionIf the current path and the input_path have different roots

◆ operator std::string()

a_util::filesystem::Path::operator std::string ( ) const

Converts the path to the systems native string representation

Return values
Thestring representation of the path (or empty string for empty path), using the platform native separator

◆ operator=()

Path & a_util::filesystem::Path::operator= ( const Path & path)

Assignment Operator

Parameters
[in]pathThe instance to copy from return Reference to *this

◆ removeLastElement()

Path & a_util::filesystem::Path::removeLastElement ( )

Remove the last part of the path, making it a directory path.

Note
Does nothing if the current path already is at the root.
Returns
*this

◆ replaceExtension()

Path & a_util::filesystem::Path::replaceExtension ( const std::string & extension)

Replace the extension of the path

Note
If the path is a file path, the extension is replaced. If it's a directory path, the extension is added, making it a file path in the process
Parameters
[in]extensionThe new extension (without '.')
Returns
*this

◆ setPath()

bool a_util::filesystem::Path::setPath ( const std::string & path)

Set the current path of the instance, handling platform aspects or any trailing slashes.

Parameters
[in]pathThe new path
Returns
false if the path is invalid and cannot be set, true on success

◆ toString()

std::string a_util::filesystem::Path::toString ( PathSeparator separator = PS_Native) const

Get the string representation of the current path using the specified separator

Parameters
[in]separatorThe separator to use
Return values
Thestring representation of the path (or empty string for empty path)

Friends And Related Symbol Documentation

◆ operator==

bool operator== ( const Path & lhs,
const Path & rhs )
friend

Check two paths for equality

Note
The paths are compared syntactically, any canonicalization must be done manually!
Parameters
[in]lhsRight hand side object
[in]rhsLeft hand side object
Return values
trueif the paths are equal, false otherwise

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