SDL 3.0
SDL_iostream.h
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21
22/* WIKI CATEGORY: IOStream */
23
24/**
25 * # CategoryIOStream
26 *
27 * SDL provides an abstract interface for reading and writing data streams. It
28 * offers implementations for files, memory, etc, and the app can provideo
29 * their own implementations, too.
30 *
31 * SDL_IOStream is not related to the standard C++ iostream class, other than
32 * both are abstract interfaces to read/write data.
33 */
34
35#ifndef SDL_iostream_h_
36#define SDL_iostream_h_
37
38#include <SDL3/SDL_stdinc.h>
39#include <SDL3/SDL_error.h>
40#include <SDL3/SDL_properties.h>
41
42#include <SDL3/SDL_begin_code.h>
43/* Set up for C function definitions, even when using C++ */
44#ifdef __cplusplus
45extern "C" {
46#endif
47
48/**
49 * SDL_IOStream status, set by a read or write operation.
50 *
51 * \since This enum is available since SDL 3.0.0.
52 */
53typedef enum SDL_IOStatus
54{
55 SDL_IO_STATUS_READY, /**< Everything is ready (no errors and not EOF). */
56 SDL_IO_STATUS_ERROR, /**< Read or write I/O error */
57 SDL_IO_STATUS_EOF, /**< End of file */
58 SDL_IO_STATUS_NOT_READY, /**< Non blocking I/O, not ready */
59 SDL_IO_STATUS_READONLY, /**< Tried to write a read-only buffer */
60 SDL_IO_STATUS_WRITEONLY /**< Tried to read a write-only buffer */
62
63/**
64 * Possible `whence` values for SDL_IOStream seeking.
65 *
66 * These map to the same "whence" concept that `fseek` or `lseek` use in the
67 * standard C runtime.
68 *
69 * \since This enum is available since SDL 3.0.0.
70 */
71typedef enum SDL_IOWhence
72{
73 SDL_IO_SEEK_SET, /**< Seek from the beginning of data */
74 SDL_IO_SEEK_CUR, /**< Seek relative to current read point */
75 SDL_IO_SEEK_END, /**< Seek relative to the end of data */
77
78/**
79 * The function pointers that drive an SDL_IOStream.
80 *
81 * Applications can provide this struct to SDL_OpenIO() to create their own
82 * implementation of SDL_IOStream. This is not necessarily required, as SDL
83 * already offers several common types of I/O streams, via functions like
84 * SDL_IOFromFile() and SDL_IOFromMem().
85 *
86 * \since This struct is available since SDL 3.0.0.
87 */
89{
90 /**
91 * Return the number of bytes in this SDL_IOStream
92 *
93 * \return the total size of the data stream, or -1 on error.
94 */
95 Sint64 (SDLCALL *size)(void *userdata);
96
97 /**
98 * Seek to `offset` relative to `whence`, one of stdio's whence values:
99 * SDL_IO_SEEK_SET, SDL_IO_SEEK_CUR, SDL_IO_SEEK_END
100 *
101 * \return the final offset in the data stream, or -1 on error.
102 */
103 Sint64 (SDLCALL *seek)(void *userdata, Sint64 offset, SDL_IOWhence whence);
104
105 /**
106 * Read up to `size` bytes from the data stream to the area pointed
107 * at by `ptr`.
108 *
109 * On an incomplete read, you should set `*status` to a value from the
110 * SDL_IOStatus enum. You do not have to explicitly set this on
111 * a complete, successful read.
112 *
113 * \return the number of bytes read
114 */
115 size_t (SDLCALL *read)(void *userdata, void *ptr, size_t size, SDL_IOStatus *status);
116
117 /**
118 * Write exactly `size` bytes from the area pointed at by `ptr`
119 * to data stream.
120 *
121 * On an incomplete write, you should set `*status` to a value from the
122 * SDL_IOStatus enum. You do not have to explicitly set this on
123 * a complete, successful write.
124 *
125 * \return the number of bytes written
126 */
127 size_t (SDLCALL *write)(void *userdata, const void *ptr, size_t size, SDL_IOStatus *status);
128
129 /**
130 * Close and free any allocated resources.
131 *
132 * The SDL_IOStream is still destroyed even if this fails, so clean up anything
133 * even if flushing to disk returns an error.
134 *
135 * \return 0 if successful or -1 on write error when flushing data.
136 */
137 int (SDLCALL *close)(void *userdata);
139
140
141/**
142 * The read/write operation structure.
143 *
144 * This operates as an opaque handle. There are several APIs to create various
145 * types of I/O streams, or an app can supply an SDL_IOStreamInterface to
146 * SDL_OpenIO() to provide their own stream implementation behind this
147 * struct's abstract interface.
148 *
149 * \since This struct is available since SDL 3.0.0.
150 */
152
153
154/**
155 * \name IOFrom functions
156 *
157 * Functions to create SDL_IOStream structures from various data streams.
158 */
159/* @{ */
160
161/**
162 * Use this function to create a new SDL_IOStream structure for reading from
163 * and/or writing to a named file.
164 *
165 * The `mode` string is treated roughly the same as in a call to the C
166 * library's fopen(), even if SDL doesn't happen to use fopen() behind the
167 * scenes.
168 *
169 * Available `mode` strings:
170 *
171 * - "r": Open a file for reading. The file must exist.
172 * - "w": Create an empty file for writing. If a file with the same name
173 * already exists its content is erased and the file is treated as a new
174 * empty file.
175 * - "a": Append to a file. Writing operations append data at the end of the
176 * file. The file is created if it does not exist.
177 * - "r+": Open a file for update both reading and writing. The file must
178 * exist.
179 * - "w+": Create an empty file for both reading and writing. If a file with
180 * the same name already exists its content is erased and the file is
181 * treated as a new empty file.
182 * - "a+": Open a file for reading and appending. All writing operations are
183 * performed at the end of the file, protecting the previous content to be
184 * overwritten. You can reposition (fseek, rewind) the internal pointer to
185 * anywhere in the file for reading, but writing operations will move it
186 * back to the end of file. The file is created if it does not exist.
187 *
188 * **NOTE**: In order to open a file as a binary file, a "b" character has to
189 * be included in the `mode` string. This additional "b" character can either
190 * be appended at the end of the string (thus making the following compound
191 * modes: "rb", "wb", "ab", "r+b", "w+b", "a+b") or be inserted between the
192 * letter and the "+" sign for the mixed modes ("rb+", "wb+", "ab+").
193 * Additional characters may follow the sequence, although they should have no
194 * effect. For example, "t" is sometimes appended to make explicit the file is
195 * a text file.
196 *
197 * This function supports Unicode filenames, but they must be encoded in UTF-8
198 * format, regardless of the underlying operating system.
199 *
200 * In Android, SDL_IOFromFile() can be used to open content:// URIs. As a
201 * fallback, SDL_IOFromFile() will transparently open a matching filename in
202 * the app's `assets`.
203 *
204 * Closing the SDL_IOStream will close SDL's internal file handle.
205 *
206 * The following properties may be set at creation time by SDL:
207 *
208 * - `SDL_PROP_IOSTREAM_WINDOWS_HANDLE_POINTER`: a pointer, that can be cast
209 * to a win32 `HANDLE`, that this SDL_IOStream is using to access the
210 * filesystem. If the program isn't running on Windows, or SDL used some
211 * other method to access the filesystem, this property will not be set.
212 * - `SDL_PROP_IOSTREAM_STDIO_FILE_POINTER`: a pointer, that can be cast to a
213 * stdio `FILE *`, that this SDL_IOStream is using to access the filesystem.
214 * If SDL used some other method to access the filesystem, this property
215 * will not be set. PLEASE NOTE that if SDL is using a different C runtime
216 * than your app, trying to use this pointer will almost certainly result in
217 * a crash! This is mostly a problem on Windows; make sure you build SDL and
218 * your app with the same compiler and settings to avoid it.
219 * - `SDL_PROP_IOSTREAM_ANDROID_AASSET_POINTER`: a pointer, that can be cast
220 * to an Android NDK `AAsset *`, that this SDL_IOStream is using to access
221 * the filesystem. If SDL used some other method to access the filesystem,
222 * this property will not be set.
223 *
224 * \param file a UTF-8 string representing the filename to open.
225 * \param mode an ASCII string representing the mode to be used for opening
226 * the file.
227 * \returns a pointer to the SDL_IOStream structure that is created or NULL on
228 * failure; call SDL_GetError() for more information.
229 *
230 * \since This function is available since SDL 3.0.0.
231 *
232 * \sa SDL_CloseIO
233 * \sa SDL_ReadIO
234 * \sa SDL_SeekIO
235 * \sa SDL_TellIO
236 * \sa SDL_WriteIO
237 */
238extern SDL_DECLSPEC SDL_IOStream * SDLCALL SDL_IOFromFile(const char *file, const char *mode);
239
240#define SDL_PROP_IOSTREAM_WINDOWS_HANDLE_POINTER "SDL.iostream.windows.handle"
241#define SDL_PROP_IOSTREAM_STDIO_FILE_POINTER "SDL.iostream.stdio.file"
242#define SDL_PROP_IOSTREAM_ANDROID_AASSET_POINTER "SDL.iostream.android.aasset"
243
244/**
245 * Use this function to prepare a read-write memory buffer for use with
246 * SDL_IOStream.
247 *
248 * This function sets up an SDL_IOStream struct based on a memory area of a
249 * certain size, for both read and write access.
250 *
251 * This memory buffer is not copied by the SDL_IOStream; the pointer you
252 * provide must remain valid until you close the stream. Closing the stream
253 * will not free the original buffer.
254 *
255 * If you need to make sure the SDL_IOStream never writes to the memory
256 * buffer, you should use SDL_IOFromConstMem() with a read-only buffer of
257 * memory instead.
258 *
259 * \param mem a pointer to a buffer to feed an SDL_IOStream stream.
260 * \param size the buffer size, in bytes.
261 * \returns a pointer to a new SDL_IOStream structure or NULL on failure; call
262 * SDL_GetError() for more information.
263 *
264 * \since This function is available since SDL 3.0.0.
265 *
266 * \sa SDL_IOFromConstMem
267 * \sa SDL_CloseIO
268 * \sa SDL_ReadIO
269 * \sa SDL_SeekIO
270 * \sa SDL_TellIO
271 * \sa SDL_WriteIO
272 */
273extern SDL_DECLSPEC SDL_IOStream * SDLCALL SDL_IOFromMem(void *mem, size_t size);
274
275/**
276 * Use this function to prepare a read-only memory buffer for use with
277 * SDL_IOStream.
278 *
279 * This function sets up an SDL_IOStream struct based on a memory area of a
280 * certain size. It assumes the memory area is not writable.
281 *
282 * Attempting to write to this SDL_IOStream stream will report an error
283 * without writing to the memory buffer.
284 *
285 * This memory buffer is not copied by the SDL_IOStream; the pointer you
286 * provide must remain valid until you close the stream. Closing the stream
287 * will not free the original buffer.
288 *
289 * If you need to write to a memory buffer, you should use SDL_IOFromMem()
290 * with a writable buffer of memory instead.
291 *
292 * \param mem a pointer to a read-only buffer to feed an SDL_IOStream stream.
293 * \param size the buffer size, in bytes.
294 * \returns a pointer to a new SDL_IOStream structure or NULL on failure; call
295 * SDL_GetError() for more information.
296 *
297 * \since This function is available since SDL 3.0.0.
298 *
299 * \sa SDL_IOFromMem
300 * \sa SDL_CloseIO
301 * \sa SDL_ReadIO
302 * \sa SDL_SeekIO
303 * \sa SDL_TellIO
304 */
305extern SDL_DECLSPEC SDL_IOStream * SDLCALL SDL_IOFromConstMem(const void *mem, size_t size);
306
307/**
308 * Use this function to create an SDL_IOStream that is backed by dynamically
309 * allocated memory.
310 *
311 * This supports the following properties to provide access to the memory and
312 * control over allocations:
313 *
314 * - `SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER`: a pointer to the internal
315 * memory of the stream. This can be set to NULL to transfer ownership of
316 * the memory to the application, which should free the memory with
317 * SDL_free(). If this is done, the next operation on the stream must be
318 * SDL_CloseIO().
319 * - `SDL_PROP_IOSTREAM_DYNAMIC_CHUNKSIZE_NUMBER`: memory will be allocated in
320 * multiples of this size, defaulting to 1024.
321 *
322 * \returns a pointer to a new SDL_IOStream structure or NULL on failure; call
323 * SDL_GetError() for more information.
324 *
325 * \since This function is available since SDL 3.0.0.
326 *
327 * \sa SDL_CloseIO
328 * \sa SDL_ReadIO
329 * \sa SDL_SeekIO
330 * \sa SDL_TellIO
331 * \sa SDL_WriteIO
332 */
333extern SDL_DECLSPEC SDL_IOStream * SDLCALL SDL_IOFromDynamicMem(void);
334
335#define SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER "SDL.iostream.dynamic.memory"
336#define SDL_PROP_IOSTREAM_DYNAMIC_CHUNKSIZE_NUMBER "SDL.iostream.dynamic.chunksize"
337
338/* @} *//* IOFrom functions */
339
340
341/**
342 * Create a custom SDL_IOStream.
343 *
344 * Applications do not need to use this function unless they are providing
345 * their own SDL_IOStream implementation. If you just need an SDL_IOStream to
346 * read/write a common data source, you should use the built-in
347 * implementations in SDL, like SDL_IOFromFile() or SDL_IOFromMem(), etc.
348 *
349 * You must free the returned pointer with SDL_CloseIO().
350 *
351 * This function makes a copy of `iface` and the caller does not need to keep
352 * this data around after this call.
353 *
354 * \param iface the function pointers that implement this SDL_IOStream.
355 * \param userdata the app-controlled pointer that is passed to iface's
356 * functions when called.
357 * \returns a pointer to the allocated memory on success or NULL on failure;
358 * call SDL_GetError() for more information.
359 *
360 * \since This function is available since SDL 3.0.0.
361 *
362 * \sa SDL_CloseIO
363 * \sa SDL_IOFromConstMem
364 * \sa SDL_IOFromFile
365 * \sa SDL_IOFromMem
366 */
367extern SDL_DECLSPEC SDL_IOStream * SDLCALL SDL_OpenIO(const SDL_IOStreamInterface *iface, void *userdata);
368
369/**
370 * Close and free an allocated SDL_IOStream structure.
371 *
372 * SDL_CloseIO() closes and cleans up the SDL_IOStream stream. It releases any
373 * resources used by the stream and frees the SDL_IOStream itself. This
374 * returns 0 on success, or -1 if the stream failed to flush to its output
375 * (e.g. to disk).
376 *
377 * Note that if this fails to flush the stream to disk, this function reports
378 * an error, but the SDL_IOStream is still invalid once this function returns.
379 *
380 * \param context SDL_IOStream structure to close.
381 * \returns 0 on success or a negative error code on failure; call
382 * SDL_GetError() for more information.
383 *
384 * \since This function is available since SDL 3.0.0.
385 *
386 * \sa SDL_OpenIO
387 */
388extern SDL_DECLSPEC int SDLCALL SDL_CloseIO(SDL_IOStream *context);
389
390/**
391 * Get the properties associated with an SDL_IOStream.
392 *
393 * \param context a pointer to an SDL_IOStream structure.
394 * \returns a valid property ID on success or 0 on failure; call
395 * SDL_GetError() for more information.
396 *
397 * \since This function is available since SDL 3.0.0.
398 */
399extern SDL_DECLSPEC SDL_PropertiesID SDLCALL SDL_GetIOProperties(SDL_IOStream *context);
400
401/**
402 * Query the stream status of an SDL_IOStream.
403 *
404 * This information can be useful to decide if a short read or write was due
405 * to an error, an EOF, or a non-blocking operation that isn't yet ready to
406 * complete.
407 *
408 * An SDL_IOStream's status is only expected to change after a SDL_ReadIO or
409 * SDL_WriteIO call; don't expect it to change if you just call this query
410 * function in a tight loop.
411 *
412 * \param context the SDL_IOStream to query.
413 * \returns an SDL_IOStatus enum with the current state.
414 *
415 * \threadsafety This function should not be called at the same time that
416 * another thread is operating on the same SDL_IOStream.
417 *
418 * \since This function is available since SDL 3.0.0.
419 */
420extern SDL_DECLSPEC SDL_IOStatus SDLCALL SDL_GetIOStatus(SDL_IOStream *context);
421
422/**
423 * Use this function to get the size of the data stream in an SDL_IOStream.
424 *
425 * \param context the SDL_IOStream to get the size of the data stream from.
426 * \returns the size of the data stream in the SDL_IOStream on success or a
427 * negative error code on failure; call SDL_GetError() for more
428 * information.
429 *
430 * \since This function is available since SDL 3.0.0.
431 */
432extern SDL_DECLSPEC Sint64 SDLCALL SDL_GetIOSize(SDL_IOStream *context);
433
434/**
435 * Seek within an SDL_IOStream data stream.
436 *
437 * This function seeks to byte `offset`, relative to `whence`.
438 *
439 * `whence` may be any of the following values:
440 *
441 * - `SDL_IO_SEEK_SET`: seek from the beginning of data
442 * - `SDL_IO_SEEK_CUR`: seek relative to current read point
443 * - `SDL_IO_SEEK_END`: seek relative to the end of data
444 *
445 * If this stream can not seek, it will return -1.
446 *
447 * \param context a pointer to an SDL_IOStream structure.
448 * \param offset an offset in bytes, relative to `whence` location; can be
449 * negative.
450 * \param whence any of `SDL_IO_SEEK_SET`, `SDL_IO_SEEK_CUR`,
451 * `SDL_IO_SEEK_END`.
452 * \returns the final offset in the data stream after the seek or a negative
453 * error code on failure; call SDL_GetError() for more information.
454 *
455 * \since This function is available since SDL 3.0.0.
456 *
457 * \sa SDL_TellIO
458 */
459extern SDL_DECLSPEC Sint64 SDLCALL SDL_SeekIO(SDL_IOStream *context, Sint64 offset, SDL_IOWhence whence);
460
461/**
462 * Determine the current read/write offset in an SDL_IOStream data stream.
463 *
464 * SDL_TellIO is actually a wrapper function that calls the SDL_IOStream's
465 * `seek` method, with an offset of 0 bytes from `SDL_IO_SEEK_CUR`, to
466 * simplify application development.
467 *
468 * \param context an SDL_IOStream data stream object from which to get the
469 * current offset.
470 * \returns the current offset in the stream, or -1 if the information can not
471 * be determined.
472 *
473 * \since This function is available since SDL 3.0.0.
474 *
475 * \sa SDL_SeekIO
476 */
477extern SDL_DECLSPEC Sint64 SDLCALL SDL_TellIO(SDL_IOStream *context);
478
479/**
480 * Read from a data source.
481 *
482 * This function reads up `size` bytes from the data source to the area
483 * pointed at by `ptr`. This function may read less bytes than requested. It
484 * will return zero when the data stream is completely read, or on error. To
485 * determine if there was an error or all data was read, call
486 * SDL_GetIOStatus().
487 *
488 * \param context a pointer to an SDL_IOStream structure.
489 * \param ptr a pointer to a buffer to read data into.
490 * \param size the number of bytes to read from the data source.
491 * \returns the number of bytes read, or 0 on end of file or other failure;
492 * call SDL_GetError() for more information.
493 *
494 * \since This function is available since SDL 3.0.0.
495 *
496 * \sa SDL_WriteIO
497 * \sa SDL_GetIOStatus
498 */
499extern SDL_DECLSPEC size_t SDLCALL SDL_ReadIO(SDL_IOStream *context, void *ptr, size_t size);
500
501/**
502 * Write to an SDL_IOStream data stream.
503 *
504 * This function writes exactly `size` bytes from the area pointed at by `ptr`
505 * to the stream. If this fails for any reason, it'll return less than `size`
506 * to demonstrate how far the write progressed. On success, it returns `size`.
507 *
508 * On error, this function still attempts to write as much as possible, so it
509 * might return a positive value less than the requested write size.
510 *
511 * The caller can use SDL_GetIOStatus() to determine if the problem is
512 * recoverable, such as a non-blocking write that can simply be retried later,
513 * or a fatal error.
514 *
515 * \param context a pointer to an SDL_IOStream structure.
516 * \param ptr a pointer to a buffer containing data to write.
517 * \param size the number of bytes to write.
518 * \returns the number of bytes written, which will be less than `size` on
519 * failure; call SDL_GetError() for more information.
520 *
521 * \since This function is available since SDL 3.0.0.
522 *
523 * \sa SDL_IOprintf
524 * \sa SDL_ReadIO
525 * \sa SDL_SeekIO
526 * \sa SDL_GetIOStatus
527 */
528extern SDL_DECLSPEC size_t SDLCALL SDL_WriteIO(SDL_IOStream *context, const void *ptr, size_t size);
529
530/**
531 * Print to an SDL_IOStream data stream.
532 *
533 * This function does formatted printing to the stream.
534 *
535 * \param context a pointer to an SDL_IOStream structure.
536 * \param fmt a printf() style format string.
537 * \param ... additional parameters matching % tokens in the `fmt` string, if
538 * any.
539 * \returns the number of bytes written or 0 on failure; call SDL_GetError()
540 * for more information.
541 *
542 * \since This function is available since SDL 3.0.0.
543 *
544 * \sa SDL_IOvprintf
545 * \sa SDL_WriteIO
546 */
547extern SDL_DECLSPEC size_t SDLCALL SDL_IOprintf(SDL_IOStream *context, SDL_PRINTF_FORMAT_STRING const char *fmt, ...) SDL_PRINTF_VARARG_FUNC(2);
548
549/**
550 * Print to an SDL_IOStream data stream.
551 *
552 * This function does formatted printing to the stream.
553 *
554 * \param context a pointer to an SDL_IOStream structure.
555 * \param fmt a printf() style format string.
556 * \param ap a variable argument list.
557 * \returns the number of bytes written or 0 on failure; call SDL_GetError()
558 * for more information.
559 *
560 * \since This function is available since SDL 3.0.0.
561 *
562 * \sa SDL_IOprintf
563 * \sa SDL_WriteIO
564 */
565extern SDL_DECLSPEC size_t SDLCALL SDL_IOvprintf(SDL_IOStream *context, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(2);
566
567/**
568 * Load all the data from an SDL data stream.
569 *
570 * The data is allocated with a zero byte at the end (null terminated) for
571 * convenience. This extra byte is not included in the value reported via
572 * `datasize`.
573 *
574 * The data should be freed with SDL_free().
575 *
576 * \param src the SDL_IOStream to read all available data from.
577 * \param datasize if not NULL, will store the number of bytes read.
578 * \param closeio if SDL_TRUE, calls SDL_CloseIO() on `src` before returning,
579 * even in the case of an error.
580 * \returns the data or NULL on failure; call SDL_GetError() for more
581 * information.
582 *
583 * \since This function is available since SDL 3.0.0.
584 *
585 * \sa SDL_LoadFile
586 */
587extern SDL_DECLSPEC void * SDLCALL SDL_LoadFile_IO(SDL_IOStream *src, size_t *datasize, SDL_bool closeio);
588
589/**
590 * Load all the data from a file path.
591 *
592 * The data is allocated with a zero byte at the end (null terminated) for
593 * convenience. This extra byte is not included in the value reported via
594 * `datasize`.
595 *
596 * The data should be freed with SDL_free().
597 *
598 * \param file the path to read all available data from.
599 * \param datasize if not NULL, will store the number of bytes read.
600 * \returns the data or NULL on failure; call SDL_GetError() for more
601 * information.
602 *
603 * \since This function is available since SDL 3.0.0.
604 *
605 * \sa SDL_LoadFile_IO
606 */
607extern SDL_DECLSPEC void * SDLCALL SDL_LoadFile(const char *file, size_t *datasize);
608
609/**
610 * \name Read endian functions
611 *
612 * Read an item of the specified endianness and return in native format.
613 */
614/* @{ */
615
616/**
617 * Use this function to read a byte from an SDL_IOStream.
618 *
619 * \param src the SDL_IOStream to read from.
620 * \param value a pointer filled in with the data read.
621 * \returns SDL_TRUE on success or SDL_FALSE on failure; call SDL_GetError()
622 * for more information.
623 *
624 * \since This function is available since SDL 3.0.0.
625 */
626extern SDL_DECLSPEC SDL_bool SDLCALL SDL_ReadU8(SDL_IOStream *src, Uint8 *value);
627
628/**
629 * Use this function to read a signed byte from an SDL_IOStream.
630 *
631 * \param src the SDL_IOStream to read from.
632 * \param value a pointer filled in with the data read.
633 * \returns SDL_TRUE on success or SDL_FALSE on failure; call SDL_GetError()
634 * for more information.
635 *
636 * \since This function is available since SDL 3.0.0.
637 */
638extern SDL_DECLSPEC SDL_bool SDLCALL SDL_ReadS8(SDL_IOStream *src, Sint8 *value);
639
640/**
641 * Use this function to read 16 bits of little-endian data from an
642 * SDL_IOStream and return in native format.
643 *
644 * SDL byteswaps the data only if necessary, so the data returned will be in
645 * the native byte order.
646 *
647 * \param src the stream from which to read data.
648 * \param value a pointer filled in with the data read.
649 * \returns SDL_TRUE on successful write or SDL_FALSE on failure; call
650 * SDL_GetError() for more information.
651 *
652 * \since This function is available since SDL 3.0.0.
653 */
654extern SDL_DECLSPEC SDL_bool SDLCALL SDL_ReadU16LE(SDL_IOStream *src, Uint16 *value);
655
656/**
657 * Use this function to read 16 bits of little-endian data from an
658 * SDL_IOStream and return in native format.
659 *
660 * SDL byteswaps the data only if necessary, so the data returned will be in
661 * the native byte order.
662 *
663 * \param src the stream from which to read data.
664 * \param value a pointer filled in with the data read.
665 * \returns SDL_TRUE on successful write or SDL_FALSE on failure; call
666 * SDL_GetError() for more information.
667 *
668 * \since This function is available since SDL 3.0.0.
669 */
670extern SDL_DECLSPEC SDL_bool SDLCALL SDL_ReadS16LE(SDL_IOStream *src, Sint16 *value);
671
672/**
673 * Use this function to read 16 bits of big-endian data from an SDL_IOStream
674 * and return in native format.
675 *
676 * SDL byteswaps the data only if necessary, so the data returned will be in
677 * the native byte order.
678 *
679 * \param src the stream from which to read data.
680 * \param value a pointer filled in with the data read.
681 * \returns SDL_TRUE on successful write or SDL_FALSE on failure; call
682 * SDL_GetError() for more information.
683 *
684 * \since This function is available since SDL 3.0.0.
685 */
686extern SDL_DECLSPEC SDL_bool SDLCALL SDL_ReadU16BE(SDL_IOStream *src, Uint16 *value);
687
688/**
689 * Use this function to read 16 bits of big-endian data from an SDL_IOStream
690 * and return in native format.
691 *
692 * SDL byteswaps the data only if necessary, so the data returned will be in
693 * the native byte order.
694 *
695 * \param src the stream from which to read data.
696 * \param value a pointer filled in with the data read.
697 * \returns SDL_TRUE on successful write or SDL_FALSE on failure; call
698 * SDL_GetError() for more information.
699 *
700 * \since This function is available since SDL 3.0.0.
701 */
702extern SDL_DECLSPEC SDL_bool SDLCALL SDL_ReadS16BE(SDL_IOStream *src, Sint16 *value);
703
704/**
705 * Use this function to read 32 bits of little-endian data from an
706 * SDL_IOStream and return in native format.
707 *
708 * SDL byteswaps the data only if necessary, so the data returned will be in
709 * the native byte order.
710 *
711 * \param src the stream from which to read data.
712 * \param value a pointer filled in with the data read.
713 * \returns SDL_TRUE on successful write or SDL_FALSE on failure; call
714 * SDL_GetError() for more information.
715 *
716 * \since This function is available since SDL 3.0.0.
717 */
718extern SDL_DECLSPEC SDL_bool SDLCALL SDL_ReadU32LE(SDL_IOStream *src, Uint32 *value);
719
720/**
721 * Use this function to read 32 bits of little-endian data from an
722 * SDL_IOStream and return in native format.
723 *
724 * SDL byteswaps the data only if necessary, so the data returned will be in
725 * the native byte order.
726 *
727 * \param src the stream from which to read data.
728 * \param value a pointer filled in with the data read.
729 * \returns SDL_TRUE on successful write or SDL_FALSE on failure; call
730 * SDL_GetError() for more information.
731 *
732 * \since This function is available since SDL 3.0.0.
733 */
734extern SDL_DECLSPEC SDL_bool SDLCALL SDL_ReadS32LE(SDL_IOStream *src, Sint32 *value);
735
736/**
737 * Use this function to read 32 bits of big-endian data from an SDL_IOStream
738 * and return in native format.
739 *
740 * SDL byteswaps the data only if necessary, so the data returned will be in
741 * the native byte order.
742 *
743 * \param src the stream from which to read data.
744 * \param value a pointer filled in with the data read.
745 * \returns SDL_TRUE on successful write or SDL_FALSE on failure; call
746 * SDL_GetError() for more information.
747 *
748 * \since This function is available since SDL 3.0.0.
749 */
750extern SDL_DECLSPEC SDL_bool SDLCALL SDL_ReadU32BE(SDL_IOStream *src, Uint32 *value);
751
752/**
753 * Use this function to read 32 bits of big-endian data from an SDL_IOStream
754 * and return in native format.
755 *
756 * SDL byteswaps the data only if necessary, so the data returned will be in
757 * the native byte order.
758 *
759 * \param src the stream from which to read data.
760 * \param value a pointer filled in with the data read.
761 * \returns SDL_TRUE on successful write or SDL_FALSE on failure; call
762 * SDL_GetError() for more information.
763 *
764 * \since This function is available since SDL 3.0.0.
765 */
766extern SDL_DECLSPEC SDL_bool SDLCALL SDL_ReadS32BE(SDL_IOStream *src, Sint32 *value);
767
768/**
769 * Use this function to read 64 bits of little-endian data from an
770 * SDL_IOStream and return in native format.
771 *
772 * SDL byteswaps the data only if necessary, so the data returned will be in
773 * the native byte order.
774 *
775 * \param src the stream from which to read data.
776 * \param value a pointer filled in with the data read.
777 * \returns SDL_TRUE on successful write or SDL_FALSE on failure; call
778 * SDL_GetError() for more information.
779 *
780 * \since This function is available since SDL 3.0.0.
781 */
782extern SDL_DECLSPEC SDL_bool SDLCALL SDL_ReadU64LE(SDL_IOStream *src, Uint64 *value);
783
784/**
785 * Use this function to read 64 bits of little-endian data from an
786 * SDL_IOStream and return in native format.
787 *
788 * SDL byteswaps the data only if necessary, so the data returned will be in
789 * the native byte order.
790 *
791 * \param src the stream from which to read data.
792 * \param value a pointer filled in with the data read.
793 * \returns SDL_TRUE on successful write or SDL_FALSE on failure; call
794 * SDL_GetError() for more information.
795 *
796 * \since This function is available since SDL 3.0.0.
797 */
798extern SDL_DECLSPEC SDL_bool SDLCALL SDL_ReadS64LE(SDL_IOStream *src, Sint64 *value);
799
800/**
801 * Use this function to read 64 bits of big-endian data from an SDL_IOStream
802 * and return in native format.
803 *
804 * SDL byteswaps the data only if necessary, so the data returned will be in
805 * the native byte order.
806 *
807 * \param src the stream from which to read data.
808 * \param value a pointer filled in with the data read.
809 * \returns SDL_TRUE on successful write or SDL_FALSE on failure; call
810 * SDL_GetError() for more information.
811 *
812 * \since This function is available since SDL 3.0.0.
813 */
814extern SDL_DECLSPEC SDL_bool SDLCALL SDL_ReadU64BE(SDL_IOStream *src, Uint64 *value);
815
816/**
817 * Use this function to read 64 bits of big-endian data from an SDL_IOStream
818 * and return in native format.
819 *
820 * SDL byteswaps the data only if necessary, so the data returned will be in
821 * the native byte order.
822 *
823 * \param src the stream from which to read data.
824 * \param value a pointer filled in with the data read.
825 * \returns SDL_TRUE on successful write or SDL_FALSE on failure; call
826 * SDL_GetError() for more information.
827 *
828 * \since This function is available since SDL 3.0.0.
829 */
830extern SDL_DECLSPEC SDL_bool SDLCALL SDL_ReadS64BE(SDL_IOStream *src, Sint64 *value);
831/* @} *//* Read endian functions */
832
833/**
834 * \name Write endian functions
835 *
836 * Write an item of native format to the specified endianness.
837 */
838/* @{ */
839
840/**
841 * Use this function to write a byte to an SDL_IOStream.
842 *
843 * \param dst the SDL_IOStream to write to.
844 * \param value the byte value to write.
845 * \returns SDL_TRUE on successful write or SDL_FALSE on failure; call
846 * SDL_GetError() for more information.
847 *
848 * \since This function is available since SDL 3.0.0.
849 */
850extern SDL_DECLSPEC SDL_bool SDLCALL SDL_WriteU8(SDL_IOStream *dst, Uint8 value);
851
852/**
853 * Use this function to write a signed byte to an SDL_IOStream.
854 *
855 * \param dst the SDL_IOStream to write to.
856 * \param value the byte value to write.
857 * \returns SDL_TRUE on successful write or SDL_FALSE on failure; call
858 * SDL_GetError() for more information.
859 *
860 * \since This function is available since SDL 3.0.0.
861 */
862extern SDL_DECLSPEC SDL_bool SDLCALL SDL_WriteS8(SDL_IOStream *dst, Sint8 value);
863
864/**
865 * Use this function to write 16 bits in native format to an SDL_IOStream as
866 * little-endian data.
867 *
868 * SDL byteswaps the data only if necessary, so the application always
869 * specifies native format, and the data written will be in little-endian
870 * format.
871 *
872 * \param dst the stream to which data will be written.
873 * \param value the data to be written, in native format.
874 * \returns SDL_TRUE on successful write or SDL_FALSE on failure; call
875 * SDL_GetError() for more information.
876 *
877 * \since This function is available since SDL 3.0.0.
878 */
879extern SDL_DECLSPEC SDL_bool SDLCALL SDL_WriteU16LE(SDL_IOStream *dst, Uint16 value);
880
881/**
882 * Use this function to write 16 bits in native format to an SDL_IOStream as
883 * little-endian data.
884 *
885 * SDL byteswaps the data only if necessary, so the application always
886 * specifies native format, and the data written will be in little-endian
887 * format.
888 *
889 * \param dst the stream to which data will be written.
890 * \param value the data to be written, in native format.
891 * \returns SDL_TRUE on successful write or SDL_FALSE on failure; call
892 * SDL_GetError() for more information.
893 *
894 * \since This function is available since SDL 3.0.0.
895 */
896extern SDL_DECLSPEC SDL_bool SDLCALL SDL_WriteS16LE(SDL_IOStream *dst, Sint16 value);
897
898/**
899 * Use this function to write 16 bits in native format to an SDL_IOStream as
900 * big-endian data.
901 *
902 * SDL byteswaps the data only if necessary, so the application always
903 * specifies native format, and the data written will be in big-endian format.
904 *
905 * \param dst the stream to which data will be written.
906 * \param value the data to be written, in native format.
907 * \returns SDL_TRUE on successful write or SDL_FALSE on failure; call
908 * SDL_GetError() for more information.
909 *
910 * \since This function is available since SDL 3.0.0.
911 */
912extern SDL_DECLSPEC SDL_bool SDLCALL SDL_WriteU16BE(SDL_IOStream *dst, Uint16 value);
913
914/**
915 * Use this function to write 16 bits in native format to an SDL_IOStream as
916 * big-endian data.
917 *
918 * SDL byteswaps the data only if necessary, so the application always
919 * specifies native format, and the data written will be in big-endian format.
920 *
921 * \param dst the stream to which data will be written.
922 * \param value the data to be written, in native format.
923 * \returns SDL_TRUE on successful write or SDL_FALSE on failure; call
924 * SDL_GetError() for more information.
925 *
926 * \since This function is available since SDL 3.0.0.
927 */
928extern SDL_DECLSPEC SDL_bool SDLCALL SDL_WriteS16BE(SDL_IOStream *dst, Sint16 value);
929
930/**
931 * Use this function to write 32 bits in native format to an SDL_IOStream as
932 * little-endian data.
933 *
934 * SDL byteswaps the data only if necessary, so the application always
935 * specifies native format, and the data written will be in little-endian
936 * format.
937 *
938 * \param dst the stream to which data will be written.
939 * \param value the data to be written, in native format.
940 * \returns SDL_TRUE on successful write or SDL_FALSE on failure; call
941 * SDL_GetError() for more information.
942 *
943 * \since This function is available since SDL 3.0.0.
944 */
945extern SDL_DECLSPEC SDL_bool SDLCALL SDL_WriteU32LE(SDL_IOStream *dst, Uint32 value);
946
947/**
948 * Use this function to write 32 bits in native format to an SDL_IOStream as
949 * little-endian data.
950 *
951 * SDL byteswaps the data only if necessary, so the application always
952 * specifies native format, and the data written will be in little-endian
953 * format.
954 *
955 * \param dst the stream to which data will be written.
956 * \param value the data to be written, in native format.
957 * \returns SDL_TRUE on successful write or SDL_FALSE on failure; call
958 * SDL_GetError() for more information.
959 *
960 * \since This function is available since SDL 3.0.0.
961 */
962extern SDL_DECLSPEC SDL_bool SDLCALL SDL_WriteS32LE(SDL_IOStream *dst, Sint32 value);
963
964/**
965 * Use this function to write 32 bits in native format to an SDL_IOStream as
966 * big-endian data.
967 *
968 * SDL byteswaps the data only if necessary, so the application always
969 * specifies native format, and the data written will be in big-endian format.
970 *
971 * \param dst the stream to which data will be written.
972 * \param value the data to be written, in native format.
973 * \returns SDL_TRUE on successful write or SDL_FALSE on failure; call
974 * SDL_GetError() for more information.
975 *
976 * \since This function is available since SDL 3.0.0.
977 */
978extern SDL_DECLSPEC SDL_bool SDLCALL SDL_WriteU32BE(SDL_IOStream *dst, Uint32 value);
979
980/**
981 * Use this function to write 32 bits in native format to an SDL_IOStream as
982 * big-endian data.
983 *
984 * SDL byteswaps the data only if necessary, so the application always
985 * specifies native format, and the data written will be in big-endian format.
986 *
987 * \param dst the stream to which data will be written.
988 * \param value the data to be written, in native format.
989 * \returns SDL_TRUE on successful write or SDL_FALSE on failure; call
990 * SDL_GetError() for more information.
991 *
992 * \since This function is available since SDL 3.0.0.
993 */
994extern SDL_DECLSPEC SDL_bool SDLCALL SDL_WriteS32BE(SDL_IOStream *dst, Sint32 value);
995
996/**
997 * Use this function to write 64 bits in native format to an SDL_IOStream as
998 * little-endian data.
999 *
1000 * SDL byteswaps the data only if necessary, so the application always
1001 * specifies native format, and the data written will be in little-endian
1002 * format.
1003 *
1004 * \param dst the stream to which data will be written.
1005 * \param value the data to be written, in native format.
1006 * \returns SDL_TRUE on successful write or SDL_FALSE on failure; call
1007 * SDL_GetError() for more information.
1008 *
1009 * \since This function is available since SDL 3.0.0.
1010 */
1011extern SDL_DECLSPEC SDL_bool SDLCALL SDL_WriteU64LE(SDL_IOStream *dst, Uint64 value);
1012
1013/**
1014 * Use this function to write 64 bits in native format to an SDL_IOStream as
1015 * little-endian data.
1016 *
1017 * SDL byteswaps the data only if necessary, so the application always
1018 * specifies native format, and the data written will be in little-endian
1019 * format.
1020 *
1021 * \param dst the stream to which data will be written.
1022 * \param value the data to be written, in native format.
1023 * \returns SDL_TRUE on successful write or SDL_FALSE on failure; call
1024 * SDL_GetError() for more information.
1025 *
1026 * \since This function is available since SDL 3.0.0.
1027 */
1028extern SDL_DECLSPEC SDL_bool SDLCALL SDL_WriteS64LE(SDL_IOStream *dst, Sint64 value);
1029
1030/**
1031 * Use this function to write 64 bits in native format to an SDL_IOStream as
1032 * big-endian data.
1033 *
1034 * SDL byteswaps the data only if necessary, so the application always
1035 * specifies native format, and the data written will be in big-endian format.
1036 *
1037 * \param dst the stream to which data will be written.
1038 * \param value the data to be written, in native format.
1039 * \returns SDL_TRUE on successful write or SDL_FALSE on failure; call
1040 * SDL_GetError() for more information.
1041 *
1042 * \since This function is available since SDL 3.0.0.
1043 */
1044extern SDL_DECLSPEC SDL_bool SDLCALL SDL_WriteU64BE(SDL_IOStream *dst, Uint64 value);
1045
1046/**
1047 * Use this function to write 64 bits in native format to an SDL_IOStream as
1048 * big-endian data.
1049 *
1050 * SDL byteswaps the data only if necessary, so the application always
1051 * specifies native format, and the data written will be in big-endian format.
1052 *
1053 * \param dst the stream to which data will be written.
1054 * \param value the data to be written, in native format.
1055 * \returns SDL_TRUE on successful write or SDL_FALSE on failure; call
1056 * SDL_GetError() for more information.
1057 *
1058 * \since This function is available since SDL 3.0.0.
1059 */
1060extern SDL_DECLSPEC SDL_bool SDLCALL SDL_WriteS64BE(SDL_IOStream *dst, Sint64 value);
1061
1062/* @} *//* Write endian functions */
1063
1064/* Ends C function definitions when using C++ */
1065#ifdef __cplusplus
1066}
1067#endif
1068#include <SDL3/SDL_close_code.h>
1069
1070#endif /* SDL_iostream_h_ */
SDL_bool SDL_ReadU8(SDL_IOStream *src, Uint8 *value)
SDL_IOStream * SDL_IOFromConstMem(const void *mem, size_t size)
SDL_IOStream * SDL_OpenIO(const SDL_IOStreamInterface *iface, void *userdata)
SDL_IOStatus
@ SDL_IO_STATUS_ERROR
@ SDL_IO_STATUS_EOF
@ SDL_IO_STATUS_NOT_READY
@ SDL_IO_STATUS_READY
@ SDL_IO_STATUS_WRITEONLY
@ SDL_IO_STATUS_READONLY
SDL_bool SDL_ReadS64LE(SDL_IOStream *src, Sint64 *value)
SDL_bool SDL_ReadS32BE(SDL_IOStream *src, Sint32 *value)
SDL_bool SDL_ReadU16LE(SDL_IOStream *src, Uint16 *value)
size_t SDL_WriteIO(SDL_IOStream *context, const void *ptr, size_t size)
SDL_bool SDL_ReadS64BE(SDL_IOStream *src, Sint64 *value)
Sint64 SDL_SeekIO(SDL_IOStream *context, Sint64 offset, SDL_IOWhence whence)
SDL_bool SDL_ReadU32BE(SDL_IOStream *src, Uint32 *value)
size_t SDL_IOvprintf(SDL_IOStream *context, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(2)
SDL_bool SDL_WriteU32BE(SDL_IOStream *dst, Uint32 value)
SDL_bool SDL_ReadU64LE(SDL_IOStream *src, Uint64 *value)
SDL_IOStream * SDL_IOFromFile(const char *file, const char *mode)
SDL_bool SDL_ReadS16LE(SDL_IOStream *src, Sint16 *value)
Sint64 SDL_TellIO(SDL_IOStream *context)
SDL_bool SDL_WriteS32LE(SDL_IOStream *dst, Sint32 value)
SDL_bool SDL_WriteS64BE(SDL_IOStream *dst, Sint64 value)
size_t SDL_IOprintf(SDL_IOStream *context, SDL_PRINTF_FORMAT_STRING const char *fmt,...) SDL_PRINTF_VARARG_FUNC(2)
void * SDL_LoadFile_IO(SDL_IOStream *src, size_t *datasize, SDL_bool closeio)
SDL_bool SDL_WriteU32LE(SDL_IOStream *dst, Uint32 value)
SDL_bool SDL_WriteS64LE(SDL_IOStream *dst, Sint64 value)
SDL_bool SDL_WriteS16BE(SDL_IOStream *dst, Sint16 value)
SDL_bool SDL_WriteS32BE(SDL_IOStream *dst, Sint32 value)
SDL_bool SDL_ReadU16BE(SDL_IOStream *src, Uint16 *value)
SDL_bool SDL_ReadU32LE(SDL_IOStream *src, Uint32 *value)
SDL_IOStream * SDL_IOFromDynamicMem(void)
SDL_bool SDL_WriteU64LE(SDL_IOStream *dst, Uint64 value)
SDL_bool SDL_ReadU64BE(SDL_IOStream *src, Uint64 *value)
struct SDL_IOStream SDL_IOStream
SDL_bool SDL_ReadS8(SDL_IOStream *src, Sint8 *value)
Sint64 SDL_GetIOSize(SDL_IOStream *context)
SDL_bool SDL_WriteU16LE(SDL_IOStream *dst, Uint16 value)
SDL_bool SDL_WriteU16BE(SDL_IOStream *dst, Uint16 value)
SDL_bool SDL_ReadS32LE(SDL_IOStream *src, Sint32 *value)
SDL_bool SDL_WriteS8(SDL_IOStream *dst, Sint8 value)
SDL_bool SDL_WriteU8(SDL_IOStream *dst, Uint8 value)
SDL_bool SDL_WriteU64BE(SDL_IOStream *dst, Uint64 value)
SDL_IOStatus SDL_GetIOStatus(SDL_IOStream *context)
SDL_IOStream * SDL_IOFromMem(void *mem, size_t size)
SDL_bool SDL_WriteS16LE(SDL_IOStream *dst, Sint16 value)
SDL_PropertiesID SDL_GetIOProperties(SDL_IOStream *context)
SDL_bool SDL_ReadS16BE(SDL_IOStream *src, Sint16 *value)
size_t SDL_ReadIO(SDL_IOStream *context, void *ptr, size_t size)
int SDL_CloseIO(SDL_IOStream *context)
void * SDL_LoadFile(const char *file, size_t *datasize)
SDL_IOWhence
@ SDL_IO_SEEK_CUR
@ SDL_IO_SEEK_SET
@ SDL_IO_SEEK_END
Uint32 SDL_PropertiesID
uint8_t Uint8
Definition SDL_stdinc.h:229
int64_t Sint64
Definition SDL_stdinc.h:276
uint16_t Uint16
Definition SDL_stdinc.h:247
#define SDL_PRINTF_VARARG_FUNCV(fmtargnumber)
Definition SDL_stdinc.h:456
int8_t Sint8
Definition SDL_stdinc.h:220
int32_t Sint32
Definition SDL_stdinc.h:256
SDL_MALLOC size_t size
Definition SDL_stdinc.h:531
int SDL_bool
Definition SDL_stdinc.h:213
int16_t Sint16
Definition SDL_stdinc.h:238
#define SDL_PRINTF_FORMAT_STRING
Definition SDL_stdinc.h:444
#define SDL_PRINTF_VARARG_FUNC(fmtargnumber)
Definition SDL_stdinc.h:455
uint64_t Uint64
Definition SDL_stdinc.h:287
uint32_t Uint32
Definition SDL_stdinc.h:265
Sint64(* size)(void *userdata)
size_t(* read)(void *userdata, void *ptr, size_t size, SDL_IOStatus *status)
Sint64(* seek)(void *userdata, Sint64 offset, SDL_IOWhence whence)
size_t(* write)(void *userdata, const void *ptr, size_t size, SDL_IOStatus *status)
int(* close)(void *userdata)