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
230// Forward decl
231struct StorageRef;
232
233// Forward decl
234struct Storage;
235
245
255
272using StorageInterface = SDL_StorageInterface;
273
287struct StorageRef : Resource<SDL_Storage*>
288{
289 using Resource::Resource;
290
303 bool Ready() const { return SDL_StorageReady(get()); }
304
317 std::optional<Uint64> GetFileSize(StringParam path) const
318 {
319 if (Uint64 length; SDL_GetStorageFileSize(get(), path, &length)) {
320 return length;
321 }
322 return 0;
323 }
324
343 std::string ReadFile(StringParam path) const
344 {
345 auto sz = GetFileSize(path.c_str());
346 if (!sz || *sz == 0) return {};
347 std::string buffer(*sz, 0);
348 if (ReadFile(std::move(path), buffer)) return buffer;
349 return {};
350 }
351
371 bool ReadFile(StringParam path, TargetBytes destination) const
372 {
373 return SDL_ReadStorageFile(
374 get(), path, destination.data, destination.size_bytes);
375 }
376
395 template<class T>
396 std::vector<T> ReadFileAs(StringParam path) const
397 {
398 auto sz = GetFileSize(path.c_str());
399 if (!sz || *sz == 0) return {};
400 std::vector<T> buffer(*sz / sizeof(T) + (*sz % sizeof(T) ? 1 : 0), 0);
401 if (ReadFile(std::move(path), {buffer.data(), *sz})) return buffer;
402 return {};
403 }
404
419 {
420 SDL_WriteStorageFile(get(), path, source.data, source.size_bytes);
421 }
422
434 {
435 CheckError(SDL_CreateStorageDirectory(get(), path));
436 }
437
446 std::vector<Path> EnumerateDirectory(StringParam path)
447 {
448 std::vector<Path> r;
449 EnumerateDirectory(std::move(path), [&](const char*, const char* fname) {
450 r.emplace_back(fname);
451 return ENUM_CONTINUE;
452 });
453 return r;
454 }
455
482 {
484 std::move(path),
485 [](void* userdata, const char* dirname, const char* fname) {
486 auto& cb = *static_cast<EnumerateDirectoryCB*>(userdata);
487 return cb(dirname, fname);
488 },
489 &callback);
490 }
491
520 void* userdata)
521 {
522 CheckError(SDL_EnumerateStorageDirectory(get(), path, callback, userdata));
523 }
524
536 {
537 CheckError(SDL_RemoveStoragePath(get(), path));
538 }
539
551 void RenamePath(StringParam oldpath, StringParam newpath)
552 {
553 CheckError(SDL_RenameStoragePath(get(), oldpath, newpath));
554 }
555
567 void CopyFile(StringParam oldpath, StringParam newpath)
568 {
569 CheckError(SDL_CopyStorageFile(get(), oldpath, newpath));
570 }
571
584 {
585 if (PathInfo info; SDL_GetStoragePathInfo(get(), path, &info)) {
586 return info;
587 }
588 return {};
589 }
590
601 Uint64 GetSpaceRemaining() const
602 {
603 return SDL_GetStorageSpaceRemaining(get());
604 }
605
640 StringParam pattern,
641 GlobFlags flags)
642 {
643 int count;
644 auto data =
645 CheckError(SDL_GlobStorageDirectory(get(), path, pattern, flags, &count));
646 return OwnArray<char*>{data, size_t(count)};
647 }
648
662 static void reset(SDL_Storage* resource)
663 {
664 CheckError(SDL_CloseStorage(resource));
665 }
666};
667
675struct Storage : ResourceUnique<StorageRef>
676{
678
695 {
696 return Storage(CheckError(SDL_OpenTitleStorage(override, props)));
697 }
698
724 {
725 return Storage(CheckError(SDL_OpenUserStorage(org, app, props)));
726 }
727
751 {
752 return Storage(CheckError(SDL_OpenFileStorage(path)));
753 }
754
782 static Storage Open(const StorageInterface& iface, void* userdata)
783 {
784 return Storage(CheckError(SDL_OpenStorage(&iface, userdata)));
785 }
786
799 void Close() { reset(); }
804
805};
806
807
809{
810 return StorageShared(std::move(*this));
811}
812
822struct StorageUnsafe : ResourceUnsafe<StorageRef>
823{
825
829 constexpr explicit StorageUnsafe(Storage&& other)
830 : StorageUnsafe(other.release())
831 {
832 }
833};
834
836
837} // namespace SDL
838
839#endif /* SDL3PP_STORAGE_H_ */
Base class for SDL memory allocated array wrap.
Definition SDL3pp_ownPtr.h:43
RESOURCE release()
Returns reference and reset this.
Definition SDL3pp_resource.h:178
Implement shared ownership for a resource.
Definition SDL3pp_resource.h:283
Implement unique ownership for a resource.
Definition SDL3pp_resource.h:226
constexpr ResourceUnique(std::nullptr_t=nullptr)
Default constructor.
Definition SDL3pp_resource.h:231
void reset()
Resets the value, destroying the resource if not nullptr.
Definition SDL3pp_resource.h:265
A dumb pointer to resource.
Definition SDL3pp_resource.h:197
constexpr ResourceUnsafe()=default
Default constructor.
Implement weak ownership for a resource.
Definition SDL3pp_resource.h:328
A SDL managed resource.
Definition SDL3pp_resource.h:29
constexpr Resource(T resource={})
Constructs from the underlying resource.
Definition SDL3pp_resource.h:37
constexpr SDL_Storage * get() const
Return contained resource;.
Definition SDL3pp_resource.h:76
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
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:206
std::function< EnumerationResult(const char *dirname, const char *fname)> EnumerateDirectoryCB
Callback for directory enumeration.
Definition SDL3pp_filesystem.h:476
constexpr EnumerationResult ENUM_CONTINUE
Value that requests that enumeration continue.
Definition SDL3pp_filesystem.h:412
SDL_EnumerateDirectoryCallback EnumerateDirectoryCallback
Callback for directory enumeration.
Definition SDL3pp_filesystem.h:449
Uint32 GlobFlags
Flags for path matching.
Definition SDL3pp_filesystem.h:380
SDL_StorageInterface StorageInterface
Function interface for Storage.
Definition SDL3pp_storage.h:272
ResourceShared< Storage > StorageShared
Handle to a shared storage.
Definition SDL3pp_storage.h:244
StorageShared share()
Move this storage into a StorageShared.
Definition SDL3pp_storage.h:808
the main namespace where all SDL3pp public functions and types live
Definition SDL3pp_assert.h:7
Information about a path on the filesystem.
Definition SDL3pp_filesystem.h:346
SDL properties ID.
Definition SDL3pp_properties.h:209
Source byte stream.
Definition SDL3pp_strings.h:239
size_t size_bytes
The size in bytes.
Definition SDL3pp_strings.h:241
const void * data
The data copied from.
Definition SDL3pp_strings.h:240
An abstract interface for filesystem access.
Definition SDL3pp_storage.h:288
std::vector< Path > EnumerateDirectory(StringParam path)
Enumerate a directory.
Definition SDL3pp_storage.h:446
void EnumerateDirectory(StringParam path, EnumerateDirectoryCB callback)
Enumerate a directory in a storage container through a callback function.
Definition SDL3pp_storage.h:481
static void reset(SDL_Storage *resource)
Closes and frees a storage container.
Definition SDL3pp_storage.h:662
bool Ready() const
Checks if the storage container is ready to use.
Definition SDL3pp_storage.h:303
void RemovePath(StringParam path)
Remove a file or an empty directory in a writable storage container.
Definition SDL3pp_storage.h:535
PathInfo GetPathInfo(StringParam path) const
Get information about a filesystem path in a storage container.
Definition SDL3pp_storage.h:583
OwnArray< char * > GlobDirectory(StringParam path, StringParam pattern, GlobFlags flags)
Enumerate a directory tree, filtered by pattern, and return a list.
Definition SDL3pp_storage.h:639
std::string ReadFile(StringParam path) const
Synchronously read a file from a storage container into a client-provided buffer.
Definition SDL3pp_storage.h:343
bool ReadFile(StringParam path, TargetBytes destination) const
Synchronously read a file from a storage container into a client-provided buffer.
Definition SDL3pp_storage.h:371
std::optional< Uint64 > GetFileSize(StringParam path) const
Query the size of a file within a storage container.
Definition SDL3pp_storage.h:317
Uint64 GetSpaceRemaining() const
Queries the remaining space in a storage container.
Definition SDL3pp_storage.h:601
void CopyFile(StringParam oldpath, StringParam newpath)
Copy a file in a writable storage container.
Definition SDL3pp_storage.h:567
void EnumerateDirectory(StringParam path, EnumerateDirectoryCallback callback, void *userdata)
Enumerate a directory in a storage container through a callback function.
Definition SDL3pp_storage.h:518
void WriteFile(StringParam path, SourceBytes source)
Synchronously write a file from client memory into a storage container.
Definition SDL3pp_storage.h:418
void CreateDirectory(StringParam path)
Create a directory in a writable storage container.
Definition SDL3pp_storage.h:433
void RenamePath(StringParam oldpath, StringParam newpath)
Rename a file or directory in a writable storage container.
Definition SDL3pp_storage.h:551
std::vector< T > ReadFileAs(StringParam path) const
Synchronously read a file from a storage container into a client-provided buffer.
Definition SDL3pp_storage.h:396
Unsafe Handle to storage.
Definition SDL3pp_storage.h:823
constexpr StorageUnsafe(Storage &&other)
Constructs StorageUnsafe from Storage.
Definition SDL3pp_storage.h:829
Handle to an owned storage.
Definition SDL3pp_storage.h:676
static Storage Open(const StorageInterface &iface, void *userdata)
Opens up a container using a client-provided storage interface.
Definition SDL3pp_storage.h:782
static Storage OpenUser(StringParam org, StringParam app, PropertiesRef props)
Opens up a container for a user's unique read/write filesystem.
Definition SDL3pp_storage.h:723
void Close()
Closes and frees a storage container.
Definition SDL3pp_storage.h:799
static Storage OpenFile(StringParam path)
Opens up a container for local filesystem storage.
Definition SDL3pp_storage.h:750
static Storage OpenTitle(StringParam override, PropertiesRef props)
Opens up a read-only container for the application's filesystem.
Definition SDL3pp_storage.h:694
Target byte stream.
Definition SDL3pp_strings.h:305
size_t size_bytes
The size in bytes.
Definition SDL3pp_strings.h:307
void * data
The address to have data copied to it.
Definition SDL3pp_strings.h:306