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
225
226// Forward decl
227struct Storage;
228
230using StorageRaw = SDL_Storage*;
231
238
255using StorageInterface = SDL_StorageInterface;
256
268struct Storage : ResourceBase<StorageRaw>
269{
271
279 constexpr explicit Storage(StorageRaw resource) noexcept
280 : ResourceBase(resource)
281 {
282 }
283
285 constexpr Storage(const Storage& other) = delete;
286
288 constexpr Storage(Storage&& other) noexcept
289 : Storage(other.release())
290 {
291 }
292
293 constexpr Storage(const StorageRef& other) = delete;
294
295 constexpr Storage(StorageRef&& other) = delete;
296
316 Storage(StringParam override, PropertiesRef props);
317
343
366 Storage(StringParam path);
367
395 Storage(const StorageInterface& iface, void* userdata);
396
398 ~Storage() { CheckError(SDL_CloseStorage(get())); }
399
401 constexpr Storage& operator=(Storage&& other) noexcept
402 {
403 swap(*this, other);
404 return *this;
405 }
406
408 Storage& operator=(const Storage& other) = delete;
409
425 bool Close();
426
439 bool Ready();
440
453 std::optional<Uint64> GetFileSize(StringParam path);
454
474 bool ReadFile(StringParam path, TargetBytes destination);
475
494 std::string ReadFile(StringParam path);
495
514 template<class T>
515 std::vector<T> ReadFileAs(StringParam path);
516
530 void WriteFile(StringParam path, SourceBytes source);
531
542 void CreateDirectory(StringParam path);
543
571 void* userdata);
572
597 std::vector<Path> EnumerateDirectory(StringParam path);
598
624
635 void RemovePath(StringParam path);
636
648 void RenamePath(StringParam oldpath, StringParam newpath);
649
661 void CopyFile(StringParam oldpath, StringParam newpath);
662
675
687
722 StringParam pattern,
723 GlobFlags flags);
724};
725
746{
747 return Storage(std::move(override), props);
748}
749
751 : Storage(CheckError(SDL_OpenTitleStorage(override, props)))
752{
753}
754
756 : Storage(CheckError(SDL_OpenUserStorage(org, app, props)))
757{
758}
759
761 : Storage(CheckError(SDL_OpenFileStorage(path)))
762{
763}
764
765inline Storage::Storage(const StorageInterface& iface, void* userdata)
766 : Storage(CheckError(SDL_OpenStorage(&iface, userdata)))
767{
768}
769
795 StringParam app,
796 PropertiesRef props)
797{
798 return Storage(std::move(org), std::move(app), props);
799}
800
824{
825 return Storage(std::move(path));
826}
827
855inline Storage OpenStorage(const StorageInterface& iface, void* userdata)
856{
857 return Storage(iface, userdata);
858}
859
876inline bool CloseStorage(StorageRaw storage)
877{
878 return SDL_CloseStorage(storage);
879}
880
881inline bool Storage::Close() { return CloseStorage(release()); }
882
896inline bool StorageReady(StorageRef storage)
897{
898 return SDL_StorageReady(storage);
899}
900
901inline bool Storage::Ready() { return SDL::StorageReady(get()); }
902
916inline std::optional<Uint64> GetStorageFileSize(StorageRef storage,
917 StringParam path)
918{
919 if (Uint64 length; SDL_GetStorageFileSize(storage, path, &length)) {
920 return length;
921 }
922 return {};
923}
924
925inline std::optional<Uint64> Storage::GetFileSize(StringParam path)
926{
927 return SDL::GetStorageFileSize(get(), std::move(path));
928}
929
950inline bool ReadStorageFile(StorageRef storage,
951 StringParam path,
952 TargetBytes destination)
953{
954 return SDL_ReadStorageFile(
955 storage, path, destination.data(), destination.size_bytes());
956}
957
973inline std::string ReadStorageFile(StorageRef storage, StringParam path)
974{
975 auto sz = GetStorageFileSize(storage, path.c_str());
976 if (!sz || *sz == 0) return {};
977 std::string buffer(*sz, 0);
978 CheckError(ReadStorageFile(storage, std::move(path), buffer));
979 return buffer;
980}
981
982inline bool Storage::ReadFile(StringParam path, TargetBytes destination)
983{
984 return SDL::ReadStorageFile(get(), std::move(path), std::move(destination));
985}
986
987inline std::string Storage::ReadFile(StringParam path)
988{
989 return SDL::ReadStorageFile(get(), std::move(path));
990}
991
1007template<class T>
1008inline std::vector<T> ReadStorageFileAs(StorageRef storage, StringParam path)
1009{
1010 auto sz = GetStorageFileSize(storage, path.c_str());
1011 if (!sz || *sz == 0) return {};
1012 std::vector<T> buffer(*sz / sizeof(T) + (*sz % sizeof(T) ? 1 : 0), 0);
1013 CheckError(ReadFile(std::move(path), {buffer.data(), *sz}));
1014 return buffer;
1015}
1016
1017template<class T>
1018inline std::vector<T> Storage::ReadFileAs(StringParam path)
1019{
1020 return SDL::ReadStorageFileAs<T>(get(), std::move(path));
1021}
1022
1037inline void WriteStorageFile(StorageRef storage,
1038 StringParam path,
1039 SourceBytes source)
1040{
1041 CheckError(
1042 SDL_WriteStorageFile(storage, path, source.data(), source.size_bytes()));
1043}
1044
1046{
1047 SDL::WriteStorageFile(get(), std::move(path), std::move(source));
1048}
1049
1062{
1063 CheckError(SDL_CreateStorageDirectory(storage, path));
1064}
1065
1067{
1068 SDL::CreateStorageDirectory(get(), std::move(path));
1069}
1070
1096 StringParam path,
1098 void* userdata)
1099{
1100 CheckError(SDL_EnumerateStorageDirectory(storage, path, callback, userdata));
1101}
1102
1127 StringParam path,
1128 EnumerateDirectoryCB callback)
1129{
1131 storage,
1132 std::move(path),
1133 [](void* userdata, const char* dirname, const char* fname) {
1134 auto& cb = *static_cast<EnumerateDirectoryCB*>(userdata);
1135 return cb(dirname, fname);
1136 },
1137 &callback);
1138}
1139
1163inline std::vector<Path> EnumerateStorageDirectory(StorageRef storage,
1164 StringParam path)
1165{
1166 std::vector<Path> r;
1168 storage, std::move(path), [&](const char*, const char* fname) {
1169 r.emplace_back(fname);
1170 return ENUM_CONTINUE;
1171 });
1172 return r;
1173}
1174
1177 void* userdata)
1178{
1179 SDL::EnumerateStorageDirectory(get(), std::move(path), callback, userdata);
1180}
1181
1182inline std::vector<Path> Storage::EnumerateDirectory(StringParam path)
1183{
1184 return SDL::EnumerateStorageDirectory(get(), std::move(path));
1185}
1186
1188 EnumerateDirectoryCB callback)
1189{
1190 SDL::EnumerateStorageDirectory(get(), std::move(path), callback);
1191}
1192
1204inline void RemoveStoragePath(StorageRef storage, StringParam path)
1205{
1206 CheckError(SDL_RemoveStoragePath(storage, path));
1207}
1208
1210{
1211 SDL::RemoveStoragePath(get(), std::move(path));
1212}
1213
1226inline void RenameStoragePath(StorageRef storage,
1227 StringParam oldpath,
1228 StringParam newpath)
1229{
1230 CheckError(SDL_RenameStoragePath(storage, oldpath, newpath));
1231}
1232
1233inline void Storage::RenamePath(StringParam oldpath, StringParam newpath)
1234{
1235 SDL::RenameStoragePath(get(), std::move(oldpath), std::move(newpath));
1236}
1237
1250inline void CopyStorageFile(StorageRef storage,
1251 StringParam oldpath,
1252 StringParam newpath)
1253{
1254 CheckError(SDL_CopyStorageFile(storage, oldpath, newpath));
1255}
1256
1257inline void Storage::CopyFile(StringParam oldpath, StringParam newpath)
1258{
1259 SDL::CopyStorageFile(get(), std::move(oldpath), std::move(newpath));
1260}
1261
1275{
1276 if (PathInfo info; SDL_GetStoragePathInfo(storage, path, &info)) {
1277 return info;
1278 }
1279 return {};
1280}
1281
1283{
1284 return SDL::GetStoragePathInfo(get(), std::move(path));
1285}
1286
1299{
1300 return SDL_GetStorageSpaceRemaining(storage);
1301}
1302
1307
1342 StringParam path,
1343 StringParam pattern,
1344 GlobFlags flags)
1345{
1346 int count;
1347 auto data =
1348 CheckError(SDL_GlobStorageDirectory(storage, path, pattern, flags, &count));
1349 return OwnArray<char*>{data, size_t(count)};
1350}
1351
1353 StringParam pattern,
1354 GlobFlags flags)
1355{
1357 get(), std::move(path), std::move(pattern), flags);
1358}
1359
1361
1362} // namespace SDL
1363
1364#endif /* SDL3PP_STORAGE_H_ */
Base class for SDL memory allocated array wrap.
Definition SDL3pp_ownPtr.h:44
constexpr RawPointer get() const noexcept
Definition SDL3pp_resource.h:53
constexpr RawPointer release() noexcept
Definition SDL3pp_resource.h:56
constexpr ResourceBase(RawPointer resource)
Constructs from resource pointer.
Definition SDL3pp_resource.h:29
Source byte stream.
Definition SDL3pp_strings.h:237
constexpr size_t size_bytes() const
Retrieves contained size in bytes.
Definition SDL3pp_strings.h:301
constexpr const char * data() const
Retrieves contained data.
Definition SDL3pp_strings.h:304
Helpers to use C++ strings parameters.
Definition SDL3pp_strings.h:43
const char * c_str() const
Converts to a null terminated C string.
Definition SDL3pp_strings.h:118
Target byte stream.
Definition SDL3pp_strings.h:323
constexpr char * data() const
Retrieves contained data.
Definition SDL3pp_strings.h:409
constexpr size_t size_bytes() const
Retrieves contained size in bytes.
Definition SDL3pp_strings.h:406
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:199
constexpr EnumerationResult ENUM_CONTINUE
Value that requests that enumeration continue.
Definition SDL3pp_filesystem.h:426
Uint32 GlobFlags
Flags for path matching.
Definition SDL3pp_filesystem.h:392
EnumerationResult( SDLCALL *)(void *userdata, const char *dirname, const char *fname) EnumerateDirectoryCallback
Callback for directory enumeration.
Definition SDL3pp_filesystem.h:459
std::function< EnumerationResult(const char *dirname, const char *fname)> EnumerateDirectoryCB
Callback for directory enumeration.
Definition SDL3pp_filesystem.h:487
ResourceRef< Properties > PropertiesRef
Reference for Properties.
Definition SDL3pp_properties.h:54
::Uint64 Uint64
An unsigned 64-bit integer type.
Definition SDL3pp_stdinc.h:320
Uint64 GetStorageSpaceRemaining(StorageRef storage)
Queries the remaining space in a storage container.
Definition SDL3pp_storage.h:1298
void CreateDirectory(StringParam path)
Create a directory in a writable storage container.
Definition SDL3pp_storage.h:1066
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:950
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:1008
PathInfo GetStoragePathInfo(StorageRef storage, StringParam path)
Get information about a filesystem path in a storage container.
Definition SDL3pp_storage.h:1274
PathInfo GetPathInfo(StringParam path)
Get information about a filesystem path in a storage container.
Definition SDL3pp_storage.h:1282
Storage OpenTitleStorage(StringParam override, PropertiesRef props)
Opens up a read-only container for the application's filesystem.
Definition SDL3pp_storage.h:745
Storage OpenStorage(const StorageInterface &iface, void *userdata)
Opens up a container using a client-provided storage interface.
Definition SDL3pp_storage.h:855
void WriteFile(StringParam path, SourceBytes source)
Synchronously write a file from client memory into a storage container.
Definition SDL3pp_storage.h:1045
Uint64 GetSpaceRemaining()
Queries the remaining space in a storage container.
Definition SDL3pp_storage.h:1303
OwnArray< char * > GlobDirectory(StringParam path, StringParam pattern, GlobFlags flags)
Enumerate a directory tree, filtered by pattern, and return a list.
Definition SDL3pp_storage.h:1352
Storage OpenUserStorage(StringParam org, StringParam app, PropertiesRef props)
Opens up a container for a user's unique read/write filesystem.
Definition SDL3pp_storage.h:794
std::vector< T > ReadFileAs(StringParam path)
Synchronously read a file from a storage container into a client-provided buffer.
Definition SDL3pp_storage.h:1018
bool ReadFile(StringParam path, TargetBytes destination)
Synchronously read a file from a storage container into a client-provided buffer.
Definition SDL3pp_storage.h:982
std::optional< Uint64 > GetFileSize(StringParam path)
Query the size of a file within a storage container.
Definition SDL3pp_storage.h:925
bool Close()
Closes and frees a storage container.
Definition SDL3pp_storage.h:881
void RemovePath(StringParam path)
Remove a file or an empty directory in a writable storage container.
Definition SDL3pp_storage.h:1209
void RemoveStoragePath(StorageRef storage, StringParam path)
Remove a file or an empty directory in a writable storage container.
Definition SDL3pp_storage.h:1204
SDL_Storage * StorageRaw
Alias to raw representation for Storage.
Definition SDL3pp_storage.h:230
void CopyStorageFile(StorageRef storage, StringParam oldpath, StringParam newpath)
Copy a file in a writable storage container.
Definition SDL3pp_storage.h:1250
std::optional< Uint64 > GetStorageFileSize(StorageRef storage, StringParam path)
Query the size of a file within a storage container.
Definition SDL3pp_storage.h:916
bool Ready()
Checks if the storage container is ready to use.
Definition SDL3pp_storage.h:901
SDL_StorageInterface StorageInterface
Function interface for Storage.
Definition SDL3pp_storage.h:255
void RenamePath(StringParam oldpath, StringParam newpath)
Rename a file or directory in a writable storage container.
Definition SDL3pp_storage.h:1233
bool StorageReady(StorageRef storage)
Checks if the storage container is ready to use.
Definition SDL3pp_storage.h:896
Storage OpenFileStorage(StringParam path)
Opens up a container for local filesystem storage.
Definition SDL3pp_storage.h:823
void CreateStorageDirectory(StorageRef storage, StringParam path)
Create a directory in a writable storage container.
Definition SDL3pp_storage.h:1061
void CopyFile(StringParam oldpath, StringParam newpath)
Copy a file in a writable storage container.
Definition SDL3pp_storage.h:1257
bool CloseStorage(StorageRaw storage)
Closes and frees a storage container.
Definition SDL3pp_storage.h:876
void WriteStorageFile(StorageRef storage, StringParam path, SourceBytes source)
Synchronously write a file from client memory into a storage container.
Definition SDL3pp_storage.h:1037
void EnumerateDirectory(StringParam path, EnumerateDirectoryCallback callback, void *userdata)
Enumerate a directory in a storage container through a callback function.
Definition SDL3pp_storage.h:1175
void RenameStoragePath(StorageRef storage, StringParam oldpath, StringParam newpath)
Rename a file or directory in a writable storage container.
Definition SDL3pp_storage.h:1226
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:1341
ResourceRef< Storage > StorageRef
Reference for Storage.
Definition SDL3pp_storage.h:237
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:1095
Main include header for the SDL3pp library.
Information about a path on the filesystem.
Definition SDL3pp_filesystem.h:352
A non-owning reference wrapper for a given resource.
Definition SDL3pp_resource.h:156
An abstract interface for filesystem access.
Definition SDL3pp_storage.h:269
constexpr Storage(const Storage &other)=delete
Copy constructor.
constexpr ResourceBase(RawPointer resource)
Constructs from resource pointer.
Definition SDL3pp_resource.h:29
Storage & operator=(const Storage &other)=delete
Assignment operator.
~Storage()
Destructor.
Definition SDL3pp_storage.h:398
constexpr Storage(Storage &&other) noexcept
Move constructor.
Definition SDL3pp_storage.h:288
constexpr Storage(StorageRaw resource) noexcept
Constructs from raw Storage.
Definition SDL3pp_storage.h:279
constexpr Storage & operator=(Storage &&other) noexcept
Assignment operator.
Definition SDL3pp_storage.h:401