Golias Engine
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
2 #include "stdafx.h"
3 
14 std::string load_assets_file(const std::string& file_path);
15 
26 std::vector<char> load_file_into_memory(const std::string& file_path);
27 
34 struct Ember_File {
35  SDL_IOStream* stream;
36 };
37 
38 
39 
50 enum class ModeFlags {
51  READ,
52  WRITE,
53  READ_WRITE,
54  WRITE_READ
55 };
56 
64 class FileAccess {
65 public:
71  explicit FileAccess(const std::string& file_path, ModeFlags mode_flags = ModeFlags::READ);
72 
76  FileAccess() = default;
77 
81  ~FileAccess();
82 
91  bool open(const std::string& file_path, ModeFlags mode_flags = ModeFlags::READ);
92 
97  [[nodiscard]] bool is_open() const;
98 
103  std::vector<char> get_file_as_bytes();
104 
110  static bool file_exists(const std::string& file_path);
111 
116  std::string get_file_as_str();
117 
122  [[nodiscard]] std::string get_absolute_path() const;
123 
128  [[nodiscard]] std::string get_path() const;
129 
135  bool store_string(const std::string& content = "");
136 
142  bool store_bytes(const std::vector<char>& content);
143 
148  void seek(int length);
149 
154  void seek_end(int position);
155 
159  void close();
160 
161  FileAccess(const FileAccess&) = delete;
162 
163  FileAccess& operator=(const FileAccess&) = delete;
164 
165  SDL_IOStream* get_handle() const {
166  return _file;
167  }
168 
169 private:
170  SDL_IOStream* _file = nullptr;
171  std::string _file_path;
172 
173  bool resolve_path(const std::string& file_path, ModeFlags mode_flags);
174 };
A RAII wrapper for SDL3 file operations supporting read/write access.
Definition: file_system.h:64
void seek(int length)
Move the file pointer to the given offset from the beginning.
Definition: file_system.cpp:164
~FileAccess()
Destructor automatically closes the file if it is open.
Definition: file_system.cpp:112
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:176
std::vector< char > get_file_as_bytes()
Read the entire file as a vector of bytes.
Definition: file_system.cpp:120
void seek_end(int position)
Move the file pointer relative to the end of the file.
Definition: file_system.cpp:168
bool store_string(const std::string &content="")
Write a string to the file.
Definition: file_system.cpp:185
bool store_bytes(const std::vector< char > &content)
Write a byte array to the file.
Definition: file_system.cpp:200
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:92
FileAccess(const FileAccess &)=delete
SDL_IOStream * get_handle() const
Definition: file_system.h:165
std::string get_file_as_str()
Read the entire file as a String.
Definition: file_system.cpp:137
static bool file_exists(const std::string &file_path)
Check if a file exists at the given path.
Definition: file_system.cpp:142
bool is_open() const
Check if the file is currently open.
Definition: file_system.cpp:116
std::string get_absolute_path() const
Get the absolute resolved path to the file.
Definition: file_system.cpp:172
void close()
Close the file if it is open.
Definition: file_system.cpp:216
std::string load_assets_file(const std::string &file_path)
Loads a given file from res folder.
Definition: file_system.cpp:5
ModeFlags
Flags to indicate access modes.
Definition: file_system.h:50
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:14
@ READ
Read-only access.
@ READ_WRITE
Read then Write access.
@ WRITE
Write-only access.
@ WRITE_READ
Write then Read access.
Ember file (SDL_stream)
Definition: file_system.h:34
SDL_IOStream * stream
Definition: file_system.h:35