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"
65 SDL_IO_STATUS_NOT_READY;
68 SDL_IO_STATUS_READONLY;
71 SDL_IO_STATUS_WRITEONLY;
172 auto size = SDL_GetIOSize(
get());
173 if (size < 0)
throw Error{};
205 return SDL_SeekIO(
get(), offset, whence);
224 Sint64
Tell()
const {
return SDL_TellIO(
get()); }
248 std::string
Read(
size_t size = -1)
251 auto curSize = SDL_GetIOSize(
get());
252 if ((curSize < 0 || pos < 0)) {
253 if (size ==
size_t(-1))
return {};
254 }
else if (curSize - pos <= 0) {
256 }
else if (curSize - pos < size) {
257 size = curSize - pos;
259 std::string result(size, 0);
260 auto actualSize =
Read(result);
261 if (actualSize < size) result.resize(actualSize);
328 size_t print(std::string_view fmt,
auto... args)
330 return Write(std::vformat(fmt, std::make_format_args(args...)));
336 size_t println(std::string_view fmt,
auto... args)
339 std::vformat(fmt, std::make_format_args(args...)) +
"\n";
340 return Write(result);
363 size_t printf(SDL_PRINTF_FORMAT_STRING
const char* fmt, ...)
394 size_t vprintf(SDL_PRINTF_FORMAT_STRING
const char* fmt, va_list ap)
396 return SDL_IOvprintf(
get(), fmt, ap);
437 auto data =
static_cast<char*
>(SDL_LoadFile_IO(
get(), &datasize,
false));
462 auto data =
static_cast<T*
>(SDL_LoadFile_IO(
get(), &datasize,
false));
1079 if (Uint8 value; SDL_ReadU8(
get(), &value))
return value;
1099 if (Sint8 value; SDL_ReadS8(
get(), &value))
return value;
1123 if (Uint16 value; SDL_ReadU16LE(
get(), &value))
return value;
1147 if (Sint16 value; SDL_ReadS16LE(
get(), &value))
return value;
1171 if (Uint16 value; SDL_ReadU16BE(
get(), &value))
return value;
1195 if (Sint16 value; SDL_ReadS16BE(
get(), &value))
return value;
1219 if (Uint32 value; SDL_ReadU32LE(
get(), &value))
return value;
1243 if (Sint32 value; SDL_ReadS32LE(
get(), &value))
return value;
1267 if (Uint32 value; SDL_ReadU32BE(
get(), &value))
return value;
1291 if (Sint32 value; SDL_ReadS32BE(
get(), &value))
return value;
1315 if (Uint64 value; SDL_ReadU64LE(
get(), &value))
return value;
1339 if (Sint64 value; SDL_ReadS64LE(
get(), &value))
return value;
1363 if (Uint64 value; SDL_ReadU64BE(
get(), &value))
return value;
1387 if (Sint64 value; SDL_ReadS64BE(
get(), &value))
return value;
1421 static void reset(SDL_IOStream* resource)
1522 return IOStream(SDL_IOFromFile(file, mode));
1739namespace prop::IOStream {
1741constexpr auto WINDOWS_HANDLE_POINTER =
1742 SDL_PROP_IOSTREAM_WINDOWS_HANDLE_POINTER;
1744constexpr auto STDIO_FILE_POINTER = SDL_PROP_IOSTREAM_STDIO_FILE_POINTER;
1746constexpr auto FILE_DESCRIPTOR_NUMBER =
1747 SDL_PROP_IOSTREAM_FILE_DESCRIPTOR_NUMBER;
1749constexpr auto ANDROID_AASSET_POINTER =
1750 SDL_PROP_IOSTREAM_ANDROID_AASSET_POINTER;
1752constexpr auto MEMORY_POINTER = SDL_PROP_IOSTREAM_MEMORY_POINTER;
1754constexpr auto MEMORY_SIZE_NUMBER = SDL_PROP_IOSTREAM_MEMORY_SIZE_NUMBER;
1756constexpr auto DYNAMIC_MEMORY_POINTER =
1757 SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER;
1759constexpr auto DYNAMIC_CHUNKSIZE_NUMBER =
1760 SDL_PROP_IOSTREAM_DYNAMIC_CHUNKSIZE_NUMBER;
1784 size_t datasize = 0;
1785 auto data =
static_cast<char*
>(SDL_LoadFile(file, &datasize));
1810 size_t datasize = 0;
1811 auto data =
static_cast<T*
>(SDL_LoadFile(file, &datasize));
1838#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
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_IOStream * get() const
Return contained resource;.
Definition SDL3pp_resource.h:76
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
ResourceShared< IOStream > IOStreamShared
Handle to a shared iOStream.
Definition SDL3pp_iostream.h:37
StringResult LoadFile(StringParam file)
Load all the data from a file path.
Definition SDL3pp_iostream.h:1782
SDL_IOWhence IOWhence
Possible whence values for IOStream seeking.
Definition SDL3pp_iostream.h:81
OwnArray< T > LoadFileAs(StringParam file)
Load all the data from a file path.
Definition SDL3pp_iostream.h:1808
constexpr IOStatus IO_STATUS_ERROR
Read or write I/O error.
Definition SDL3pp_iostream.h:59
constexpr IOStatus IO_STATUS_EOF
End of file.
Definition SDL3pp_iostream.h:62
constexpr IOStatus IO_STATUS_WRITEONLY
Tried to read a write-only buffer.
Definition SDL3pp_iostream.h:70
IOStreamShared share()
Move this iOStream into a IOStreamShared.
Definition SDL3pp_iostream.h:1712
void SaveFile(StringParam file, SourceBytes data)
Save all the data into a file path.
Definition SDL3pp_iostream.h:1830
constexpr IOWhence IO_SEEK_CUR
Seek relative to current read point.
Definition SDL3pp_iostream.h:86
SDL_IOStreamInterface IOStreamInterface
The function pointers that drive an IOStream.
Definition SDL3pp_iostream.h:106
SDL_IOStatus IOStatus
IOStream status, set by a read or write operation.
Definition SDL3pp_iostream.h:54
constexpr IOStatus IO_STATUS_READONLY
Tried to write a read-only buffer.
Definition SDL3pp_iostream.h:67
constexpr IOStatus IO_STATUS_READY
Everything is ready (no errors and not EOF).
Definition SDL3pp_iostream.h:56
constexpr IOStatus IO_STATUS_NOT_READY
Non blocking I/O, not ready.
Definition SDL3pp_iostream.h:64
constexpr IOWhence IO_SEEK_SET
Seek from the beginning of data.
Definition SDL3pp_iostream.h:83
constexpr IOWhence IO_SEEK_END
Seek relative to the end of data.
Definition SDL3pp_iostream.h:89
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:123
std::optional< Sint16 > TryReadS16BE()
Use this function to read 16 bits of big-endian data from an IOStreamRef and return in native format.
Definition SDL3pp_iostream.h:1193
void WriteS8(Sint8 value)
Use this function to write a signed byte to an IOStreamRef.
Definition SDL3pp_iostream.h:863
Uint64 ReadU64LE()
Use this function to read 64 bits of little-endian data from an IOStreamRef and return in native form...
Definition SDL3pp_iostream.h:756
Sint8 ReadS8()
Use this function to read a signed byte from an IOStreamRef.
Definition SDL3pp_iostream.h:522
void WriteU64BE(Uint64 value)
Use this function to write 64 bits in native format to an IOStreamRef as big-endian data.
Definition SDL3pp_iostream.h:1045
PropertiesRef GetProperties() const
Get the properties associated with an IOStreamRef.
Definition SDL3pp_iostream.h:136
IOStatus GetStatus() const
Query the stream status of an IOStreamRef.
Definition SDL3pp_iostream.h:158
void WriteU16LE(Uint16 value)
Use this function to write 16 bits in native format to an IOStreamRef as little-endian data.
Definition SDL3pp_iostream.h:880
void WriteS64LE(Sint64 value)
Use this function to write 64 bits in native format to an IOStreamRef as little-endian data.
Definition SDL3pp_iostream.h:1029
Sint16 ReadS16BE()
Use this function to read 16 bits of big-endian data from an IOStreamRef and return in native format.
Definition SDL3pp_iostream.h:626
Sint64 GetSize() const
Use this function to get the size of the data stream in an IOStreamRef.
Definition SDL3pp_iostream.h:170
std::optional< Sint32 > TryReadS32BE()
Use this function to read 32 bits of big-endian data from an IOStreamRef and return in native format.
Definition SDL3pp_iostream.h:1289
void WriteS64BE(Sint64 value)
Use this function to write 64 bits in native format to an IOStreamRef as big-endian data.
Definition SDL3pp_iostream.h:1061
size_t print(std::string_view fmt, auto... args)
Definition SDL3pp_iostream.h:328
void WriteU32BE(Uint32 value)
Use this function to write 32 bits in native format to an IOStreamRef as big-endian data.
Definition SDL3pp_iostream.h:979
std::optional< Uint16 > TryReadU16LE()
Use this function to read 16 bits of little-endian data from an IOStreamRef and return in native form...
Definition SDL3pp_iostream.h:1121
static void reset(SDL_IOStream *resource)
Close and free an allocated IOStreamRef structure.
Definition SDL3pp_iostream.h:1421
Uint32 ReadU32LE()
Use this function to read 32 bits of little-endian data from an IOStreamRef and return in native form...
Definition SDL3pp_iostream.h:652
void WriteU64LE(Uint64 value)
Use this function to write 64 bits in native format to an IOStreamRef as little-endian data.
Definition SDL3pp_iostream.h:1012
OwnArray< T > LoadFileAs()
Load all the data from an SDL data stream.
Definition SDL3pp_iostream.h:459
Sint16 ReadS16LE()
Use this function to read 16 bits of little-endian data from an IOStreamRef and return in native form...
Definition SDL3pp_iostream.h:574
std::optional< Sint16 > TryReadS16LE()
Use this function to read 16 bits of little-endian data from an IOStreamRef and return in native form...
Definition SDL3pp_iostream.h:1145
void WriteU16BE(Uint16 value)
Use this function to write 16 bits in native format to an IOStreamRef as big-endian data.
Definition SDL3pp_iostream.h:913
void SaveFile(SourceBytes data)
Save all the data into an SDL data stream.
Definition SDL3pp_iostream.h:480
std::optional< Sint8 > TryReadS8()
Use this function to read a byte from an IOStreamRef.
Definition SDL3pp_iostream.h:1097
std::optional< Sint64 > TryReadS64BE()
Use this function to read 64 bits of big-endian data from an IOStreamRef and return in native format.
Definition SDL3pp_iostream.h:1385
Sint64 ReadS64BE()
Use this function to read 64 bits of big-endian data from an IOStreamRef and return in native format.
Definition SDL3pp_iostream.h:834
Uint16 ReadU16BE()
Use this function to read 16 bits of big-endian data from an IOStreamRef and return in native format.
Definition SDL3pp_iostream.h:600
size_t println(std::string_view fmt, auto... args)
Definition SDL3pp_iostream.h:336
std::optional< Sint32 > TryReadS32LE()
Use this function to read 32 bits of little-endian data from an IOStreamRef and return in native form...
Definition SDL3pp_iostream.h:1241
void WriteU32LE(Uint32 value)
Use this function to write 32 bits in native format to an IOStreamRef as little-endian data.
Definition SDL3pp_iostream.h:946
Sint32 ReadS32LE()
Use this function to read 32 bits of little-endian data from an IOStreamRef and return in native form...
Definition SDL3pp_iostream.h:678
std::optional< Uint32 > TryReadU32BE()
Use this function to read 32 bits of big-endian data from an IOStreamRef and return in native format.
Definition SDL3pp_iostream.h:1265
size_t Read(TargetBytes buf)
Read from a data source.
Definition SDL3pp_iostream.h:287
Uint32 ReadU32BE()
Use this function to read 32 bits of big-endian data from an IOStreamRef and return in native format.
Definition SDL3pp_iostream.h:704
Uint8 ReadU8()
Use this function to read a byte from an IOStreamRef.
Definition SDL3pp_iostream.h:500
size_t printf(SDL_PRINTF_FORMAT_STRING const char *fmt,...)
Print to an IOStreamRef data stream.
Definition SDL3pp_iostream.h:363
Sint32 ReadS32BE()
Use this function to read 32 bits of big-endian data from an IOStreamRef and return in native format.
Definition SDL3pp_iostream.h:730
Uint16 ReadU16LE()
Use this function to read 16 bits of little-endian data from an IOStreamRef and return in native form...
Definition SDL3pp_iostream.h:548
void WriteS32BE(Sint32 value)
Use this function to write 32 bits in native format to an IOStreamRef as big-endian data.
Definition SDL3pp_iostream.h:995
std::optional< Uint64 > TryReadU64BE()
Use this function to read 64 bits of big-endian data from an IOStreamRef and return in native format.
Definition SDL3pp_iostream.h:1361
void WriteU8(Uint8 value)
Use this function to write a byte to an IOStreamRef.
Definition SDL3pp_iostream.h:851
void Flush()
Flush any buffered data in the stream.
Definition SDL3pp_iostream.h:415
std::optional< Uint64 > TryReadU64LE()
Use this function to read 64 bits of little-endian data from an IOStreamRef and return in native form...
Definition SDL3pp_iostream.h:1313
std::optional< Sint64 > TryReadS64LE()
Use this function to read 64 bits of little-endian data from an IOStreamRef and return in native form...
Definition SDL3pp_iostream.h:1337
StringResult LoadFile()
Load all the data from an SDL data stream.
Definition SDL3pp_iostream.h:434
size_t vprintf(SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap)
Print to an IOStreamRef data stream.
Definition SDL3pp_iostream.h:394
Sint64 Tell() const
Determine the current read/write offset in an IOStreamRef data stream.
Definition SDL3pp_iostream.h:224
size_t Write(SourceBytes buf)
Write to an IOStreamRef data stream.
Definition SDL3pp_iostream.h:320
void WriteS16LE(Sint16 value)
Use this function to write 16 bits in native format to an IOStreamRef as little-endian data.
Definition SDL3pp_iostream.h:897
std::optional< Uint32 > TryReadU32LE()
Use this function to read 32 bits of little-endian data from an IOStreamRef and return in native form...
Definition SDL3pp_iostream.h:1217
std::optional< Uint8 > TryReadU8()
Use this function to read a byte from an IOStreamRef.
Definition SDL3pp_iostream.h:1077
void WriteS32LE(Sint32 value)
Use this function to write 32 bits in native format to an IOStreamRef as little-endian data.
Definition SDL3pp_iostream.h:963
std::string Read(size_t size=-1)
Read from a data source.
Definition SDL3pp_iostream.h:248
Sint64 Seek(Sint64 offset, IOWhence whence)
Seek within an IOStreamRef data stream.
Definition SDL3pp_iostream.h:203
Uint64 ReadU64BE()
Use this function to read 64 bits of big-endian data from an IOStreamRef and return in native format.
Definition SDL3pp_iostream.h:808
std::optional< Uint16 > TryReadU16BE()
Use this function to read 16 bits of big-endian data from an IOStreamRef and return in native format.
Definition SDL3pp_iostream.h:1169
void WriteS16BE(Sint16 value)
Use this function to write 16 bits in native format to an IOStreamRef as big-endian data.
Definition SDL3pp_iostream.h:929
Sint64 ReadS64LE()
Use this function to read 64 bits of little-endian data from an IOStreamRef and return in native form...
Definition SDL3pp_iostream.h:782
Unsafe Handle to iOStream.
Definition SDL3pp_iostream.h:1727
constexpr IOStreamUnsafe(IOStream &&other)
Constructs IOStreamUnsafe from IOStream.
Definition SDL3pp_iostream.h:1733
Handle to an owned iOStream.
Definition SDL3pp_iostream.h:1435
static IOStream Open(const IOStreamInterface &iface, void *userdata)
Create a custom IOStreamRef.
Definition SDL3pp_iostream.h:1669
void Close()
Close and free an allocated IOStreamRef structure.
Definition SDL3pp_iostream.h:1703
static IOStream FromMem(TargetBytes mem)
Use this function to prepare a read-write memory buffer for use with IOStream.
Definition SDL3pp_iostream.h:1563
static IOStream FromDynamicMem()
Use this function to create an IOStreamRef that is backed by dynamically allocated memory.
Definition SDL3pp_iostream.h:1639
static IOStream FromConstMem(SourceBytes mem)
Use this function to prepare a read-only memory buffer for use with IOStreamRef.
Definition SDL3pp_iostream.h:1606
static IOStream FromFile(StringParam file, StringParam mode)
Use this function to create a new IOStream structure for reading from and/or writing to a named file.
Definition SDL3pp_iostream.h:1520
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
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