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
236// Forward decl
237struct StorageBase;
238
239// Forward decl
240struct StorageRef;
241
242// Forward decl
243struct Storage;
244
261using StorageInterface = SDL_StorageInterface;
262
277struct StorageBase : Resource<SDL_Storage*>
278{
279 using Resource::Resource;
280
297 : Resource(CheckError(SDL_OpenTitleStorage(override, props.get())))
298 {
299 }
300
325 : Resource(CheckError(SDL_OpenUserStorage(org, app, props.get())))
326 {
327 }
328
351 : Resource(CheckError(SDL_OpenFileStorage(path)))
352 {
353 }
354
381 StorageBase(const StorageInterface& iface, void* userdata)
382 : Resource(CheckError(SDL_OpenStorage(&iface, userdata)))
383 {
384 }
385
398 bool Ready() const { return SDL_StorageReady(get()); }
399
412 std::optional<Uint64> GetFileSize(StringParam path) const
413 {
414 if (Uint64 length; SDL_GetStorageFileSize(get(), path, &length)) {
415 return length;
416 }
417 return 0;
418 }
419
438 std::string ReadFile(StringParam path) const
439 {
440 auto sz = GetFileSize(path.c_str());
441 if (!sz || *sz == 0) return {};
442 std::string buffer(*sz, 0);
443 if (ReadFile(std::move(path), buffer)) return buffer;
444 return {};
445 }
446
466 bool ReadFile(StringParam path, TargetBytes destination) const
467 {
468 return SDL_ReadStorageFile(
469 get(), path, destination.data, destination.size_bytes);
470 }
471
490 template<class T>
491 std::vector<T> ReadFileAs(StringParam path) const
492 {
493 auto sz = GetFileSize(path.c_str());
494 if (!sz || *sz == 0) return {};
495 std::vector<T> buffer(*sz / sizeof(T) + (*sz % sizeof(T) ? 1 : 0), 0);
496 if (ReadFile(std::move(path), {buffer.data(), *sz})) return buffer;
497 return {};
498 }
499
514 {
515 SDL_WriteStorageFile(get(), path, source.data, source.size_bytes);
516 }
517
529 {
530 CheckError(SDL_CreateStorageDirectory(get(), path));
531 }
532
541 std::vector<Path> EnumerateDirectory(StringParam path)
542 {
543 std::vector<Path> r;
544 EnumerateDirectory(std::move(path), [&](const char*, const char* fname) {
545 r.emplace_back(fname);
546 return ENUM_CONTINUE;
547 });
548 return r;
549 }
550
577 {
579 std::move(path),
580 [](void* userdata, const char* dirname, const char* fname) {
581 auto& cb = *static_cast<EnumerateDirectoryCB*>(userdata);
582 return cb(dirname, fname);
583 },
584 &callback);
585 }
586
615 void* userdata)
616 {
617 CheckError(SDL_EnumerateStorageDirectory(get(), path, callback, userdata));
618 }
619
631 {
632 CheckError(SDL_RemoveStoragePath(get(), path));
633 }
634
646 void RenamePath(StringParam oldpath, StringParam newpath)
647 {
648 CheckError(SDL_RenameStoragePath(get(), oldpath, newpath));
649 }
650
662 void CopyFile(StringParam oldpath, StringParam newpath)
663 {
664 CheckError(SDL_CopyStorageFile(get(), oldpath, newpath));
665 }
666
679 {
680 if (PathInfo info; SDL_GetStoragePathInfo(get(), path, &info)) {
681 return info;
682 }
683 return {};
684 }
685
696 Uint64 GetSpaceRemaining() const
697 {
698 return SDL_GetStorageSpaceRemaining(get());
699 }
700
735 StringParam pattern,
736 GlobFlags flags)
737 {
738 int count;
739 auto data =
740 CheckError(SDL_GlobStorageDirectory(get(), path, pattern, flags, &count));
741 return OwnArray<char*>{data, size_t(count)};
742 }
743};
744
754{
756
760 constexpr StorageRef(const StorageRef& other)
761 : StorageBase(other.get())
762 {
763 }
764
768 constexpr StorageRef(StorageRef&& other)
769 : StorageBase(other.release())
770 {
771 }
772
776 constexpr ~StorageRef() = default;
777
782 {
783 release(other.release());
784 return *this;
785 }
786
802 bool reset(SDL_Storage* newResource = {})
803 {
804 return SDL_CloseStorage(release(newResource));
805 }
806
819 bool Close() { return reset(); }
820};
821
831{
833
837 constexpr explicit Storage(SDL_Storage* resource = {})
838 : StorageRef(resource)
839 {
840 }
841
842 constexpr Storage(const Storage& other) = delete;
843
847 constexpr Storage(Storage&& other) = default;
848
853
858 {
859 reset(other.release());
860 return *this;
861 }
862};
863
865} // namespace SDL
866
867#endif /* SDL3PP_STORAGE_H_ */
Base class for SDL memory allocated array wrap.
Definition SDL3pp_ownPtr.h:43
A SDL managed resource.
Definition SDL3pp_resource.h:17
constexpr SDL_Storage * release(SDL_Storage * newResource={})
Return contained resource and empties or replace value.
Definition SDL3pp_resource.h:60
constexpr Resource(T resource={})
Constructs the underlying resource.
Definition SDL3pp_resource.h:22
constexpr SDL_Storage * get() const
Return contained resource;.
Definition SDL3pp_resource.h:57
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:475
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 StorageBase.
Definition SDL3pp_storage.h:261
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
Wrap properties id.
Definition SDL3pp_properties.h:203
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:278
std::optional< Uint64 > GetFileSize(StringParam path) const
Query the size of a file within a storage container.
Definition SDL3pp_storage.h:412
PathInfo GetPathInfo(StringParam path) const
Get information about a filesystem path in a storage container.
Definition SDL3pp_storage.h:678
StorageBase(const StorageInterface &iface, void *userdata)
Opens up a container using a client-provided storage interface.
Definition SDL3pp_storage.h:381
void EnumerateDirectory(StringParam path, EnumerateDirectoryCallback callback, void *userdata)
Enumerate a directory in a storage container through a callback function.
Definition SDL3pp_storage.h:613
StorageBase(StringParam path)
Opens up a container for local filesystem storage.
Definition SDL3pp_storage.h:350
Uint64 GetSpaceRemaining() const
Queries the remaining space in a storage container.
Definition SDL3pp_storage.h:696
void WriteFile(StringParam path, SourceBytes source)
Synchronously write a file from client memory into a storage container.
Definition SDL3pp_storage.h:513
bool Ready() const
Checks if the storage container is ready to use.
Definition SDL3pp_storage.h:398
std::vector< Path > EnumerateDirectory(StringParam path)
Enumerate a directory.
Definition SDL3pp_storage.h:541
StorageBase(StringParam org, StringParam app, PropertiesBase &props)
Opens up a container for a user's unique read/write filesystem.
Definition SDL3pp_storage.h:324
void CopyFile(StringParam oldpath, StringParam newpath)
Copy a file in a writable storage container.
Definition SDL3pp_storage.h:662
bool ReadFile(StringParam path, TargetBytes destination) const
Synchronously read a file from a storage container into a client-provided buffer.
Definition SDL3pp_storage.h:466
std::string ReadFile(StringParam path) const
Synchronously read a file from a storage container into a client-provided buffer.
Definition SDL3pp_storage.h:438
void RemovePath(StringParam path)
Remove a file or an empty directory in a writable storage container.
Definition SDL3pp_storage.h:630
void RenamePath(StringParam oldpath, StringParam newpath)
Rename a file or directory in a writable storage container.
Definition SDL3pp_storage.h:646
void CreateDirectory(StringParam path)
Create a directory in a writable storage container.
Definition SDL3pp_storage.h:528
StorageBase(StringParam override, PropertiesBase &props)
Opens up a read-only container for the application's filesystem.
Definition SDL3pp_storage.h:296
OwnArray< char * > GlobDirectory(StringParam path, StringParam pattern, GlobFlags flags)
Enumerate a directory tree, filtered by pattern, and return a list.
Definition SDL3pp_storage.h:734
std::vector< T > ReadFileAs(StringParam path) const
Synchronously read a file from a storage container into a client-provided buffer.
Definition SDL3pp_storage.h:491
void EnumerateDirectory(StringParam path, EnumerateDirectoryCB callback)
Enumerate a directory in a storage container through a callback function.
Definition SDL3pp_storage.h:576
Handle to a non owned storage.
Definition SDL3pp_storage.h:754
constexpr StorageRef(StorageRef &&other)
Move constructor.
Definition SDL3pp_storage.h:768
StorageRef & operator=(StorageRef other)
Assignment operator.
Definition SDL3pp_storage.h:781
constexpr StorageRef(const StorageRef &other)
Copy constructor.
Definition SDL3pp_storage.h:760
bool Close()
Closes and frees a storage container.
Definition SDL3pp_storage.h:819
constexpr ~StorageRef()=default
Default constructor.
bool reset(SDL_Storage *newResource={})
Closes and frees a storage container.
Definition SDL3pp_storage.h:802
Handle to an owned storage.
Definition SDL3pp_storage.h:831
constexpr Storage(SDL_Storage *resource={})
Constructs from the underlying resource.
Definition SDL3pp_storage.h:837
constexpr Storage(Storage &&other)=default
Move constructor.
Storage & operator=(Storage other)
Assignment operator.
Definition SDL3pp_storage.h:857
~Storage()
Frees up resource when object goes out of scope.
Definition SDL3pp_storage.h:852
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