9 #ifndef CPPCOMMON_FILESYSTEM_PATH_H
10 #define CPPCOMMON_FILESYSTEM_PATH_H
122 explicit operator bool() const noexcept {
return !
empty(); }
128 {
return Path(path1).Append(path2); }
134 {
return Path(path1).Concat(path2); }
250 static bool deprecated(
char character) noexcept;
252 static bool deprecated(
wchar_t character) noexcept;
288 static
Path CopyIf(const
Path& src, const
Path& dst, const std::
string& pattern = "",
bool overwrite = false);
364 friend std::istream& operator>>(std::istream& is,
Path& path)
365 { is >> path._path;
return is; }
368 { os << path.
_path;
return os; }
static std::wstring FromUTF8(std::string_view str)
Convert UTF-8 encoded string to system wide-string.
Path root() const
Decompose root path from the current path.
Path absolute() const
Transform the current path to the real path on a filesystem.
Path(const std::wstring &path)
Initialize path with a given wide string value.
Path & ReplaceExtension(const Path &extension)
Replace the current path extension with a given one.
bool IsSymlink() const
Is the path points to symbolic link?
static Path unique()
Get the unique filename in UUID format "00000000-0000-0000-0000-000000000000".
Path & operator+=(const Path &path)
UtcTimestamp created() const
Get the path created UTC timestamp.
static Path RemoveAll(const Path &path)
Recursively remove the given path (file, empty directory, symlink, etc) from the filesystem.
Path & Assign(const Path &path)
Assign the given path to the current one.
Path & operator=(Path &&)=default
Path(const std::string &path)
Initialize path with a given string value.
std::wstring wstring() const
Get the path value as a wide string.
Path(const wchar_t *path)
Initialize path with a given wide C-string value.
bool HasStem() const
Has stem?
size_t hardlinks() const
Get the path count of hardlinks.
Path validate(char placeholder='_') const
Transform the current path and replace all deprecated characters with a given placeholder (default is...
friend bool operator>=(const Path &path1, const Path &path2)
bool HasExtension() const
Has extension?
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,...
Flags< FileAttributes > attributes() const
Get the path file attributes.
Path()
Initialize path with an empty value.
bool IsDirectory() const
Is the path points to directory?
Path & RemoveTrailingSeparators()
Remove all trailing separators form the current path.
friend std::ostream & operator<<(std::ostream &os, const Path &path)
Output instance into the given output stream.
Path & operator/=(const Path &path)
friend bool operator!=(const Path &path1, const Path &path2)
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)
Path relative() const
Decompose relative path from the current path.
static Path Remove(const Path &path)
Remove the given path (file, empty directory, symlink, etc) from the filesystem.
Path & Concat(const Path &path)
Concatenate the given path to the current one.
bool IsOther() const
Is the path points to special file (block, character, FIFO, socket)?
bool IsRegularFile() const
Is the path points to regular file?
bool HasParent() const
Has parent path?
bool HasRoot() const
Has root path?
Path(const Path &)=default
static Path initial()
Get the initial path of the process.
bool IsExists() const
Is the path exists?
bool IsAbsolute() const
Is absolute path?
static Path home()
Get the home path of the process.
static void SetCreated(const Path &path, const UtcTimestamp ×tamp)
Set created UTC timestamp for the given path.
friend bool operator>(const Path &path1, const Path &path2)
Path & ReplaceFilename(const Path &filename)
Replace the current path filename with a given one.
friend bool operator<=(const Path &path1, const Path &path2)
Path stem() const
Decompose stem from the current path.
friend bool operator==(const Path &path1, const Path &path2)
bool HasFilename() const
Has filename?
static void SetAttributes(const Path &path, const Flags< FileAttributes > &attributes)
Set file attributes for the given path.
static void SetModified(const Path &path, const UtcTimestamp ×tamp)
Set modified UTC timestamp for the given path.
SpaceInfo space() const
Get the path space information.
Path & MakePreferred()
Convert all path separators to system ones ('\' for Windows or '/' for Unix)
static char separator() noexcept
Get the system path separator character ('\' for Windows or '/' for Unix)
Path parent() const
Decompose parent path from the current path.
static void Touch(const Path &path)
Touch the given path and set its modified UTC timestamp to the current value.
static Path CopyAll(const Path &src, const Path &dst, bool overwrite=false)
Recursively copy the given source path to destination path (files, directories, symlinks,...
Path canonical() const
Transform the current path and replace all '.' and '..' properly.
friend bool operator<(const Path &path1, const Path &path2)
Path & RemoveExtension()
Remove the current path extension.
Path(const char *path)
Initialize path with a given C-string value.
void swap(Path &path) noexcept
Swap two instances.
Path extension() const
Decompose extension from the current path.
static void SetCurrent(const Path &path)
Set the given path of the process as a current one.
bool empty() const noexcept
Is the path empty?
Path & Append(const Path &path)
Append the given path to the current one.
Path & operator=(const Path &)=default
Path filename() const
Decompose filename from the current path.
Flags< FilePermissions > permissions() const
Get the path file permissions.
FileType type() const
Get the path file type.
const std::string & string() const noexcept
Get the path value as UTF-8 string.
static std::string deprecated()
Get filesystem deprecated characters ('\', '/', '?', '', '*', ':', '|', '"', '<', '>')
static Path temp()
Get the temporary path of the process.
friend Path operator/(const Path &path1, const Path &path2)
friend Path operator+(const Path &path1, const Path &path2)
static void SetPermissions(const Path &path, const Flags< FilePermissions > &permissions)
Set file permissions for the given path.
static Path RemoveIf(const Path &path, const std::string &pattern="")
Recursively remove the given path matched to the given pattern (file, empty directory,...
UtcTimestamp modified() const
Get the path modified UTC timestamp.
bool IsEquivalent(const Path &path) const
Is the current path is equivalent to the given one (points to the same node on a filesystem)?
static Path Rename(const Path &src, const Path &dst)
Rename the given source path to destination path (file, empty directory, symlink, etc)
Path & RemoveFilename()
Remove the current path filename.
bool IsRelative() const
Is relative path?
static Path current()
Get the current path of the process.
bool HasRelative() const
Has relative path?
std::string _path
Path string.
void Clear() noexcept
Clear the path content.
static Path executable()
Get the executable path of the process.
Encoding utilities definition.
File system exceptions definition.
Enum-based flags definition.
C++ Common project definitions.
@ CHARACTER
Character device.
@ NONE
None (file not found)
FilePermissions
File permissions (Unix specific)
@ IXOTH
Execute or search permission bit for other users.
@ IWUSR
Write permission bit for the owner of the file.
@ IRWXG
This is equivalent to IRGRP | IWGRP | IXGRP.
@ IWGRP
Write permission bit for the group owner of the file.
@ IXUSR
Execute (for ordinary files) or search (for directories) permission bit for the owner of the file.
@ IRWXO
This is equivalent to IROTH | IWOTH | IXOTH.
@ ISVTX
This is the sticky bit.
@ IRGRP
Read permission bit for the group owner of the file.
@ IRUSR
Read permission bit for the owner of the file.
@ IRWXU
This is equivalent to IRUSR | IWUSR | IXUSR.
@ IXGRP
Execute or search permission bit for the group owner of the file.
@ IWOTH
Write permission bit for other users.
@ IROTH
Read permission bit for other users.
@ ISUID
This is the set-user-ID on execute bit.
@ ISGID
This is the set-group-ID on execute bit.
FileAttributes
File attributes (Windows specific)
Filesystem path inline implementation.
Filesystem space information.
uint64_t capacity
Total size of the filesystem, in bytes.
uint64_t free
Free space on the filesystem, in bytes.
uint64_t available
Free space available to a non-privileged process (may be equal or less than free)