ImFusion SDK 4.3
Directory Class Reference

#include <ImFusion/Core/Filesystem/Directory.h>

Entity representing a Directory in the filesystem. More...

+ Inheritance diagram for Directory:

Detailed Description

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.

Examples
Filesystem.cpp.

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< Pathscan (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< PathscanRecursive (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).
 

Member Enumeration Documentation

◆ SortOption

enum class SortOption
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.

See also
scan()
Enumerator
Size 

by file extension

Time 

by file-size: directories are not sorted and returned at the beginning of the output

IgnoreCase 

by last-modified time

DirsFirst 

case-insensitive sorting by name or extension

DirsLast 

directories are sorted first

Reversed 

directories are sorted last

Constructor & Destructor Documentation

◆ Directory() [1/3]

Directory ( const Path & path)

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.

Note
This will not create the directory on the filesystem.

◆ Directory() [2/3]

Directory ( const std::string & 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.

Note
This will not create the directory on the filesystem.

◆ Directory() [3/3]

Directory ( const char * 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.

Note
This will not create the directory on the filesystem.

Member Function Documentation

◆ scan()

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

Parameters
filterOptional 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.
optionsOptional options specifying how to sort the resulting vector of paths (default is sorting by filename)

◆ scanRecursive()

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.

Parameters
filterOptional 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.

◆ copyTo()

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.

Parameters
destinationTarget directory name of copy. Must be a directory if already existing.
copyOptionsOptional flags to control the behavior of this function.
filterOptional 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.

◆ rename()

bool rename ( const Path & newName)

Renames the directory.

Returns true upon success.

Parameters
newNameThe new name

◆ remove()

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.

◆ removeRecursive()

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.

◆ removeContents()

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.

◆ create()

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.

◆ createRecursive()

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.

Examples
Filesystem.cpp.

The documentation for this class was generated from the following file:
Search Tab / S to search, Esc to close