2 Simple DirectMedia Layer
3 Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
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.
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:
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.
25 * This file provides a general interface for SDL to read and write
26 * data streams. It can easily be extended to files, memory, etc.
32 #include "SDL_stdinc.h"
33 #include "SDL_error.h"
35 #include "begin_code.h"
36 /* Set up for C function definitions, even when using C++ */
42 #define SDL_RWOPS_UNKNOWN 0U /**< Unknown stream type */
43 #define SDL_RWOPS_WINFILE 1U /**< Win32 file */
44 #define SDL_RWOPS_STDFILE 2U /**< Stdio file */
45 #define SDL_RWOPS_JNIFILE 3U /**< Android asset */
46 #define SDL_RWOPS_MEMORY 4U /**< Memory stream */
47 #define SDL_RWOPS_MEMORY_RO 5U /**< Read-Only memory stream */
49 #define SDL_RWOPS_VITAFILE 6U /**< Vita file */
53 * This is the read/write operation structure -- very basic.
55 typedef struct SDL_RWops
58 * Return the size of the file in this rwops, or -1 if unknown
60 Sint64 (SDLCALL * size) (struct SDL_RWops * context);
63 * Seek to \c offset relative to \c whence, one of stdio's whence values:
64 * RW_SEEK_SET, RW_SEEK_CUR, RW_SEEK_END
66 * \return the final offset in the data stream, or -1 on error.
68 Sint64 (SDLCALL * seek) (struct SDL_RWops * context, Sint64 offset,
72 * Read up to \c maxnum objects each of size \c size from the data
73 * stream to the area pointed at by \c ptr.
75 * \return the number of objects read, or 0 at error or end of file.
77 size_t (SDLCALL * read) (struct SDL_RWops * context, void *ptr,
78 size_t size, size_t maxnum);
81 * Write exactly \c num objects each of size \c size from the area
82 * pointed at by \c ptr to data stream.
84 * \return the number of objects written, or 0 at error or end of file.
86 size_t (SDLCALL * write) (struct SDL_RWops * context, const void *ptr,
87 size_t size, size_t num);
90 * Close and free an allocated SDL_RWops structure.
92 * \return 0 if successful or -1 on write error when flushing data.
94 int (SDLCALL * close) (struct SDL_RWops * context);
99 #if defined(__ANDROID__)
104 #elif defined(__WIN32__)
116 #elif defined(__VITA__)
153 * \name RWFrom functions
155 * Functions to create SDL_RWops structures from various data streams.
160 * Use this function to create a new SDL_RWops structure for reading from
161 * and/or writing to a named file.
163 * The `mode` string is treated roughly the same as in a call to the C
164 * library's fopen(), even if SDL doesn't happen to use fopen() behind the
167 * Available `mode` strings:
169 * - "r": Open a file for reading. The file must exist.
170 * - "w": Create an empty file for writing. If a file with the same name
171 * already exists its content is erased and the file is treated as a new
173 * - "a": Append to a file. Writing operations append data at the end of the
174 * file. The file is created if it does not exist.
175 * - "r+": Open a file for update both reading and writing. The file must
177 * - "w+": Create an empty file for both reading and writing. If a file with
178 * the same name already exists its content is erased and the file is
179 * treated as a new empty file.
180 * - "a+": Open a file for reading and appending. All writing operations are
181 * performed at the end of the file, protecting the previous content to be
182 * overwritten. You can reposition (fseek, rewind) the internal pointer to
183 * anywhere in the file for reading, but writing operations will move it
184 * back to the end of file. The file is created if it does not exist.
186 * **NOTE**: In order to open a file as a binary file, a "b" character has to
187 * be included in the `mode` string. This additional "b" character can either
188 * be appended at the end of the string (thus making the following compound
189 * modes: "rb", "wb", "ab", "r+b", "w+b", "a+b") or be inserted between the
190 * letter and the "+" sign for the mixed modes ("rb+", "wb+", "ab+").
191 * Additional characters may follow the sequence, although they should have no
192 * effect. For example, "t" is sometimes appended to make explicit the file is
195 * This function supports Unicode filenames, but they must be encoded in UTF-8
196 * format, regardless of the underlying operating system.
198 * As a fallback, SDL_RWFromFile() will transparently open a matching filename
199 * in an Android app's `assets`.
201 * Closing the SDL_RWops will close the file handle SDL is holding internally.
203 * \param file a UTF-8 string representing the filename to open
204 * \param mode an ASCII string representing the mode to be used for opening
206 * \returns a pointer to the SDL_RWops structure that is created, or NULL on
207 * failure; call SDL_GetError() for more information.
209 * \since This function is available since SDL 2.0.0.
212 * \sa SDL_RWFromConstMem
220 extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromFile(const char *file,
225 extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromFP(FILE * fp, SDL_bool autoclose);
230 * Use this function to create an SDL_RWops structure from a standard I/O file
231 * pointer (stdio.h's `FILE*`).
233 * This function is not available on Windows, since files opened in an
234 * application on that platform cannot be used by a dynamically linked
237 * On some platforms, the first parameter is a `void*`, on others, it's a
238 * `FILE*`, depending on what system headers are available to SDL. It is
239 * always intended to be the `FILE*` type from the C runtime's stdio.h.
241 * \param fp the `FILE*` that feeds the SDL_RWops stream
242 * \param autoclose SDL_TRUE to close the `FILE*` when closing the SDL_RWops,
243 * SDL_FALSE to leave the `FILE*` open when the RWops is
245 * \returns a pointer to the SDL_RWops structure that is created, or NULL on
246 * failure; call SDL_GetError() for more information.
248 * \since This function is available since SDL 2.0.0.
251 * \sa SDL_RWFromConstMem
259 extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromFP(void * fp,
264 * Use this function to prepare a read-write memory buffer for use with
267 * This function sets up an SDL_RWops struct based on a memory area of a
268 * certain size, for both read and write access.
270 * This memory buffer is not copied by the RWops; the pointer you provide must
271 * remain valid until you close the stream. Closing the stream will not free
272 * the original buffer.
274 * If you need to make sure the RWops never writes to the memory buffer, you
275 * should use SDL_RWFromConstMem() with a read-only buffer of memory instead.
277 * \param mem a pointer to a buffer to feed an SDL_RWops stream
278 * \param size the buffer size, in bytes
279 * \returns a pointer to a new SDL_RWops structure, or NULL if it fails; call
280 * SDL_GetError() for more information.
282 * \since This function is available since SDL 2.0.0.
285 * \sa SDL_RWFromConstMem
294 extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromMem(void *mem, int size);
297 * Use this function to prepare a read-only memory buffer for use with RWops.
299 * This function sets up an SDL_RWops struct based on a memory area of a
300 * certain size. It assumes the memory area is not writable.
302 * Attempting to write to this RWops stream will report an error without
303 * writing to the memory buffer.
305 * This memory buffer is not copied by the RWops; the pointer you provide must
306 * remain valid until you close the stream. Closing the stream will not free
307 * the original buffer.
309 * If you need to write to a memory buffer, you should use SDL_RWFromMem()
310 * with a writable buffer of memory instead.
312 * \param mem a pointer to a read-only buffer to feed an SDL_RWops stream
313 * \param size the buffer size, in bytes
314 * \returns a pointer to a new SDL_RWops structure, or NULL if it fails; call
315 * SDL_GetError() for more information.
317 * \since This function is available since SDL 2.0.0.
320 * \sa SDL_RWFromConstMem
328 extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromConstMem(const void *mem,
331 /* @} *//* RWFrom functions */
335 * Use this function to allocate an empty, unpopulated SDL_RWops structure.
337 * Applications do not need to use this function unless they are providing
338 * their own SDL_RWops implementation. If you just need a SDL_RWops to
339 * read/write a common data source, you should use the built-in
340 * implementations in SDL, like SDL_RWFromFile() or SDL_RWFromMem(), etc.
342 * You must free the returned pointer with SDL_FreeRW(). Depending on your
343 * operating system and compiler, there may be a difference between the
344 * malloc() and free() your program uses and the versions SDL calls
345 * internally. Trying to mix the two can cause crashing such as segmentation
346 * faults. Since all SDL_RWops must free themselves when their **close**
347 * method is called, all SDL_RWops must be allocated through this function, so
348 * they can all be freed correctly with SDL_FreeRW().
350 * \returns a pointer to the allocated memory on success, or NULL on failure;
351 * call SDL_GetError() for more information.
353 * \since This function is available since SDL 2.0.0.
357 extern DECLSPEC SDL_RWops *SDLCALL SDL_AllocRW(void);
360 * Use this function to free an SDL_RWops structure allocated by
363 * Applications do not need to use this function unless they are providing
364 * their own SDL_RWops implementation. If you just need a SDL_RWops to
365 * read/write a common data source, you should use the built-in
366 * implementations in SDL, like SDL_RWFromFile() or SDL_RWFromMem(), etc, and
367 * call the **close** method on those SDL_RWops pointers when you are done
370 * Only use SDL_FreeRW() on pointers returned by SDL_AllocRW(). The pointer is
371 * invalid as soon as this function returns. Any extra memory allocated during
372 * creation of the SDL_RWops is not freed by SDL_FreeRW(); the programmer must
373 * be responsible for managing that memory in their **close** method.
375 * \param area the SDL_RWops structure to be freed
377 * \since This function is available since SDL 2.0.0.
381 extern DECLSPEC void SDLCALL SDL_FreeRW(SDL_RWops * area);
383 #define RW_SEEK_SET 0 /**< Seek from the beginning of data */
384 #define RW_SEEK_CUR 1 /**< Seek relative to current read point */
385 #define RW_SEEK_END 2 /**< Seek relative to the end of data */
388 * Use this function to get the size of the data stream in an SDL_RWops.
390 * Prior to SDL 2.0.10, this function was a macro.
392 * \param context the SDL_RWops to get the size of the data stream from
393 * \returns the size of the data stream in the SDL_RWops on success, -1 if
394 * unknown or a negative error code on failure; call SDL_GetError()
395 * for more information.
397 * \since This function is available since SDL 2.0.10.
399 extern DECLSPEC Sint64 SDLCALL SDL_RWsize(SDL_RWops *context);
402 * Seek within an SDL_RWops data stream.
404 * This function seeks to byte `offset`, relative to `whence`.
406 * `whence` may be any of the following values:
408 * - `RW_SEEK_SET`: seek from the beginning of data
409 * - `RW_SEEK_CUR`: seek relative to current read point
410 * - `RW_SEEK_END`: seek relative to the end of data
412 * If this stream can not seek, it will return -1.
414 * SDL_RWseek() is actually a wrapper function that calls the SDL_RWops's
415 * `seek` method appropriately, to simplify application development.
417 * Prior to SDL 2.0.10, this function was a macro.
419 * \param context a pointer to an SDL_RWops structure
420 * \param offset an offset in bytes, relative to **whence** location; can be
422 * \param whence any of `RW_SEEK_SET`, `RW_SEEK_CUR`, `RW_SEEK_END`
423 * \returns the final offset in the data stream after the seek or -1 on error.
425 * \since This function is available since SDL 2.0.10.
428 * \sa SDL_RWFromConstMem
436 extern DECLSPEC Sint64 SDLCALL SDL_RWseek(SDL_RWops *context,
437 Sint64 offset, int whence);
440 * Determine the current read/write offset in an SDL_RWops data stream.
442 * SDL_RWtell is actually a wrapper function that calls the SDL_RWops's `seek`
443 * method, with an offset of 0 bytes from `RW_SEEK_CUR`, to simplify
444 * application development.
446 * Prior to SDL 2.0.10, this function was a macro.
448 * \param context a SDL_RWops data stream object from which to get the current
450 * \returns the current offset in the stream, or -1 if the information can not
453 * \since This function is available since SDL 2.0.10.
456 * \sa SDL_RWFromConstMem
464 extern DECLSPEC Sint64 SDLCALL SDL_RWtell(SDL_RWops *context);
467 * Read from a data source.
469 * This function reads up to `maxnum` objects each of size `size` from the
470 * data source to the area pointed at by `ptr`. This function may read less
471 * objects than requested. It will return zero when there has been an error or
472 * the data stream is completely read.
474 * SDL_RWread() is actually a function wrapper that calls the SDL_RWops's
475 * `read` method appropriately, to simplify application development.
477 * Prior to SDL 2.0.10, this function was a macro.
479 * \param context a pointer to an SDL_RWops structure
480 * \param ptr a pointer to a buffer to read data into
481 * \param size the size of each object to read, in bytes
482 * \param maxnum the maximum number of objects to be read
483 * \returns the number of objects read, or 0 at error or end of file; call
484 * SDL_GetError() for more information.
486 * \since This function is available since SDL 2.0.10.
489 * \sa SDL_RWFromConstMem
496 extern DECLSPEC size_t SDLCALL SDL_RWread(SDL_RWops *context,
497 void *ptr, size_t size,
501 * Write to an SDL_RWops data stream.
503 * This function writes exactly `num` objects each of size `size` from the
504 * area pointed at by `ptr` to the stream. If this fails for any reason, it'll
505 * return less than `num` to demonstrate how far the write progressed. On
506 * success, it returns `num`.
508 * SDL_RWwrite is actually a function wrapper that calls the SDL_RWops's
509 * `write` method appropriately, to simplify application development.
511 * Prior to SDL 2.0.10, this function was a macro.
513 * \param context a pointer to an SDL_RWops structure
514 * \param ptr a pointer to a buffer containing data to write
515 * \param size the size of an object to write, in bytes
516 * \param num the number of objects to write
517 * \returns the number of objects written, which will be less than **num** on
518 * error; call SDL_GetError() for more information.
520 * \since This function is available since SDL 2.0.10.
523 * \sa SDL_RWFromConstMem
530 extern DECLSPEC size_t SDLCALL SDL_RWwrite(SDL_RWops *context,
531 const void *ptr, size_t size,
535 * Close and free an allocated SDL_RWops structure.
537 * SDL_RWclose() closes and cleans up the SDL_RWops stream. It releases any
538 * resources used by the stream and frees the SDL_RWops itself with
539 * SDL_FreeRW(). This returns 0 on success, or -1 if the stream failed to
540 * flush to its output (e.g. to disk).
542 * Note that if this fails to flush the stream to disk, this function reports
543 * an error, but the SDL_RWops is still invalid once this function returns.
545 * Prior to SDL 2.0.10, this function was a macro.
547 * \param context SDL_RWops structure to close
548 * \returns 0 on success or a negative error code on failure; call
549 * SDL_GetError() for more information.
551 * \since This function is available since SDL 2.0.10.
553 * \sa SDL_RWFromConstMem
561 extern DECLSPEC int SDLCALL SDL_RWclose(SDL_RWops *context);
564 * Load all the data from an SDL data stream.
566 * The data is allocated with a zero byte at the end (null terminated) for
567 * convenience. This extra byte is not included in the value reported via
570 * The data should be freed with SDL_free().
572 * \param src the SDL_RWops to read all available data from
573 * \param datasize if not NULL, will store the number of bytes read
574 * \param freesrc if non-zero, calls SDL_RWclose() on `src` before returning
575 * \returns the data, or NULL if there was an error.
577 * \since This function is available since SDL 2.0.6.
579 extern DECLSPEC void *SDLCALL SDL_LoadFile_RW(SDL_RWops *src,
584 * Load all the data from a file path.
586 * The data is allocated with a zero byte at the end (null terminated) for
587 * convenience. This extra byte is not included in the value reported via
590 * The data should be freed with SDL_free().
592 * Prior to SDL 2.0.10, this function was a macro wrapping around
595 * \param file the path to read all available data from
596 * \param datasize if not NULL, will store the number of bytes read
597 * \returns the data, or NULL if there was an error.
599 * \since This function is available since SDL 2.0.10.
601 extern DECLSPEC void *SDLCALL SDL_LoadFile(const char *file, size_t *datasize);
604 * \name Read endian functions
606 * Read an item of the specified endianness and return in native format.
611 * Use this function to read a byte from an SDL_RWops.
613 * \param src the SDL_RWops to read from
614 * \returns the read byte on success or 0 on failure; call SDL_GetError() for
617 * \since This function is available since SDL 2.0.0.
621 extern DECLSPEC Uint8 SDLCALL SDL_ReadU8(SDL_RWops * src);
624 * Use this function to read 16 bits of little-endian data from an SDL_RWops
625 * and return in native format.
627 * SDL byteswaps the data only if necessary, so the data returned will be in
628 * the native byte order.
630 * \param src the stream from which to read data
631 * \returns 16 bits of data in the native byte order of the platform.
633 * \since This function is available since SDL 2.0.0.
637 extern DECLSPEC Uint16 SDLCALL SDL_ReadLE16(SDL_RWops * src);
640 * Use this function to read 16 bits of big-endian data from an SDL_RWops and
641 * return in native format.
643 * SDL byteswaps the data only if necessary, so the data returned will be in
644 * the native byte order.
646 * \param src the stream from which to read data
647 * \returns 16 bits of data in the native byte order of the platform.
649 * \since This function is available since SDL 2.0.0.
653 extern DECLSPEC Uint16 SDLCALL SDL_ReadBE16(SDL_RWops * src);
656 * Use this function to read 32 bits of little-endian data from an SDL_RWops
657 * and return in native format.
659 * SDL byteswaps the data only if necessary, so the data returned will be in
660 * the native byte order.
662 * \param src the stream from which to read data
663 * \returns 32 bits of data in the native byte order of the platform.
665 * \since This function is available since SDL 2.0.0.
669 extern DECLSPEC Uint32 SDLCALL SDL_ReadLE32(SDL_RWops * src);
672 * Use this function to read 32 bits of big-endian data from an SDL_RWops and
673 * return in native format.
675 * SDL byteswaps the data only if necessary, so the data returned will be in
676 * the native byte order.
678 * \param src the stream from which to read data
679 * \returns 32 bits of data in the native byte order of the platform.
681 * \since This function is available since SDL 2.0.0.
685 extern DECLSPEC Uint32 SDLCALL SDL_ReadBE32(SDL_RWops * src);
688 * Use this function to read 64 bits of little-endian data from an SDL_RWops
689 * and return in native format.
691 * SDL byteswaps the data only if necessary, so the data returned will be in
692 * the native byte order.
694 * \param src the stream from which to read data
695 * \returns 64 bits of data in the native byte order of the platform.
697 * \since This function is available since SDL 2.0.0.
701 extern DECLSPEC Uint64 SDLCALL SDL_ReadLE64(SDL_RWops * src);
704 * Use this function to read 64 bits of big-endian data from an SDL_RWops and
705 * return in native format.
707 * SDL byteswaps the data only if necessary, so the data returned will be in
708 * the native byte order.
710 * \param src the stream from which to read data
711 * \returns 64 bits of data in the native byte order of the platform.
713 * \since This function is available since SDL 2.0.0.
717 extern DECLSPEC Uint64 SDLCALL SDL_ReadBE64(SDL_RWops * src);
718 /* @} *//* Read endian functions */
721 * \name Write endian functions
723 * Write an item of native format to the specified endianness.
728 * Use this function to write a byte to an SDL_RWops.
730 * \param dst the SDL_RWops to write to
731 * \param value the byte value to write
732 * \returns 1 on success or 0 on failure; call SDL_GetError() for more
735 * \since This function is available since SDL 2.0.0.
739 extern DECLSPEC size_t SDLCALL SDL_WriteU8(SDL_RWops * dst, Uint8 value);
742 * Use this function to write 16 bits in native format to a SDL_RWops as
743 * little-endian data.
745 * SDL byteswaps the data only if necessary, so the application always
746 * specifies native format, and the data written will be in little-endian
749 * \param dst the stream to which data will be written
750 * \param value the data to be written, in native format
751 * \returns 1 on successful write, 0 on error.
753 * \since This function is available since SDL 2.0.0.
757 extern DECLSPEC size_t SDLCALL SDL_WriteLE16(SDL_RWops * dst, Uint16 value);
760 * Use this function to write 16 bits in native format to a SDL_RWops as
763 * SDL byteswaps the data only if necessary, so the application always
764 * specifies native format, and the data written will be in big-endian format.
766 * \param dst the stream to which data will be written
767 * \param value the data to be written, in native format
768 * \returns 1 on successful write, 0 on error.
770 * \since This function is available since SDL 2.0.0.
774 extern DECLSPEC size_t SDLCALL SDL_WriteBE16(SDL_RWops * dst, Uint16 value);
777 * Use this function to write 32 bits in native format to a SDL_RWops as
778 * little-endian data.
780 * SDL byteswaps the data only if necessary, so the application always
781 * specifies native format, and the data written will be in little-endian
784 * \param dst the stream to which data will be written
785 * \param value the data to be written, in native format
786 * \returns 1 on successful write, 0 on error.
788 * \since This function is available since SDL 2.0.0.
792 extern DECLSPEC size_t SDLCALL SDL_WriteLE32(SDL_RWops * dst, Uint32 value);
795 * Use this function to write 32 bits in native format to a SDL_RWops as
798 * SDL byteswaps the data only if necessary, so the application always
799 * specifies native format, and the data written will be in big-endian format.
801 * \param dst the stream to which data will be written
802 * \param value the data to be written, in native format
803 * \returns 1 on successful write, 0 on error.
805 * \since This function is available since SDL 2.0.0.
809 extern DECLSPEC size_t SDLCALL SDL_WriteBE32(SDL_RWops * dst, Uint32 value);
812 * Use this function to write 64 bits in native format to a SDL_RWops as
813 * little-endian data.
815 * SDL byteswaps the data only if necessary, so the application always
816 * specifies native format, and the data written will be in little-endian
819 * \param dst the stream to which data will be written
820 * \param value the data to be written, in native format
821 * \returns 1 on successful write, 0 on error.
823 * \since This function is available since SDL 2.0.0.
827 extern DECLSPEC size_t SDLCALL SDL_WriteLE64(SDL_RWops * dst, Uint64 value);
830 * Use this function to write 64 bits in native format to a SDL_RWops as
833 * SDL byteswaps the data only if necessary, so the application always
834 * specifies native format, and the data written will be in big-endian format.
836 * \param dst the stream to which data will be written
837 * \param value the data to be written, in native format
838 * \returns 1 on successful write, 0 on error.
840 * \since This function is available since SDL 2.0.0.
844 extern DECLSPEC size_t SDLCALL SDL_WriteBE64(SDL_RWops * dst, Uint64 value);
845 /* @} *//* Write endian functions */
847 /* Ends C function definitions when using C++ */
851 #include "close_code.h"
853 #endif /* SDL_rwops_h_ */
855 /* vi: set ts=4 sw=4 expandtab: */