cppfs  1.2.0.5b71c2c98fb9
Cross-platform C++ file system library supporting multiple backends (Local-FS, SSH)
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
cppfs::FilePath Class Reference

Path to file or directory. More...

#include <cppfs/include/cppfs/FilePath.h>

Public Member Functions

 FilePath ()
 Constructor. More...
 
 FilePath (const FilePath &filePath)
 Copy constructor. More...
 
 FilePath (FilePath &&filePath)
 Move constructor. More...
 
 FilePath (const std::string &path)
 Constructor. More...
 
 FilePath (std::string &&path)
 Constructor. More...
 
 FilePath (const char *path)
 Constructor. More...
 
virtual ~FilePath ()
 Destructor. More...
 
FilePathoperator= (const FilePath &filePath)
 Copy assignment operator. More...
 
FilePathoperator= (FilePath &&filePath)
 Move assignment operator. More...
 
const std::string & path () const
 Get path as string. More...
 
void setPath (const std::string &path)
 Set path. More...
 
void setPath (std::string &&path)
 Set path. More...
 
std::string toNative () const
 Get native path as string. More...
 
bool isEmpty () const
 Check if path is empty. More...
 
bool pointsToContent () const
 Check if path points to the contents of a directory or container. More...
 
const std::string & fullPath () const
 Get full path. More...
 
const std::string & fileName () const
 Get file name. More...
 
const std::string & baseName () const
 Get base name. More...
 
const std::string & extension () const
 Get file extension. More...
 
const std::string & directoryPath () const
 Get directory path. More...
 
const std::string & driveLetter () const
 Get drive letter. More...
 
bool isAbsolute () const
 Check if path is absolute. More...
 
bool isRelative () const
 Check if path is relative. More...
 
FilePath resolve (const FilePath &path) const
 Resolve relative path from this path. More...
 
std::string resolved () const
 Resolve path (removed '.' and '..' entries if possible) More...
 

Protected Member Functions

void analyze () const
 Analyze path and fill in the additional information. More...
 

Protected Attributes

std::string m_path
 Path (unified format) More...
 
bool m_pointsToContent
 'true' if the path has a trailing separator, else 'false' More...
 
bool m_details
 'true' if path details have been analyzed, else 'false' More...
 
std::string m_fullPath
 Full path (without trailing separators) More...
 
std::string m_filename
 Filename component. More...
 
std::string m_basename
 Basename component. More...
 
std::string m_extension
 Extension component. More...
 
std::string m_directoryPath
 Path to containing directory. More...
 
std::string m_driveLetter
 Drive letter component. More...
 
bool m_absolute
 'true' if path is absolute, else 'false' More...
 

Detailed Description

Path to file or directory.

This class stores a path to a file or directory and provides common operations like getting the file name or extension.

FilePath uses a unified format for storing paths that can be used consistently on every platform, using only '/' as a separator. When a FilePath is constructed from a string, the path is translated into the unified format and can then be used in a cross-platform way throughout an application. All operations on FilePath also return paths in the same unified format. To obtain a path in native platform format, use toNative().

All operations are completely string-based and don't use any system information. Therefore, paths are treated purely syntactically and do not imply that for example a file does really exist.

Constructor & Destructor Documentation

cppfs::FilePath::FilePath ( )

Constructor.

cppfs::FilePath::FilePath ( const FilePath filePath)

Copy constructor.

Parameters
[in]filePathFile path to copy
cppfs::FilePath::FilePath ( FilePath &&  filePath)

Move constructor.

Parameters
[in]filePathFile path to move
cppfs::FilePath::FilePath ( const std::string &  path)

Constructor.

Parameters
[in]pathFile path
cppfs::FilePath::FilePath ( std::string &&  path)

Constructor.

Parameters
[in]pathFile path
cppfs::FilePath::FilePath ( const char *  path)

Constructor.

Parameters
[in]stringFile path
virtual cppfs::FilePath::~FilePath ( )
virtual

Destructor.

Member Function Documentation

FilePath& cppfs::FilePath::operator= ( const FilePath filePath)

Copy assignment operator.

Parameters
[in]filePathRight-hand value to copy
Returns
Reference to this value
FilePath& cppfs::FilePath::operator= ( FilePath &&  filePath)

Move assignment operator.

Parameters
[in]filePathRight-hand value to move
Returns
Reference to this value
const std::string& cppfs::FilePath::path ( ) const

Get path as string.

Returns
File path (unified format)
Remarks
The path will be returned in unified format, but otherwise unchanged (e.g., trailing separators are preserved). To get the path without trailing separators, use fullPath().
void cppfs::FilePath::setPath ( const std::string &  path)

Set path.

Parameters
[in]pathFile path
void cppfs::FilePath::setPath ( std::string &&  path)

Set path.

