CppCommon  1.0.4.1
C++ Common Library
Public Member Functions | Static Public Member Functions | Protected Attributes | Friends | List of all members
CppCommon::Path Class Reference

Filesystem path. More...

#include <path.h>

Inheritance diagram for CppCommon::Path:
CppCommon::Directory CppCommon::File CppCommon::Symlink

Public Member Functions

 Path ()
 Initialize path with an empty value. More...
 
 Path (const char *path)
 Initialize path with a given C-string value. More...
 
 Path (const std::string &path)
 Initialize path with a given string value. More...
 
 Path (const wchar_t *path)
 Initialize path with a given wide C-string value. More...
 
 Path (const std::wstring &path)
 Initialize path with a given wide string value. More...
 
 Path (const Path &)=default
 
 Path (Path &&)=default
 
 ~Path ()=default
 
Pathoperator= (const Path &)=default
 
Pathoperator= (Path &&)=default
 
 operator bool () const noexcept
 Check if the path is not empty. More...
 
Pathoperator/= (const Path &path)
 
Pathoperator+= (const Path &path)
 
const std::string & string () const noexcept
 Get the path value as UTF-8 string. More...
 
std::wstring wstring () const
 Get the path value as a wide string. More...
 
Path root () const
 Decompose root path from the current path. More...
 
Path relative () const
 Decompose relative path from the current path. More...
 
Path parent () const
 Decompose parent path from the current path. More...
 
Path filename () const
 Decompose filename from the current path. More...
 
Path stem () const
 Decompose stem from the current path. More...
 
Path extension () const
 Decompose extension from the current path. More...
 
Path absolute () const
 Transform the current path to the real path on a filesystem. More...
 
Path canonical () const
 Transform the current path and replace all '.' and '..' properly. More...
 
Path validate (char placeholder='_') const
 Transform the current path and replace all deprecated characters with a given placeholder (default is '_') More...
 
FileType type () const
 Get the path file type. More...
 
Flags< FileAttributesattributes () const
 Get the path file attributes. More...
 
Flags< FilePermissionspermissions () const
 Get the path file permissions. More...
 
UtcTimestamp created () const
 Get the path created UTC timestamp. More...
 
UtcTimestamp modified () const
 Get the path modified UTC timestamp. More...
 
size_t hardlinks () const
 Get the path count of hardlinks. More...
 
SpaceInfo space () const
 Get the path space information. More...
 
bool empty () const noexcept
 Is the path empty? More...
 
bool HasRoot () const
 Has root path? More...
 
bool HasRelative () const
 Has relative path? More...
 
bool HasParent () const
 Has parent path? More...
 
bool HasFilename () const
 Has filename? More...
 
bool HasStem () const
 Has stem? More...
 
bool HasExtension () const
 Has extension? More...
 
bool IsAbsolute () const
 Is absolute path? More...
 
bool IsRelative () const
 Is relative path? More...
 
bool IsExists () const
 Is the path exists? More...
 
bool IsRegularFile () const
 Is the path points to regular file? More...
 
bool IsDirectory () const
 Is the path points to directory? More...
 
bool IsSymlink () const
 Is the path points to symbolic link? More...
 
bool IsOther () const
 Is the path points to special file (block, character, FIFO, socket)? More...
 
bool IsEquivalent (const Path &path) const
 Is the current path is equivalent to the given one (points to the same node on a filesystem)? More...
 
void Clear () noexcept
 Clear the path content. More...
 
PathAssign (const Path &path)
 Assign the given path to the current one. More...
 
PathAppend (const Path &path)
 Append the given path to the current one. More...
 
PathConcat (const Path &path)
 Concatenate the given path to the current one. More...
 
