![]() |
ImFusion SDK 4.3
|
#include <ImFusion/Core/Filesystem/Directory.h>
Entity representing a Directory in the filesystem. More...
Entity representing a Directory in the filesystem.
The file does not necessarily exist and Directory will not perform any checks whether the given file name represents a valid identifier for the current platform.
The underlying Path will always be absolute and without trailing slash: if you construct an instance using a relative path or with a trailing slash the constructor will make it absolute using the currentWorkingDirectory() and remove trailing slashes with removeTrailingSlash(). All subsequent calls to member functions will correspond to the resulting path.
Public Types | |
enum class | SortOption { Unsorted = 0x0 , Name = 0x1 , Extension = 0x2 , Size = 0x4 , Time = 0x8 , IgnoreCase = 0x10 , DirsFirst = 0x20 , DirsLast = 0x40 , Reversed = 0x80 } |
Sorting options for paths inside a directory One of the first 5 options (Unsorted, Name, Extension, Size, Time) might be or-combined with one or multiple of the following options. More... | |
using | SortOptions = Flags<SortOption> |
Public Member Functions | |
Directory (const Path &path) | |
Create a new Directory instance for the given path. | |
Directory (const std::string &path) | |
Create a new Directory instance for the given path. | |
Directory (const char *path) | |
Create a new Directory instance for the given path. | |
operator Path () const | |
Allow for implicit conversion to a Path. | |
Query directory properties | |
Path | path () const |
Gets the path of the directory. | |
std::string | string () const |
Gets the path of the directory as string. | |
Directory | parentDirectory () const |
Returns the absolute path to the parent directory, convenience function for Directory(path().parentPath()) . | |
Path | parentPath () const |
Returns the absolute path to the parent directory, convenience function for path().parentPath() . | |
std::string | name () const |
Gets the name of the directory, convenience function for path().fileName() . | |
bool | exists () const |
Check if the directory exists and is actually a directory. | |
bool | isEmpty () const |
Check if the directory is empty. | |
bool | isWritable () const |
Checks if the directory exists and is writable, in other words if a file can be created in it. | |
bool | contains (const std::string &filename) const |
Check if the directory contains a file or subdirectory named filename. | |
std::vector< Path > | scan (const std::function< bool(const Path &)> &filter=nullptr, SortOptions options=SortOption::Name) const |
Scans the directory and collects the paths inside that pass the (optional) lambda filter. | |
std::vector< Path > | scanRecursive (const std::function< bool(const Path &)> &filter=nullptr) const |
Recursively scans the directory and collects the paths inside that pass the (optional) lambda filter. | |
Perform filesystem interactions | |
bool | copyTo (const Path &destination, Flags< CopyOptions > copyOptions=CopyOptions::None, std::function< bool(const Path &sourcePath)> filter=nullptr) const |
Copies this directory and its contents to destination based on the provided options and optional filter. | |
bool | rename (const Path &newName) |
Renames the directory. | |
bool | remove () const |
Delete the directory if it's empty. | |
bool | removeRecursive () const |
Delete the directory and all of its contents. | |
bool | removeContents () const |
Delete all the contents of the directory without deleting the directory itself. | |
bool | create () const |
Creates the directory on the filesystem (the parent directory must already exist). | |
bool | createRecursive () const |
Recursively create the directory (including parent directories if needed). | |
|
strong |
Sorting options for paths inside a directory One of the first 5 options (Unsorted, Name, Extension, Size, Time) might be or-combined with one or multiple of the following options.
Create a new Directory instance for the given path.
If path is relative it will be made absolute wrt. currentWorkingDirectory() and all future calls to member functions will use this absolute path.
|
explicit |
Create a new Directory instance for the given path.
If path is relative it will be made absolute wrt. currentWorkingDirectory() and all future calls to member functions will use this absolute path.
|
explicit |
Create a new Directory instance for the given path.
If path is relative it will be made absolute wrt. currentWorkingDirectory() and all future calls to member functions will use this absolute path.
std::vector< Path > scan | ( | const std::function< bool(const Path &)> & | filter = nullptr, |
SortOptions | options = SortOption::Name ) const |
Scans the directory and collects the paths inside that pass the (optional) lambda filter.
If no filter is applied, it returns all the paths. SortOptions can be specified for the result
filter | Optional filter function (e.g. check for pattern, extension, type). Provides the absolute path as argument, scan() will only list elements where this function returns true. |
options | Optional options specifying how to sort the resulting vector of paths (default is sorting by filename) |
std::vector< Path > scanRecursive | ( | const std::function< bool(const Path &)> & | filter = nullptr | ) | const |
Recursively scans the directory and collects the paths inside that pass the (optional) lambda filter.
If no filter is applied, it returns all the paths.
filter | Optional filter function (e.g. check for pattern, extension, type). Provides the absolute path as argument, scanRecursive() will only list elements where this function returns true. However, even if you reject a directory through a filter it will still be scanned recursively. |
bool copyTo | ( | const Path & | destination, |
Flags< CopyOptions > | copyOptions = CopyOptions::None, | ||
std::function< bool(const Path &sourcePath)> | filter = nullptr ) const |
Copies this directory and its contents to destination based on the provided options and optional filter.
destination | Target directory name of copy. Must be a directory if already existing. |
copyOptions | Optional flags to control the behavior of this function. |
filter | Optional filter function, if provided will copy only those elements where it returns true . This function is forwarded to scan() in order to determine the directory contents to copy. |
bool rename | ( | const Path & | newName | ) |
Renames the directory.
Returns true upon success.
newName | The new name |
bool remove | ( | ) | const |
Delete the directory if it's empty.
If the path is a symbolic link, it removes only the link. If the directory did not exist in the first place, does nothing and returns true
.
bool removeRecursive | ( | ) | const |
Delete the directory and all of its contents.
If the path is a symbolic link, it removes only the link. If the directory did not exist in the first place, does nothing and returns true
.
bool removeContents | ( | ) | const |
Delete all the contents of the directory without deleting the directory itself.
If the directory did not exist and/or was empty in the first place, does nothing and returns true
.
bool create | ( | ) | const |
Creates the directory on the filesystem (the parent directory must already exist).
Fails if the parent directory does not yet exist. If this instance resolves to an already existing directory, no error is reported.
bool createRecursive | ( | ) | const |
Recursively create the directory (including parent directories if needed).
If this instance resolves to an already existing directory, no error is reported.