ADTF  3.18.2
Path

File/Directory path class. More...

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)
 Copy CTOR. More...
 
Pathoperator= (const Path &path)
 Assignment Operator. More...
 
 Path (const std::string &path)
 Initializing CTOR. More...
 
 Path (const char *path)
 Initializing CTOR. More...
 
 ~Path ()
 DTOR.
 
 operator std::string () const
 Converts the path to the systems native string representation. More...
 
std::string toString (PathSeparator separator=PS_Native) const
 Get the string representation of the current path using the specified separator. More...
 
bool setPath (const std::string &path)
 Set the current path of the instance, handling platform aspects or any trailing slashes. More...
 
Pathappend (const Path &path)
 Appends a relative file or directory path or another file extension to the current path. More...
 
Path getRoot () const
 Get the root path of the directory/file. More...
 
Path getRootName () const
 Get the root name of the directory/file. More...
 
Path getRootPath () const
 Get the root path of the directory/file. More...
 
Path getParent () const
 Get the parent path of the directory or file. More...
 
Path getLastElement () const
 Get the name of the directory or file. More...
 
std::string getExtension () const
 Get the extension of the file (without the '. More...
 
PathappendToBasename (const std::string &str)
 Append a string to a basename. More...
 
PathreplaceExtension (const std::string &extension)
 Replace the extension of the path. More...
 
PathremoveLastElement ()
 Remove the last part of the path, making it a directory path. More...
 
PathmakeCanonical ()
 Make the current path canonical. More...
 
PathmakeAbsolute ()
 Convert the path to an absolute path relative to the current working directory. More...
 
PathmakeAbsolute (Path base_path)
 Convert the path to an absolute path relative to the given base_path. More...
 
PathmakeRelative ()
 Convert the path to a relative path relative to the current working directory. More...
 
PathmakeRelative (Path input_path)
 Convert the path to a relative path relative to the input_path. More...
 
bool isRelative () const
 Returns whether the current path is relative. More...
 
bool isAbsolute () const
 Returns whether the current path is absolute. More...
 
bool isEmpty () const
 Returns whether the current path is empty. More...
 
PathClear ()
 Clears the path and resets it to the empty state. More...
 

Private Attributes

std::unique_ptr< Implementation > _impl
 

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.

Definition at line 45 of file path.h.

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.

Definition at line 48 of file path.h.

Constructor & Destructor Documentation

◆ Path() [1/3]

Path ( const Path path)

Copy CTOR.

Parameters
[in]pathThe instance to copy from

◆ Path() [2/3]

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]

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& 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& 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& 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 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 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 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 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:" @dev_essential_deprecated Use getRootName() instead.

Returns
A path instance pointing to the root directory

◆ getRootName()

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 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 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

◆ isEmpty()

bool isEmpty ( ) const

Returns whether the current path is empty.

Return values
trueif the path is empty, false otherwise

◆ isRelative()

bool 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& makeAbsolute ( )

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

Returns
*this containing the absolute path

◆ makeAbsolute() [2/2]

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& 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& makeRelative ( )

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

Returns
*this containing the relative path

◆ makeRelative() [2/2]

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()

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& operator= ( const Path path)

Assignment Operator.

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

◆ removeLastElement()

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& 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 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 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)