SPH
Public Member Functions | Static Public Member Functions | Friends | List of all members
Path Class Reference

Object representing a path on a filesystem. More...

#include <Path.h>

Public Member Functions

 Path ()=default
 Constructs an empty path. More...
 
 Path (const std::string &path)
 Constructs a path from string. More...
 
bool empty () const
 Checks if the path is empty. More...
 
bool isHidden () const
 Checks if the file is hidden, i.e. starts with a dot (makes only sense on Unit based systems). More...
 
bool isAbsolute () const
 
bool isRelative () const
 Checks if the path is relative. Empty path is not considered relative. More...
 
bool isRoot () const
 Checks if the object holds root path. More...
 
bool hasExtension () const
 Checks if the file has an extension. More...
 
Path parentPath () const
 Returns the parent directory. If the path is empty or root, return empty path. More...
 
Path fileName () const
 Returns the filename of the path. More...
 
Path extension () const
 Returns the extension of the filename. More...
 
std::string native () const
 Returns the native version of the path. More...
 
PathreplaceExtension (const std::string &newExtension)
 Changes the extension of the file. More...
 
PathremoveExtension ()
 Removes the extension from the path. More...
 
PathremoveSpecialDirs ()
 Removes . and .. directories from the path. More...
 
PathmakeAbsolute ()
 Turns the path into an absolute path. More...
 
PathmakeRelative ()
 Turns the path into a relative path. More...
 
Path operator/ (const Path &other) const
 Appends two paths together. More...
 
Pathoperator/= (const Path &other)
 Appends another path to this one. More...
 
bool operator== (const Path &other) const
 Checks if two objects represent the same path. More...
 
bool operator!= (const Path &other) const
 Checks if two objects represent different paths. More...
 
bool operator< (const Path &other) const
 Does lexicographical comparison of two paths. More...
 

Static Public Member Functions

static Path currentPath ()
 Returns the current working directory, or empty path if the function failed. More...
 

Friends

std::ostream & operator<< (std::ostream &stream, const Path &path)
 Prints the path into the stream. More...
 

Detailed Description

Object representing a path on a filesystem.

Can represent both directory and file paths. Object does not check existence or accesibility of the path in any way, only syntactic aspects of the path are considered.

Definition at line 17 of file Path.h.

Constructor & Destructor Documentation

◆ Path() [1/2]

Path::Path ( )
default

Constructs an empty path.

◆ Path() [2/2]

NAMESPACE_SPH_BEGIN Path::Path ( const std::string &  path)
explicit

Constructs a path from string.

Definition at line 5 of file Path.cpp.

Member Function Documentation

◆ currentPath()

Path Path::currentPath ( )
static

Returns the current working directory, or empty path if the function failed.

Definition at line 166 of file Path.cpp.

◆ empty()

bool Path::empty ( ) const

Checks if the path is empty.

Definition at line 10 of file Path.cpp.

◆ extension()

Path Path::extension ( ) const

Returns the extension of the filename.

This is an empty path for directories or for files without extensions. If the file has more than one extension, returns only the last one. Example: /usr/lib -> "" file.txt -> "txt" archive.tar.gz -> "gz" .gitignore -> ""

Definition at line 59 of file Path.cpp.

◆ fileName()

Path Path::fileName ( ) const

Returns the filename of the path.

This is an empty path for directories. The file name includes the extension, if present.

Definition at line 47 of file Path.cpp.

◆ hasExtension()

bool Path::hasExtension ( ) const

Checks if the file has an extension.

Definition at line 31 of file Path.cpp.

◆ isAbsolute()

bool Path::isAbsolute ( ) const

Checks if the path is absolute.

Todo:
generalize, currently only checks for path starting with '/'

Definition at line 19 of file Path.cpp.

◆ isHidden()

bool Path::isHidden ( ) const

Checks if the file is hidden, i.e. starts with a dot (makes only sense on Unit based systems).

Definition at line 14 of file Path.cpp.

◆ isRelative()

bool Path::isRelative ( ) const

Checks if the path is relative. Empty path is not considered relative.

Definition at line 23 of file Path.cpp.

◆ isRoot()

bool Path::isRoot ( ) const

Checks if the object holds root path.

Definition at line 27 of file Path.cpp.

◆ makeAbsolute()

Path & Path::makeAbsolute ( )

Turns the path into an absolute path.

If the path already is absolute, function does nothing.

Definition at line 130 of file Path.cpp.

◆ makeRelative()

Path & Path::makeRelative ( )

Turns the path into a relative path.

If the path already is relative, function does nothing.

Definition at line 138 of file Path.cpp.

◆ native()

std::string Path::native ( ) const

Returns the native version of the path.

Todo:
change for windows

Definition at line 71 of file Path.cpp.

◆ operator!=()

bool Path::operator!= ( const Path other) const

Checks if two objects represent different paths.

Definition at line 196 of file Path.cpp.

◆ operator/()

Path Path::operator/ ( const Path other) const

Appends two paths together.

Definition at line 177 of file Path.cpp.

◆ operator/=()

Path & Path::operator/= ( const Path other)

Appends another path to this one.

Definition at line 187 of file Path.cpp.

◆ operator<()

bool Path::operator< ( const Path other) const

Does lexicographical comparison of two paths.

Mainly needed for using Path in associative containers.

Definition at line 200 of file Path.cpp.

◆ operator==()

bool Path::operator== ( const Path other) const

Checks if two objects represent the same path.

Note that this does not check for the actual file, so relative and absolute path to the same file are considered different, it ignores symlinks etc.

Definition at line 192 of file Path.cpp.

◆ parentPath()

Path Path::parentPath ( ) const

Returns the parent directory. If the path is empty or root, return empty path.

Definition at line 35 of file Path.cpp.

◆ removeExtension()

Path & Path::removeExtension ( )

Removes the extension from the path.

If the path has no extension, the function does nothing.

Definition at line 100 of file Path.cpp.

◆ removeSpecialDirs()

Path & Path::removeSpecialDirs ( )

Removes . and .. directories from the path.

Definition at line 115 of file Path.cpp.

◆ replaceExtension()

Path & Path::replaceExtension ( const std::string &  newExtension)

Changes the extension of the file.

If the file has no extension, adds the given extension. For files with more than one extensions (.tar.gz, etc.), removes all extensions and adds the new one. If the new extension is empty, it behaves as removeExtension function.

Returns
Reference to itself, allowing to chain function calls

Definition at line 76 of file Path.cpp.

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  stream,
const Path path 
)
friend

Prints the path into the stream.

Definition at line 204 of file Path.cpp.


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