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 StorageBase;
228
229// Forward decl
230struct Storage;
231
233using StorageRaw = SDL_Storage*;
234
241
258using StorageInterface = SDL_StorageInterface;
259
265struct StorageBase : ResourceBaseT<StorageRaw>
266{
268
284 bool Close();
285
298 bool Ready();
299
312 std::optional<Uint64> GetFileSize(StringParam path);
313
333 bool ReadFile(StringParam path, TargetBytes destination);
334
353 std::string ReadFile(StringParam path);
354
373 template<class T>
374 std::vector<T> ReadFileAs(StringParam path);
375
389 void WriteFile(StringParam path, SourceBytes source);
390
401 void CreateDirectory(StringParam path);
402
430 void* userdata);
431
456 std::vector<Path> EnumerateDirectory(StringParam path);
457
483
494 void RemovePath(StringParam path);
495
507 void RenamePath(StringParam oldpath, StringParam newpath);
508
520 void CopyFile(StringParam oldpath, StringParam newpath);
521
534
546
581 StringParam pattern,
582 GlobFlags flags);
583};
584
597{
598 using StorageBase::StorageBase;
599
607 constexpr explicit Storage(StorageRaw resource) noexcept
608 : StorageBase(resource)
609 {
610 }
611
613 constexpr Storage(Storage&& other) noexcept
614 : Storage(other.release())
615 {
616 }
617
637 Storage(StringParam override, PropertiesRef props);
638
664
687 Storage(StringParam path);
688
716 Storage(const StorageInterface& iface, void* userdata);
717
719 ~Storage() { CheckError(SDL_CloseStorage(get())); }
720
722 constexpr Storage& operator=(Storage&& other) noexcept
723 {
724 swap(*this, other);
725 return *this;
726 }
727};
728
749{
750 return Storage(override, props);
751}
752
754 : Storage(CheckError(SDL_OpenTitleStorage(override, props)))
755{
756}
757
759 : Storage(CheckError(SDL_OpenUserStorage(org, app, props)))
760{
761}
762
764 : Storage(CheckError(SDL_OpenFileStorage(path)))
765{
766}
767
768inline Storage::Storage(const StorageInterface& iface, void* userdata)
769 : Storage(CheckError(SDL_OpenStorage(&iface, userdata)))
770{
771}
772
798 StringParam app,
799 PropertiesRef props)
800{
801 return Storage(org, app, props);
802}
803
826inline Storage OpenFileStorage(StringParam path) { return Storage(path); }
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 StorageBase::Close() { return CloseStorage(release()); }
882
896inline bool StorageReady(StorageRef storage)
897{
898 return SDL_StorageReady(storage);
899}
900
901inline bool StorageBase::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> StorageBase::GetFileSize(StringParam path)
926{
927 return SDL::GetStorageFileSize(get(), 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, path, buffer));
979 return buffer;
980}
981
982inline bool StorageBase::ReadFile(StringParam path, TargetBytes destination)
983{
984 return SDL::ReadStorageFile(get(), path, destination);
985}
986
987inline std::string StorageBase::ReadFile(StringParam path)
988{
989 return SDL::ReadStorageFile(get(), 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(path, {buffer.data(), *sz}));
1014 return buffer;
1015}
1016
1017template<class T>
1018inline std::vector<T> StorageBase::ReadFileAs(StringParam path)
1019{
1020 return SDL::ReadStorageFileAs<T>(get(), 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(), path, source);
1048}
1049
1062{
1063 CheckError(SDL_CreateStorageDirectory(storage, path));
1064}
1065
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 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;
1167 EnumerateStorageDirectory(storage, path, [&](const char*, const char* fname) {
1168 r.emplace_back(fname);
1169 return ENUM_CONTINUE;
1170 });
1171 return r;
1172}
1173
1176 void* userdata)
1177{
1178 SDL::EnumerateStorageDirectory(get(), path, callback, userdata);
1179}
1180
1181inline std::vector<Path> StorageBase::EnumerateDirectory(StringParam path)
1182{
1183 return SDL::EnumerateStorageDirectory(get(), path);
1184}
1185
1187 EnumerateDirectoryCB callback)
1188{
1189 SDL::EnumerateStorageDirectory(get(), path, callback);
1190}
1191
1203inline void RemoveStoragePath(StorageRef storage, StringParam path)
1204{
1205 CheckError(SDL_RemoveStoragePath(storage, path));
1206}
1207
1209{
1210 SDL::RemoveStoragePath(get(), path);
1211}
1212
1225inline void RenameStoragePath(StorageRef storage,
1226 StringParam oldpath,
1227 StringParam newpath)
1228{
1229 CheckError(SDL_RenameStoragePath(storage, oldpath, newpath));
1230}
1231
1233{
1234 SDL::RenameStoragePath(get(), oldpath, newpath);
1235}
1236
1249inline void CopyStorageFile(StorageRef storage,
1250 StringParam oldpath,
1251 StringParam newpath)
1252{
1253 CheckError(SDL_CopyStorageFile(storage, oldpath, newpath));
1254}
1255
1256inline void StorageBase::CopyFile(StringParam oldpath, StringParam newpath)
1257{
1258 SDL::CopyStorageFile(get(), oldpath, newpath);
1259}
1260
1274{
1275 if (PathInfo info; SDL_GetStoragePathInfo(storage, path, &info)) {
1276 return info;
1277 }
1278 return {};
1279}
1280
1282{
1283 return SDL::GetStoragePathInfo(get(), path);
1284}
1285
1298{
1299 return SDL_GetStorageSpaceRemaining(storage);
1300}
1301
1306
1341 StringParam path,
1342 StringParam pattern,
1343 GlobFlags flags)
1344{
1345 int count;
1346 auto data =
1347 CheckError(SDL_GlobStorageDirectory(storage, path, pattern, flags, &count));
1348 return OwnArray<char*>{data, size_t(count)};
1349}
1350
1352 StringParam pattern,
1353 GlobFlags flags)
1354{
1355 return SDL::GlobStorageDirectory(get(), path, pattern, flags);
1356}
1357
1359
1360} // namespace SDL
1361
1362#endif /* SDL3PP_STORAGE_H_ */
Base class for SDL memory allocated array wrap.
Definition SDL3pp_ownPtr.h:44
constexpr RawPointer release() noexcept
Definition SDL3pp_resource.h:57
friend constexpr void swap(ResourceBaseT &lhs, ResourceBaseT &rhs) noexcept
Definition SDL3pp_resource.h:65
constexpr RawPointer get() const noexcept
Definition SDL3pp_resource.h:54
constexpr ResourceBaseT()=default
Default constructor, creates null/invalid resource.
Source byte stream.
Definition SDL3pp_strings.h:246
constexpr size_t size_bytes() const
Retrieves contained size in bytes.
Definition SDL3pp_strings.h:310
constexpr const char * data() const
Retrieves contained data.
Definition SDL3pp_strings.h:313
Helpers to use C++ strings parameters.
Definition SDL3pp_strings.h:58
constexpr const char * c_str() const
Converts to a null terminated C string.
Definition SDL3pp_strings.h:109
Target byte stream.
Definition SDL3pp_strings.h:332
constexpr char * data() const
Retrieves contained data.
Definition SDL3pp_strings.h:418
constexpr size_t size_bytes() const
Retrieves contained size in bytes.
Definition SDL3pp_strings.h:415
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
ResourceRefT< PropertiesBase > PropertiesRef
Reference for Properties.
Definition SDL3pp_properties.h:55
::Uint64 Uint64
An unsigned 64-bit integer type.
Definition SDL3pp_stdinc.h:326
Uint64 GetStorageSpaceRemaining(StorageRef storage)
Queries the remaining space in a storage container.
Definition SDL3pp_storage.h:1297
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 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
bool Ready()
Checks if the storage container is ready to use.
Definition SDL3pp_storage.h:901
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 GetPathInfo(StringParam path)
Get information about a filesystem path in a storage container.
Definition SDL3pp_storage.h:1281
PathInfo GetStoragePathInfo(StorageRef storage, StringParam path)
Get information about a filesystem path in a storage container.
Definition SDL3pp_storage.h:1273
void EnumerateDirectory(StringParam path, EnumerateDirectoryCallback callback, void *userdata)
Enumerate a directory in a storage container through a callback function.
Definition SDL3pp_storage.h:1174
ResourceRefT< StorageBase > StorageRef
Reference for Storage.
Definition SDL3pp_storage.h:240
Storage OpenTitleStorage(StringParam override, PropertiesRef props)
Opens up a read-only container for the application's filesystem.
Definition SDL3pp_storage.h:748
Storage OpenStorage(const StorageInterface &iface, void *userdata)
Opens up a container using a client-provided storage interface.
Definition SDL3pp_storage.h:855
Storage OpenUserStorage(StringParam org, StringParam app, PropertiesRef props)
Opens up a container for a user's unique read/write filesystem.
Definition SDL3pp_storage.h:797
void WriteFile(StringParam path, SourceBytes source)
Synchronously write a file from client memory into a storage container.
Definition SDL3pp_storage.h:1045
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
void RemoveStoragePath(StorageRef storage, StringParam path)
Remove a file or an empty directory in a writable storage container.
Definition SDL3pp_storage.h:1203
SDL_Storage * StorageRaw
Alias to raw representation for Storage.
Definition SDL3pp_storage.h:233
void CopyStorageFile(StorageRef storage, StringParam oldpath, StringParam newpath)
Copy a file in a writable storage container.
Definition SDL3pp_storage.h:1249
std::optional< Uint64 > GetStorageFileSize(StorageRef storage, StringParam path)
Query the size of a file within a storage container.
Definition SDL3pp_storage.h:916
void CopyFile(StringParam oldpath, StringParam newpath)
Copy a file in a writable storage container.
Definition SDL3pp_storage.h:1256
SDL_StorageInterface StorageInterface
Function interface for Storage.
Definition SDL3pp_storage.h:258
bool StorageReady(StorageRef storage)
Checks if the storage container is ready to use.
Definition SDL3pp_storage.h:896
void RemovePath(StringParam path)
Remove a file or an empty directory in a writable storage container.
Definition SDL3pp_storage.h:1208
Storage OpenFileStorage(StringParam path)
Opens up a container for local filesystem storage.
Definition SDL3pp_storage.h:826
void CreateStorageDirectory(StorageRef storage, StringParam path)
Create a directory in a writable storage container.
Definition SDL3pp_storage.h:1061
void RenamePath(StringParam oldpath, StringParam newpath)
Rename a file or directory in a writable storage container.
Definition SDL3pp_storage.h:1232
bool Close()
Closes and frees a storage container.
Definition SDL3pp_storage.h:881
void CreateDirectory(StringParam path)
Create a directory in a writable storage container.
Definition SDL3pp_storage.h:1066
bool CloseStorage(StorageRaw storage)
Closes and frees a storage container.
Definition SDL3pp_storage.h:876
OwnArray< char * > GlobDirectory(StringParam path, StringParam pattern, GlobFlags flags)
Enumerate a directory tree, filtered by pattern, and return a list.
Definition SDL3pp_storage.h:1351
Uint64 GetSpaceRemaining()
Queries the remaining space in a storage container.
Definition SDL3pp_storage.h:1302
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 RenameStoragePath(StorageRef storage, StringParam oldpath, StringParam newpath)
Rename a file or directory in a writable storage container.
Definition SDL3pp_storage.h:1225
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:1340
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:93
Base class to Storage.
Definition SDL3pp_storage.h:266
constexpr ResourceBaseT()=default
Default constructor, creates null/invalid resource.
An abstract interface for filesystem access.
Definition SDL3pp_storage.h:597
~Storage()
Destructor.
Definition SDL3pp_storage.h:719
constexpr Storage(Storage &&other) noexcept
Move constructor.
Definition SDL3pp_storage.h:613
constexpr Storage(StorageRaw resource) noexcept
Constructs from raw Storage.
Definition SDL3pp_storage.h:607
constexpr Storage & operator=(Storage &&other) noexcept
Assignment operator.
Definition SDL3pp_storage.h:722