Ember
A C++ 20 'game engine' built with SDL3 with wide platform support.
file_system.h
Go to the documentation of this file.
1 #pragma once
3 
4 
14 std::string load_assets_file(const std::string& file_path);
15 
25 std::vector<char> load_file_into_memory(const std::string& file_path);
26 
33 struct Ember_File {
34  SDL_IOStream* stream;
35 };
36 
43 struct Ember_VFS {
44  ma_vfs_callbacks base;
45 };
46 
47 
48 ma_result _ember_init_vfs(Ember_VFS* vfs);
49 
59 enum class ModeFlags {
60  READ,
61  WRITE,
62  READ_WRITE,
63  WRITE_READ
64 };
65 
72 class FileAccess {
73 public:
74 
80  explicit FileAccess(const std::string& file_path, ModeFlags mode_flags = ModeFlags::READ);
81 
85  FileAccess() = default;
86 
90  ~FileAccess();
91 
100  bool open(const std::string& file_path, ModeFlags mode_flags = ModeFlags::READ);
101 
106  [[nodiscard]] bool is_open() const;
107 
112  std::vector<char> get_file_as_bytes();
113 
119  static bool file_exists(const std::string& file_path);
120 
125  std::string get_file_as_str();
126 
131  [[nodiscard]] std::string get_absolute_path() const;
132 
137  [[nodiscard]] std::string get_path() const;
138 
144  bool store_string(const std::string& content = "");
145 
151  bool store_bytes(const std::vector<char>& content);
152 
157  void seek(int length);
158 
163  void seek_end(int position);
164 
168  void close();
169 
170  FileAccess(const FileAccess&) = delete;
171 
172  FileAccess& operator=(const FileAccess&) = delete;
173 
174 private:
175  SDL_IOStream* _file = nullptr;
176  std::string _file_path;
177 
178  bool resolve_path(const std::string& file_path, ModeFlags mode_flags);
179 };
A RAII wrapper for SDL3 file operations supporting read/write access.
Definition: file_system.h:72
void seek(int length)
Move the file pointer to the given offset from the beginning.
Definition: file_system.cpp:253
~FileAccess()
Destructor automatically closes the file if it is open.
Definition: file_system.cpp:201
FileAccess()=default
Default constructor (empty, file not opened)
std::string get_path() const
Get the path relative to the base (removes res:// or user:// prefix)
Definition: file_system.cpp:265
std::vector< char > get_file_as_bytes()
Read the entire file as a vector of bytes.
Definition: file_system.cpp:209
void seek_end(int position)
Move the file pointer relative to the end of the file.
Definition: file_system.cpp:257
bool store_string(const std::string &content="")
Write a string to the file.
Definition: file_system.cpp:274
bool store_bytes(const std::vector< char > &content)
Write a byte array to the file.
Definition: file_system.cpp:289
FileAccess & operator=(const FileAccess &)=delete
bool open(const std::string &file_path, ModeFlags mode_flags=ModeFlags::READ)
Open a file for reading/writing.
Definition: file_system.cpp:181
FileAccess(const FileAccess &)=delete
std::string get_file_as_str()
Read the entire file as a String.
Definition: file_system.cpp:226
static bool file_exists(const std::string &file_path)
Check if a file exists at the given path.
Definition: file_system.cpp:231
bool is_open() const
Check if the file is currently open.
Definition: file_system.cpp:205
std::string get_absolute_path() const
Get the absolute resolved path to the file.
Definition: file_system.cpp:261
void close()
Close the file if it is open.
Definition: file_system.cpp:305
std::string load_assets_file(const std::string &file_path)
Loads a given file from res folder.
Definition: file_system.cpp:4
ModeFlags
Flags to indicate access modes.
Definition: file_system.h:59
@ READ
Read-only access.
@ READ_WRITE
Read then Write access.
@ WRITE
Write-only access.
@ WRITE_READ
Write then Read access.
ma_result _ember_init_vfs(Ember_VFS *vfs)
Definition: file_system.cpp:118
std::vector< char > load_file_into_memory(const std::string &file_path)
Loads a given file from res folder into memory.
Definition: file_system.cpp:12
Ember file (SDL_stream)
Definition: file_system.h:33
SDL_IOStream * stream
Definition: file_system.h:34
Engine virtual file system.
Definition: file_system.h:43
ma_vfs_callbacks base
Definition: file_system.h:44