1#ifndef SDL3PP_IOSTREAM_H_
2#define SDL3PP_IOSTREAM_H_
4#include <SDL3/SDL_iostream.h>
5#include "SDL3pp_properties.h"
6#include "SDL3pp_stdinc.h"
48 SDL_IO_STATUS_NOT_READY;
51 SDL_IO_STATUS_READONLY;
54 SDL_IO_STATUS_WRITEONLY;
193 :
Resource(SDL_IOFromFile(file, mode))
276 auto size = SDL_GetIOSize(
get());
277 if (size < 0)
throw Error{};
309 return SDL_SeekIO(
get(), offset, whence);
328 Sint64
Tell()
const {
return SDL_TellIO(
get()); }
352 std::string
Read(
size_t size = -1)
355 auto curSize = SDL_GetIOSize(
get());
356 if ((curSize < 0 || pos < 0)) {
357 if (size ==
size_t(-1))
return {};
358 }
else if (curSize - pos <= 0) {
360 }
else if (curSize - pos < size) {
361 size = curSize - pos;
363 std::string result(size, 0);
364 auto actualSize =
Read(result);
365 if (actualSize < size) result.resize(actualSize);
432 size_t print(std::string_view fmt,
auto... args)
434 return Write(std::vformat(fmt, std::make_format_args(args...)));
440 size_t println(std::string_view fmt,
auto... args)
443 std::vformat(fmt, std::make_format_args(args...)) +
"\n";
444 return Write(result);
467 size_t printf(SDL_PRINTF_FORMAT_STRING
const char* fmt, ...)
498 size_t vprintf(SDL_PRINTF_FORMAT_STRING
const char* fmt, va_list ap)
500 return SDL_IOvprintf(
get(), fmt, ap);
541 auto data =
static_cast<char*
>(SDL_LoadFile_IO(
get(), &datasize,
false));
566 auto data =
static_cast<T*
>(SDL_LoadFile_IO(
get(), &datasize,
false));
1183 if (Uint8 value; SDL_ReadU8(
get(), &value))
return value;
1203 if (Sint8 value; SDL_ReadS8(
get(), &value))
return value;
1227 if (Uint16 value; SDL_ReadU16LE(
get(), &value))
return value;
1251 if (Sint16 value; SDL_ReadS16LE(
get(), &value))
return value;
1275 if (Uint16 value; SDL_ReadU16BE(
get(), &value))
return value;
1299 if (Sint16 value; SDL_ReadS16BE(
get(), &value))
return value;
1323 if (Uint32 value; SDL_ReadU32LE(
get(), &value))
return value;
1347 if (Sint32 value; SDL_ReadS32LE(
get(), &value))
return value;
1371 if (Uint32 value; SDL_ReadU32BE(
get(), &value))
return value;
1395 if (Sint32 value; SDL_ReadS32BE(
get(), &value))
return value;
1419 if (Uint64 value; SDL_ReadU64LE(
get(), &value))
return value;
1443 if (Sint64 value; SDL_ReadS64LE(
get(), &value))
return value;
1467 if (Uint64 value; SDL_ReadU64BE(
get(), &value))
return value;
1491 if (Sint64 value; SDL_ReadS64BE(
get(), &value))
return value;
1567 void reset(SDL_IOStream* newResource = {})
1619 constexpr explicit IOStream(SDL_IOStream* resource = {})
1624 constexpr IOStream(
const IOStream& other) =
delete;
1646namespace prop::IOStream {
1648constexpr auto WINDOWS_HANDLE_POINTER =
1649 SDL_PROP_IOSTREAM_WINDOWS_HANDLE_POINTER;
1651constexpr auto STDIO_FILE_POINTER = SDL_PROP_IOSTREAM_STDIO_FILE_POINTER;
1653constexpr auto FILE_DESCRIPTOR_NUMBER =
1654 SDL_PROP_IOSTREAM_FILE_DESCRIPTOR_NUMBER;
1656constexpr auto ANDROID_AASSET_POINTER =
1657 SDL_PROP_IOSTREAM_ANDROID_AASSET_POINTER;
1659constexpr auto MEMORY_POINTER = SDL_PROP_IOSTREAM_MEMORY_POINTER;
1661constexpr auto MEMORY_SIZE_NUMBER = SDL_PROP_IOSTREAM_MEMORY_SIZE_NUMBER;
1663constexpr auto DYNAMIC_MEMORY_POINTER =
1664 SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER;
1666constexpr auto DYNAMIC_CHUNKSIZE_NUMBER =
1667 SDL_PROP_IOSTREAM_DYNAMIC_CHUNKSIZE_NUMBER;
1807 size_t datasize = 0;
1808 auto data =
static_cast<char*
>(SDL_LoadFile(file, &datasize));
1833 size_t datasize = 0;
1834 auto data =
static_cast<T*
>(SDL_LoadFile(file, &datasize));
1861#pragma endregion impl
An exception that returns GetError()
Definition SDL3pp_error.h:167
Base class for SDL memory allocated array wrap.
Definition SDL3pp_ownPtr.h:43
A SDL managed resource.
Definition SDL3pp_resource.h:17
constexpr SDL_IOStream * release(SDL_IOStream * 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_IOStream * get() const
Return contained resource;.
Definition SDL3pp_resource.h:57
Helpers to use C++ strings parameters.
Definition SDL3pp_strings.h:43
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:206
IOStream IOFromMem(TargetBytes mem)
Use this function to prepare a read-write memory buffer for use with IOStreamBase.
Definition SDL3pp_iostream.h:1709
StringResult LoadFile(StringParam file)
Load all the data from a file path.
Definition SDL3pp_iostream.h:1805
IOStream IOFromConstMem(SourceBytes mem)
Use this function to prepare a read-only memory buffer for use with IOStreamBase.
Definition SDL3pp_iostream.h:1752
SDL_IOWhence IOWhence
Possible whence values for IOStreamBase seeking.
Definition SDL3pp_iostream.h:64
IOStream IOFromDynamicMem()
Use this function to create an IOStreamBase that is backed by dynamically allocated memory.
Definition SDL3pp_iostream.h:1785
OwnArray< T > LoadFileAs(StringParam file)
Load all the data from a file path.
Definition SDL3pp_iostream.h:1831
constexpr IOStatus IO_STATUS_ERROR
Read or write I/O error.
Definition SDL3pp_iostream.h:42
constexpr IOStatus IO_STATUS_EOF
End of file.
Definition SDL3pp_iostream.h:45
constexpr IOStatus IO_STATUS_WRITEONLY
Tried to read a write-only buffer.
Definition SDL3pp_iostream.h:53
void SaveFile(StringParam file, SourceBytes data)
Save all the data into a file path.
Definition SDL3pp_iostream.h:1853
constexpr IOWhence IO_SEEK_CUR
Seek relative to current read point.
Definition SDL3pp_iostream.h:69
SDL_IOStreamInterface IOStreamInterface
The function pointers that drive an IOStreamBase.
Definition SDL3pp_iostream.h:89
SDL_IOStatus IOStatus
IOStreamBase status, set by a read or write operation.
Definition SDL3pp_iostream.h:37
constexpr IOStatus IO_STATUS_READONLY
Tried to write a read-only buffer.
Definition SDL3pp_iostream.h:50
constexpr IOStatus IO_STATUS_READY
Everything is ready (no errors and not EOF).
Definition SDL3pp_iostream.h:39
constexpr IOStatus IO_STATUS_NOT_READY
Non blocking I/O, not ready.
Definition SDL3pp_iostream.h:47
constexpr IOWhence IO_SEEK_SET
Seek from the beginning of data.
Definition SDL3pp_iostream.h:66
constexpr IOWhence IO_SEEK_END
Seek relative to the end of data.
Definition SDL3pp_iostream.h:72
the main namespace where all SDL3pp public functions and types live
Definition SDL3pp_assert.h:7
The read/write operation structure.
Definition SDL3pp_iostream.h:107
std::optional< Uint64 > TryReadU64LE()
Use this function to read 64 bits of little-endian data from an IOStreamBase and return in native for...
Definition SDL3pp_iostream.h:1417
void WriteS16BE(Sint16 value)
Use this function to write 16 bits in native format to an IOStreamBase as big-endian data.
Definition SDL3pp_iostream.h:1033
std::string Read(size_t size=-1)
Read from a data source.
Definition SDL3pp_iostream.h:352
Uint32 ReadU32LE()
Use this function to read 32 bits of little-endian data from an IOStreamBase and return in native for...
Definition SDL3pp_iostream.h:756
Uint32 ReadU32BE()
Use this function to read 32 bits of big-endian data from an IOStreamBase and return in native format...
Definition SDL3pp_iostream.h:808
Uint16 ReadU16LE()
Use this function to read 16 bits of little-endian data from an IOStreamBase and return in native for...
Definition SDL3pp_iostream.h:652
void WriteS16LE(Sint16 value)
Use this function to write 16 bits in native format to an IOStreamBase as little-endian data.
Definition SDL3pp_iostream.h:1001
Sint64 ReadS64BE()
Use this function to read 64 bits of big-endian data from an IOStreamBase and return in native format...
Definition SDL3pp_iostream.h:938
void WriteU32BE(Uint32 value)
Use this function to write 32 bits in native format to an IOStreamBase as big-endian data.
Definition SDL3pp_iostream.h:1083
std::optional< Sint8 > TryReadS8()
Use this function to read a byte from an IOStreamBase.
Definition SDL3pp_iostream.h:1201
Sint64 Seek(Sint64 offset, IOWhence whence)
Seek within an IOStreamBase data stream.
Definition SDL3pp_iostream.h:307
std::optional< Sint16 > TryReadS16BE()
Use this function to read 16 bits of big-endian data from an IOStreamBase and return in native format...
Definition SDL3pp_iostream.h:1297
std::optional< Sint32 > TryReadS32BE()
Use this function to read 32 bits of big-endian data from an IOStreamBase and return in native format...
Definition SDL3pp_iostream.h:1393
size_t Write(SourceBytes buf)
Write to an IOStreamBase data stream.
Definition SDL3pp_iostream.h:424
OwnArray< T > LoadFileAs()
Load all the data from an SDL data stream.
Definition SDL3pp_iostream.h:563
void WriteU16BE(Uint16 value)
Use this function to write 16 bits in native format to an IOStreamBase as big-endian data.
Definition SDL3pp_iostream.h:1017
StringResult LoadFile()
Load all the data from an SDL data stream.
Definition SDL3pp_iostream.h:538
std::optional< Uint32 > TryReadU32BE()
Use this function to read 32 bits of big-endian data from an IOStreamBase and return in native format...
Definition SDL3pp_iostream.h:1369
void WriteU64LE(Uint64 value)
Use this function to write 64 bits in native format to an IOStreamBase as little-endian data.
Definition SDL3pp_iostream.h:1116
IOStatus GetStatus() const
Query the stream status of an IOStreamBase.
Definition SDL3pp_iostream.h:262
size_t println(std::string_view fmt, auto... args)
Definition SDL3pp_iostream.h:440
std::optional< Sint32 > TryReadS32LE()
Use this function to read 32 bits of little-endian data from an IOStreamBase and return in native for...
Definition SDL3pp_iostream.h:1345
std::optional< Uint16 > TryReadU16LE()
Use this function to read 16 bits of little-endian data from an IOStreamBase and return in native for...
Definition SDL3pp_iostream.h:1225
PropertiesRef GetProperties() const
Get the properties associated with an IOStreamBase.
Definition SDL3pp_iostream.h:240
Uint64 ReadU64LE()
Use this function to read 64 bits of little-endian data from an IOStreamBase and return in native for...
Definition SDL3pp_iostream.h:860
size_t printf(SDL_PRINTF_FORMAT_STRING const char *fmt,...)
Print to an IOStreamBase data stream.
Definition SDL3pp_iostream.h:467
Sint64 GetSize() const
Use this function to get the size of the data stream in an IOStreamBase.
Definition SDL3pp_iostream.h:274
void Flush()
Flush any buffered data in the stream.
Definition SDL3pp_iostream.h:519
Sint8 ReadS8()
Use this function to read a signed byte from an IOStreamBase.
Definition SDL3pp_iostream.h:626
std::optional< Sint16 > TryReadS16LE()
Use this function to read 16 bits of little-endian data from an IOStreamBase and return in native for...
Definition SDL3pp_iostream.h:1249
std::optional< Uint64 > TryReadU64BE()
Use this function to read 64 bits of big-endian data from an IOStreamBase and return in native format...
Definition SDL3pp_iostream.h:1465
Uint16 ReadU16BE()
Use this function to read 16 bits of big-endian data from an IOStreamBase and return in native format...
Definition SDL3pp_iostream.h:704
std::optional< Sint64 > TryReadS64LE()
Use this function to read 64 bits of little-endian data from an IOStreamBase and return in native for...
Definition SDL3pp_iostream.h:1441
std::optional< Uint16 > TryReadU16BE()
Use this function to read 16 bits of big-endian data from an IOStreamBase and return in native format...
Definition SDL3pp_iostream.h:1273
IOStreamBase(const IOStreamInterface &iface, void *userdata)
Create a custom IOStreamBase.
Definition SDL3pp_iostream.h:225
void WriteU64BE(Uint64 value)
Use this function to write 64 bits in native format to an IOStreamBase as big-endian data.
Definition SDL3pp_iostream.h:1149
IOStreamBase(StringParam file, StringParam mode)
Use this function to create a new IOStreamBase structure for reading from and/or writing to a named f...
Definition SDL3pp_iostream.h:192
Sint16 ReadS16LE()
Use this function to read 16 bits of little-endian data from an IOStreamBase and return in native for...
Definition SDL3pp_iostream.h:678
size_t Read(TargetBytes buf)
Read from a data source.
Definition SDL3pp_iostream.h:391
size_t vprintf(SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap)
Print to an IOStreamBase data stream.
Definition SDL3pp_iostream.h:498
Sint64 ReadS64LE()
Use this function to read 64 bits of little-endian data from an IOStreamBase and return in native for...
Definition SDL3pp_iostream.h:886
size_t print(std::string_view fmt, auto... args)
Definition SDL3pp_iostream.h:432
Sint32 ReadS32BE()
Use this function to read 32 bits of big-endian data from an IOStreamBase and return in native format...
Definition SDL3pp_iostream.h:834
void WriteU32LE(Uint32 value)
Use this function to write 32 bits in native format to an IOStreamBase as little-endian data.
Definition SDL3pp_iostream.h:1050
std::optional< Sint64 > TryReadS64BE()
Use this function to read 64 bits of big-endian data from an IOStreamBase and return in native format...
Definition SDL3pp_iostream.h:1489
Sint32 ReadS32LE()
Use this function to read 32 bits of little-endian data from an IOStreamBase and return in native for...
Definition SDL3pp_iostream.h:782
Sint64 Tell() const
Determine the current read/write offset in an IOStreamBase data stream.
Definition SDL3pp_iostream.h:328
void WriteS64LE(Sint64 value)
Use this function to write 64 bits in native format to an IOStreamBase as little-endian data.
Definition SDL3pp_iostream.h:1133
Uint64 ReadU64BE()
Use this function to read 64 bits of big-endian data from an IOStreamBase and return in native format...
Definition SDL3pp_iostream.h:912
void WriteS64BE(Sint64 value)
Use this function to write 64 bits in native format to an IOStreamBase as big-endian data.
Definition SDL3pp_iostream.h:1165
Uint8 ReadU8()
Use this function to read a byte from an IOStreamBase.
Definition SDL3pp_iostream.h:604
void WriteU16LE(Uint16 value)
Use this function to write 16 bits in native format to an IOStreamBase as little-endian data.
Definition SDL3pp_iostream.h:984
void WriteS32BE(Sint32 value)
Use this function to write 32 bits in native format to an IOStreamBase as big-endian data.
Definition SDL3pp_iostream.h:1099
void SaveFile(SourceBytes data)
Save all the data into an SDL data stream.
Definition SDL3pp_iostream.h:584
void WriteS8(Sint8 value)
Use this function to write a signed byte to an IOStreamBase.
Definition SDL3pp_iostream.h:967
void WriteU8(Uint8 value)
Use this function to write a byte to an IOStreamBase.
Definition SDL3pp_iostream.h:955
void WriteS32LE(Sint32 value)
Use this function to write 32 bits in native format to an IOStreamBase as little-endian data.
Definition SDL3pp_iostream.h:1067
Sint16 ReadS16BE()
Use this function to read 16 bits of big-endian data from an IOStreamBase and return in native format...
Definition SDL3pp_iostream.h:730
std::optional< Uint32 > TryReadU32LE()
Use this function to read 32 bits of little-endian data from an IOStreamBase and return in native for...
Definition SDL3pp_iostream.h:1321
std::optional< Uint8 > TryReadU8()
Use this function to read a byte from an IOStreamBase.
Definition SDL3pp_iostream.h:1181
Handle to a non owned iOStream.
Definition SDL3pp_iostream.h:1505
void Close()
Close and free an allocated IOStreamBase structure.
Definition SDL3pp_iostream.h:1601
constexpr IOStreamRef(IOStreamRef &&other)
Move constructor.
Definition SDL3pp_iostream.h:1519
void reset(SDL_IOStream *newResource={})
Close and free an allocated IOStreamBase structure.
Definition SDL3pp_iostream.h:1567
constexpr IOStreamRef(const IOStreamRef &other)
Copy constructor.
Definition SDL3pp_iostream.h:1511
IOStreamRef & operator=(IOStreamRef other)
Assignment operator.
Definition SDL3pp_iostream.h:1532
constexpr ~IOStreamRef()=default
Default constructor.
Handle to an owned iOStream.
Definition SDL3pp_iostream.h:1613
~IOStream()
Frees up resource when object goes out of scope.
Definition SDL3pp_iostream.h:1634
constexpr IOStream(SDL_IOStream *resource={})
Constructs from the underlying resource.
Definition SDL3pp_iostream.h:1619
IOStream & operator=(IOStream other)
Assignment operator.
Definition SDL3pp_iostream.h:1639
constexpr IOStream(IOStream &&other)=default
Move constructor.
Handle to a non owned properties.
Definition SDL3pp_properties.h:693
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
A simple std::string-like interface for SDL allocated strings.
Definition SDL3pp_strings.h:153
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