Parameters
[in]pathFile path
std::string cppfs::FilePath::toNative ( ) const

Get native path as string.

Returns
File path (native format)
Remarks
The path will be returned in native format, but otherwise unchanged (e.g., trailing separators are preserved).
bool cppfs::FilePath::isEmpty ( ) const

Check if path is empty.

Returns
'true' if path is empty, else 'false'
bool cppfs::FilePath::pointsToContent ( ) const

Check if path points to the contents of a directory or container.

Returns
'true' if it points to the contents, else 'false'
Remarks
A path can either end with a filename (e.g., '/path/to/dir'), or with a separator (e.g., '/path/to/dir/'). For files, there is no difference in both. For directories however, it is assumed that a path that ends with a separator points to the contents of the directory rather than the directory itself. The same may be true for containers, such as archive files. This function can therefore be used to determine if the path ends with a separator, yet it makes no difference between files and directories (as it can operate only on the string).
const std::string& cppfs::FilePath::fullPath ( ) const

Get full path.

Returns
Full path in unified format, but without a trailing separator
Remarks
If you want trailing separators to remain in the string, use path(). Calling this function triggers a full analysis of the path (costly operation).
const std::string& cppfs::FilePath::fileName ( ) const

Get file name.

Returns
File name of the stored path (with extension)
Remarks
This function returns "something.ex" for both "/path/to/something.ex" and "/path/to/something.ex/". Calling this function triggers a full analysis of the path (costly operation).
const std::string& cppfs::FilePath::baseName ( ) const

Get base name.

Returns
Base name of the stored path (without extension)
Remarks
This function returns "something" for both "/path/to/something.ex" and "/path/to/something.ex/". Calling this function triggers a full analysis of the path (costly operation).
const std::string& cppfs::FilePath::extension ( ) const

Get file extension.

Returns
Extension of the stored path
Remarks
This function returns ".ex" for both "/path/to/something.ex" and "/path/to/something.ex/". If the path has no extension, an empty string is returned. Calling this function triggers a full analysis of the path (costly operation).
const std::string& cppfs::FilePath::directoryPath ( ) const

Get directory path.

Returns
Path to the directory, with trailing slashes
Remarks
This function returns "/path/to/" as directory path for both "/path/to/directory" and "/path/to/directory/". Calling this function triggers a full analysis of the path (costly operation).
const std::string& cppfs::FilePath::driveLetter ( ) const

Get drive letter.

Returns
Drive letter of the path on Windows (e.g., "C:").
Remarks
If there is no drive letter (Linux, Mac), an empty string is returned. Calling this function triggers a full analysis of the path (costly operation).
bool cppfs::FilePath::isAbsolute ( ) const

Check if path is absolute.

Returns
'true' if path is absolute, else 'false'
bool cppfs::FilePath::isRelative ( ) const

Check if path is relative.

Returns
'true' if path is relative, else 'false'
FilePath cppfs::FilePath::resolve ( const FilePath path) const

Resolve relative path from this path.

Parameters
[in]pathPath
Returns
Combined path
Remarks
This function applies the specified path to the current path of this object. It uses the following rules:
  • If path is absolute, path is returned
  • If path is relative, it is appended to this path
  • If path ends with a separator, the new path also will
  • Trailing separators of this path are not preserved when adding path
std::string cppfs::FilePath::resolved ( ) const

Resolve path (removed '.' and '..' entries if possible)

Parameters
[in]pathPath
Returns
Resolved path
Remarks
This function resolves occurences of '.' and '..', taking into account the root path, e.g.: 'a/b/../c' -> 'a/c' '../../a' -> '../../a' 'a/../../b' -> '../b' '/a/../b/' - '/b' etc.
void cppfs::FilePath::analyze ( ) const
protected

Analyze path and fill in the additional information.

Remarks
This function will populate additional information, such as m_filename, etc. and set m_details to 'true'. If m_details is already 'true', it returns immediately.

Member Data Documentation

std::string cppfs::FilePath::m_path
protected

Path (unified format)

bool cppfs::FilePath::m_pointsToContent
protected

'true' if the path has a trailing separator, else 'false'

bool cppfs::FilePath::m_details
mutableprotected

'true' if path details have been analyzed, else 'false'

std::string cppfs::FilePath::m_fullPath
mutableprotected

Full path (without trailing separators)

std::string cppfs::FilePath::m_filename
mutableprotected

Filename component.

std::string cppfs::FilePath::m_basename
mutableprotected

Basename component.

std::string cppfs::FilePath::m_extension
mutableprotected

Extension component.

std::string cppfs::FilePath::m_directoryPath
mutableprotected

Path to containing directory.

std::string cppfs::FilePath::m_driveLetter
mutableprotected

Drive letter component.

bool cppfs::FilePath::m_absolute
mutableprotected

'true' if path is absolute, else 'false'


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