Oxygen Engine
Modern C++ 3D Engine using OpenGL
Loading...
Searching...
No Matches
oe::io::FileSystem Class Reference

Virtual file system wrapper. More...

#include <OxygenEngine/io/file_system.h>

Public Member Functions

 FileSystem (const char *argv0=nullptr)
 Creates a file system.
 
bool mount (const std::filesystem::path &real_path, const std::filesystem::path &mount_path, const bool &append=true)
 Add an archive or a folder into the virtual file hierarchy.
 
bool unmount (const std::string &real_path)
 Remove a previously mounted folder/archive.
 
bool mkdir (const std::string &path)
 Create a new folder.
 
bool isDir (const std::string &path) const noexcept
 Check if the path points a dir in the virtual file system.
 
bool remove (const std::string &path)
 Remove a file.
 
size_t getFileSize (const std::string &filename) const noexcept
 Get a file size in bytes.
 
size_t getLastAccessTime (const std::string &filename) const noexcept
 Get a file last access time in UNIX timestamp.
 
size_t getLastModificationTime (const std::string &filename) const noexcept
 Get a file last modification time in UNIX timestamp.
 
bool fileExists (const std::string &filename) const noexcept
 Check if a file exists.
 
std::vector< uint8_t > readFile (const std::string &filename)
 Read binary file into a ByteArray.
 
std::string readTextFile (const std::string &filename)
 Read textual file into a string.
 
std::filesystem::path getRealPath (const std::filesystem::path &path) const noexcept
 Get actual folder on the disk where is stored the file/folder in virtual path.
 
std::filesystem::space_info getDiskSpace (const std::filesystem::path &path) const noexcept
 Get space information about the disk on which the virtual path is located.
 
bool setWritePath (const std::string &real_path)
 Bind a path on the actual disk where write is allowed if real_path not exists, it is recursively created (like mkdir -p)
 
std::string getWritePath () const
 Get the path used for writing.
 
std::vector< std::string > scandir (const std::filesystem::path &directory) const
 Return list of filenames contained in the directory.
 
std::string getFilename (const std::string &partial_path, const std::vector< std::string > &allowed_suffixes)
 Get file location from partial filename without extension and a list of allowed suffixes Example to find file providing extensions:
 
std::string getBaseDir ()
 
std::string generatePrefDir (const std::string &organization, const std::string &application)
 Get (and create if not already exists) the 'pref dir' where users can write personal files (preferences / games saves, etc) that are specific to your application. This directory is unique per user, per application.
 
FileSystemError getLastErrorCode () const noexcept
 Get last error code.
 
std::string getLastError () const noexcept
 Get last error description.
 

Static Public Member Functions

static FileSystemgetInstance ()
 

Detailed Description

Virtual file system wrapper.

Note
Due to PHYFS underlying mecanism, the write folder is not mounted by default for reading. All write function should get the path relative to the write folder

Constructor & Destructor Documentation

◆ FileSystem()

oe::io::FileSystem::FileSystem ( const char *  argv0 = nullptr)

Creates a file system.

Parameters
argv0argv[0] string passed to the main function (usually, the program filename)

Member Function Documentation

◆ generatePrefDir()

std::string oe::io::FileSystem::generatePrefDir ( const std::string &  organization,
const std::string &  application 
)

Get (and create if not already exists) the 'pref dir' where users can write personal files (preferences / games saves, etc) that are specific to your application. This directory is unique per user, per application.

Parameters
organizationThe name of your organization (you can use a domain name you own)
applicationThe name of your application
Returns
User dir in platform-dependent notation

◆ getDiskSpace()

std::filesystem::space_info oe::io::FileSystem::getDiskSpace ( const std::filesystem::path &  path) const
noexcept

Get space information about the disk on which the virtual path is located.

member description
capacity total size of the filesystem, in bytes
free free space on the filesystem, in bytes
available free space available to a non-privileged process (may be equal or less than free)

◆ getFilename()

std::string oe::io::FileSystem::getFilename ( const std::string &  partial_path,
const std::vector< std::string > &  allowed_suffixes 
)

Get file location from partial filename without extension and a list of allowed suffixes Example to find file providing extensions:

partial_path = textures/gabuzomeu
allowed_suffixes = {".tga", ".png", ".jpg"}
Returns
Complete filepath found or ""

◆ getLastError()

std::string oe::io::FileSystem::getLastError ( ) const
noexcept

Get last error description.

Note
Use this to retrieve an user-readable description of the error if any function failed

◆ getLastErrorCode()

FileSystemError oe::io::FileSystem::getLastErrorCode ( ) const
noexcept

Get last error code.

Note
Use this to get error value if any function failed

◆ getRealPath()

std::filesystem::path oe::io::FileSystem::getRealPath ( const std::filesystem::path &  path) const
noexcept

Get actual folder on the disk where is stored the file/folder in virtual path.

Returns
same path as mounted, empty string if path not exists

Example: if 'media' is mounted as 'assets', getRealPath('/assets/images/myimage.png') will return 'media'

◆ getWritePath()

std::string oe::io::FileSystem::getWritePath ( ) const

Get the path used for writing.

See also
setWritePath

◆ isDir()

bool oe::io::FileSystem::isDir ( const std::string &  path) const
noexcept

Check if the path points a dir in the virtual file system.

Parameters
pathVirtual path

◆ mkdir()

bool oe::io::FileSystem::mkdir ( const std::string &  path)

Create a new folder.

Parameters
pathPath relative to write folder

◆ scandir()

std::vector< std::string > oe::io::FileSystem::scandir ( const std::filesystem::path &  directory) const

Return list of filenames contained in the directory.

Note
files are alphabetically sorted

◆ setWritePath()

bool oe::io::FileSystem::setWritePath ( const std::string &  real_path)

Bind a path on the actual disk where write is allowed if real_path not exists, it is recursively created (like mkdir -p)

Note
For security reasons, the write path is not automatically added to the virtual filesystem. You are expected to mount it if you plan to read it

The documentation for this class was generated from the following file: