ImFusion SDK 4.3
File Class Reference

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

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

+ Inheritance diagram for File:

Detailed Description

Entity representing a file in the filesystem.

The file does not necessarily exist and File 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: if you construct an instance using a relative path the constructor will make it absolute using the currentWorkingDirectory() and all subsequent calls to member functions will correspond to that absolute path.

Examples
Filesystem.cpp.

Public Member Functions

 File (const Path &path)
 Create a new File instance for the given path.
 
 File (const std::string &path)
 Create a new File instance for the given path.
 
 File (const char *path)
 Create a new File instance for the given path.
 
 operator Path () const
 Allow for implicit conversion to a Path.
 

Query file properties

const Pathpath () const
 Gets the path of the file.
 
std::string string () const
 Gets the path of the file 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 file including the extension.
 
bool exists () const
 Checks if the file exists and actually is a file.
 
bool isExecutable () const
 Checks if the file is executable (and exists).
 
ByteSize size () const
 Gets the size of the file in bytes.
 
time_t lastModified () const
 Gets the time of the last modification to the file.
 

Perform filesystem interactions

bool copyTo (const Path &destination, Flags< CopyOptions > copyOptions=CopyOptions::None) const
 Copies this single file to destination using the provided copy options.
 
bool rename (const Path &newName)
 Renames the file, including extension.
 
bool remove () const
 Delete the file.
 
bool create () const
 Creates the file without any content.
 
bool createRecursive () const
 Create the file without any content including the parent directory if needed.
 

Read/write content from/to the file

ByteBuffer readBinary () const
 Read the entire content of the file in a binary fashion and return it as buffer.
 
std::vector< std::stringreadText () const
 Read the entire content of the file in a text-based fashion and return it as vector of lines.
 
bool writeBinary (const ByteBufferView &data) const
 Write the given data to the file in a binary fashion.
 
bool writeText (const std::vector< std::string > &lines) const
 Write the given list of strings into the file in a text-based fashion inserting a newline character (\n) after each line.
 
bool appendBinary (const ByteBufferView &data) const
 Append the given data to the file in a binary fashion.
 
bool appendText (const std::vector< std::string > &lines) const
 Append the given list of strings into the file in a text-based fashion inserting a newline character (\n) after each line.
 
std::ifstream openForReading (std::ios_base::openmode openmode=std::ios_base::in) const
 Create an input file stream for reading from the file.
 
std::ofstream openForWriting (std::ios_base::openmode openmode=std::ios_base::out) const
 Create an output file stream for writing to the file.
 

Constructor & Destructor Documentation

◆ File() [1/3]

File ( const Path & path)

Create a new File 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 file on the filesystem.

◆ File() [2/3]

File ( const std::string & path)
explicit

Create a new File 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 file on the filesystem.

◆ File() [3/3]

File ( const char * path)
explicit

Create a new File 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 file on the filesystem.

Member Function Documentation

◆ isExecutable()

bool isExecutable ( ) const

Checks if the file is executable (and exists).

On Windows, the file must be a 32- or 64-bit "Windows-based" application (according to GetBinaryType()) On Unix, the file must grant the executable permission (according to access())

◆ copyTo()

bool copyTo ( const Path & destination,
Flags< CopyOptions > copyOptions = CopyOptions::None ) const

Copies this single file to destination using the provided copy options.

Parameters
destinationTarget file name of the copy. If destination describes an existing directory, then behaves as copyTo(destination / source.filename(), copyOptions).
copyOptionsOptional flags to control the behavior of this function. Flags irrelevant to files are ignored and do not report an error.

◆ rename()

bool rename ( const Path & newName)

Renames the file, including extension.

Returns true upon success.

◆ remove()

bool remove ( ) const

Delete the file.

If the path is a symbolic link, it removes only the link. If the file did not exist in the first place, does nothing.

◆ create()

bool create ( ) const

Creates the file without any content.

This function will fail if the parent directory does not exist, use createRecursive() for this functionality.

◆ readBinary()

ByteBuffer readBinary ( ) const

Read the entire content of the file in a binary fashion and return it as buffer.

Note
Please resort to std::ifstream if you need more advanced functionality such as random access or stream processing.

◆ readText()

std::vector< std::string > readText ( ) const

Read the entire content of the file in a text-based fashion and return it as vector of lines.

This function will use std::ifstream in text mode internally and consume the content using std::getline() until the end of the file is reached. The newline characters (\n) after each line are removed; no further post-processing is performed. The corresponding platform-dependent behavior of C-style text file I/O applies.

Note
Please resort to std::ifstream if you need more advanced functionality such as random access or stream processing.

◆ writeBinary()

bool writeBinary ( const ByteBufferView & data) const

Write the given data to the file in a binary fashion.

If the file does not exist, it will be created recursively. If the file already exists, all content will be overwritten.

Note
Please resort to std::ofstream if you need more advanced functionality such as random access or text-based processing.

◆ writeText()

bool writeText ( const std::vector< std::string > & lines) const

Write the given list of strings into the file in a text-based fashion inserting a newline character (\n) after each line.

If the file already exists, all content will be overwritten. This function will use std::ofstream in text mode internally, thus the corresponding platform-dependent behavior of C-style text file I/O applies.

◆ appendBinary()

bool appendBinary ( const ByteBufferView & data) const

Append the given data to the file in a binary fashion.

If the file does not exist, it will be created recursively.

Note
Please resort to std::ofstream if you need more advanced functionality such as random access or text-based processing.

◆ appendText()

bool appendText ( const std::vector< std::string > & lines) const

Append the given list of strings into the file in a text-based fashion inserting a newline character (\n) after each line.

This function will use std::ofstream in text mode internally, thus the corresponding platform- dependent behavior of C-style text file I/O applies.

◆ openForReading()

std::ifstream openForReading ( std::ios_base::openmode openmode = std::ios_base::in) const

Create an input file stream for reading from the file.

Convenience function for calling std::ifstream(Platform::widen(file.path().string(), openmode).

Parameters
openmodeBitmask defining stream open mode, directly forwarded to std::ifstream constructor

◆ openForWriting()

std::ofstream openForWriting ( std::ios_base::openmode openmode = std::ios_base::out) const

Create an output file stream for writing to the file.

Convenience function for calling std::ofstream(Platform::widen(file.path().string(), openmode).

Parameters
openmodeBitmask defining stream open mode, directly forwarded to std::ofstream constructor

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