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
229// Forward decl
230struct Storage;
231
233using StorageRaw = SDL_Storage*;
234
235// Forward decl
236struct StorageRef;
237
240{
242
245 : value(value)
246 {
247 }
248
250 constexpr StorageParam(std::nullptr_t _ = nullptr)
251 : value(nullptr)
252 {
253 }
254
256 constexpr explicit operator bool() const { return !!value; }
257
259 constexpr auto operator<=>(const StorageParam& other) const = default;
260
262 constexpr operator StorageRaw() const { return value; }
263};
264
281using StorageInterface = SDL_StorageInterface;
282
295{
296 StorageRaw m_resource = nullptr;
297
298public:
300 constexpr Storage() = default;
301
309 constexpr explicit Storage(const StorageRaw resource)
310 : m_resource(resource)
311 {
312 }
313
315 constexpr Storage(const Storage& other) = delete;
316
318 constexpr Storage(Storage&& other)
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
440 {
441 std::swap(m_resource, other.m_resource);
442 return *this;
443 }
444
446 constexpr StorageRaw get() const { return m_resource; }
447
449 constexpr StorageRaw release()
450 {
451 auto r = m_resource;
452 m_resource = nullptr;
453 return r;
454 }
455
457 constexpr auto operator<=>(const Storage& other) const = default;
458
460 constexpr bool operator==(std::nullptr_t _) const { return !m_resource; }
461
463 constexpr explicit operator bool() const { return !!m_resource; }
464
466 constexpr operator StorageParam() const { return {m_resource}; }
467
480 bool Close();
481
494 bool Ready();
495
508 std::optional<Uint64> GetFileSize(StringParam path);
509
529 bool ReadFile(StringParam path, TargetBytes destination);
530
549 std::string ReadFile(StringParam path);
550
564 void WriteFile(StringParam path, SourceBytes source);
565
576 void CreateDirectory(StringParam path);
577
606 void* userdata);
607
633 std::vector<Path> EnumerateDirectory(StringParam path);
634
661
672 void RemovePath(StringParam path);
673
685 void RenamePath(StringParam oldpath, StringParam newpath);
686
698 void CopyFile(StringParam oldpath, StringParam newpath);
699
712
724
759 StringParam pattern,
760 GlobFlags flags);
761
780 template<class T>
781 std::vector<T> ReadFileAs(StringParam path);
782};
783
786{
795 : Storage(resource.value)
796 {
797 }
798
800 StorageRef(const StorageRef& other)
801 : Storage(other.get())
802 {
803 }
804
807};
808
825{
826 return Storage(std::move(override), props);
827}
828
854 StringParam app,
855 PropertiesParam props)
856{
857 return Storage(std::move(org), std::move(app), props);
858}
859
883{
884 return Storage(std::move(path));
885}
886
914inline Storage OpenStorage(const StorageInterface& iface, void* userdata)
915{
916 return Storage(iface, userdata);
917}
918
935inline bool CloseStorage(StorageRaw storage)
936{
937 return SDL_CloseStorage(storage);
938}
939
940inline bool Storage::Close() { return CloseStorage(release()); }
941
955inline bool StorageReady(StorageParam storage)
956{
957 return SDL_StorageReady(storage);
958}
959
960inline bool Storage::Ready() { return SDL::StorageReady(m_resource); }
961
975inline std::optional<Uint64> GetStorageFileSize(StorageParam storage,
976 StringParam path)
977{
978 if (Uint64 length; SDL_GetStorageFileSize(storage, path, &length)) {
979 return length;
980 }
981 return {};
982}
983
984inline std::optional<Uint64> Storage::GetFileSize(StringParam path)
985{
986 return SDL::GetStorageFileSize(m_resource, std::move(path));
987}
988
1005inline bool ReadStorageFile(StorageParam storage,
1006 StringParam path,
1007 TargetBytes destination)
1008{
1009 return SDL_ReadStorageFile(
1010 storage, path, destination.data(), destination.size_bytes());
1011}
1012
1028inline std::string ReadStorageFile(StorageParam storage, StringParam path)
1029{
1030 auto sz = GetStorageFileSize(storage, path.c_str());
1031 if (!sz || *sz == 0) return {};
1032 std::string buffer(*sz, 0);
1033 CheckError(ReadStorageFile(storage, std::move(path), buffer));
1034 return buffer;
1035}
1036
1037inline bool Storage::ReadFile(StringParam path, TargetBytes destination)
1038{
1039 return SDL::ReadStorageFile(
1040 m_resource, std::move(path), std::move(destination));
1041}
1042
1043inline std::string Storage::ReadFile(StringParam path)
1044{
1045 return SDL::ReadStorageFile(m_resource, std::move(path));
1046}
1047
1063template<class T>
1064inline std::vector<T> ReadStorageFileAs(StorageParam storage, StringParam path)
1065{
1066 auto sz = GetStorageFileSize(storage, path.c_str());
1067 if (!sz || *sz == 0) return {};
1068 std::vector<T> buffer(*sz / sizeof(T) + (*sz % sizeof(T) ? 1 : 0), 0);
1069 CheckError(ReadFile(std::move(path), {buffer.data(), *sz}));
1070 return buffer;
1071}
1072
1073template<class T>
1074inline std::vector<T> Storage::ReadFileAs(StringParam path)
1075{
1076 return SDL::ReadStorageFileAs<T>(m_resource, std::move(path));
1077}
1078
1093inline void WriteStorageFile(StorageParam storage,
1094 StringParam path,
1095 SourceBytes source)
1096{
1097 CheckError(
1098 SDL_WriteStorageFile(storage, path, source.data(), source.size_bytes()));
1099}
1100
1102{
1103 SDL::WriteStorageFile(m_resource, std::move(path), std::move(source));
1104}
1105
1118{
1119 CheckError(SDL_CreateStorageDirectory(storage, path));
1120}
1121
1123{
1124 SDL::CreateStorageDirectory(m_resource, std::move(path));
1125}
1126
1154 StringParam path,
1156 void* userdata)
1157{
1158 CheckError(SDL_EnumerateStorageDirectory(storage, path, callback, userdata));
1159}
1160
1187 StringParam path,
1188 EnumerateDirectoryCB callback)
1189{
1191 storage,
1192 std::move(path),
1193 [](void* userdata, const char* dirname, const char* fname) {
1194 auto& cb = *static_cast<EnumerateDirectoryCB*>(userdata);
1195 return cb(dirname, fname);
1196 },
1197 &callback);
1198}
1199
1225inline std::vector<Path> EnumerateStorageDirectory(StorageParam storage,
1226 StringParam path)
1227{
1228 std::vector<Path> r;
1229 EnumerateDirectory(std::move(path), [&](const char*, const char* fname) {
1230 r.emplace_back(fname);
1231 return ENUM_CONTINUE;
1232 });
1233 return r;
1234}
1235
1238 void* userdata)
1239{
1241 m_resource, std::move(path), callback, userdata);
1242}
1243
1244inline std::vector<Path> Storage::EnumerateDirectory(StringParam path)
1245{
1246 return SDL::EnumerateStorageDirectory(m_resource, std::move(path));
1247}
1248
1250 EnumerateDirectoryCB callback)
1251{
1252 SDL::EnumerateStorageDirectory(m_resource, std::move(path), callback);
1253}
1254
1267{
1268 CheckError(SDL_RemoveStoragePath(storage, path));
1269}
1270
1272{
1273 SDL::RemoveStoragePath(m_resource, std::move(path));
1274}
1275
1289 StringParam oldpath,
1290 StringParam newpath)
1291{
1292 CheckError(SDL_RenameStoragePath(storage, oldpath, newpath));
1293}
1294
1295inline void Storage::RenamePath(StringParam oldpath, StringParam newpath)
1296{
1297 SDL::RenameStoragePath(m_resource, std::move(oldpath), std::move(newpath));
1298}
1299
1312inline void CopyStorageFile(StorageParam storage,
1313 StringParam oldpath,
1314 StringParam newpath)
1315{
1316 CheckError(SDL_CopyStorageFile(storage, oldpath, newpath));
1317}
1318
1319inline void Storage::CopyFile(StringParam oldpath, StringParam newpath)
1320{
1321 SDL::CopyStorageFile(m_resource, std::move(oldpath), std::move(newpath));
1322}
1323
1337{
1338 if (PathInfo info; SDL_GetStoragePathInfo(storage, path, &info)) {
1339 return info;
1340 }
1341 return {};
1342}
1343
1345{
1346 return SDL::GetStoragePathInfo(m_resource, std::move(path));
1347}
1348
1361{
1362 return SDL_GetStorageSpaceRemaining(storage);
1363}
1364
1366{
1367 return SDL::GetStorageSpaceRemaining(m_resource);
1368}
1369
1404 StringParam path,
1405 StringParam pattern,
1406 GlobFlags flags)
1407{
1408 int count;
1409 auto data =
1410 CheckError(SDL_GlobStorageDirectory(storage, path, pattern, flags, &count));
1411 return OwnArray<char*>{data, size_t(count)};
1412}
1413
1415 StringParam pattern,
1416 GlobFlags flags)
1417{
1419 m_resource, std::move(path), std::move(pattern), flags);
1420}
1421
1423
1424} // namespace SDL
1425
1426#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:295
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 StorageRaw get() const
Retrieves underlying StorageRaw.
Definition: SDL3pp_storage.h:446
constexpr Storage(Storage &&other)
Move constructor.
Definition: SDL3pp_storage.h:318
Storage & operator=(Storage other)
Assignment operator.
Definition: SDL3pp_storage.h:439
Storage(StringParam path)
Opens up a container for local filesystem storage.
Definition: SDL3pp_storage.h:398
constexpr bool operator==(std::nullptr_t _) const
Comparison.
Definition: SDL3pp_storage.h:460
constexpr Storage(const Storage &other)=delete
Copy constructor.
~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 StorageRaw release()
Retrieves underlying StorageRaw and clear this.
Definition: SDL3pp_storage.h:449
Storage(StringParam override, PropertiesParam props)
Opens up a read-only container for the application's filesystem.
Definition: SDL3pp_storage.h:342
constexpr auto operator<=>(const Storage &other) const =default
Comparison.
constexpr Storage()=default
Default ctor.
constexpr Storage(const StorageRaw resource)
Constructs from StorageParam.
Definition: SDL3pp_storage.h:309
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:198
std::function< EnumerationResult(const char *dirname, const char *fname)> EnumerateDirectoryCB
Callback for directory enumeration.
Definition: SDL3pp_filesystem.h:469
constexpr EnumerationResult ENUM_CONTINUE
Value that requests that enumeration continue.
Definition: SDL3pp_filesystem.h:409
void EnumerateDirectory(StringParam path, EnumerateDirectoryCallback callback, void *userdata)
Enumerate a directory through a callback function.
Definition: SDL3pp_filesystem.h:491
SDL_EnumerateDirectoryCallback EnumerateDirectoryCallback
Callback for directory enumeration.
Definition: SDL3pp_filesystem.h:442
Uint32 GlobFlags
Flags for path matching.
Definition: SDL3pp_filesystem.h:377
Uint64 Uint64
An unsigned 64-bit integer type.
Definition: SDL3pp_stdinc.h:366
void CreateDirectory(StringParam path)
Create a directory in a writable storage container.
Definition: SDL3pp_storage.h:1122
std::optional< Uint64 > GetStorageFileSize(StorageParam storage, StringParam path)
Query the size of a file within a storage container.
Definition: SDL3pp_storage.h:975
Storage OpenUserStorage(StringParam org, StringParam app, PropertiesParam props)
Opens up a container for a user's unique read/write filesystem.
Definition: SDL3pp_storage.h:853
Storage OpenTitleStorage(StringParam override, PropertiesParam props)
Opens up a read-only container for the application's filesystem.
Definition: SDL3pp_storage.h:824
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:1403
SDL_StorageInterface StorageInterface
Function interface for Storage.
Definition: SDL3pp_storage.h:281
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:1005
PathInfo GetPathInfo(StringParam path)
Get information about a filesystem path in a storage container.
Definition: SDL3pp_storage.h:1344
void RenameStoragePath(StorageParam storage, StringParam oldpath, StringParam newpath)
Rename a file or directory in a writable storage container.
Definition: SDL3pp_storage.h:1288
Storage OpenStorage(const StorageInterface &iface, void *userdata)
Opens up a container using a client-provided storage interface.
Definition: SDL3pp_storage.h:914
void WriteFile(StringParam path, SourceBytes source)
Synchronously write a file from client memory into a storage container.
Definition: SDL3pp_storage.h:1101
Uint64 GetSpaceRemaining()
Queries the remaining space in a storage container.
Definition: SDL3pp_storage.h:1365
OwnArray< char * > GlobDirectory(StringParam path, StringParam pattern, GlobFlags flags)
Enumerate a directory tree, filtered by pattern, and return a list.
Definition: SDL3pp_storage.h:1414
std::vector< T > ReadFileAs(StringParam path)
Synchronously read a file from a storage container into a client-provided buffer.
Definition: SDL3pp_storage.h:1074
SDL_Storage * StorageRaw
Alias to raw representation for Storage.
Definition: SDL3pp_storage.h:233
bool StorageReady(StorageParam storage)
Checks if the storage container is ready to use.
Definition: SDL3pp_storage.h:955
bool ReadFile(StringParam path, TargetBytes destination)
Synchronously read a file from a storage container into a client-provided buffer.
Definition: SDL3pp_storage.h:1037
std::optional< Uint64 > GetFileSize(StringParam path)
Query the size of a file within a storage container.
Definition: SDL3pp_storage.h:984
void RemoveStoragePath(StorageParam storage, StringParam path)
Remove a file or an empty directory in a writable storage container.
Definition: SDL3pp_storage.h:1266
bool Close()
Closes and frees a storage container.
Definition: SDL3pp_storage.h:940
void RemovePath(StringParam path)
Remove a file or an empty directory in a writable storage container.
Definition: SDL3pp_storage.h:1271
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:1153
bool Ready()
Checks if the storage container is ready to use.
Definition: SDL3pp_storage.h:960
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:1064
void CopyStorageFile(StorageParam storage, StringParam oldpath, StringParam newpath)
Copy a file in a writable storage container.
Definition: SDL3pp_storage.h:1312
void RenamePath(StringParam oldpath, StringParam newpath)
Rename a file or directory in a writable storage container.
Definition: SDL3pp_storage.h:1295
Storage OpenFileStorage(StringParam path)
Opens up a container for local filesystem storage.
Definition: SDL3pp_storage.h:882
void WriteStorageFile(StorageParam storage, StringParam path, SourceBytes source)
Synchronously write a file from client memory into a storage container.
Definition: SDL3pp_storage.h:1093
PathInfo GetStoragePathInfo(StorageParam storage, StringParam path)
Get information about a filesystem path in a storage container.
Definition: SDL3pp_storage.h:1336
void CreateStorageDirectory(StorageParam storage, StringParam path)
Create a directory in a writable storage container.
Definition: SDL3pp_storage.h:1117
void CopyFile(StringParam oldpath, StringParam newpath)
Copy a file in a writable storage container.
Definition: SDL3pp_storage.h:1319
Uint64 GetStorageSpaceRemaining(StorageParam storage)
Queries the remaining space in a storage container.
Definition: SDL3pp_storage.h:1360
bool CloseStorage(StorageRaw storage)
Closes and frees a storage container.
Definition: SDL3pp_storage.h:935
void EnumerateDirectory(StringParam path, EnumerateDirectoryCallback callback, void *userdata)
Enumerate a directory in a storage container through a callback function.
Definition: SDL3pp_storage.h:1236
Main include header for the SDL3pp library.
Information about a path on the filesystem.
Definition: SDL3pp_filesystem.h:343
Safely wrap Properties for non owning parameters.
Definition: SDL3pp_properties.h:52
Safely wrap Storage for non owning parameters.
Definition: SDL3pp_storage.h:240
constexpr StorageParam(StorageRaw value)
Constructs from StorageRaw.
Definition: SDL3pp_storage.h:244
constexpr auto operator<=>(const StorageParam &other) const =default
Comparison.
constexpr StorageParam(std::nullptr_t _=nullptr)
Constructs null/invalid.
Definition: SDL3pp_storage.h:250
StorageRaw value
parameter's StorageRaw
Definition: SDL3pp_storage.h:241
Semi-safe reference for Storage.
Definition: SDL3pp_storage.h:786
StorageRef(StorageParam resource)
Constructs from StorageParam.
Definition: SDL3pp_storage.h:794
StorageRef(const StorageRef &other)
Copy constructor.
Definition: SDL3pp_storage.h:800
~StorageRef()
Destructor.
Definition: SDL3pp_storage.h:806