Ember
A C++ 20 'game engine' built with SDL3 with wide platform support.
Public Member Functions | Static Public Member Functions | List of all members
FileAccess Class Reference

A RAII wrapper for SDL3 file operations supporting read/write access. More...

#include <file_system.h>

Public Member Functions

 FileAccess (const std::string &file_path, ModeFlags mode_flags=ModeFlags::READ)
 Construct and optionally open a file. More...
 
 FileAccess ()=default
 Default constructor (empty, file not opened) More...
 
 ~FileAccess ()
 Destructor automatically closes the file if it is open. More...
 
bool open (const std::string &file_path, ModeFlags mode_flags=ModeFlags::READ)
 Open a file for reading/writing. More...
 
bool is_open () const
 Check if the file is currently open. More...
 
std::vector< char > get_file_as_bytes ()
 Read the entire file as a vector of bytes. More...
 
std::string get_file_as_str ()
 Read the entire file as a String. More...
 
std::string get_absolute_path () const
 Get the absolute resolved path to the file. More...
 
std::string get_path () const
 Get the path relative to the base (removes res:// or user:// prefix) More...
 
bool store_string (const std::string &content="")
 Write a string to the file. More...
 
bool store_bytes (const std::vector< char > &content)
 Write a byte array to the file. More...
 
void seek (int length)
 Move the file pointer to the given offset from the beginning. More...
 
void seek_end (int position)
 Move the file pointer relative to the end of the file. More...
 
void close ()
 Close the file if it is open. More...
 
 FileAccess (const FileAccess &)=delete
 
FileAccessoperator= (const FileAccess &)=delete
 

Static Public Member Functions

static bool file_exists (const std::string &file_path)
 Check if a file exists at the given path. More...
 

Detailed Description

A RAII wrapper for SDL3 file operations supporting read/write access.

Constructor & Destructor Documentation

◆ FileAccess() [1/3]

FileAccess::FileAccess ( const std::string &  file_path,
ModeFlags  mode_flags = ModeFlags::READ 
)
explicit

Construct and optionally open a file.

Parameters
file_pathPath to the file (can be res:// or user://)
mode_flagsMode of access (default: READ)

◆ FileAccess() [2/3]

FileAccess::FileAccess ( )
default

Default constructor (empty, file not opened)

◆ ~FileAccess()

FileAccess::~FileAccess ( )

Destructor automatically closes the file if it is open.

◆ FileAccess() [3/3]

FileAccess::FileAccess ( const FileAccess )
delete

Member Function Documentation

◆ close()

void FileAccess::close ( )

Close the file if it is open.

◆ file_exists()

bool FileAccess::file_exists ( const std::string &  file_path)
static

Check if a file exists at the given path.

Parameters
file_pathPath to the file
Returns
true if the file exists, false otherwise

◆ get_absolute_path()

std::string FileAccess::get_absolute_path ( ) const

Get the absolute resolved path to the file.

Returns
Full path on the filesystem

◆ get_file_as_bytes()

std::vector< char > FileAccess::get_file_as_bytes ( )

Read the entire file as a vector of bytes.

Returns
File contents as Array of Bytes.

◆ get_file_as_str()

std::string FileAccess::get_file_as_str ( )

Read the entire file as a String.

Returns
File contents as String

◆ get_path()

std::string FileAccess::get_path ( ) const

Get the path relative to the base (removes res:// or user:// prefix)

Returns
Relative path string

◆ is_open()

bool FileAccess::is_open ( ) const

Check if the file is currently open.

Returns
true if open, false otherwise

◆ open()

bool FileAccess::open ( const std::string &  file_path,
ModeFlags  mode_flags = ModeFlags::READ 
)

Open a file for reading/writing.

Parameters
file_pathPath to the file (res:// or user://)
mode_flagsAccess mode

By default, get from res://

Returns
true if the file was successfully opened, false otherwise

◆ operator=()

FileAccess& FileAccess::operator= ( const FileAccess )
delete

◆ seek()

void FileAccess::seek ( int  length)

Move the file pointer to the given offset from the beginning.

Parameters
lengthOffset in bytes

◆ seek_end()

void FileAccess::seek_end ( int  position)

Move the file pointer relative to the end of the file.

Parameters
positionOffset in bytes from the end

◆ store_bytes()

bool FileAccess::store_bytes ( const std::vector< char > &  content)

Write a byte array to the file.

Parameters
contentVector of bytes to write
Returns
true if successful

◆ store_string()

bool FileAccess::store_string ( const std::string &  content = "")

Write a string to the file.

Parameters
contentString content to write
Returns
true if successful

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