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
251using StorageInterface = SDL_StorageInterface;
252
265{
266 StorageRaw m_resource = nullptr;
267
268public:
270 constexpr Storage(std::nullptr_t = nullptr) noexcept
271 : m_resource(nullptr)
272 {
273 }
274
282 constexpr explicit Storage(const StorageRaw resource) noexcept
283 : m_resource(resource)
284 {
285 }
286
287protected:
289 constexpr Storage(const Storage& other) noexcept = default;
290
291public:
293 constexpr Storage(Storage&& other) noexcept
294 : Storage(other.release())
295 {
296 }
297
298 constexpr Storage(const StorageRef& other) = delete;
299
300 constexpr Storage(StorageRef&& other) = delete;
301
322
348
372
400 Storage(const StorageInterface& iface, void* userdata);
401
403 ~Storage() { CheckError(SDL_CloseStorage(m_resource)); }
404
406 constexpr Storage& operator=(Storage&& other) noexcept
407 {
408 std::swap(m_resource, other.m_resource);
409 return *this;
410 }
411
412protected:
414 constexpr Storage& operator=(const Storage& other) noexcept = default;
415
416public:
418 constexpr StorageRaw get() const noexcept { return m_resource; }
419
421 constexpr StorageRaw release() noexcept
422 {
423 auto r = m_resource;
424 m_resource = nullptr;
425 return r;
426 }
427
429 constexpr auto operator<=>(const Storage& other) const noexcept = default;
430
432 constexpr explicit operator bool() const noexcept { return !!m_resource; }
433
446 bool Close();
447
460 bool Ready();
461
474 std::optional<Uint64> GetFileSize(StringParam path);
475
495 bool ReadFile(StringParam path, TargetBytes destination);
496
515 std::string ReadFile(StringParam path);
516
535 template<class T>
536 std::vector<T> ReadFileAs(StringParam path);
537
551 void WriteFile(StringParam path, SourceBytes source);
552
563 void CreateDirectory(StringParam path);
564
592 void* userdata);
593
618 std::vector<Path> EnumerateDirectory(StringParam path);
619
645
656 void RemovePath(StringParam path);
657
669 void RenamePath(StringParam oldpath, StringParam newpath);
670
682 void CopyFile(StringParam oldpath, StringParam newpath);
683
696
708
743 StringParam pattern,
744 GlobFlags flags);
745};
746
753{
754 using Storage::Storage;
755
763 StorageRef(StorageRaw resource) noexcept
764 : Storage(resource)
765 {
766 }
767
775 constexpr StorageRef(const Storage& resource) noexcept
776 : Storage(resource.get())
777 {
778 }
779
787 constexpr StorageRef(Storage&& resource) noexcept
788 : Storage(std::move(resource).release())
789 {
790 }
791
793 constexpr StorageRef(const StorageRef& other) noexcept
794 : Storage(other.get())
795 {
796 }
797
799 constexpr StorageRef(StorageRef&& other) noexcept
800 : Storage(other.get())
801 {
802 }
803
806
808 constexpr StorageRef& operator=(const StorageRef& other) noexcept = default;
809
811 constexpr operator StorageRaw() const noexcept { return get(); }
812};
813
834{
835 return Storage(std::move(override), props);
836}
837
839 : m_resource(CheckError(SDL_OpenTitleStorage(override, props)))
840{
841}
842
844 : m_resource(CheckError(SDL_OpenUserStorage(org, app, props)))
845{
846}
847
849 : m_resource(CheckError(SDL_OpenFileStorage(path)))
850{
851}
852
853inline Storage::Storage(const StorageInterface& iface, void* userdata)
854 : m_resource(CheckError(SDL_OpenStorage(&iface, userdata)))
855{
856}
857
883 StringParam app,
884 PropertiesRef props)
885{
886 return Storage(std::move(org), std::move(app), props);
887}
888
912{
913 return Storage(std::move(path));
914}
915
942inline Storage OpenStorage(const StorageInterface& iface, void* userdata)
943{
944 return Storage(iface, userdata);
945}
946
963inline bool CloseStorage(StorageRaw storage)
964{
965 return SDL_CloseStorage(storage);
966}
967
968inline bool Storage::Close() { return CloseStorage(release()); }
969
983inline bool StorageReady(StorageRef storage)
984{
985 return SDL_StorageReady(storage);
986}
987
988inline bool Storage::Ready() { return SDL::StorageReady(m_resource); }
989
1003inline std::optional<Uint64> GetStorageFileSize(StorageRef storage,
1004 StringParam path)
1005{
1006 if (Uint64 length; SDL_GetStorageFileSize(storage, path, &length)) {
1007 return length;
1008 }
1009 return {};
1010}
1011
1012inline std::optional<Uint64> Storage::GetFileSize(StringParam path)
1013{
1014 return SDL::GetStorageFileSize(m_resource, std::move(path));
1015}
1016
1037inline bool ReadStorageFile(StorageRef storage,
1038 StringParam path,
1039 TargetBytes destination)
1040{
1041 return SDL_ReadStorageFile(
1042 storage, path, destination.data(), destination.size_bytes());
1043}
1044
1060inline std::string ReadStorageFile(StorageRef storage, StringParam path)
1061{
1062 auto sz = GetStorageFileSize(storage, path.c_str());
1063 if (!sz || *sz == 0) return {};
1064 std::string buffer(*sz, 0);
1065 CheckError(ReadStorageFile(storage, std::move(path), buffer));
1066 return buffer;
1067}
1068
1069inline bool Storage::ReadFile(StringParam path, TargetBytes destination)
1070{
1071 return SDL::ReadStorageFile(
1072 m_resource, std::move(path), std::move(destination));
1073}
1074
1075inline std::string Storage::ReadFile(StringParam path)
1076{
1077 return SDL::ReadStorageFile(m_resource, std::move(path));
1078}
1079
1095template<class T>
1096inline std::vector<T> ReadStorageFileAs(StorageRef storage, StringParam path)
1097{
1098 auto sz = GetStorageFileSize(storage, path.c_str());
1099 if (!sz || *sz == 0) return {};
1100 std::vector<T> buffer(*sz / sizeof(T) + (*sz % sizeof(T) ? 1 : 0), 0);
1101 CheckError(ReadFile(std::move(path), {buffer.data(), *sz}));
1102 return buffer;
1103}
1104
1105template<class T>
1106inline std::vector<T> Storage::ReadFileAs(StringParam path)
1107{
1108 return SDL::ReadStorageFileAs<T>(m_resource, std::move(path));
1109}
1110
1125inline void WriteStorageFile(StorageRef storage,
1126 StringParam path,
1127 SourceBytes source)
1128{
1129 CheckError(
1130 SDL_WriteStorageFile(storage, path, source.data(), source.size_bytes()));
1131}
1132
1134{
1135 SDL::WriteStorageFile(m_resource, std::move(path), std::move(source));
1136}
1137
1150{
1151 CheckError(SDL_CreateStorageDirectory(storage, path));
1152}
1153
1155{
1156 SDL::CreateStorageDirectory(m_resource, std::move(path));
1157}
1158
1184 StringParam path,
1186 void* userdata)
1187{
1188 CheckError(SDL_EnumerateStorageDirectory(storage, path, callback, userdata));
1189}
1190
1215 StringParam path,
1216 EnumerateDirectoryCB callback)
1217{
1219 storage,
1220 std::move(path),
1221 [](void* userdata, const char* dirname, const char* fname) {
1222 auto& cb = *static_cast<EnumerateDirectoryCB*>(userdata);
1223 return cb(dirname, fname);
1224 },
1225 &callback);
1226}
1227
1251inline std::vector<Path> EnumerateStorageDirectory(StorageRef storage,
1252 StringParam path)
1253{
1254 std::vector<Path> r;
1255 EnumerateDirectory(std::move(path), [&](const char*, const char* fname) {
1256 r.emplace_back(fname);
1257 return ENUM_CONTINUE;
1258 });
1259 return r;
1260}
1261
1264 void* userdata)
1265{
1267 m_resource, std::move(path), callback, userdata);
1268}
1269
1270inline std::vector<Path> Storage::EnumerateDirectory(StringParam path)
1271{
1272 return SDL::EnumerateStorageDirectory(m_resource, std::move(path));
1273}
1274
1276 EnumerateDirectoryCB callback)
1277{
1278 SDL::EnumerateStorageDirectory(m_resource, std::move(path), callback);
1279}
1280
1292inline void RemoveStoragePath(StorageRef storage, StringParam path)
1293{
1294 CheckError(SDL_RemoveStoragePath(storage, path));
1295}
1296
1298{
1299 SDL::RemoveStoragePath(m_resource, std::move(path));
1300}
1301
1314inline void RenameStoragePath(StorageRef storage,
1315 StringParam oldpath,
1316 StringParam newpath)
1317{
1318 CheckError(SDL_RenameStoragePath(storage, oldpath, newpath));
1319}
1320
1321inline void Storage::RenamePath(StringParam oldpath, StringParam newpath)
1322{
1323 SDL::RenameStoragePath(m_resource, std::move(oldpath), std::move(newpath));
1324}
1325
1338inline void CopyStorageFile(StorageRef storage,
1339 StringParam oldpath,
1340 StringParam newpath)
1341{
1342 CheckError(SDL_CopyStorageFile(storage, oldpath, newpath));
1343}
1344
1345inline void Storage::CopyFile(StringParam oldpath, StringParam newpath)
1346{
1347 SDL::CopyStorageFile(m_resource, std::move(oldpath), std::move(newpath));
1348}
1349
1363{
1364 if (PathInfo info; SDL_GetStoragePathInfo(storage, path, &info)) {
1365 return info;
1366 }
1367 return {};
1368}
1369
1371{
1372 return SDL::GetStoragePathInfo(m_resource, std::move(path));
1373}
1374
1387{
1388 return SDL_GetStorageSpaceRemaining(storage);
1389}
1390
1392{
1393 return SDL::GetStorageSpaceRemaining(m_resource);
1394}
1395
1430 StringParam path,
1431 StringParam pattern,
1432 GlobFlags flags)
1433{
1434 int count;
1435 auto data =
1436 CheckError(SDL_GlobStorageDirectory(storage, path, pattern, flags, &count));
1437 return OwnArray<char*>{data, size_t(count)};
1438}
1439
1441 StringParam pattern,
1442 GlobFlags flags)
1443{
1445 m_resource, std::move(path), std::move(pattern), flags);
1446}
1447
1449
1450} // namespace SDL
1451
1452#endif /* SDL3PP_STORAGE_H_ */
Base class for SDL memory allocated array wrap.
Definition: SDL3pp_ownPtr.h:44
Source byte stream.
Definition: SDL3pp_strings.h:240
constexpr size_t size_bytes() const
Retrieves contained size in bytes.
Definition: SDL3pp_strings.h:304
constexpr const char * data() const
Retrieves contained data.
Definition: SDL3pp_strings.h:307
An abstract interface for filesystem access.
Definition: SDL3pp_storage.h:265
constexpr auto operator<=>(const Storage &other) const noexcept=default
Comparison.
constexpr Storage & operator=(const Storage &other) noexcept=default
Assignment operator.
constexpr Storage(std::nullptr_t=nullptr) noexcept
Default ctor.
Definition: SDL3pp_storage.h:270
constexpr StorageRaw release() noexcept
Retrieves underlying StorageRaw and clear this.
Definition: SDL3pp_storage.h:421
~Storage()
Destructor.
Definition: SDL3pp_storage.h:403
constexpr Storage(Storage &&other) noexcept
Move constructor.
Definition: SDL3pp_storage.h:293
constexpr Storage(const Storage &other) noexcept=default
Copy constructor.
constexpr Storage & operator=(Storage &&other) noexcept
Assignment operator.
Definition: SDL3pp_storage.h:406
constexpr Storage(const StorageRaw resource) noexcept
Constructs from StorageRef.
Definition: SDL3pp_storage.h:282
constexpr StorageRaw get() const noexcept
Retrieves underlying StorageRaw.
Definition: SDL3pp_storage.h:418
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:326
constexpr char * data() const
Retrieves contained data.
Definition: SDL3pp_strings.h:412
constexpr size_t size_bytes() const
Retrieves contained size in bytes.
Definition: SDL3pp_strings.h:409
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:488
constexpr EnumerationResult ENUM_CONTINUE
Value that requests that enumeration continue.
Definition: SDL3pp_filesystem.h:426
EnumerationResult(SDLCALL *)(void *userdata, const char *dirname, const char *fname) EnumerateDirectoryCallback
Callback for directory enumeration.
Definition: SDL3pp_filesystem.h:460
void EnumerateDirectory(StringParam path, EnumerateDirectoryCallback callback, void *userdata)
Enumerate a directory through a callback function.
Definition: SDL3pp_filesystem.h:510
Uint32 GlobFlags
Flags for path matching.
Definition: SDL3pp_filesystem.h:392
::Uint64 Uint64
An unsigned 64-bit integer type.
Definition: SDL3pp_stdinc.h:310
Uint64 GetStorageSpaceRemaining(StorageRef storage)
Queries the remaining space in a storage container.
Definition: SDL3pp_storage.h:1386
void CreateDirectory(StringParam path)
Create a directory in a writable storage container.
Definition: SDL3pp_storage.h:1154
bool ReadStorageFile(StorageRef storage, StringParam path, TargetBytes destination)
Synchronously read a file from a storage container into a client-provided buffer.
Definition: SDL3pp_storage.h:1037
std::vector< T > ReadStorageFileAs(StorageRef storage, StringParam path)
Synchronously read a file from a storage container into a client-provided buffer.
Definition: SDL3pp_storage.h:1096
SDL_StorageInterface StorageInterface
Function interface for Storage.
Definition: SDL3pp_storage.h:251
PathInfo GetStoragePathInfo(StorageRef storage, StringParam path)
Get information about a filesystem path in a storage container.
Definition: SDL3pp_storage.h:1362
PathInfo GetPathInfo(StringParam path)
Get information about a filesystem path in a storage container.
Definition: SDL3pp_storage.h:1370
Storage OpenTitleStorage(StringParam override, PropertiesRef props)
Opens up a read-only container for the application's filesystem.
Definition: SDL3pp_storage.h:833
Storage OpenStorage(const StorageInterface &iface, void *userdata)
Opens up a container using a client-provided storage interface.
Definition: SDL3pp_storage.h:942
void WriteFile(StringParam path, SourceBytes source)
Synchronously write a file from client memory into a storage container.
Definition: SDL3pp_storage.h:1133
Uint64 GetSpaceRemaining()
Queries the remaining space in a storage container.
Definition: SDL3pp_storage.h:1391
OwnArray< char * > GlobDirectory(StringParam path, StringParam pattern, GlobFlags flags)
Enumerate a directory tree, filtered by pattern, and return a list.
Definition: SDL3pp_storage.h:1440
Storage OpenUserStorage(StringParam org, StringParam app, PropertiesRef props)
Opens up a container for a user's unique read/write filesystem.
Definition: SDL3pp_storage.h:882
std::vector< T > ReadFileAs(StringParam path)
Synchronously read a file from a storage container into a client-provided buffer.
Definition: SDL3pp_storage.h:1106
SDL_Storage * StorageRaw
Alias to raw representation for Storage.
Definition: SDL3pp_storage.h:230
bool ReadFile(StringParam path, TargetBytes destination)
Synchronously read a file from a storage container into a client-provided buffer.
Definition: SDL3pp_storage.h:1069
std::optional< Uint64 > GetFileSize(StringParam path)
Query the size of a file within a storage container.
Definition: SDL3pp_storage.h:1012
bool Close()
Closes and frees a storage container.
Definition: SDL3pp_storage.h:968
void RemovePath(StringParam path)
Remove a file or an empty directory in a writable storage container.
Definition: SDL3pp_storage.h:1297
void RemoveStoragePath(StorageRef storage, StringParam path)
Remove a file or an empty directory in a writable storage container.
Definition: SDL3pp_storage.h:1292
void CopyStorageFile(StorageRef storage, StringParam oldpath, StringParam newpath)
Copy a file in a writable storage container.
Definition: SDL3pp_storage.h:1338
std::optional< Uint64 > GetStorageFileSize(StorageRef storage, StringParam path)
Query the size of a file within a storage container.
Definition: SDL3pp_storage.h:1003
bool Ready()
Checks if the storage container is ready to use.
Definition: SDL3pp_storage.h:988
void RenamePath(StringParam oldpath, StringParam newpath)
Rename a file or directory in a writable storage container.
Definition: SDL3pp_storage.h:1321
bool StorageReady(StorageRef storage)
Checks if the storage container is ready to use.
Definition: SDL3pp_storage.h:983
Storage OpenFileStorage(StringParam path)
Opens up a container for local filesystem storage.
Definition: SDL3pp_storage.h:911
void CreateStorageDirectory(StorageRef storage, StringParam path)
Create a directory in a writable storage container.
Definition: SDL3pp_storage.h:1149
void CopyFile(StringParam oldpath, StringParam newpath)
Copy a file in a writable storage container.
Definition: SDL3pp_storage.h:1345
bool CloseStorage(StorageRaw storage)
Closes and frees a storage container.
Definition: SDL3pp_storage.h:963
void WriteStorageFile(StorageRef storage, StringParam path, SourceBytes source)
Synchronously write a file from client memory into a storage container.
Definition: SDL3pp_storage.h:1125
void EnumerateDirectory(StringParam path, EnumerateDirectoryCallback callback, void *userdata)
Enumerate a directory in a storage container through a callback function.
Definition: SDL3pp_storage.h:1262
void RenameStoragePath(StorageRef storage, StringParam oldpath, StringParam newpath)
Rename a file or directory in a writable storage container.
Definition: SDL3pp_storage.h:1314
OwnArray< char * > GlobStorageDirectory(StorageRef storage, StringParam path, StringParam pattern, GlobFlags flags)
Enumerate a directory tree, filtered by pattern, and return a list.
Definition: SDL3pp_storage.h:1429
void EnumerateStorageDirectory(StorageRef storage, StringParam path, EnumerateDirectoryCallback callback, void *userdata)
Enumerate a directory in a storage container through a callback function.
Definition: SDL3pp_storage.h:1183
Main include header for the SDL3pp library.
Information about a path on the filesystem.
Definition: SDL3pp_filesystem.h:352
Reference for Properties.
Definition: SDL3pp_properties.h:691
Reference for Storage.
Definition: SDL3pp_storage.h:753
constexpr StorageRef(const Storage &resource) noexcept
Constructs from Storage.
Definition: SDL3pp_storage.h:775
constexpr StorageRef(StorageRef &&other) noexcept
Move constructor.
Definition: SDL3pp_storage.h:799
StorageRef(StorageRaw resource) noexcept
Constructs from raw Storage.
Definition: SDL3pp_storage.h:763
constexpr StorageRef & operator=(const StorageRef &other) noexcept=default
Assignment operator.
~StorageRef()
Destructor.
Definition: SDL3pp_storage.h:805
constexpr StorageRef(const StorageRef &other) noexcept
Copy constructor.
Definition: SDL3pp_storage.h:793
constexpr StorageRef(Storage &&resource) noexcept
Constructs from Storage.
Definition: SDL3pp_storage.h:787