SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
SDL3pp_storage.h
1#ifndef SDL3PP_STORAGE_H_
2#define SDL3PP_STORAGE_H_
3
4#include <SDL3/SDL_storage.h>
5#include "SDL3pp_filesystem.h"
6#include "SDL3pp_properties.h"
7#include "SDL3pp_stdinc.h"
8
9namespace SDL {
10
226// Forward decl
227struct Storage;
228
230using StorageRaw = SDL_Storage*;
231
232// Forward decl
233struct StorageRef;
234
237{
239
242 : value(value)
243 {
244 }
245
247 constexpr StorageParam(std::nullptr_t _ = nullptr)
248 : value(nullptr)
249 {
250 }
251
253 constexpr explicit operator bool() const { return !!value; }
254
256 constexpr auto operator<=>(const StorageParam& other) const = default;
257
259 constexpr operator StorageRaw() const { return value; }
260};
261
278using StorageInterface = SDL_StorageInterface;
279
292{
293 StorageRaw m_resource = nullptr;
294
295public:
297 constexpr Storage(std::nullptr_t = nullptr) noexcept
298 : m_resource(0)
299 {
300 }
301
309 constexpr explicit Storage(const StorageRaw resource) noexcept
310 : m_resource(resource)
311 {
312 }
313
315 constexpr Storage(const Storage& other) = delete;
316
318 constexpr Storage(Storage&& other) noexcept
319 : Storage(other.release())
320 {
321 }
322
323 constexpr Storage(const StorageRef& other) = delete;
324
325 constexpr Storage(StorageRef&& other) = delete;
326
343 : m_resource(CheckError(SDL_OpenTitleStorage(override, props)))
344 {
345 }
346
372 : m_resource(CheckError(SDL_OpenUserStorage(org, app, props)))
373 {
374 }
375
399 : m_resource(CheckError(SDL_OpenFileStorage(path)))
400 {
401 }
402
430 Storage(const StorageInterface& iface, void* userdata)
431 : m_resource(CheckError(SDL_OpenStorage(&iface, userdata)))
432 {
433 }
434
436 ~Storage() { CheckError(SDL_CloseStorage(m_resource)); }
437
439 constexpr Storage& operator=(Storage&& other) noexcept
440 {
441 std::swap(m_resource, other.m_resource);
442 return *this;
443 }
444
445protected:
447 constexpr Storage& operator=(const Storage& other) noexcept = default;
448
449public:
451 constexpr StorageRaw get() const noexcept { return m_resource; }
452
454 constexpr StorageRaw release() noexcept
455 {
456 auto r = m_resource;
457 m_resource = nullptr;
458 return r;
459 }
460
462 constexpr auto operator<=>(const Storage& other) const noexcept = default;
463
465 constexpr explicit operator bool() const noexcept { return !!m_resource; }
466
468 constexpr operator StorageParam() const noexcept { return {m_resource}; }
469
482 bool Close();
483
496 bool Ready();
497
510 std::optional<Uint64> GetFileSize(StringParam path);
511
531 bool ReadFile(StringParam path, TargetBytes destination);
532
551 std::string ReadFile(StringParam path);
552
571 template<class T>
572 std::vector<T> ReadFileAs(StringParam path);
573
587 void WriteFile(StringParam path, SourceBytes source);
588
599 void CreateDirectory(StringParam path);
600
628 void* userdata);
629
654 std::vector<Path> EnumerateDirectory(StringParam path);
655
681
692 void RemovePath(StringParam path);
693
705 void RenamePath(StringParam oldpath, StringParam newpath);
706
718 void CopyFile(StringParam oldpath, StringParam newpath);
719
732
744
779 StringParam pattern,
780 GlobFlags flags);
781};
782
785{
786 using Storage::Storage;
787
795 StorageRef(StorageParam resource) noexcept
796 : Storage(resource.value)
797 {
798 }
799
807 StorageRef(StorageRaw resource) noexcept
808 : Storage(resource)
809 {
810 }
811
813 StorageRef(const StorageRef& other) noexcept
814 : Storage(other.get())
815 {
816 }
817
820};
821
838{
839 return Storage(std::move(override), props);
840}
841
867 StringParam app,
868 PropertiesParam props)
869{
870 return Storage(std::move(org), std::move(app), props);
871}
872
896{
897 return Storage(std::move(path));
898}
899
926inline Storage OpenStorage(const StorageInterface& iface, void* userdata)
927{
928 return Storage(iface, userdata);
929}
930
947inline bool CloseStorage(StorageRaw storage)
948{
949 return SDL_CloseStorage(storage);
950}
951
952inline bool Storage::Close() { return CloseStorage(release()); }
953
967inline bool StorageReady(StorageParam storage)
968{
969 return SDL_StorageReady(storage);
970}
971
972inline bool Storage::Ready() { return SDL::StorageReady(m_resource); }
973
987inline std::optional<Uint64> GetStorageFileSize(StorageParam storage,
988 StringParam path)
989{
990 if (Uint64 length; SDL_GetStorageFileSize(storage, path, &length)) {
991 return length;
992 }
993 return {};
994}
995
996inline std::optional<Uint64> Storage::GetFileSize(StringParam path)
997{
998 return SDL::GetStorageFileSize(m_resource, std::move(path));
999}
1000
1021inline bool ReadStorageFile(StorageParam storage,
1022 StringParam path,
1023 TargetBytes destination)
1024{
1025 return SDL_ReadStorageFile(
1026 storage, path, destination.data(), destination.size_bytes());
1027}
1028
1044inline std::string ReadStorageFile(StorageParam storage, StringParam path)
1045{
1046 auto sz = GetStorageFileSize(storage, path.c_str());
1047 if (!sz || *sz == 0) return {};
1048 std::string buffer(*sz, 0);
1049 CheckError(ReadStorageFile(storage, std::move(path), buffer));
1050 return buffer;
1051}
1052
1053inline bool Storage::ReadFile(StringParam path, TargetBytes destination)
1054{
1055 return SDL::ReadStorageFile(
1056 m_resource, std::move(path), std::move(destination));
1057}
1058
1059inline std::string Storage::ReadFile(StringParam path)
1060{
1061 return SDL::ReadStorageFile(m_resource, std::move(path));
1062}
1063
1079template<class T>
1080inline std::vector<T> ReadStorageFileAs(StorageParam storage, StringParam path)
1081{
1082 auto sz = GetStorageFileSize(storage, path.c_str());
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}));
1086 return buffer;
1087}
1088
1089template<class T>
1090inline std::vector<T> Storage::ReadFileAs(StringParam path)
1091{
1092 return SDL::ReadStorageFileAs<T>(m_resource, std::move(path));
1093}
1094
1109inline void WriteStorageFile(StorageParam storage,
1110 StringParam path,
1111 SourceBytes source)
1112{
1113 CheckError(
1114 SDL_WriteStorageFile(storage, path, source.data(), source.size_bytes()));
1115}
1116
1118{
1119 SDL::WriteStorageFile(m_resource, std::move(path), std::move(source));
1120}
1121
1134{
1135 CheckError(SDL_CreateStorageDirectory(storage, path));
1136}
1137
1139{
1140 SDL::CreateStorageDirectory(m_resource, std::move(path));
1141}
1142
1168 StringParam path,
1170 void* userdata)
1171{
1172 CheckError(SDL_EnumerateStorageDirectory(storage, path, callback, userdata));
1173}
1174
1199 StringParam path,
1200 EnumerateDirectoryCB callback)
1201{
1203 storage,
1204 std::move(path),
1205 [](void* userdata, const char* dirname, const char* fname) {
1206 auto& cb = *static_cast<EnumerateDirectoryCB*>(userdata);
1207 return cb(dirname, fname);
1208 },
1209 &callback);
1210}
1211
1235inline std::vector<Path> EnumerateStorageDirectory(StorageParam storage,
1236 StringParam path)
1237{
1238 std::vector<Path> r;
1239 EnumerateDirectory(std::move(path), [&](const char*, const char* fname) {
1240 r.emplace_back(fname);
1241 return ENUM_CONTINUE;
1242 });
1243 return r;
1244}
1245
1248 void* userdata)
1249{
1251 m_resource, std::move(path), callback, userdata);
1252}
1253
1254inline std::vector<Path> Storage::EnumerateDirectory(StringParam path)
1255{
1256 return SDL::EnumerateStorageDirectory(m_resource, std::move(path));
1257}
1258
1260 EnumerateDirectoryCB callback)
1261{
1262 SDL::EnumerateStorageDirectory(m_resource, std::move(path), callback);
1263}
1264
1277{
1278 CheckError(SDL_RemoveStoragePath(storage, path));
1279}
1280
1282{
1283 SDL::RemoveStoragePath(m_resource, std::move(path));
1284}
1285
1299 StringParam oldpath,
1300 StringParam newpath)
1301{
1302 CheckError(SDL_RenameStoragePath(storage, oldpath, newpath));
1303}
1304
1305inline void Storage::RenamePath(StringParam oldpath, StringParam newpath)
1306{
1307 SDL::RenameStoragePath(m_resource, std::move(oldpath), std::move(newpath));
1308}
1309
1322inline void CopyStorageFile(StorageParam storage,
1323 StringParam oldpath,
1324 StringParam newpath)
1325{
1326 CheckError(SDL_CopyStorageFile(storage, oldpath, newpath));
1327}
1328
1329inline void Storage::CopyFile(StringParam oldpath, StringParam newpath)
1330{
1331 SDL::CopyStorageFile(m_resource, std::move(oldpath), std::move(newpath));
1332}
1333
1347{
1348 if (PathInfo info; SDL_GetStoragePathInfo(storage, path, &info)) {
1349 return info;
1350 }
1351 return {};
1352}
1353
1355{
1356 return SDL::GetStoragePathInfo(m_resource, std::move(path));
1357}
1358
1371{
1372 return SDL_GetStorageSpaceRemaining(storage);
1373}
1374
1376{
1377 return SDL::GetStorageSpaceRemaining(m_resource);
1378}
1379
1414 StringParam path,
1415 StringParam pattern,
1416 GlobFlags flags)
1417{
1418 int count;
1419 auto data =
1420 CheckError(SDL_GlobStorageDirectory(storage, path, pattern, flags, &count));
1421 return OwnArray<char*>{data, size_t(count)};
1422}
1423
1425 StringParam pattern,
1426 GlobFlags flags)
1427{
1429 m_resource, std::move(path), std::move(pattern), flags);
1430}
1431
1433
1434} // namespace SDL
1435
1436#endif /* SDL3PP_STORAGE_H_ */
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