PathMakePreferred ()
 Convert all path separators to system ones ('\' for Windows or '/' for Unix) More...
 
PathReplaceFilename (const Path &filename)
 Replace the current path filename with a given one. More...
 
PathReplaceExtension (const Path &extension)
 Replace the current path extension with a given one. More...
 
PathRemoveFilename ()
 Remove the current path filename. More...
 
PathRemoveExtension ()
 Remove the current path extension. More...
 
PathRemoveTrailingSeparators ()
 Remove all trailing separators form the current path. More...
 
void swap (Path &path) noexcept
 Swap two instances. More...
 

Static Public Member Functions

static bool deprecated (char character) noexcept
 Is the given character is deprecated by the filesystem? More...
 
static bool deprecated (wchar_t character) noexcept
 Is the given wide character is deprecated by the filesystem? More...
 
static std::string deprecated ()
 Get filesystem deprecated characters ('\', '/', '?', '', '*', ':', '|', '"', '<', '>') More...
 
static char separator () noexcept
 Get the system path separator character ('\' for Windows or '/' for Unix) More...
 
static Path initial ()
 Get the initial path of the process. More...
 
static Path current ()
 Get the current path of the process. More...
 
static Path executable ()
 Get the executable path of the process. More...
 
static Path home ()
 Get the home path of the process. More...
 
static Path temp ()
 Get the temporary path of the process. More...
 
static Path unique ()
 Get the unique filename in UUID format "00000000-0000-0000-0000-000000000000". More...
 
static Path Copy (const Path &src, const Path &dst, bool overwrite=false)
 Copy the given source path to destination path (file, empty directory, symlink, etc) More...
 
static Path CopyIf (const Path &src, const Path &dst, const std::string &pattern="", bool overwrite=false)
 Copy all matched files from the the given source path to destination path (files, directories, symlinks, etc) More...
 
static Path CopyAll (const Path &src, const Path &dst, bool overwrite=false)
 Recursively copy the given source path to destination path (files, directories, symlinks, etc) More...
 
static Path Rename (const Path &src, const Path &dst)
 Rename the given source path to destination path (file, empty directory, symlink, etc) More...
 
static Path Remove (const Path &path)
 Remove the given path (file, empty directory, symlink, etc) from the filesystem. More...
 
static Path RemoveAll (const Path &path)
 Recursively remove the given path (file, empty directory, symlink, etc) from the filesystem. More...
 
static Path RemoveIf (const Path &path, const std::string &pattern="")
 Recursively remove the given path matched to the given pattern (file, empty directory, symlink, etc) from the filesystem. More...
 
static void SetAttributes (const Path &path, const Flags< FileAttributes > &attributes)
 Set file attributes for the given path. More...
 
static void SetPermissions (const Path &path, const Flags< FilePermissions > &permissions)
 Set file permissions for the given path. More...
 
static void SetCreated (const Path &path, const UtcTimestamp &timestamp)
 Set created UTC timestamp for the given path. More...
 
static void SetModified (const Path &path, const UtcTimestamp &timestamp)
 Set modified UTC timestamp for the given path. More...
 
static void SetCurrent (const Path &path)
 Set the given path of the process as a current one. More...
 
static void Touch (const Path &path)
 Touch the given path and set its modified UTC timestamp to the current value. More...
 

Protected Attributes

std::string _path
 Path string. More...
 

Friends

Path operator/ (const Path &path1, const Path &path2)
 
Path operator+ (const Path &path1, const Path &path2)
 
bool operator== (const Path &path1, const Path &path2)
 
bool operator!= (const Path &path1, const Path &path2)
 
bool operator< (const Path &path1, const Path &path2)
 
bool operator> (const Path &path1, const Path &path2)
 
bool operator<= (const Path &path1, const Path &path2)
 
bool operator>= (const Path &path1, const Path &path2)
 
std::istream & operator>> (std::istream &is, Path &path)
 Input instance from the given input stream. More...
 
std::ostream & operator<< (std::ostream &os, const Path &path)
 Output instance into the given output stream. More...
 
void swap (Path &path1, Path &path2) noexcept
 

Detailed Description

Filesystem path.

Filesystem path wraps string directory, filename, symlink and other path types in a class and provide utility methods to check type of the path, status and permissions in a file system. Additionally path contains operators and methods for path manipulation (concatenation, canonization, absolute path).

Path is managed in UTF-8 encoding!

Not thread-safe.

Definition at line 89 of file path.h.

Constructor & Destructor Documentation

◆ Path() [1/7]

CppCommon::Path::Path ( )
inline

Initialize path with an empty value.

Definition at line 93 of file path.h.

◆ Path() [2/7]

CppCommon::Path::Path ( const char *  path)
inline

Initialize path with a given C-string value.

Parameters
path- Path value as C-string

Definition at line 98 of file path.h.

◆ Path() [3/7]

CppCommon::Path::Path ( const std::string &  path)
inline

Initialize path with a given string value.

Parameters
path- Path value as string

Definition at line 103 of file path.h.

◆ Path() [4/7]

CppCommon::Path::Path ( const wchar_t *  path)
inline

Initialize path with a given wide C-string value.

Parameters
path- Path value as wide C-string

Definition at line 108 of file path.h.

◆ Path() [5/7]

CppCommon::Path::Path ( const std::wstring &  path)
inline

Initialize path with a given wide string value.

Parameters
path- Path value as wide string

Definition at line 113 of file path.h.

◆ Path() [6/7]

CppCommon::Path::Path ( const Path )
default

◆ Path() [7/7]

CppCommon::Path::Path ( Path &&  )
default

◆ ~Path()

CppCommon::Path::~Path ( )
default

Member Function Documentation

◆ absolute()

Path CppCommon::Path::absolute ( ) const

Transform the current path to the real path on a filesystem.

Definition at line 310 of file path.cpp.

◆ Append()

Path & CppCommon::Path::Append ( const Path path)

Append the given path to the current one.

Definition at line 737 of file path.cpp.

◆ Assign()

Path & CppCommon::Path::Assign ( const Path path)
inline

Assign the given path to the current one.

Definition at line 20 of file path.inl.

◆ attributes()

Flags< FileAttributes > CppCommon::Path::attributes ( ) const

Get the path file attributes.

Definition at line 472 of file path.cpp.

◆ canonical()

Path CppCommon::Path::canonical ( ) const

Transform the current path and replace all '.' and '..' properly.

Definition at line 349 of file path.cpp.

◆ Clear()

void CppCommon::Path::Clear ( )
inlinenoexcept

Clear the path content.

Definition at line 227 of file path.h.

◆ Concat()

Path & CppCommon::Path::Concat ( const Path path)
inline

Concatenate the given path to the current one.

Definition at line 26 of file path.inl.

◆ Copy()

Path CppCommon::Path::Copy ( const Path src,
const Path dst,
bool  overwrite = false 
)
static

Copy the given source path to destination path (file, empty directory, symlink, etc)

Parameters
src- Source path
dst- Destination path
overwrite- Overwrite destination path (default is false)
Returns
Copied path

Definition at line 1017 of file path.cpp.

◆ CopyAll()

Path CppCommon::Path::CopyAll ( const Path src,
const Path dst,
bool  overwrite = false 
)
static

Recursively copy the given source path to destination path (files, directories, symlinks, etc)

Parameters
src- Source path
dst- Destination path
overwrite- Overwrite destination path (default is false)
Returns
Copied path

Definition at line 1156 of file path.cpp.

◆ CopyIf()

Path CppCommon::Path::CopyIf ( const Path src,
const Path dst,
const std::string &  pattern = "",
bool  overwrite = false 
)
static

Copy all matched files from the the given source path to destination path (files, directories, symlinks, etc)

Parameters
src- Source path
dst- Destination path
pattern- Regular expression pattern (default is "")
overwrite- Overwrite destination path (default is false)
Returns
Copied path

Definition at line 1118 of file path.cpp.

◆ created()

UtcTimestamp CppCommon::Path::created ( ) const

Get the path created UTC timestamp.

Definition at line 541 of file path.cpp.

◆ current()

Path CppCommon::Path::current ( )
static

Get the current path of the process.

Examples
filesystem_path.cpp.

Definition at line 880 of file path.cpp.

◆ deprecated() [1/3]

std::string CppCommon::Path::deprecated ( )
static

Get filesystem deprecated characters ('\', '/', '?', '', '*', ':', '|', '"', '<', '>')

Examples
filesystem_path.cpp.

Definition at line 861 of file path.cpp.

◆ deprecated() [2/3]

bool CppCommon::Path::deprecated ( char  character)
staticnoexcept

Is the given character is deprecated by the filesystem?

Definition at line 849 of file path.cpp.

◆ deprecated() [3/3]

bool CppCommon::Path::deprecated ( wchar_t  character)
staticnoexcept

Is the given wide character is deprecated by the filesystem?

Definition at line 855 of file path.cpp.

◆ empty()

bool CppCommon::Path::empty ( ) const
inlinenoexcept

Is the path empty?

Definition at line 191 of file path.h.

◆ executable()

Path CppCommon::Path::executable ( )
static

Get the executable path of the process.

Examples
filesystem_path.cpp, and filesystem_symlink.cpp.

Definition at line 910 of file path.cpp.

◆ extension()

Path CppCommon::Path::extension ( ) const

Decompose extension from the current path.

Definition at line 280 of file path.cpp.

◆ filename()

Path CppCommon::Path::filename ( ) const

Decompose filename from the current path.

Definition at line 209 of file path.cpp.

◆ hardlinks()

size_t CppCommon::Path::hardlinks ( ) const

Get the path count of hardlinks.

Definition at line 614 of file path.cpp.

◆ HasExtension()

bool CppCommon::Path::HasExtension ( ) const
inline

Has extension?

Definition at line 204 of file path.h.

◆ HasFilename()

bool CppCommon::Path::HasFilename ( ) const
inline

Has filename?

Definition at line 200 of file path.h.

◆ HasParent()

bool CppCommon::Path::HasParent ( ) const
inline

Has parent path?

Definition at line 198 of file path.h.

◆ HasRelative()

bool CppCommon::Path::HasRelative ( ) const
inline

Has relative path?

Definition at line 196 of file path.h.

◆ HasRoot()

bool CppCommon::Path::HasRoot ( ) const
inline

Has root path?

Definition at line 194 of file path.h.

◆ HasStem()

bool CppCommon::Path::HasStem ( ) const
inline

Has stem?

Definition at line 202 of file path.h.

◆ home()

Path CppCommon::Path::home ( )
static

Get the home path of the process.

Examples
filesystem_path.cpp.

Definition at line 945 of file path.cpp.

◆ initial()

Path CppCommon::Path::initial ( )
static

Get the initial path of the process.

Examples
filesystem_path.cpp.

Definition at line 875 of file path.cpp.

◆ IsAbsolute()

bool CppCommon::Path::IsAbsolute ( ) const
inline

Is absolute path?

Definition at line 207 of file path.h.

◆ IsDirectory()

bool CppCommon::Path::IsDirectory ( ) const
inline

Is the path points to directory?

Definition at line 217 of file path.h.

◆ IsEquivalent()

bool CppCommon::Path::IsEquivalent ( const Path path) const

Is the current path is equivalent to the given one (points to the same node on a filesystem)?

Definition at line 676 of file path.cpp.

◆ IsExists()

bool CppCommon::Path::IsExists ( ) const
inline

Is the path exists?

Definition at line 212 of file path.h.

◆ IsOther()

bool CppCommon::Path::IsOther ( ) const
inline

Is the path points to special file (block, character, FIFO, socket)?

Definition at line 14 of file path.inl.

◆ IsRegularFile()

bool CppCommon::Path::IsRegularFile ( ) const
inline

Is the path points to regular file?

Definition at line 215 of file path.h.

◆ IsRelative()

bool CppCommon::Path::IsRelative ( ) const
inline

Is relative path?

Definition at line 209 of file path.h.

◆ IsSymlink()

bool CppCommon::Path::IsSymlink ( ) const
inline

Is the path points to symbolic link?

Definition at line 219 of file path.h.

◆ MakePreferred()

Path & CppCommon::Path::MakePreferred ( )

Convert all path separators to system ones ('\' for Windows or '/' for Unix)

Definition at line 756 of file path.cpp.

◆ modified()

UtcTimestamp CppCommon::Path::modified ( ) const

Get the path modified UTC timestamp.

Definition at line 577 of file path.cpp.

◆ operator bool()

CppCommon::Path::operator bool ( ) const
inlineexplicitnoexcept

Check if the path is not empty.

Definition at line 122 of file path.h.

◆ operator+=()

Path& CppCommon::Path::operator+= ( const Path path)
inline

Definition at line 131 of file path.h.

◆ operator/=()

Path& CppCommon::Path::operator/= ( const Path path)
inline

Definition at line 125 of file path.h.

◆ operator=() [1/2]

Path& CppCommon::Path::operator= ( const Path )
default

◆ operator=() [2/2]

Path& CppCommon::Path::operator= ( Path &&  )
default

◆ parent()

Path CppCommon::Path::parent ( ) const

Decompose parent path from the current path.

Definition at line 144 of file path.cpp.

◆ permissions()

Flags< FilePermissions > CppCommon::Path::permissions ( ) const

Get the path file permissions.

Definition at line 499 of file path.cpp.

◆ relative()

Path CppCommon::Path::relative ( ) const

Decompose relative path from the current path.

Definition at line 137 of file path.cpp.

◆ Remove()

Path CppCommon::Path::Remove ( const Path path)
static

Remove the given path (file, empty directory, symlink, etc) from the filesystem.

Parameters
path- Path to remove
Returns
Parent path
Examples
filesystem_file.cpp, and filesystem_symlink.cpp.

Definition at line 1209 of file path.cpp.

◆ RemoveAll()

Path CppCommon::Path::RemoveAll ( const Path path)
static

Recursively remove the given path (file, empty directory, symlink, etc) from the filesystem.

Parameters
path- Path to remove
Returns
Parent path

Definition at line 1282 of file path.cpp.

◆ RemoveExtension()

Path& CppCommon::Path::RemoveExtension ( )
inline

Remove the current path extension.

Definition at line 245 of file path.h.

◆ RemoveFilename()

Path& CppCommon::Path::RemoveFilename ( )
inline

Remove the current path filename.

Definition at line 243 of file path.h.

◆ RemoveIf()

Path CppCommon::Path::RemoveIf ( const Path path,
const std::string &  pattern = "" 
)
static

Recursively remove the given path matched to the given pattern (file, empty directory, symlink, etc) from the filesystem.

All files/symlinks will be matched to the given pattern!

Parameters
path- Path to remove
pattern- Regular expression pattern (default is "")
Returns
Parent path

Definition at line 1248 of file path.cpp.

◆ RemoveTrailingSeparators()

Path & CppCommon::Path::RemoveTrailingSeparators ( )

Remove all trailing separators form the current path.

Definition at line 831 of file path.cpp.

◆ Rename()

Path CppCommon::Path::Rename ( const Path src,
const Path dst 
)
static

Rename the given source path to destination path (file, empty directory, symlink, etc)

Parameters
src- Source path
dst- Destination path
Returns
Renamed path

Definition at line 1196 of file path.cpp.

◆ ReplaceExtension()

Path & CppCommon::Path::ReplaceExtension ( const Path extension)

Replace the current path extension with a given one.

Definition at line 792 of file path.cpp.

◆ ReplaceFilename()

Path & CppCommon::Path::ReplaceFilename ( const Path filename)

Replace the current path filename with a given one.

Definition at line 766 of file path.cpp.

◆ root()

Path CppCommon::Path::root ( ) const

Decompose root path from the current path.

Definition at line 132 of file path.cpp.

◆ separator()

char CppCommon::Path::separator ( )
staticnoexcept

Get the system path separator character ('\' for Windows or '/' for Unix)

Examples
filesystem_path.cpp.

Definition at line 866 of file path.cpp.

◆ SetAttributes()

void CppCommon::Path::SetAttributes ( const Path path,
const Flags< FileAttributes > &  attributes 
)
static

Set file attributes for the given path.

Parameters
path- Path to modify
attributes- Path attributes

Definition at line 1309 of file path.cpp.

◆ SetCreated()

void CppCommon::Path::SetCreated ( const Path path,
const UtcTimestamp timestamp 
)
static

Set created UTC timestamp for the given path.

Parameters
path- Path to modify
timestamp- Created timestamp

Definition at line 1390 of file path.cpp.

◆ SetCurrent()

void CppCommon::Path::SetCurrent ( const Path path)
static

Set the given path of the process as a current one.

Parameters
path- Current path

Definition at line 1478 of file path.cpp.

◆ SetModified()

void CppCommon::Path::SetModified ( const Path path,
const UtcTimestamp timestamp 
)
static

Set modified UTC timestamp for the given path.

Parameters
path- Path to modify
timestamp- Modified timestamp

Definition at line 1434 of file path.cpp.

◆ SetPermissions()

void CppCommon::Path::SetPermissions ( const Path path,
const Flags< FilePermissions > &  permissions 
)
static

Set file permissions for the given path.

Parameters
path- Path to modify
permissions- Path permissions

Definition at line 1355 of file path.cpp.

◆ space()

SpaceInfo CppCommon::Path::space ( ) const

Get the path space information.

Examples
filesystem_path.cpp.

Definition at line 644 of file path.cpp.

◆ stem()

Path CppCommon::Path::stem ( ) const

Decompose stem from the current path.

Definition at line 232 of file path.cpp.

◆ string()

const std::string& CppCommon::Path::string ( ) const
inlinenoexcept

Get the path value as UTF-8 string.

Definition at line 151 of file path.h.

◆ swap()

void CppCommon::Path::swap ( Path path)
inlinenoexcept

Swap two instances.

Definition at line 32 of file path.inl.

◆ temp()

Path CppCommon::Path::temp ( )
static

Get the temporary path of the process.

Examples
filesystem_path.cpp.

Definition at line 987 of file path.cpp.

◆ Touch()

void CppCommon::Path::Touch ( const Path path)
static

Touch the given path and set its modified UTC timestamp to the current value.

Parameters
path- Path to touch

Definition at line 1491 of file path.cpp.

◆ type()

FileType CppCommon::Path::type ( ) const

Get the path file type.

Definition at line 423 of file path.cpp.

◆ unique()

Path CppCommon::Path::unique ( )
static

Get the unique filename in UUID format "00000000-0000-0000-0000-000000000000".

Examples
filesystem_path.cpp.

Definition at line 1012 of file path.cpp.

◆ validate()

Path CppCommon::Path::validate ( char  placeholder = '_') const

Transform the current path and replace all deprecated characters with a given placeholder (default is '_')

Definition at line 412 of file path.cpp.

◆ wstring()

std::wstring CppCommon::Path::wstring ( ) const
inline

Get the path value as a wide string.

Definition at line 153 of file path.h.

Friends And Related Function Documentation

◆ operator!=

bool operator!= ( const Path path1,
const Path path2 
)
friend

Definition at line 139 of file path.h.

◆ operator+

Path operator+ ( const Path path1,
const Path path2 
)
friend

Definition at line 133 of file path.h.

◆ operator/

Path operator/ ( const Path path1,
const Path path2 
)
friend

Definition at line 127 of file path.h.

◆ operator<

bool operator< ( const Path path1,
const Path path2 
)
friend

Definition at line 141 of file path.h.

◆ operator<<

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

Output instance into the given output stream.

Definition at line 367 of file path.h.

◆ operator<=

bool operator<= ( const Path path1,
const Path path2 
)
friend

Definition at line 145 of file path.h.

◆ operator==

bool operator== ( const Path path1,
const Path path2 
)
friend

Definition at line 137 of file path.h.

◆ operator>

bool operator> ( const Path path1,
const Path path2 
)
friend

Definition at line 143 of file path.h.

◆ operator>=

bool operator>= ( const Path path1,
const Path path2 
)
friend

Definition at line 147 of file path.h.

◆ operator>>

std::istream& operator>> ( std::istream &  is,
Path path 
)
friend

Input instance from the given input stream.

Definition at line 364 of file path.h.

◆ swap

void swap ( Path path1,
Path path2 
)
friend

Definition at line 38 of file path.inl.

Member Data Documentation

◆ _path

std::string CppCommon::Path::_path
protected

Path string.

Definition at line 376 of file path.h.


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