|
CeresEngine 0.2.0
A game development framework
|
Handle for a file or directory. More...
#include <CeresEngine/Foundation/IO/FileHandle.hpp>
Public Types | |
| enum class | RemoveDirectoryFlag { FollowSymlinks = (1u << 0u) , Recursive = (1u << 1u) , None = 0 , Default = None } |
A set of flags that customize removeDirectory() behavior. More... | |
| using | ImplementationType = Poly< IFileHandle, sizeof(void *) *4 > |
| using | VisitFunc = std::function< bool(const FileHandle &)> |
| using | RemoveDirectoryFlags = Flags< RemoveDirectoryFlag > |
A set of flags that customize removeDirectory() behavior. | |
Public Member Functions | |
| FileHandle () noexcept | |
Constructs a new empty FileHandle. | |
| FileHandle (const FilePath &path) | |
Creates a new FileHandle from a path on the local file system. | |
| FileHandle (ImplementationType &&backend) | |
Creates a new FileHandle from a concrete implementation. | |
| FileHandle (const FileHandle &other) | |
Copies a FileHandle from another. | |
| FileHandle & | operator= (const FileHandle &other) |
Copies a FileHandle from another. | |
| FileHandle (FileHandle &&other) noexcept | |
Moves a FileHandle from another. | |
| FileHandle & | operator= (FileHandle &&fileHandle) noexcept |
Moves a FileHandle from another. | |
| ~FileHandle () noexcept | |
Destroys the FileHandle and releases any resources it may have allocated. | |
| IFileSystem * | getFileSystem () const |
Gets the backing IFileSystem for the handle. | |
| StringView | getPath () const |
| StringView | getFileName () const |
| void | updateFileInfo () const |
| Update file information. | |
| bool | exists () const |
| Checks if the file exists on the file system. | |
| bool | isFile () const |
| Checks if item is a file. | |
| bool | isDirectory () const |
| Checks if item is a directory. | |
| bool | isSymbolicLink () const |
| Checks if item is a symbolic link. | |
| Vector< String > | listFiles () const |
| List files in directory. | |
| void | traverse (VisitFunc funcFileEntry) const |
| Traverse directory tree with callback functions. | |
| void | traverse (VisitFunc funcFile, VisitFunc funcDirectory) const |
| Traverse directory tree with callback functions. | |
| void | traverse (FileVisitor &visitor) const |
| Traverse directory tree with a visitor. | |
| FileIterator | begin () const |
| Gets an iterator that points to the first directory entry. | |
| FileIterator | end () const |
| Gets an iterator that points after the last directory entry. | |
| UInt64 | getSize () const |
| Gets the file size. | |
| Int64 | getAccessTime () const |
| Gets the time of last access. | |
| Int64 | getModificationTime () const |
| Gets time of last modification. | |
| FileHandle | getParentDirectory () const |
Gets a FileHandle to the parent directory. | |
| FileHandle | open (StringView path) const |
| Opens a new file handle using a relative path from the current file/directory. | |
| bool | createDirectory () const |
| Creates a new directory. | |
| bool | removeDirectory (const RemoveDirectoryFlags &flags=RemoveDirectoryFlag::Default) const |
| Removes a directory. | |
| void | copyDirectory (const FileHandle &dstDir) const |
| Copies a directory recursively. | |
| bool | copy (const FileHandle &dest) const |
| Copies a file. | |
| bool | move (const FileHandle &dest) const |
| Moves a file. | |
| bool | createLink (const FileHandle &dest) const |
| Creates a hard link. | |
| bool | createSymbolicLink (const FileHandle &dest) const |
| Creates a symbolic link. | |
| bool | rename (StringView filename) const |
| Renames a file or directory. | |
| bool | remove () const |
| Removes a file. | |
| FileWatcher | watch (ExecutionContext &executionContext, const FileEvents &events=FileEvent::Default, bool recursive=true) const |
| Creates new file system watcher for this file handle. | |
| InputStream | createInputStream (std::ios_base::openmode mode=std::ios_base::in) const |
| Creates an input stream to read from the file. | |
| OutputStream | createOutputStream (std::ios_base::openmode mode=std::ios_base::out) const |
| Creates an output stream to write to the file. | |
| Optional< String > | readFile () const |
| Reads the file contents to a string. | |
| bool | writeFile (StringView content) const |
| Write the contents of a string to the file. | |
| operator bool () const noexcept | |
| bool | operator== (const FileHandle &other) const noexcept |
| bool | operator!= (const FileHandle &other) const noexcept |
Private Member Functions | |
| bool | genericCopy (const FileHandle &dest) const |
| Copies the file by stream copy. | |
| bool | genericMove (const FileHandle &dest) const |
| Moves a file by stream copy and delete. | |
Private Attributes | |
| ImplementationType | mImplementation = nullptr |
| The file handle implementation. | |
Handle for a file or directory.
A file handle is used to access a file or directory. It can be obtained either by calling open() on a FileSystem object, or by iterating over directories using the FileSystemIterator.
If the handle points to a valid file or directory, exists() returns true, otherwise false. The type of the entry can be determined by the methods isFile() and isDirectory().
File handles can be copied or moved. Overhead for these functions are limited, so for example, file or directories will not be opened automatically.
A set of flags that customize removeDirectory() behavior.
| using CeresEngine::FileHandle::VisitFunc = std::function<bool(const FileHandle&)> |
A set of flags that customize removeDirectory() behavior.
| Enumerator | |
|---|---|
| FollowSymlinks | To enter/follow symlink directories or not. |
| Recursive | Whether the file removal should be recursive or not. |
| None | |
| Default | |
|
noexcept |
Constructs a new empty FileHandle.
Creates a new FileHandle from a path on the local file system.
| CeresEngine::FileHandle::FileHandle | ( | ImplementationType && | backend | ) |
Creates a new FileHandle from a concrete implementation.
| CeresEngine::FileHandle::FileHandle | ( | const FileHandle & | other | ) |
Copies a FileHandle from another.
|
noexcept |
Moves a FileHandle from another.
|
noexcept |
Destroys the FileHandle and releases any resources it may have allocated.
The file won't be deleted.
| FileIterator CeresEngine::FileHandle::begin | ( | ) | const |
Gets an iterator that points to the first directory entry.
| bool CeresEngine::FileHandle::copy | ( | const FileHandle & | dest | ) | const |
Copies a file.
| dest | The destination file or directory. |
true if successful, else false. | void CeresEngine::FileHandle::copyDirectory | ( | const FileHandle & | dstDir | ) | const |
Copies a directory recursively.
| dstDir | The destination directory. |
| bool CeresEngine::FileHandle::createDirectory | ( | ) | const |
Creates a new directory.
true if successful, else false. | InputStream CeresEngine::FileHandle::createInputStream | ( | std::ios_base::openmode | mode = std::ios_base::in | ) | const |
Creates an input stream to read from the file.
nullptr if opening the stream failed. | bool CeresEngine::FileHandle::createLink | ( | const FileHandle & | dest | ) | const |
Creates a hard link.
| dest | The destination file or directory. |
true if successful, else false. | OutputStream CeresEngine::FileHandle::createOutputStream | ( | std::ios_base::openmode | mode = std::ios_base::out | ) | const |
Creates an output stream to write to the file.
nullptr if opening the stream failed. | bool CeresEngine::FileHandle::createSymbolicLink | ( | const FileHandle & | dest | ) | const |
Creates a symbolic link.
| dest | The destination file or directory. |
true if successful, else false. | FileIterator CeresEngine::FileHandle::end | ( | ) | const |
Gets an iterator that points after the last directory entry.
| bool CeresEngine::FileHandle::exists | ( | ) | const |
Checks if the file exists on the file system.
true if it exists, else false.
|
private |
Copies the file by stream copy.
| dest | The destination file or directory. |
true if successful, else false.
|
private |
Moves a file by stream copy and delete.
| dest | The destination file or directory. |
true if successful, else false. | Int64 CeresEngine::FileHandle::getAccessTime | ( | ) | const |
Gets the time of last access.
| StringView CeresEngine::FileHandle::getFileName | ( | ) | const |
| IFileSystem * CeresEngine::FileHandle::getFileSystem | ( | ) | const |
Gets the backing IFileSystem for the handle.
Can be nullptr if the handle is empty.
| Int64 CeresEngine::FileHandle::getModificationTime | ( | ) | const |
Gets time of last modification.
| FileHandle CeresEngine::FileHandle::getParentDirectory | ( | ) | const |
Gets a FileHandle to the parent directory.
| StringView CeresEngine::FileHandle::getPath | ( | ) | const |
| UInt64 CeresEngine::FileHandle::getSize | ( | ) | const |
Gets the file size.
| bool CeresEngine::FileHandle::isDirectory | ( | ) | const |
Checks if item is a directory.
true if it is a directory, else false. | bool CeresEngine::FileHandle::isFile | ( | ) | const |
Checks if item is a file.
true if it is a file, else false. | bool CeresEngine::FileHandle::isSymbolicLink | ( | ) | const |
Checks if item is a symbolic link.
true if it is a symbolic link, else false. List files in directory.
| bool CeresEngine::FileHandle::move | ( | const FileHandle & | dest | ) | const |
Moves a file.
| dest | The destination file or directory. |
true if successful, else false. | FileHandle CeresEngine::FileHandle::open | ( | StringView | path | ) | const |
Opens a new file handle using a relative path from the current file/directory.
| path | The relative path of the new file to open. |
|
inlineexplicitnoexcept |
|
noexcept |
| FileHandle & CeresEngine::FileHandle::operator= | ( | const FileHandle & | other | ) |
Copies a FileHandle from another.
|
noexcept |
Moves a FileHandle from another.
|
noexcept |
Reads the file contents to a string.
| bool CeresEngine::FileHandle::remove | ( | ) | const |
Removes a file.
true if successful, else false.| bool CeresEngine::FileHandle::removeDirectory | ( | const RemoveDirectoryFlags & | flags = RemoveDirectoryFlag::Default | ) | const |
Removes a directory.
true if successful, else false. RemoveDirectoryFlag::Recursive is not set, the call will only succeed if the directory exists and is empty. | bool CeresEngine::FileHandle::rename | ( | StringView | filename | ) | const |
Renames a file or directory.
| filename | The new file name to rename the file to. |
true if successful, else false. | void CeresEngine::FileHandle::traverse | ( | FileVisitor & | visitor | ) | const |
Traverse directory tree with a visitor.
| visitor | Visitor that is invoked for each entry in the directory tree. |
Traverse directory tree with callback functions.
| funcFile | Function that is call on each file. |
| funcDirectory | Function that is call on each directory. |
Traverse directory tree with callback functions.
| funcFileEntry | Function that is call on each file entry (files and directories). |
| void CeresEngine::FileHandle::updateFileInfo | ( | ) | const |
Update file information.
Must reload and update the file information. It will for example be called after a file has been created, copied, or removed to ensure that the file information returned by the handle is correct.
| FileWatcher CeresEngine::FileHandle::watch | ( | ExecutionContext & | executionContext, |
| const FileEvents & | events = FileEvent::Default, |
||
| bool | recursive = true |
||
| ) | const |
Creates new file system watcher for this file handle.
FileWatcher and adding file handles to watch. Will only work if the file handle points to a valid directory. To watch more than one directory at a time, construct a FileWatcher and add directories to it.. Avoid creating more than one FileWatcher, as OS limits can be reached.| events | The events that are watched (a combination of FileEvent values). |
| recursive | Determines if a directory is watched recursively. |
| bool CeresEngine::FileHandle::writeFile | ( | StringView | content | ) | const |
Write the contents of a string to the file.
| content | The contents to write to the file. |
true on success, else false.
|
mutableprivate |
The file handle implementation.