1#ifndef SDL3PP_STORAGE_H_
2#define SDL3PP_STORAGE_H_
4#include <SDL3/SDL_storage.h>
5#include "SDL3pp_filesystem.h"
6#include "SDL3pp_properties.h"
7#include "SDL3pp_stdinc.h"
253 constexpr explicit operator bool()
const {
return !!
value; }
297 constexpr Storage(std::nullptr_t =
nullptr) noexcept
310 : m_resource(resource)
343 : m_resource(
CheckError(SDL_OpenTitleStorage(override, props)))
372 : m_resource(
CheckError(SDL_OpenUserStorage(org, app, props)))
399 : m_resource(
CheckError(SDL_OpenFileStorage(path)))
431 : m_resource(
CheckError(SDL_OpenStorage(&iface, userdata)))
441 std::swap(m_resource, other.m_resource);
457 m_resource =
nullptr;
465 constexpr explicit operator bool() const noexcept {
return !!m_resource; }
468 constexpr operator StorageParam() const noexcept {
return {m_resource}; }
839 return Storage(std::move(
override), props);
870 return Storage(std::move(org), std::move(app), props);
897 return Storage(std::move(path));
928 return Storage(iface, userdata);
949 return SDL_CloseStorage(storage);
969 return SDL_StorageReady(storage);
990 if (
Uint64 length; SDL_GetStorageFileSize(storage, path, &length)) {
1025 return SDL_ReadStorageFile(
1047 if (!sz || *sz == 0)
return {};
1048 std::string buffer(*sz, 0);
1056 m_resource, std::move(path), std::move(destination));
1083 if (!sz || *sz == 0)
return {};
1084 std::vector<T> buffer(*sz /
sizeof(T) + (*sz %
sizeof(T) ? 1 : 0), 0);
1085 CheckError(ReadFile(std::move(path), {buffer.data(), *sz}));
1092 return SDL::ReadStorageFileAs<T>(m_resource, std::move(path));
1114 SDL_WriteStorageFile(storage, path, source.
data(), source.
size_bytes()));
1135 CheckError(SDL_CreateStorageDirectory(storage, path));
1172 CheckError(SDL_EnumerateStorageDirectory(storage, path, callback, userdata));
1205 [](
void* userdata,
const char* dirname,
const char* fname) {
1207 return cb(dirname, fname);
1238 std::vector<Path> r;
1240 r.emplace_back(fname);
1251 m_resource, std::move(path), callback, userdata);
1278 CheckError(SDL_RemoveStoragePath(storage, path));
1302 CheckError(SDL_RenameStoragePath(storage, oldpath, newpath));
1326 CheckError(SDL_CopyStorageFile(storage, oldpath, newpath));
1348 if (
PathInfo info; SDL_GetStoragePathInfo(storage, path, &info)) {
1372 return SDL_GetStorageSpaceRemaining(storage);
1420 CheckError(SDL_GlobStorageDirectory(storage, path, pattern, flags, &count));
1429 m_resource, std::move(path), std::move(pattern), flags);
Base class for SDL memory allocated array wrap.
Definition: SDL3pp_ownPtr.h:44
Source byte stream.
Definition: SDL3pp_strings.h:239
constexpr size_t size_bytes() const
Retrieves contained size in bytes.
Definition: SDL3pp_strings.h:303
constexpr const char * data() const
Retrieves contained data.
Definition: SDL3pp_strings.h:306
An abstract interface for filesystem access.
Definition: SDL3pp_storage.h:292
constexpr auto operator<=>(const Storage &other) const noexcept=default
Comparison.
Storage(StringParam org, StringParam app, PropertiesParam props)
Opens up a container for a user's unique read/write filesystem.
Definition: SDL3pp_storage.h:371
constexpr Storage & operator=(const Storage &other) noexcept=default
Assignment operator.
Storage(StringParam path)
Opens up a container for local filesystem storage.
Definition: SDL3pp_storage.h:398
constexpr Storage(std::nullptr_t=nullptr) noexcept
Default ctor.
Definition: SDL3pp_storage.h:297
constexpr Storage(const Storage &other)=delete
Copy constructor.
constexpr StorageRaw release() noexcept
Retrieves underlying StorageRaw and clear this.
Definition: SDL3pp_storage.h:454
~Storage()
Destructor.
Definition: SDL3pp_storage.h:436
Storage(const StorageInterface &iface, void *userdata)
Opens up a container using a client-provided storage interface.
Definition: SDL3pp_storage.h:430
constexpr Storage(Storage &&other) noexcept
Move constructor.
Definition: SDL3pp_storage.h:318
Storage(StringParam override, PropertiesParam props)
Opens up a read-only container for the application's filesystem.
Definition: SDL3pp_storage.h:342
constexpr Storage & operator=(Storage &&other) noexcept
Assignment operator.
Definition: SDL3pp_storage.h:439
constexpr Storage(const StorageRaw resource) noexcept
Constructs from StorageParam.
Definition: SDL3pp_storage.h:309
constexpr StorageRaw get() const noexcept
Retrieves underlying StorageRaw.
Definition: SDL3pp_storage.h:451
Helpers to use C++ strings parameters.
Definition: SDL3pp_strings.h:43
constexpr const char * c_str() const
Converts to a null terminated C string.
Definition: SDL3pp_strings.h:124
Target byte stream.
Definition: SDL3pp_strings.h:325
constexpr char * data() const
Retrieves contained data.
Definition: SDL3pp_strings.h:411
constexpr size_t size_bytes() const
Retrieves contained size in bytes.
Definition: SDL3pp_strings.h:408
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition: SDL3pp_error.h:197
std::function< EnumerationResult(const char *dirname, const char *fname)> EnumerateDirectoryCB
Callback for directory enumeration.
Definition: SDL3pp_filesystem.h:471
constexpr EnumerationResult ENUM_CONTINUE
Value that requests that enumeration continue.
Definition: SDL3pp_filesystem.h:409
EnumerationResult(SDLCALL *)(void *userdata, const char *dirname, const char *fname) EnumerateDirectoryCallback
Callback for directory enumeration.
Definition: SDL3pp_filesystem.h:443
void EnumerateDirectory(StringParam path, EnumerateDirectoryCallback callback, void *userdata)
Enumerate a directory through a callback function.
Definition: SDL3pp_filesystem.h:491
Uint32 GlobFlags
Flags for path matching.
Definition: SDL3pp_filesystem.h:377
::Uint64 Uint64
An unsigned 64-bit integer type.
Definition: SDL3pp_stdinc.h:371
void CreateDirectory(StringParam path)
Create a directory in a writable storage container.
Definition: SDL3pp_storage.h:1138
std::optional< Uint64 > GetStorageFileSize(StorageParam storage, StringParam path)
Query the size of a file within a storage container.
Definition: SDL3pp_storage.h:987
Storage OpenUserStorage(StringParam org, StringParam app, PropertiesParam props)
Opens up a container for a user's unique read/write filesystem.
Definition: SDL3pp_storage.h:866
Storage OpenTitleStorage(StringParam override, PropertiesParam props)
Opens up a read-only container for the application's filesystem.
Definition: SDL3pp_storage.h:837
OwnArray< char * > GlobStorageDirectory(StorageParam storage, StringParam path, StringParam pattern, GlobFlags flags)
Enumerate a directory tree, filtered by pattern, and return a list.
Definition: SDL3pp_storage.h:1413
SDL_StorageInterface StorageInterface
Function interface for Storage.
Definition: SDL3pp_storage.h:278
bool ReadStorageFile(StorageParam storage, StringParam path, TargetBytes destination)
Synchronously read a file from a storage container into a client-provided buffer.
Definition: SDL3pp_storage.h:1021
PathInfo GetPathInfo(StringParam path)
Get information about a filesystem path in a storage container.
Definition: SDL3pp_storage.h:1354
void RenameStoragePath(StorageParam storage, StringParam oldpath, StringParam newpath)
Rename a file or directory in a writable storage container.
Definition: SDL3pp_storage.h:1298
Storage OpenStorage(const StorageInterface &iface, void *userdata)
Opens up a container using a client-provided storage interface.
Definition: SDL3pp_storage.h:926
void WriteFile(StringParam path, SourceBytes source)
Synchronously write a file from client memory into a storage container.
Definition: SDL3pp_storage.h:1117
Uint64 GetSpaceRemaining()
Queries the remaining space in a storage container.
Definition: SDL3pp_storage.h:1375
OwnArray< char * > GlobDirectory(StringParam path, StringParam pattern, GlobFlags flags)
Enumerate a directory tree, filtered by pattern, and return a list.
Definition: SDL3pp_storage.h:1424
std::vector< T > ReadFileAs(StringParam path)
Synchronously read a file from a storage container into a client-provided buffer.
Definition: SDL3pp_storage.h:1090
SDL_Storage * StorageRaw
Alias to raw representation for Storage.
Definition: SDL3pp_storage.h:230
bool StorageReady(StorageParam storage)
Checks if the storage container is ready to use.
Definition: SDL3pp_storage.h:967
bool ReadFile(StringParam path, TargetBytes destination)
Synchronously read a file from a storage container into a client-provided buffer.
Definition: SDL3pp_storage.h:1053
std::optional< Uint64 > GetFileSize(StringParam path)
Query the size of a file within a storage container.
Definition: SDL3pp_storage.h:996
void RemoveStoragePath(StorageParam storage, StringParam path)
Remove a file or an empty directory in a writable storage container.
Definition: SDL3pp_storage.h:1276
bool Close()
Closes and frees a storage container.
Definition: SDL3pp_storage.h:952
void RemovePath(StringParam path)
Remove a file or an empty directory in a writable storage container.
Definition: SDL3pp_storage.h:1281
void EnumerateStorageDirectory(StorageParam storage, StringParam path, EnumerateDirectoryCallback callback, void *userdata)
Enumerate a directory in a storage container through a callback function.
Definition: SDL3pp_storage.h:1167
bool Ready()
Checks if the storage container is ready to use.
Definition: SDL3pp_storage.h:972
std::vector< T > ReadStorageFileAs(StorageParam storage, StringParam path)
Synchronously read a file from a storage container into a client-provided buffer.
Definition: SDL3pp_storage.h:1080
void CopyStorageFile(StorageParam storage, StringParam oldpath, StringParam newpath)
Copy a file in a writable storage container.
Definition: SDL3pp_storage.h:1322
void RenamePath(StringParam oldpath, StringParam newpath)
Rename a file or directory in a writable storage container.
Definition: SDL3pp_storage.h:1305
Storage OpenFileStorage(StringParam path)
Opens up a container for local filesystem storage.
Definition: SDL3pp_storage.h:895
void WriteStorageFile(StorageParam storage, StringParam path, SourceBytes source)
Synchronously write a file from client memory into a storage container.
Definition: SDL3pp_storage.h:1109
PathInfo GetStoragePathInfo(StorageParam storage, StringParam path)
Get information about a filesystem path in a storage container.
Definition: SDL3pp_storage.h:1346
void CreateStorageDirectory(StorageParam storage, StringParam path)
Create a directory in a writable storage container.
Definition: SDL3pp_storage.h:1133
void CopyFile(StringParam oldpath, StringParam newpath)
Copy a file in a writable storage container.
Definition: SDL3pp_storage.h:1329
Uint64 GetStorageSpaceRemaining(StorageParam storage)
Queries the remaining space in a storage container.
Definition: SDL3pp_storage.h:1370
bool CloseStorage(StorageRaw storage)
Closes and frees a storage container.
Definition: SDL3pp_storage.h:947
void EnumerateDirectory(StringParam path, EnumerateDirectoryCallback callback, void *userdata)
Enumerate a directory in a storage container through a callback function.
Definition: SDL3pp_storage.h:1246
Main include header for the SDL3pp library.
Information about a path on the filesystem.
Definition: SDL3pp_filesystem.h:337
Safely wrap Properties for non owning parameters.
Definition: SDL3pp_properties.h:52
Safely wrap Storage for non owning parameters.
Definition: SDL3pp_storage.h:237
constexpr StorageParam(StorageRaw value)
Constructs from StorageRaw.
Definition: SDL3pp_storage.h:241
constexpr auto operator<=>(const StorageParam &other) const =default
Comparison.
constexpr StorageParam(std::nullptr_t _=nullptr)
Constructs null/invalid.
Definition: SDL3pp_storage.h:247
StorageRaw value
parameter's StorageRaw
Definition: SDL3pp_storage.h:238
Semi-safe reference for Storage.
Definition: SDL3pp_storage.h:785
StorageRef(const StorageRef &other) noexcept
Copy constructor.
Definition: SDL3pp_storage.h:813
StorageRef(StorageRaw resource) noexcept
Constructs from StorageParam.
Definition: SDL3pp_storage.h:807
~StorageRef()
Destructor.
Definition: SDL3pp_storage.h:819
StorageRef(StorageParam resource) noexcept
Constructs from StorageParam.
Definition: SDL3pp_storage.h:795