rwops: Reworked RWops for SDL3.

- SDL_RWops is now an opaque struct.
- SDL_AllocRW is gone. If an app is creating a custom RWops, they pass the
  function pointers to SDL_CreateRW(), which are stored internally.
- SDL_RWclose is gone, there is only SDL_DestroyRW(), which calls the
  implementation's `->close` method before freeing other things.
- There is only one path to create and use RWops now, so we don't have to
  worry about whether `->close` will call SDL_DestroyRW, or if this will
  risk any Properties not being released, etc.
- SDL_RWFrom* still works as expected, for getting a RWops without having
  to supply your own implementation. Objects from these functions are also
  destroyed with SDL_DestroyRW.
- Lots of other cleanup and SDL3ization of the library code.
This commit is contained in:
Ryan C. Gordon 2024-03-12 01:14:38 -04:00
parent 495e432fb9
commit 525919b315
No known key found for this signature in database
GPG key ID: FA148B892AB48044
22 changed files with 424 additions and 470 deletions

View file

@ -1156,7 +1156,11 @@ The following symbols have been renamed:
* RW_SEEK_END => SDL_RW_SEEK_END * RW_SEEK_END => SDL_RW_SEEK_END
* RW_SEEK_SET => SDL_RW_SEEK_SET * RW_SEEK_SET => SDL_RW_SEEK_SET
SDL_RWread and SDL_RWwrite (and SDL_RWops::read, SDL_RWops::write) have a different function signature in SDL3. SDL_RWops is now an opaque structure. The existing APIs to create a RWops (SDL_RWFromFile, etc) still function as expected, but to make a custom RWops with app-provided function pointers, call SDL_CreateRW and provide the function pointers through there. To call into a RWops's functionality, use the standard APIs (SDL_RWread, etc) instead of calling into function pointers directly.
The RWops function pointers are now in a separate structure called SDL_RWopsInteface, which is provided to SDL_CreateRW. All the functions now take a `void *` userdata argument for their first parameter instead of an SDL_RWops, since that's now an opaque structure.
SDL_RWread and SDL_RWwrite (and SDL_RWopsInterface::read, SDL_RWopsInterface::write) have a different function signature in SDL3.
Previously they looked more like stdio: Previously they looked more like stdio:
@ -1168,19 +1172,19 @@ size_t SDL_RWwrite(SDL_RWops *context, const void *ptr, size_t size, size_t maxn
But now they look more like POSIX: But now they look more like POSIX:
```c ```c
size_t SDL_RWread(SDL_RWops *context, void *ptr, size_t size); size_t SDL_RWread(void *userdata, void *ptr, size_t size);
size_t SDL_RWwrite(SDL_RWops *context, const void *ptr, size_t size); size_t SDL_RWwrite(void *userdata, const void *ptr, size_t size);
``` ```
Code that used to look like this: Code that used to look like this:
``` ```c
size_t custom_read(void *ptr, size_t size, size_t nitems, SDL_RWops *stream) size_t custom_read(void *ptr, size_t size, size_t nitems, SDL_RWops *stream)
{ {
return SDL_RWread(stream, ptr, size, nitems); return SDL_RWread(stream, ptr, size, nitems);
} }
``` ```
should be changed to: should be changed to:
``` ```c
size_t custom_read(void *ptr, size_t size, size_t nitems, SDL_RWops *stream) size_t custom_read(void *ptr, size_t size, size_t nitems, SDL_RWops *stream)
{ {
if (size > 0 && nitems > 0) { if (size > 0 && nitems > 0) {
@ -1190,15 +1194,28 @@ size_t custom_read(void *ptr, size_t size, size_t nitems, SDL_RWops *stream)
} }
``` ```
SDL_RWops::type was removed and has no replacement; it wasn't meaningful for app-provided implementations at all, and wasn't much use for SDL's internal implementations, either.
SDL_RWopsInterface::close implementations should clean up their own userdata, but not call SDL_DestroyRW on themselves; now the contract is always that SDL_DestroyRW is called, which calls `->close` and then frees the opaque object.
SDL_RWFromFP has been removed from the API, due to issues when the SDL library uses a different C runtime from the application. SDL_RWFromFP has been removed from the API, due to issues when the SDL library uses a different C runtime from the application.
SDL_AllocRW(), SDL_FreeRW(), SDL_RWclose() and direct access to the `->close` function pointer have been removed from the API, so there's only one path to manage RWops lifetimes now: SDL_CreateRW() and SDL_DestroyRW().
You can implement this in your own code easily: You can implement this in your own code easily:
```c ```c
#include <stdio.h> #include <stdio.h>
typedef struct RWopsStdioFPData
static Sint64 SDLCALL stdio_seek(SDL_RWops *context, Sint64 offset, int whence)
{ {
FILE *fp;
SDL_bool autoclose;
} RWopsStdioFPData;
static Sint64 SDLCALL stdio_seek(void *userdata, Sint64 offset, int whence)
{
FILE *fp = ((RWopsStdioFPData *) userdata)->fp;
int stdiowhence; int stdiowhence;
switch (whence) { switch (whence) {
@ -1215,8 +1232,8 @@ static Sint64 SDLCALL stdio_seek(SDL_RWops *context, Sint64 offset, int whence)
return SDL_SetError("Unknown value for 'whence'"); return SDL_SetError("Unknown value for 'whence'");
} }
if (fseek((FILE *)context->hidden.stdio.fp, (fseek_off_t)offset, stdiowhence) == 0) { if (fseek(fp, (fseek_off_t)offset, stdiowhence) == 0) {
Sint64 pos = ftell((FILE *)context->hidden.stdio.fp); const Sint64 pos = ftell(fp);
if (pos < 0) { if (pos < 0) {
return SDL_SetError("Couldn't get stream offset"); return SDL_SetError("Couldn't get stream offset");
} }
@ -1225,53 +1242,62 @@ static Sint64 SDLCALL stdio_seek(SDL_RWops *context, Sint64 offset, int whence)
return SDL_Error(SDL_EFSEEK); return SDL_Error(SDL_EFSEEK);
} }
static size_t SDLCALL stdio_read(SDL_RWops *context, void *ptr, size_t size) static size_t SDLCALL stdio_read(void *userdata, void *ptr, size_t size)
{ {
size_t bytes; FILE *fp = ((RWopsStdioFPData *) userdata)->fp;
const size_t bytes = fread(ptr, 1, size, fp);
bytes = fread(ptr, 1, size, (FILE *)context->hidden.stdio.fp); if (bytes == 0 && ferror(fp)) {
if (bytes == 0 && ferror((FILE *)context->hidden.stdio.fp)) {
SDL_Error(SDL_EFREAD); SDL_Error(SDL_EFREAD);
} }
return bytes; return bytes;
} }
static size_t SDLCALL stdio_write(SDL_RWops *context, const void *ptr, size_t size) static size_t SDLCALL stdio_write(void *userdata, const void *ptr, size_t size)
{ {
size_t bytes; FILE *fp = ((RWopsStdioFPData *) userdata)->fp;
const size_t bytes = fwrite(ptr, 1, size, fp);
bytes = fwrite(ptr, 1, size, (FILE *)context->hidden.stdio.fp); if (bytes == 0 && ferror(fp)) {
if (bytes == 0 && ferror((FILE *)context->hidden.stdio.fp)) {
SDL_Error(SDL_EFWRITE); SDL_Error(SDL_EFWRITE);
} }
return bytes; return bytes;
} }
static int SDLCALL stdio_close(SDL_RWops *context) static int SDLCALL stdio_close(void *userdata)
{ {
RWopsStdioData *rwopsdata = (RWopsStdioData *) userdata;
int status = 0; int status = 0;
if (context->hidden.stdio.autoclose) { if (rwopsdata->autoclose) {
if (fclose((FILE *)context->hidden.stdio.fp) != 0) { if (fclose(rwopsdata->fp) != 0) {
status = SDL_Error(SDL_EFWRITE); status = SDL_Error(SDL_EFWRITE);
} }
} }
SDL_DestroyRW(context);
return status; return status;
} }
SDL_RWops *SDL_RWFromFP(void *fp, SDL_bool autoclose) SDL_RWops *SDL_RWFromFP(FILE *fp, SDL_bool autoclose)
{ {
SDL_RWops *rwops = NULL; SDL_RWopsInterface iface;
RWopsStdioFPData *rwopsdata;
SDL_RWops *rwops;
rwops = SDL_CreateRW(); rwopsdata = (RWopsStdioFPData *) SDL_malloc(sizeof (*rwopsdata));
if (rwops != NULL) { if (!rwopsdata) {
rwops->seek = stdio_seek; return NULL;
rwops->read = stdio_read; }
rwops->write = stdio_write;
rwops->close = stdio_close; SDL_zero(iface);
rwops->hidden.stdio.fp = fp; /* There's no stdio_size because SDL_RWsize emulates it the same way we'd do it for stdio anyhow. */
rwops->hidden.stdio.autoclose = autoclose; iface.seek = stdio_seek;
rwops->type = SDL_RWOPS_STDFILE; iface.read = stdio_read;
iface.write = stdio_write;
iface.close = stdio_close;
rwopsdata->fp = fp;
rwopsdata->autoclose = autoclose;
rwops = SDL_CreateRW(&iface, rwopsdata);
if (!rwops) {
iface.close(rwopsdata);
} }
return rwops; return rwops;
} }
@ -1280,8 +1306,6 @@ SDL_RWops *SDL_RWFromFP(void *fp, SDL_bool autoclose)
The functions SDL_ReadU8(), SDL_ReadU16LE(), SDL_ReadU16BE(), SDL_ReadU32LE(), SDL_ReadU32BE(), SDL_ReadU64LE(), and SDL_ReadU64BE() now return SDL_TRUE if the read succeeded and SDL_FALSE if it didn't, and store the data in a pointer passed in as a parameter. The functions SDL_ReadU8(), SDL_ReadU16LE(), SDL_ReadU16BE(), SDL_ReadU32LE(), SDL_ReadU32BE(), SDL_ReadU64LE(), and SDL_ReadU64BE() now return SDL_TRUE if the read succeeded and SDL_FALSE if it didn't, and store the data in a pointer passed in as a parameter.
The following functions have been renamed: The following functions have been renamed:
* SDL_AllocRW() => SDL_CreateRW()
* SDL_FreeRW() => SDL_DestroyRW()
* SDL_ReadBE16() => SDL_ReadU16BE() * SDL_ReadBE16() => SDL_ReadU16BE()
* SDL_ReadBE32() => SDL_ReadU32BE() * SDL_ReadBE32() => SDL_ReadU32BE()
* SDL_ReadBE64() => SDL_ReadU64BE() * SDL_ReadBE64() => SDL_ReadU64BE()

View file

@ -1317,7 +1317,7 @@ extern DECLSPEC int SDLCALL SDL_SetAudioPostmixCallback(SDL_AudioDeviceID devid,
* ``` * ```
* *
* \param src The data source for the WAVE data * \param src The data source for the WAVE data
* \param freesrc If SDL_TRUE, calls SDL_RWclose() on `src` before returning, * \param freesrc If SDL_TRUE, calls SDL_DestroyRW() on `src` before returning,
* even in the case of an error * even in the case of an error
* \param spec A pointer to an SDL_AudioSpec that will be set to the WAVE * \param spec A pointer to an SDL_AudioSpec that will be set to the WAVE
* data's format details on successful return * data's format details on successful return

View file

@ -268,7 +268,7 @@ extern DECLSPEC int SDLCALL SDL_AddGamepadMapping(const char *mapping);
* constrained environment. * constrained environment.
* *
* \param src the data stream for the mappings to be added * \param src the data stream for the mappings to be added
* \param freesrc if SDL_TRUE, calls SDL_RWclose() on `src` before returning, * \param freesrc if SDL_TRUE, calls SDL_DestroyRW() on `src` before returning,
* even in the case of an error * even in the case of an error
* \returns the number of mappings added or -1 on error; call SDL_GetError() * \returns the number of mappings added or -1 on error; call SDL_GetError()
* for more information. * for more information.

View file

@ -39,15 +39,8 @@
extern "C" { extern "C" {
#endif #endif
/* RWops types */
#define SDL_RWOPS_UNKNOWN 0 /**< Unknown stream type */
#define SDL_RWOPS_WINFILE 1 /**< Win32 file */
#define SDL_RWOPS_STDFILE 2 /**< Stdio file */
#define SDL_RWOPS_JNIFILE 3 /**< Android asset */
#define SDL_RWOPS_MEMORY 4 /**< Memory stream */
#define SDL_RWOPS_MEMORY_RO 5 /**< Read-Only memory stream */
/* RWops status, set by a read or write operation */ /* RWops status, set by a read or write operation */
/* !!! FIXME: make this an enum? */
#define SDL_RWOPS_STATUS_READY 0 /**< Everything is ready */ #define SDL_RWOPS_STATUS_READY 0 /**< Everything is ready */
#define SDL_RWOPS_STATUS_ERROR 1 /**< Read or write I/O error */ #define SDL_RWOPS_STATUS_ERROR 1 /**< Read or write I/O error */
#define SDL_RWOPS_STATUS_EOF 2 /**< End of file */ #define SDL_RWOPS_STATUS_EOF 2 /**< End of file */
@ -55,17 +48,14 @@ extern "C" {
#define SDL_RWOPS_STATUS_READONLY 4 /**< Tried to write a read-only buffer */ #define SDL_RWOPS_STATUS_READONLY 4 /**< Tried to write a read-only buffer */
#define SDL_RWOPS_STATUS_WRITEONLY 5 /**< Tried to read a write-only buffer */ #define SDL_RWOPS_STATUS_WRITEONLY 5 /**< Tried to read a write-only buffer */
/** typedef struct SDL_RWopsInterface
* This is the read/write operation structure -- very basic.
*/
typedef struct SDL_RWops
{ {
/** /**
* Return the number of bytes in this rwops * Return the number of bytes in this rwops
* *
* \return the total size of the data stream, or -1 on error. * \return the total size of the data stream, or -1 on error.
*/ */
Sint64 (SDLCALL *size)(struct SDL_RWops *context); Sint64 (SDLCALL *size)(void *userdata);
/** /**
* Seek to \c offset relative to \c whence, one of stdio's whence values: * Seek to \c offset relative to \c whence, one of stdio's whence values:
@ -73,7 +63,7 @@ typedef struct SDL_RWops
* *
* \return the final offset in the data stream, or -1 on error. * \return the final offset in the data stream, or -1 on error.
*/ */
Sint64 (SDLCALL *seek)(struct SDL_RWops *context, Sint64 offset, int whence); Sint64 (SDLCALL *seek)(void *userdata, Sint64 offset, int whence);
/** /**
* Read up to \c size bytes from the data stream to the area pointed * Read up to \c size bytes from the data stream to the area pointed
@ -81,7 +71,7 @@ typedef struct SDL_RWops
* *
* \return the number of bytes read * \return the number of bytes read
*/ */
size_t (SDLCALL *read)(struct SDL_RWops *context, void *ptr, size_t size); size_t (SDLCALL *read)(void *userdata, void *ptr, size_t size);
/** /**
* Write exactly \c size bytes from the area pointed at by \c ptr * Write exactly \c size bytes from the area pointed at by \c ptr
@ -89,61 +79,24 @@ typedef struct SDL_RWops
* *
* \return the number of bytes written * \return the number of bytes written
*/ */
size_t (SDLCALL *write)(struct SDL_RWops *context, const void *ptr, size_t size); size_t (SDLCALL *write)(void *userdata, const void *ptr, size_t size);
/** /**
* Close and free an allocated SDL_RWops structure. * Close and free any allocated resources.
*
* The RWops is still destroyed even if this fails, so clean up anything
* even if flushing to disk returns an error.
* *
* \return 0 if successful or -1 on write error when flushing data. * \return 0 if successful or -1 on write error when flushing data.
*/ */
int (SDLCALL *close)(struct SDL_RWops *context); int (SDLCALL *close)(void *userdata);
} SDL_RWopsInterface;
Uint32 type;
Uint32 status;
SDL_PropertiesID props;
union
{
#ifdef SDL_PLATFORM_ANDROID
struct
{
void *asset;
} androidio;
#elif defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK) || defined(SDL_PLATFORM_WINRT) /**
struct * This is the read/write operation structure -- opaque, as of SDL3!
{ */
SDL_bool append; typedef struct SDL_RWops SDL_RWops;
void *h;
struct
{
void *data;
size_t size;
size_t left;
} buffer;
} windowsio;
#endif
struct
{
SDL_bool autoclose;
void *fp;
} stdio;
struct
{
Uint8 *base;
Uint8 *here;
Uint8 *stop;
} mem;
struct
{
void *data1;
void *data2;
} unknown;
} hidden;
} SDL_RWops;
/** /**
@ -195,7 +148,7 @@ typedef struct SDL_RWops
* As a fallback, SDL_RWFromFile() will transparently open a matching filename * As a fallback, SDL_RWFromFile() will transparently open a matching filename
* in an Android app's `assets`. * in an Android app's `assets`.
* *
* Closing the SDL_RWops will close the file handle SDL is holding internally. * Destroying the SDL_RWops will close the file handle SDL is holding internally.
* *
* \param file a UTF-8 string representing the filename to open * \param file a UTF-8 string representing the filename to open
* \param mode an ASCII string representing the mode to be used for opening * \param mode an ASCII string representing the mode to be used for opening
@ -205,7 +158,6 @@ typedef struct SDL_RWops
* *
* \since This function is available since SDL 3.0.0. * \since This function is available since SDL 3.0.0.
* *
* \sa SDL_RWclose
* \sa SDL_RWFromConstMem * \sa SDL_RWFromConstMem
* \sa SDL_RWFromMem * \sa SDL_RWFromMem
* \sa SDL_RWread * \sa SDL_RWread
@ -236,7 +188,6 @@ extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromFile(const char *file, const char *
* *
* \since This function is available since SDL 3.0.0. * \since This function is available since SDL 3.0.0.
* *
* \sa SDL_RWclose
* \sa SDL_RWFromConstMem * \sa SDL_RWFromConstMem
* \sa SDL_RWFromFile * \sa SDL_RWFromFile
* \sa SDL_RWFromMem * \sa SDL_RWFromMem
@ -270,7 +221,6 @@ extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromMem(void *mem, size_t size);
* *
* \since This function is available since SDL 3.0.0. * \since This function is available since SDL 3.0.0.
* *
* \sa SDL_RWclose
* \sa SDL_RWFromConstMem * \sa SDL_RWFromConstMem
* \sa SDL_RWFromFile * \sa SDL_RWFromFile
* \sa SDL_RWFromMem * \sa SDL_RWFromMem
@ -284,20 +234,14 @@ extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromConstMem(const void *mem, size_t si
/** /**
* Use this function to allocate an empty, unpopulated SDL_RWops structure. * Use this function to allocate a SDL_RWops structure.
* *
* Applications do not need to use this function unless they are providing * Applications do not need to use this function unless they are providing
* their own SDL_RWops implementation. If you just need an SDL_RWops to * their own SDL_RWops implementation. If you just need an SDL_RWops to
* read/write a common data source, you should use the built-in * read/write a common data source, you should use the built-in
* implementations in SDL, like SDL_RWFromFile() or SDL_RWFromMem(), etc. * implementations in SDL, like SDL_RWFromFile() or SDL_RWFromMem(), etc.
* *
* You must free the returned pointer with SDL_DestroyRW(). Depending on your * You must free the returned pointer with SDL_DestroyRW().
* operating system and compiler, there may be a difference between the
* malloc() and free() your program uses and the versions SDL calls
* internally. Trying to mix the two can cause crashing such as segmentation
* faults. Since all SDL_RWops must free themselves when their **close**
* method is called, all SDL_RWops must be allocated through this function, so
* they can all be freed correctly with SDL_DestroyRW().
* *
* \returns a pointer to the allocated memory on success, or NULL on failure; * \returns a pointer to the allocated memory on success, or NULL on failure;
* call SDL_GetError() for more information. * call SDL_GetError() for more information.
@ -306,32 +250,33 @@ extern DECLSPEC SDL_RWops *SDLCALL SDL_RWFromConstMem(const void *mem, size_t si
* *
* \sa SDL_DestroyRW * \sa SDL_DestroyRW
*/ */
extern DECLSPEC SDL_RWops *SDLCALL SDL_CreateRW(void); extern DECLSPEC SDL_RWops *SDLCALL SDL_CreateRW(const SDL_RWopsInterface *iface, void *userdata);
/** /**
* Use this function to free an SDL_RWops structure allocated by * Close and free an allocated SDL_RWops structure.
* SDL_CreateRW().
* *
* Applications do not need to use this function unless they are providing * SDL_DestroyRW() closes and cleans up the SDL_RWops stream. It releases any
* their own SDL_RWops implementation. If you just need an SDL_RWops to * resources used by the stream and frees the SDL_RWops itself with
* read/write a common data source, you should use the built-in * SDL_DestroyRW(). This returns 0 on success, or -1 if the stream failed to
* implementations in SDL, like SDL_RWFromFile() or SDL_RWFromMem(), etc, and * flush to its output (e.g. to disk).
* call the **close** method on those SDL_RWops pointers when you are done
* with them.
* *
* Only use SDL_DestroyRW() on pointers returned by SDL_CreateRW(). The * Note that if this fails to flush the stream to disk, this function reports
* pointer is invalid as soon as this function returns. Any extra memory * an error, but the SDL_RWops is still invalid once this function returns.
* allocated during creation of the SDL_RWops is not freed by SDL_DestroyRW();
* the programmer must be responsible for managing that memory in their
* **close** method.
* *
* \param context the SDL_RWops structure to be freed * \param context SDL_RWops structure to close
* \returns 0 on success or a negative error code on failure; call
* SDL_GetError() for more information.
* *
* \since This function is available since SDL 3.0.0. * \since This function is available since SDL 3.0.0.
* *
* \sa SDL_CreateRW * \sa SDL_RWFromConstMem
* \sa SDL_RWFromFile
* \sa SDL_RWFromMem
* \sa SDL_RWread
* \sa SDL_RWseek
* \sa SDL_RWwrite
*/ */
extern DECLSPEC void SDLCALL SDL_DestroyRW(SDL_RWops *context); extern DECLSPEC int SDLCALL SDL_DestroyRW(SDL_RWops *context);
/** /**
* Get the properties associated with an SDL_RWops. * Get the properties associated with an SDL_RWops.
@ -389,7 +334,6 @@ extern DECLSPEC Sint64 SDLCALL SDL_RWsize(SDL_RWops *context);
* *
* \since This function is available since SDL 3.0.0. * \since This function is available since SDL 3.0.0.
* *
* \sa SDL_RWclose
* \sa SDL_RWFromConstMem * \sa SDL_RWFromConstMem
* \sa SDL_RWFromFile * \sa SDL_RWFromFile
* \sa SDL_RWFromMem * \sa SDL_RWFromMem
@ -413,7 +357,6 @@ extern DECLSPEC Sint64 SDLCALL SDL_RWseek(SDL_RWops *context, Sint64 offset, int
* *
* \since This function is available since SDL 3.0.0. * \since This function is available since SDL 3.0.0.
* *
* \sa SDL_RWclose
* \sa SDL_RWFromConstMem * \sa SDL_RWFromConstMem
* \sa SDL_RWFromFile * \sa SDL_RWFromFile
* \sa SDL_RWFromMem * \sa SDL_RWFromMem
@ -444,7 +387,6 @@ extern DECLSPEC Sint64 SDLCALL SDL_RWtell(SDL_RWops *context);
* *
* \since This function is available since SDL 3.0.0. * \since This function is available since SDL 3.0.0.
* *
* \sa SDL_RWclose
* \sa SDL_RWFromConstMem * \sa SDL_RWFromConstMem
* \sa SDL_RWFromFile * \sa SDL_RWFromFile
* \sa SDL_RWFromMem * \sa SDL_RWFromMem
@ -482,7 +424,6 @@ extern DECLSPEC size_t SDLCALL SDL_RWread(SDL_RWops *context, void *ptr, size_t
* *
* \since This function is available since SDL 3.0.0. * \since This function is available since SDL 3.0.0.
* *
* \sa SDL_RWclose
* \sa SDL_RWFromConstMem * \sa SDL_RWFromConstMem
* \sa SDL_RWFromFile * \sa SDL_RWFromFile
* \sa SDL_RWFromMem * \sa SDL_RWFromMem
@ -506,7 +447,6 @@ extern DECLSPEC size_t SDLCALL SDL_RWwrite(SDL_RWops *context, const void *ptr,
* *
* \since This function is available since SDL 3.0.0. * \since This function is available since SDL 3.0.0.
* *
* \sa SDL_RWclose
* \sa SDL_RWFromConstMem * \sa SDL_RWFromConstMem
* \sa SDL_RWFromFile * \sa SDL_RWFromFile
* \sa SDL_RWFromMem * \sa SDL_RWFromMem
@ -529,7 +469,6 @@ extern DECLSPEC size_t SDLCALL SDL_RWprintf(SDL_RWops *context, SDL_PRINTF_FORMA
* *
* \since This function is available since SDL 3.0.0. * \since This function is available since SDL 3.0.0.
* *
* \sa SDL_RWclose
* \sa SDL_RWFromConstMem * \sa SDL_RWFromConstMem
* \sa SDL_RWFromFile * \sa SDL_RWFromFile
* \sa SDL_RWFromMem * \sa SDL_RWFromMem
@ -539,32 +478,6 @@ extern DECLSPEC size_t SDLCALL SDL_RWprintf(SDL_RWops *context, SDL_PRINTF_FORMA
*/ */
extern DECLSPEC size_t SDLCALL SDL_RWvprintf(SDL_RWops *context, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(2); extern DECLSPEC size_t SDLCALL SDL_RWvprintf(SDL_RWops *context, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) SDL_PRINTF_VARARG_FUNCV(2);
/**
* Close and free an allocated SDL_RWops structure.
*
* SDL_RWclose() closes and cleans up the SDL_RWops stream. It releases any
* resources used by the stream and frees the SDL_RWops itself with
* SDL_DestroyRW(). This returns 0 on success, or -1 if the stream failed to
* flush to its output (e.g. to disk).
*
* Note that if this fails to flush the stream to disk, this function reports
* an error, but the SDL_RWops is still invalid once this function returns.
*
* \param context SDL_RWops structure to close
* \returns 0 on success or a negative error code on failure; call
* SDL_GetError() for more information.
*
* \since This function is available since SDL 3.0.0.
*
* \sa SDL_RWFromConstMem
* \sa SDL_RWFromFile
* \sa SDL_RWFromMem
* \sa SDL_RWread
* \sa SDL_RWseek
* \sa SDL_RWwrite
*/
extern DECLSPEC int SDLCALL SDL_RWclose(SDL_RWops *context);
/** /**
* Load all the data from an SDL data stream. * Load all the data from an SDL data stream.
* *
@ -576,7 +489,7 @@ extern DECLSPEC int SDLCALL SDL_RWclose(SDL_RWops *context);
* *
* \param src the SDL_RWops to read all available data from * \param src the SDL_RWops to read all available data from
* \param datasize if not NULL, will store the number of bytes read * \param datasize if not NULL, will store the number of bytes read
* \param freesrc if SDL_TRUE, calls SDL_RWclose() on `src` before returning, * \param freesrc if SDL_TRUE, calls SDL_DestroyRW() on `src` before returning,
* even in the case of an error * even in the case of an error
* \returns the data, or NULL if there was an error. * \returns the data, or NULL if there was an error.
* *

View file

@ -328,7 +328,7 @@ extern DECLSPEC void SDLCALL SDL_UnlockSurface(SDL_Surface *surface);
* will result in a memory leak. * will result in a memory leak.
* *
* \param src the data stream for the surface * \param src the data stream for the surface
* \param freesrc if SDL_TRUE, calls SDL_RWclose() on `src` before returning, * \param freesrc if SDL_TRUE, calls SDL_DestroyRW() on `src` before returning,
* even in the case of an error * even in the case of an error
* \returns a pointer to a new SDL_Surface structure or NULL if there was an * \returns a pointer to a new SDL_Surface structure or NULL if there was an
* error; call SDL_GetError() for more information. * error; call SDL_GetError() for more information.
@ -370,7 +370,7 @@ extern DECLSPEC SDL_Surface *SDLCALL SDL_LoadBMP(const char *file);
* *
* \param surface the SDL_Surface structure containing the image to be saved * \param surface the SDL_Surface structure containing the image to be saved
* \param dst a data stream to save to * \param dst a data stream to save to
* \param freedst if SDL_TRUE, calls SDL_RWclose() on `dst` before returning, * \param freedst if SDL_TRUE, calls SDL_DestroyRW() on `dst` before returning,
* even in the case of an error * even in the case of an error
* \returns 0 on success or a negative error code on failure; call * \returns 0 on success or a negative error code on failure; call
* SDL_GetError() for more information. * SDL_GetError() for more information.

View file

@ -1629,7 +1629,7 @@ static int WaveReadFormat(WaveFile *file)
return -1; return -1;
} }
} else if (format->encoding == PCM_CODE) { } else if (format->encoding == PCM_CODE) {
SDL_RWclose(fmtsrc); SDL_DestroyRW(fmtsrc);
return SDL_SetError("Missing wBitsPerSample field in WAVE fmt chunk"); return SDL_SetError("Missing wBitsPerSample field in WAVE fmt chunk");
} }
@ -1649,7 +1649,7 @@ static int WaveReadFormat(WaveFile *file)
/* Extensible header must be at least 22 bytes. */ /* Extensible header must be at least 22 bytes. */
if (fmtlen < 40 || format->extsize < 22) { if (fmtlen < 40 || format->extsize < 22) {
SDL_RWclose(fmtsrc); SDL_DestroyRW(fmtsrc);
return SDL_SetError("Extensible WAVE header too small"); return SDL_SetError("Extensible WAVE header too small");
} }
@ -1661,7 +1661,7 @@ static int WaveReadFormat(WaveFile *file)
format->encoding = WaveGetFormatGUIDEncoding(format); format->encoding = WaveGetFormatGUIDEncoding(format);
} }
SDL_RWclose(fmtsrc); SDL_DestroyRW(fmtsrc);
return 0; return 0;
} }
@ -2117,7 +2117,7 @@ int SDL_LoadWAV_RW(SDL_RWops *src, SDL_bool freesrc, SDL_AudioSpec *spec, Uint8
SDL_free(file.decoderdata); SDL_free(file.decoderdata);
done: done:
if (freesrc && src) { if (freesrc && src) {
SDL_RWclose(src); SDL_DestroyRW(src);
} }
return result; return result;
} }

View file

@ -68,7 +68,7 @@ static int DISKAUDIO_CaptureFromDevice(SDL_AudioDevice *device, void *buffer, in
buflen -= br; buflen -= br;
buffer = ((Uint8 *)buffer) + br; buffer = ((Uint8 *)buffer) + br;
if (buflen > 0) { // EOF (or error, but whatever). if (buflen > 0) { // EOF (or error, but whatever).
SDL_RWclose(h->io); SDL_DestroyRW(h->io);
h->io = NULL; h->io = NULL;
} }
} }
@ -88,7 +88,7 @@ static void DISKAUDIO_CloseDevice(SDL_AudioDevice *device)
{ {
if (device->hidden) { if (device->hidden) {
if (device->hidden->io) { if (device->hidden->io) {
SDL_RWclose(device->hidden->io); SDL_DestroyRW(device->hidden->io);
} }
SDL_free(device->hidden->mixbuf); SDL_free(device->hidden->mixbuf);
SDL_free(device->hidden); SDL_free(device->hidden);

View file

@ -1956,11 +1956,12 @@ static void Internal_Android_Destroy_AssetManager()
} }
} }
int Android_JNI_FileOpen(SDL_RWops *ctx, int Android_JNI_FileOpen(void **puserdata, const char *fileName, const char *mode)
const char *fileName, const char *mode)
{ {
SDL_assert(puserdata != NULL);
AAsset *asset = NULL; AAsset *asset = NULL;
ctx->hidden.androidio.asset = NULL; *puserdata = NULL;
if (!asset_manager) { if (!asset_manager) {
Internal_Android_Create_AssetManager(); Internal_Android_Create_AssetManager();
@ -1975,14 +1976,13 @@ int Android_JNI_FileOpen(SDL_RWops *ctx,
return SDL_SetError("Couldn't open asset '%s'", fileName); return SDL_SetError("Couldn't open asset '%s'", fileName);
} }
ctx->hidden.androidio.asset = (void *)asset; *puserdata = (void *)asset;
return 0; return 0;
} }
size_t Android_JNI_FileRead(SDL_RWops *ctx, void *buffer, size_t size) size_t Android_JNI_FileRead(void *userdata, void *buffer, size_t size)
{ {
AAsset *asset = (AAsset *)ctx->hidden.androidio.asset; const int bytes = AAsset_read((AAsset *)userdata, buffer, size);
int bytes = AAsset_read(asset, buffer, size);
if (bytes < 0) { if (bytes < 0) {
SDL_SetError("AAsset_read() failed"); SDL_SetError("AAsset_read() failed");
return 0; return 0;
@ -1990,31 +1990,24 @@ size_t Android_JNI_FileRead(SDL_RWops *ctx, void *buffer, size_t size)
return (size_t)bytes; return (size_t)bytes;
} }
size_t Android_JNI_FileWrite(SDL_RWops *ctx, const void *buffer, size_t size) size_t Android_JNI_FileWrite(void *userdata, const void *buffer, size_t size)
{ {
return SDL_SetError("Cannot write to Android package filesystem"); return SDL_SetError("Cannot write to Android package filesystem");
} }
Sint64 Android_JNI_FileSize(SDL_RWops *ctx) Sint64 Android_JNI_FileSize(void *userdata)
{ {
off64_t result; return (Sint64) AAsset_getLength64((AAsset *)userdata);
AAsset *asset = (AAsset *)ctx->hidden.androidio.asset;
result = AAsset_getLength64(asset);
return result;
} }
Sint64 Android_JNI_FileSeek(SDL_RWops *ctx, Sint64 offset, int whence) Sint64 Android_JNI_FileSeek(void *userdata, Sint64 offset, int whence)
{ {
off64_t result; return (Sint64) AAsset_seek64((AAsset *)userdata, offset, whence);
AAsset *asset = (AAsset *)ctx->hidden.androidio.asset;
result = AAsset_seek64(asset, offset, whence);
return result;
} }
int Android_JNI_FileClose(SDL_RWops *ctx) int Android_JNI_FileClose(void *userdata)
{ {
AAsset *asset = (AAsset *)ctx->hidden.androidio.asset; AAsset_close((AAsset *)userdata);
AAsset_close(asset);
return 0; return 0;
} }

View file

@ -66,12 +66,12 @@ extern void Android_JNI_CloseAudioDevice(const int iscapture);
extern SDL_bool Android_IsDeXMode(void); extern SDL_bool Android_IsDeXMode(void);
extern SDL_bool Android_IsChromebook(void); extern SDL_bool Android_IsChromebook(void);
int Android_JNI_FileOpen(SDL_RWops *ctx, const char *fileName, const char *mode); int Android_JNI_FileOpen(void **puserdata, const char *fileName, const char *mode);
Sint64 Android_JNI_FileSize(SDL_RWops *ctx); Sint64 Android_JNI_FileSize(void *userdata);
Sint64 Android_JNI_FileSeek(SDL_RWops *ctx, Sint64 offset, int whence); Sint64 Android_JNI_FileSeek(void *userdata, Sint64 offset, int whence);
size_t Android_JNI_FileRead(SDL_RWops *ctx, void *buffer, size_t size); size_t Android_JNI_FileRead(void *userdata, void *buffer, size_t size);
size_t Android_JNI_FileWrite(SDL_RWops *ctx, const void *buffer, size_t size); size_t Android_JNI_FileWrite(void *userdata, const void *buffer, size_t size);
int Android_JNI_FileClose(SDL_RWops *ctx); int Android_JNI_FileClose(void *userdata);
/* Environment support */ /* Environment support */
void Android_JNI_GetManifestEnvironmentVariables(void); void Android_JNI_GetManifestEnvironmentVariables(void);

View file

@ -469,7 +469,6 @@ SDL3_0.0.0 {
SDL_RWFromConstMem; SDL_RWFromConstMem;
SDL_RWFromFile; SDL_RWFromFile;
SDL_RWFromMem; SDL_RWFromMem;
SDL_RWclose;
SDL_RWread; SDL_RWread;
SDL_RWseek; SDL_RWseek;
SDL_RWsize; SDL_RWsize;

View file

@ -493,7 +493,6 @@
#define SDL_RWFromConstMem SDL_RWFromConstMem_REAL #define SDL_RWFromConstMem SDL_RWFromConstMem_REAL
#define SDL_RWFromFile SDL_RWFromFile_REAL #define SDL_RWFromFile SDL_RWFromFile_REAL
#define SDL_RWFromMem SDL_RWFromMem_REAL #define SDL_RWFromMem SDL_RWFromMem_REAL
#define SDL_RWclose SDL_RWclose_REAL
#define SDL_RWread SDL_RWread_REAL #define SDL_RWread SDL_RWread_REAL
#define SDL_RWseek SDL_RWseek_REAL #define SDL_RWseek SDL_RWseek_REAL
#define SDL_RWsize SDL_RWsize_REAL #define SDL_RWsize SDL_RWsize_REAL

View file

@ -142,7 +142,7 @@ SDL_DYNAPI_PROC(SDL_Mutex*,SDL_CreateMutex,(void),(),return)
SDL_DYNAPI_PROC(SDL_Palette*,SDL_CreatePalette,(int a),(a),return) SDL_DYNAPI_PROC(SDL_Palette*,SDL_CreatePalette,(int a),(a),return)
SDL_DYNAPI_PROC(SDL_PixelFormat*,SDL_CreatePixelFormat,(SDL_PixelFormatEnum a),(a),return) SDL_DYNAPI_PROC(SDL_PixelFormat*,SDL_CreatePixelFormat,(SDL_PixelFormatEnum a),(a),return)
SDL_DYNAPI_PROC(SDL_Window*,SDL_CreatePopupWindow,(SDL_Window *a, int b, int c, int d, int e, Uint32 f),(a,b,c,d,e,f),return) SDL_DYNAPI_PROC(SDL_Window*,SDL_CreatePopupWindow,(SDL_Window *a, int b, int c, int d, int e, Uint32 f),(a,b,c,d,e,f),return)
SDL_DYNAPI_PROC(SDL_RWops*,SDL_CreateRW,(void),(),return) SDL_DYNAPI_PROC(SDL_RWops*,SDL_CreateRW,(const SDL_RWopsInterface *a, void *b),(a,b),return)
SDL_DYNAPI_PROC(SDL_RWLock*,SDL_CreateRWLock,(void),(),return) SDL_DYNAPI_PROC(SDL_RWLock*,SDL_CreateRWLock,(void),(),return)
SDL_DYNAPI_PROC(SDL_Renderer*,SDL_CreateRenderer,(SDL_Window *a, const char *b, Uint32 c),(a,b,c),return) SDL_DYNAPI_PROC(SDL_Renderer*,SDL_CreateRenderer,(SDL_Window *a, const char *b, Uint32 c),(a,b,c),return)
SDL_DYNAPI_PROC(SDL_Semaphore*,SDL_CreateSemaphore,(Uint32 a),(a),return) SDL_DYNAPI_PROC(SDL_Semaphore*,SDL_CreateSemaphore,(Uint32 a),(a),return)
@ -166,7 +166,7 @@ SDL_DYNAPI_PROC(void,SDL_DestroyCursor,(SDL_Cursor *a),(a),)
SDL_DYNAPI_PROC(void,SDL_DestroyMutex,(SDL_Mutex *a),(a),) SDL_DYNAPI_PROC(void,SDL_DestroyMutex,(SDL_Mutex *a),(a),)
SDL_DYNAPI_PROC(void,SDL_DestroyPalette,(SDL_Palette *a),(a),) SDL_DYNAPI_PROC(void,SDL_DestroyPalette,(SDL_Palette *a),(a),)
SDL_DYNAPI_PROC(void,SDL_DestroyPixelFormat,(SDL_PixelFormat *a),(a),) SDL_DYNAPI_PROC(void,SDL_DestroyPixelFormat,(SDL_PixelFormat *a),(a),)
SDL_DYNAPI_PROC(void,SDL_DestroyRW,(SDL_RWops *a),(a),) SDL_DYNAPI_PROC(int,SDL_DestroyRW,(SDL_RWops *a),(a),return)
SDL_DYNAPI_PROC(void,SDL_DestroyRWLock,(SDL_RWLock *a),(a),) SDL_DYNAPI_PROC(void,SDL_DestroyRWLock,(SDL_RWLock *a),(a),)
SDL_DYNAPI_PROC(void,SDL_DestroyRenderer,(SDL_Renderer *a),(a),) SDL_DYNAPI_PROC(void,SDL_DestroyRenderer,(SDL_Renderer *a),(a),)
SDL_DYNAPI_PROC(void,SDL_DestroySemaphore,(SDL_Semaphore *a),(a),) SDL_DYNAPI_PROC(void,SDL_DestroySemaphore,(SDL_Semaphore *a),(a),)
@ -538,7 +538,6 @@ SDL_DYNAPI_PROC(void,SDL_QuitSubSystem,(Uint32 a),(a),)
SDL_DYNAPI_PROC(SDL_RWops*,SDL_RWFromConstMem,(const void *a, size_t b),(a,b),return) SDL_DYNAPI_PROC(SDL_RWops*,SDL_RWFromConstMem,(const void *a, size_t b),(a,b),return)
SDL_DYNAPI_PROC(SDL_RWops*,SDL_RWFromFile,(const char *a, const char *b),(a,b),return) SDL_DYNAPI_PROC(SDL_RWops*,SDL_RWFromFile,(const char *a, const char *b),(a,b),return)
SDL_DYNAPI_PROC(SDL_RWops*,SDL_RWFromMem,(void *a, size_t b),(a,b),return) SDL_DYNAPI_PROC(SDL_RWops*,SDL_RWFromMem,(void *a, size_t b),(a,b),return)
SDL_DYNAPI_PROC(int,SDL_RWclose,(SDL_RWops *a),(a),return)
SDL_DYNAPI_PROC(size_t,SDL_RWread,(SDL_RWops *a, void *b, size_t c),(a,b,c),return) SDL_DYNAPI_PROC(size_t,SDL_RWread,(SDL_RWops *a, void *b, size_t c),(a,b,c),return)
SDL_DYNAPI_PROC(Sint64,SDL_RWseek,(SDL_RWops *a, Sint64 b, int c),(a,b,c),return) SDL_DYNAPI_PROC(Sint64,SDL_RWseek,(SDL_RWops *a, Sint64 b, int c),(a,b,c),return)
SDL_DYNAPI_PROC(Sint64,SDL_RWsize,(SDL_RWops *a),(a),return) SDL_DYNAPI_PROC(Sint64,SDL_RWsize,(SDL_RWops *a),(a),return)

View file

@ -36,6 +36,15 @@
data sources. It can easily be extended to files, memory, etc. data sources. It can easily be extended to files, memory, etc.
*/ */
struct SDL_RWops
{
SDL_RWopsInterface iface;
void *userdata;
Uint32 status;
SDL_PropertiesID props;
};
#ifdef SDL_PLATFORM_APPLE #ifdef SDL_PLATFORM_APPLE
#include "cocoa/SDL_rwopsbundlesupport.h" #include "cocoa/SDL_rwopsbundlesupport.h"
#endif /* SDL_PLATFORM_APPLE */ #endif /* SDL_PLATFORM_APPLE */
@ -50,6 +59,16 @@
#if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK) || defined(SDL_PLATFORM_WINRT) #if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK) || defined(SDL_PLATFORM_WINRT)
typedef struct RWopsWindowsData
{
SDL_bool append;
HANDLE h;
void *data;
size_t size;
size_t left;
} RWopsWindowsData;
/* Functions to read/write Win32 API file pointers */ /* Functions to read/write Win32 API file pointers */
#ifndef INVALID_SET_FILE_POINTER #ifndef INVALID_SET_FILE_POINTER
#define INVALID_SET_FILE_POINTER 0xFFFFFFFF #define INVALID_SET_FILE_POINTER 0xFFFFFFFF
@ -57,7 +76,7 @@
#define READAHEAD_BUFFER_SIZE 1024 #define READAHEAD_BUFFER_SIZE 1024
static int SDLCALL windows_file_open(SDL_RWops *context, const char *filename, const char *mode) static int SDLCALL windows_file_open(RWopsWindowsData *rwopsdata, const char *filename, const char *mode)
{ {
#if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES) && !defined(SDL_PLATFORM_WINRT) #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES) && !defined(SDL_PLATFORM_WINRT)
UINT old_error_mode; UINT old_error_mode;
@ -67,10 +86,8 @@ static int SDLCALL windows_file_open(SDL_RWops *context, const char *filename, c
DWORD must_exist, truncate; DWORD must_exist, truncate;
int a_mode; int a_mode;
context->hidden.windowsio.h = INVALID_HANDLE_VALUE; /* mark this as unusable */ SDL_zerop(rwopsdata);
context->hidden.windowsio.buffer.data = NULL; rwopsdata->h = INVALID_HANDLE_VALUE; /* mark this as unusable */
context->hidden.windowsio.buffer.size = 0;
context->hidden.windowsio.buffer.left = 0;
/* "r" = reading, file must exist */ /* "r" = reading, file must exist */
/* "w" = writing, truncate existing, file may not exist */ /* "w" = writing, truncate existing, file may not exist */
@ -90,9 +107,8 @@ static int SDLCALL windows_file_open(SDL_RWops *context, const char *filename, c
} }
/* failed (invalid call) */ /* failed (invalid call) */
context->hidden.windowsio.buffer.data = rwopsdata->data = (char *)SDL_malloc(READAHEAD_BUFFER_SIZE);
(char *)SDL_malloc(READAHEAD_BUFFER_SIZE); if (!rwopsdata->data) {
if (!context->hidden.windowsio.buffer.data) {
return -1; return -1;
} }
#if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES) && !defined(SDL_PLATFORM_WINRT) #if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES) && !defined(SDL_PLATFORM_WINRT)
@ -131,38 +147,40 @@ static int SDLCALL windows_file_open(SDL_RWops *context, const char *filename, c
#endif #endif
if (h == INVALID_HANDLE_VALUE) { if (h == INVALID_HANDLE_VALUE) {
SDL_free(context->hidden.windowsio.buffer.data); SDL_free(rwopsdata->data);
context->hidden.windowsio.buffer.data = NULL; rwopsdata->data = NULL;
SDL_SetError("Couldn't open %s", filename); SDL_SetError("Couldn't open %s", filename);
return -2; /* failed (CreateFile) */ return -2; /* failed (CreateFile) */
} }
context->hidden.windowsio.h = h; rwopsdata->h = h;
context->hidden.windowsio.append = a_mode ? SDL_TRUE : SDL_FALSE; rwopsdata->append = a_mode ? SDL_TRUE : SDL_FALSE;
return 0; /* ok */ return 0; /* ok */
} }
static Sint64 SDLCALL windows_file_size(SDL_RWops *context) static Sint64 SDLCALL windows_file_size(void *userdata)
{ {
RWopsWindowsData *rwopsdata = (RWopsWindowsData *) userdata;
LARGE_INTEGER size; LARGE_INTEGER size;
if (!GetFileSizeEx(context->hidden.windowsio.h, &size)) { if (!GetFileSizeEx(rwopsdata->h, &size)) {
return WIN_SetError("windows_file_size"); return WIN_SetError("windows_file_size");
} }
return size.QuadPart; return size.QuadPart;
} }
static Sint64 SDLCALL windows_file_seek(SDL_RWops *context, Sint64 offset, int whence) static Sint64 SDLCALL windows_file_seek(void *userdata, Sint64 offset, int whence)
{ {
RWopsWindowsData *rwopsdata = (RWopsWindowsData *) userdata;
DWORD windowswhence; DWORD windowswhence;
LARGE_INTEGER windowsoffset; LARGE_INTEGER windowsoffset;
/* FIXME: We may be able to satisfy the seek within buffered data */ // FIXME: We may be able to satisfy the seek within buffered data
if (whence == SDL_RW_SEEK_CUR && context->hidden.windowsio.buffer.left) { if ((whence == SDL_RW_SEEK_CUR) && (rwopsdata->left)) {
offset -= context->hidden.windowsio.buffer.left; offset -= rwopsdata->left;
} }
context->hidden.windowsio.buffer.left = 0; rwopsdata->left = 0;
switch (whence) { switch (whence) {
case SDL_RW_SEEK_SET: case SDL_RW_SEEK_SET:
@ -179,26 +197,27 @@ static Sint64 SDLCALL windows_file_seek(SDL_RWops *context, Sint64 offset, int w
} }
windowsoffset.QuadPart = offset; windowsoffset.QuadPart = offset;
if (!SetFilePointerEx(context->hidden.windowsio.h, windowsoffset, &windowsoffset, windowswhence)) { if (!SetFilePointerEx(rwopsdata->h, windowsoffset, &windowsoffset, windowswhence)) {
return WIN_SetError("windows_file_seek"); return WIN_SetError("windows_file_seek");
} }
return windowsoffset.QuadPart; return windowsoffset.QuadPart;
} }
static size_t SDLCALL windows_file_read(SDL_RWops *context, void *ptr, size_t size) static size_t SDLCALL windows_file_read(void *userdata, void *ptr, size_t size)
{ {
RWopsWindowsData *rwopsdata = (RWopsWindowsData *) userdata;
size_t total_need = size; size_t total_need = size;
size_t total_read = 0; size_t total_read = 0;
size_t read_ahead; size_t read_ahead;
DWORD bytes; DWORD bytes;
if (context->hidden.windowsio.buffer.left > 0) { if (rwopsdata->left > 0) {
void *data = (char *)context->hidden.windowsio.buffer.data + void *data = (char *)rwopsdata->data +
context->hidden.windowsio.buffer.size - rwopsdata->size -
context->hidden.windowsio.buffer.left; rwopsdata->left;
read_ahead = SDL_min(total_need, context->hidden.windowsio.buffer.left); read_ahead = SDL_min(total_need, rwopsdata->left);
SDL_memcpy(ptr, data, read_ahead); SDL_memcpy(ptr, data, read_ahead);
context->hidden.windowsio.buffer.left -= read_ahead; rwopsdata->left -= read_ahead;
if (read_ahead == total_need) { if (read_ahead == total_need) {
return size; return size;
@ -209,18 +228,17 @@ static size_t SDLCALL windows_file_read(SDL_RWops *context, void *ptr, size_t si
} }
if (total_need < READAHEAD_BUFFER_SIZE) { if (total_need < READAHEAD_BUFFER_SIZE) {
if (!ReadFile(context->hidden.windowsio.h, context->hidden.windowsio.buffer.data, if (!ReadFile(rwopsdata->h, rwopsdata->data, READAHEAD_BUFFER_SIZE, &bytes, NULL)) {
READAHEAD_BUFFER_SIZE, &bytes, NULL)) {
SDL_Error(SDL_EFREAD); SDL_Error(SDL_EFREAD);
return 0; return 0;
} }
read_ahead = SDL_min(total_need, bytes); read_ahead = SDL_min(total_need, bytes);
SDL_memcpy(ptr, context->hidden.windowsio.buffer.data, read_ahead); SDL_memcpy(ptr, rwopsdata->data, read_ahead);
context->hidden.windowsio.buffer.size = bytes; rwopsdata->size = bytes;
context->hidden.windowsio.buffer.left = bytes - read_ahead; rwopsdata->left = bytes - read_ahead;
total_read += read_ahead; total_read += read_ahead;
} else { } else {
if (!ReadFile(context->hidden.windowsio.h, ptr, (DWORD)total_need, &bytes, NULL)) { if (!ReadFile(rwopsdata->h, ptr, (DWORD)total_need, &bytes, NULL)) {
SDL_Error(SDL_EFREAD); SDL_Error(SDL_EFREAD);
return 0; return 0;
} }
@ -229,32 +247,31 @@ static size_t SDLCALL windows_file_read(SDL_RWops *context, void *ptr, size_t si
return total_read; return total_read;
} }
static size_t SDLCALL windows_file_write(SDL_RWops *context, const void *ptr, size_t size) static size_t SDLCALL windows_file_write(void *userdata, const void *ptr, size_t size)
{ {
RWopsWindowsData *rwopsdata = (RWopsWindowsData *) userdata;
const size_t total_bytes = size; const size_t total_bytes = size;
DWORD bytes; DWORD bytes;
if (context->hidden.windowsio.buffer.left) { if (rwopsdata->left) {
if (!SetFilePointer(context->hidden.windowsio.h, if (!SetFilePointer(rwopsdata->h, -(LONG)rwopsdata->left, NULL, FILE_CURRENT)) {
-(LONG)context->hidden.windowsio.buffer.left, NULL,
FILE_CURRENT)) {
SDL_Error(SDL_EFSEEK); SDL_Error(SDL_EFSEEK);
return 0; return 0;
} }
context->hidden.windowsio.buffer.left = 0; rwopsdata->left = 0;
} }
/* if in append mode, we must go to the EOF before write */ /* if in append mode, we must go to the EOF before write */
if (context->hidden.windowsio.append) { if (rwopsdata->append) {
LARGE_INTEGER windowsoffset; LARGE_INTEGER windowsoffset;
windowsoffset.QuadPart = 0; windowsoffset.QuadPart = 0;
if (!SetFilePointerEx(context->hidden.windowsio.h, windowsoffset, &windowsoffset, FILE_END)) { if (!SetFilePointerEx(rwopsdata->h, windowsoffset, &windowsoffset, FILE_END)) {
SDL_Error(SDL_EFSEEK); SDL_Error(SDL_EFSEEK);
return 0; return 0;
} }
} }
if (!WriteFile(context->hidden.windowsio.h, ptr, (DWORD)total_bytes, &bytes, NULL)) { if (!WriteFile(rwopsdata->h, ptr, (DWORD)total_bytes, &bytes, NULL)) {
SDL_Error(SDL_EFWRITE); SDL_Error(SDL_EFWRITE);
return 0; return 0;
} }
@ -262,17 +279,15 @@ static size_t SDLCALL windows_file_write(SDL_RWops *context, const void *ptr, si
return bytes; return bytes;
} }
static int SDLCALL windows_file_close(SDL_RWops *context) static int SDLCALL windows_file_close(void *userdata)
{ {
if (context->hidden.windowsio.h != INVALID_HANDLE_VALUE) { RWopsWindowsData *rwopsdata = (RWopsWindowsData *) userdata;
CloseHandle(context->hidden.windowsio.h); if (rwopsdata->h != INVALID_HANDLE_VALUE) {
context->hidden.windowsio.h = INVALID_HANDLE_VALUE; /* to be sure */ CloseHandle(rwopsdata->h);
rwopsdata->h = INVALID_HANDLE_VALUE; /* to be sure */
} }
if (context->hidden.windowsio.buffer.data) { SDL_free(rwopsdata->data);
SDL_free(context->hidden.windowsio.buffer.data); SDL_free(rwopsdata);
context->hidden.windowsio.buffer.data = NULL;
}
SDL_DestroyRW(context);
return 0; return 0;
} }
#endif /* defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK) */ #endif /* defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK) */
@ -281,6 +296,12 @@ static int SDLCALL windows_file_close(SDL_RWops *context)
/* Functions to read/write stdio file pointers. Not used for windows. */ /* Functions to read/write stdio file pointers. Not used for windows. */
typedef struct RWopsStdioData
{
FILE *fp;
SDL_bool autoclose;
} RWopsStdioData;
#ifdef HAVE_FOPEN64 #ifdef HAVE_FOPEN64
#define fopen fopen64 #define fopen fopen64
#endif #endif
@ -317,8 +338,9 @@ static int SDLCALL windows_file_close(SDL_RWops *context)
#define fseek_off_t long #define fseek_off_t long
#endif #endif
static Sint64 SDLCALL stdio_seek(SDL_RWops *context, Sint64 offset, int whence) static Sint64 SDLCALL stdio_seek(void *userdata, Sint64 offset, int whence)
{ {
RWopsStdioData *rwopsdata = (RWopsStdioData *) userdata;
int stdiowhence; int stdiowhence;
switch (whence) { switch (whence) {
@ -341,8 +363,8 @@ static Sint64 SDLCALL stdio_seek(SDL_RWops *context, Sint64 offset, int whence)
} }
#endif #endif
if (fseek((FILE *)context->hidden.stdio.fp, (fseek_off_t)offset, stdiowhence) == 0) { if (fseek(rwopsdata->fp, (fseek_off_t)offset, stdiowhence) == 0) {
Sint64 pos = ftell((FILE *)context->hidden.stdio.fp); const Sint64 pos = ftell(rwopsdata->fp);
if (pos < 0) { if (pos < 0) {
return SDL_SetError("Couldn't get stream offset"); return SDL_SetError("Couldn't get stream offset");
} }
@ -351,53 +373,60 @@ static Sint64 SDLCALL stdio_seek(SDL_RWops *context, Sint64 offset, int whence)
return SDL_Error(SDL_EFSEEK); return SDL_Error(SDL_EFSEEK);
} }
static size_t SDLCALL stdio_read(SDL_RWops *context, void *ptr, size_t size) static size_t SDLCALL stdio_read(void *userdata, void *ptr, size_t size)
{ {
size_t bytes; RWopsStdioData *rwopsdata = (RWopsStdioData *) userdata;
const size_t bytes = fread(ptr, 1, size, rwopsdata->fp);
bytes = fread(ptr, 1, size, (FILE *)context->hidden.stdio.fp); if (bytes == 0 && ferror(rwopsdata->fp)) {
if (bytes == 0 && ferror((FILE *)context->hidden.stdio.fp)) {
SDL_Error(SDL_EFREAD); SDL_Error(SDL_EFREAD);
} }
return bytes; return bytes;
} }
static size_t SDLCALL stdio_write(SDL_RWops *context, const void *ptr, size_t size) static size_t SDLCALL stdio_write(void *userdata, const void *ptr, size_t size)
{ {
size_t bytes; RWopsStdioData *rwopsdata = (RWopsStdioData *) userdata;
const size_t bytes = fwrite(ptr, 1, size, rwopsdata->fp);
bytes = fwrite(ptr, 1, size, (FILE *)context->hidden.stdio.fp); if (bytes == 0 && ferror(rwopsdata->fp)) {
if (bytes == 0 && ferror((FILE *)context->hidden.stdio.fp)) {
SDL_Error(SDL_EFWRITE); SDL_Error(SDL_EFWRITE);
} }
return bytes; return bytes;
} }
static int SDLCALL stdio_close(SDL_RWops *context) static int SDLCALL stdio_close(void *userdata)
{ {
RWopsStdioData *rwopsdata = (RWopsStdioData *) userdata;
int status = 0; int status = 0;
if (context->hidden.stdio.autoclose) { if (rwopsdata->autoclose) {
if (fclose((FILE *)context->hidden.stdio.fp) != 0) { if (fclose(rwopsdata->fp) != 0) {
status = SDL_Error(SDL_EFWRITE); status = SDL_Error(SDL_EFWRITE);
} }
} }
SDL_DestroyRW(context); SDL_free(rwopsdata);
return status; return status;
} }
static SDL_RWops *SDL_RWFromFP(void *fp, SDL_bool autoclose) static SDL_RWops *SDL_RWFromFP(FILE *fp, SDL_bool autoclose)
{ {
SDL_RWops *rwops = NULL; RWopsStdioData *rwopsdata = (RWopsStdioData *) SDL_malloc(sizeof (*rwopsdata));
if (!rwopsdata) {
return NULL;
}
rwops = SDL_CreateRW(); SDL_RWopsInterface iface;
if (rwops) { SDL_zero(iface);
rwops->seek = stdio_seek; // There's no stdio_size because SDL_RWsize emulates it the same way we'd do it for stdio anyhow.
rwops->read = stdio_read; iface.seek = stdio_seek;
rwops->write = stdio_write; iface.read = stdio_read;
rwops->close = stdio_close; iface.write = stdio_write;
rwops->hidden.stdio.fp = fp; iface.close = stdio_close;
rwops->hidden.stdio.autoclose = autoclose;
rwops->type = SDL_RWOPS_STDFILE; rwopsdata->fp = fp;
rwopsdata->autoclose = autoclose;
SDL_RWops *rwops = SDL_CreateRW(&iface, rwopsdata);
if (!rwops) {
iface.close(rwopsdata);
} }
return rwops; return rwops;
} }
@ -405,57 +434,69 @@ static SDL_RWops *SDL_RWFromFP(void *fp, SDL_bool autoclose)
/* Functions to read/write memory pointers */ /* Functions to read/write memory pointers */
static Sint64 SDLCALL mem_size(SDL_RWops *context) typedef struct RWopsMemData
{ {
return (context->hidden.mem.stop - context->hidden.mem.base); Uint8 *base;
Uint8 *here;
Uint8 *stop;
} RWopsMemData;
static Sint64 SDLCALL mem_size(void *userdata)
{
const RWopsMemData *rwopsdata = (RWopsMemData *) userdata;
return (rwopsdata->stop - rwopsdata->base);
} }
static Sint64 SDLCALL mem_seek(SDL_RWops *context, Sint64 offset, int whence) static Sint64 SDLCALL mem_seek(void *userdata, Sint64 offset, int whence)
{ {
RWopsMemData *rwopsdata = (RWopsMemData *) userdata;
Uint8 *newpos; Uint8 *newpos;
switch (whence) { switch (whence) {
case SDL_RW_SEEK_SET: case SDL_RW_SEEK_SET:
newpos = context->hidden.mem.base + offset; newpos = rwopsdata->base + offset;
break; break;
case SDL_RW_SEEK_CUR: case SDL_RW_SEEK_CUR:
newpos = context->hidden.mem.here + offset; newpos = rwopsdata->here + offset;
break; break;
case SDL_RW_SEEK_END: case SDL_RW_SEEK_END:
newpos = context->hidden.mem.stop + offset; newpos = rwopsdata->stop + offset;
break; break;
default: default:
return SDL_SetError("Unknown value for 'whence'"); return SDL_SetError("Unknown value for 'whence'");
} }
if (newpos < context->hidden.mem.base) { if (newpos < rwopsdata->base) {
newpos = context->hidden.mem.base; newpos = rwopsdata->base;
} }
if (newpos > context->hidden.mem.stop) { if (newpos > rwopsdata->stop) {
newpos = context->hidden.mem.stop; newpos = rwopsdata->stop;
} }
context->hidden.mem.here = newpos; rwopsdata->here = newpos;
return (Sint64)(context->hidden.mem.here - context->hidden.mem.base); return (Sint64)(rwopsdata->here - rwopsdata->base);
} }
static size_t mem_io(SDL_RWops *context, void *dst, const void *src, size_t size) static size_t mem_io(void *userdata, void *dst, const void *src, size_t size)
{ {
const size_t mem_available = (context->hidden.mem.stop - context->hidden.mem.here); RWopsMemData *rwopsdata = (RWopsMemData *) userdata;
const size_t mem_available = (rwopsdata->stop - rwopsdata->here);
if (size > mem_available) { if (size > mem_available) {
size = mem_available; size = mem_available;
} }
SDL_memcpy(dst, src, size); SDL_memcpy(dst, src, size);
context->hidden.mem.here += size; rwopsdata->here += size;
return size; return size;
} }
static size_t SDLCALL mem_read(SDL_RWops *context, void *ptr, size_t size) static size_t SDLCALL mem_read(void *userdata, void *ptr, size_t size)
{ {
return mem_io(context, ptr, context->hidden.mem.here, size); const RWopsMemData *rwopsdata = (RWopsMemData *) userdata;
return mem_io(userdata, ptr, rwopsdata->here, size);
} }
static size_t SDLCALL mem_write(SDL_RWops *context, const void *ptr, size_t size) static size_t SDLCALL mem_write(void *userdata, const void *ptr, size_t size)
{ {
return mem_io(context, context->hidden.mem.here, ptr, size); const RWopsMemData *rwopsdata = (RWopsMemData *) userdata;
return mem_io(userdata, rwopsdata->here, ptr, size);
} }
/* Functions to create SDL_RWops structures from various data sources */ /* Functions to create SDL_RWops structures from various data sources */
@ -501,15 +542,12 @@ SDL_RWops *SDL_RWFromFile(const char *file, const char *mode)
} }
} else { } else {
/* Try opening it from internal storage if it's a relative path */ /* Try opening it from internal storage if it's a relative path */
char *path; // !!! FIXME: why not just "char path[PATH_MAX];"
FILE *fp; char *path = SDL_stack_alloc(char, PATH_MAX);
/* !!! FIXME: why not just "char path[PATH_MAX];" ? */
path = SDL_stack_alloc(char, PATH_MAX);
if (path) { if (path) {
SDL_snprintf(path, PATH_MAX, "%s/%s", SDL_snprintf(path, PATH_MAX, "%s/%s",
SDL_AndroidGetInternalStoragePath(), file); SDL_AndroidGetInternalStoragePath(), file);
fp = fopen(path, mode); FILE *fp = fopen(path, mode);
SDL_stack_free(path); SDL_stack_free(path);
if (fp) { if (fp) {
if (!IsRegularFileOrPipe(fp)) { if (!IsRegularFileOrPipe(fp)) {
@ -524,38 +562,53 @@ SDL_RWops *SDL_RWFromFile(const char *file, const char *mode)
#endif /* HAVE_STDIO_H */ #endif /* HAVE_STDIO_H */
/* Try to open the file from the asset system */ /* Try to open the file from the asset system */
rwops = SDL_CreateRW();
if (!rwops) {
return NULL; /* SDL_SetError already setup by SDL_CreateRW() */
}
if (Android_JNI_FileOpen(rwops, file, mode) < 0) { void *rwopsdata = NULL;
if (Android_JNI_FileOpen(&rwopsdata, file, mode) < 0) {
SDL_DestroyRW(rwops); SDL_DestroyRW(rwops);
return NULL; return NULL;
} }
rwops->size = Android_JNI_FileSize;
rwops->seek = Android_JNI_FileSeek; SDL_RWopsInterface iface;
rwops->read = Android_JNI_FileRead; SDL_zero(iface);
rwops->write = Android_JNI_FileWrite; iface.size = Android_JNI_FileSize;
rwops->close = Android_JNI_FileClose; iface.seek = Android_JNI_FileSeek;
rwops->type = SDL_RWOPS_JNIFILE; iface.read = Android_JNI_FileRead;
iface.write = Android_JNI_FileWrite;
iface.close = Android_JNI_FileClose;
rwops = SDL_CreateRW(&iface, rwopsdata);
if (!rwops) {
iface.close(rwopsdata);
}
return rwops;
#elif defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK) || defined(SDL_PLATFORM_WINRT) #elif defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK) || defined(SDL_PLATFORM_WINRT)
rwops = SDL_CreateRW(); RWopsWindowsData *rwopsdata = (RWopsWindowsData *) SDL_malloc(sizeof (*rwopsdata));
if (!rwops) { if (!rwopsdata) {
return NULL; /* SDL_SetError already setup by SDL_CreateRW() */ return NULL;
} }
if (windows_file_open(rwops, file, mode) < 0) { if (windows_file_open(rwopsdata, file, mode) < 0) {
SDL_DestroyRW(rwops); SDL_DestroyRW(rwops);
return NULL; return NULL;
} }
rwops->size = windows_file_size;
rwops->seek = windows_file_seek; SDL_RWopsInterface iface;
rwops->read = windows_file_read; SDL_zero(iface);
rwops->write = windows_file_write; iface.size = windows_file_size;
rwops->close = windows_file_close; iface.seek = windows_file_seek;
rwops->type = SDL_RWOPS_WINFILE; iface.read = windows_file_read;
iface.write = windows_file_write;
iface.close = windows_file_close;
rwops = SDL_CreateRW(&iface, rwopsdata);
if (!rwops) {
windows_file_close(rwopsdata);
}
return rwops;
#elif defined(HAVE_STDIO_H) #elif defined(HAVE_STDIO_H)
{ {
#if defined(SDL_PLATFORM_APPLE) #if defined(SDL_PLATFORM_APPLE)
@ -568,6 +621,7 @@ SDL_RWops *SDL_RWFromFile(const char *file, const char *mode)
#else #else
FILE *fp = fopen(file, mode); FILE *fp = fopen(file, mode);
#endif #endif
if (!fp) { if (!fp) {
SDL_SetError("Couldn't open %s", file); SDL_SetError("Couldn't open %s", file);
} else if (!IsRegularFileOrPipe(fp)) { } else if (!IsRegularFileOrPipe(fp)) {
@ -578,6 +632,7 @@ SDL_RWops *SDL_RWFromFile(const char *file, const char *mode)
rwops = SDL_RWFromFP(fp, SDL_TRUE); rwops = SDL_RWFromFP(fp, SDL_TRUE);
} }
} }
#else #else
SDL_SetError("SDL not compiled with stdio support"); SDL_SetError("SDL not compiled with stdio support");
#endif /* !HAVE_STDIO_H */ #endif /* !HAVE_STDIO_H */
@ -587,72 +642,96 @@ SDL_RWops *SDL_RWFromFile(const char *file, const char *mode)
SDL_RWops *SDL_RWFromMem(void *mem, size_t size) SDL_RWops *SDL_RWFromMem(void *mem, size_t size)
{ {
SDL_RWops *rwops = NULL;
if (!mem) { if (!mem) {
SDL_InvalidParamError("mem"); SDL_InvalidParamError("mem");
return NULL; return NULL;
} } else if (!size) {
if (!size) {
SDL_InvalidParamError("size"); SDL_InvalidParamError("size");
return NULL; return NULL;
} }
rwops = SDL_CreateRW(); RWopsMemData *rwopsdata = (RWopsMemData *) SDL_malloc(sizeof (*rwopsdata));
if (rwops) { if (!rwopsdata) {
rwops->size = mem_size; return NULL;
rwops->seek = mem_seek; }
rwops->read = mem_read;
rwops->write = mem_write; SDL_RWopsInterface iface;
rwops->hidden.mem.base = (Uint8 *)mem; SDL_zero(iface);
rwops->hidden.mem.here = rwops->hidden.mem.base; iface.size = mem_size;
rwops->hidden.mem.stop = rwops->hidden.mem.base + size; iface.seek = mem_seek;
rwops->type = SDL_RWOPS_MEMORY; iface.read = mem_read;
iface.write = mem_write;
rwopsdata->base = (Uint8 *)mem;
rwopsdata->here = rwopsdata->base;
rwopsdata->stop = rwopsdata->base + size;
SDL_RWops *rwops = SDL_CreateRW(&iface, rwopsdata);
if (!rwops) {
SDL_free(rwopsdata);
} }
return rwops; return rwops;
} }
SDL_RWops *SDL_RWFromConstMem(const void *mem, size_t size) SDL_RWops *SDL_RWFromConstMem(const void *mem, size_t size)
{ {
SDL_RWops *rwops = NULL;
if (!mem) { if (!mem) {
SDL_InvalidParamError("mem"); SDL_InvalidParamError("mem");
return NULL; return NULL;
} } else if (!size) {
if (!size) {
SDL_InvalidParamError("size"); SDL_InvalidParamError("size");
return NULL; return NULL;
} }
rwops = SDL_CreateRW(); RWopsMemData *rwopsdata = (RWopsMemData *) SDL_malloc(sizeof (*rwopsdata));
if (rwops) { if (!rwopsdata) {
rwops->size = mem_size; return NULL;
rwops->seek = mem_seek; }
rwops->read = mem_read;
rwops->hidden.mem.base = (Uint8 *)mem; SDL_RWopsInterface iface;
rwops->hidden.mem.here = rwops->hidden.mem.base; SDL_zero(iface);
rwops->hidden.mem.stop = rwops->hidden.mem.base + size; iface.size = mem_size;
rwops->type = SDL_RWOPS_MEMORY_RO; iface.seek = mem_seek;
iface.read = mem_read;
// leave iface.write as NULL.
rwopsdata->base = (Uint8 *)mem;
rwopsdata->here = rwopsdata->base;
rwopsdata->stop = rwopsdata->base + size;
SDL_RWops *rwops = SDL_CreateRW(&iface, rwopsdata);
if (!rwops) {
SDL_free(rwopsdata);
} }
return rwops; return rwops;
} }
SDL_RWops *SDL_CreateRW(void) SDL_RWops *SDL_CreateRW(const SDL_RWopsInterface *iface, void *userdata)
{ {
SDL_RWops *context; if (!iface) {
SDL_InvalidParamError("iface");
context = (SDL_RWops *)SDL_calloc(1, sizeof(*context)); return NULL;
if (context) {
context->type = SDL_RWOPS_UNKNOWN;
}
return context;
} }
void SDL_DestroyRW(SDL_RWops *context) SDL_RWops *rwops = (SDL_RWops *)SDL_calloc(1, sizeof(*rwops));
if (rwops) {
SDL_copyp(&rwops->iface, iface);
rwops->userdata = userdata;
}
return rwops;
}
int SDL_DestroyRW(SDL_RWops *rwops)
{ {
SDL_DestroyProperties(context->props); int retval = 0;
SDL_free(context); if (rwops) {
if (rwops->iface.close) {
retval = rwops->iface.close(rwops->userdata);
}
SDL_DestroyProperties(rwops->props);
SDL_free(rwops);
}
return retval;
} }
/* Load all the data from an SDL data stream */ /* Load all the data from an SDL data stream */
@ -718,7 +797,7 @@ done:
*datasize = (size_t)size_total; *datasize = (size_t)size_total;
} }
if (freesrc && src) { if (freesrc && src) {
SDL_RWclose(src); SDL_DestroyRW(src);
} }
return data; return data;
} }
@ -746,7 +825,7 @@ Sint64 SDL_RWsize(SDL_RWops *context)
if (!context) { if (!context) {
return SDL_InvalidParamError("context"); return SDL_InvalidParamError("context");
} }
if (!context->size) { if (!context->iface.size) {
Sint64 pos, size; Sint64 pos, size;
pos = SDL_RWseek(context, 0, SDL_RW_SEEK_CUR); pos = SDL_RWseek(context, 0, SDL_RW_SEEK_CUR);
@ -758,18 +837,17 @@ Sint64 SDL_RWsize(SDL_RWops *context)
SDL_RWseek(context, pos, SDL_RW_SEEK_SET); SDL_RWseek(context, pos, SDL_RW_SEEK_SET);
return size; return size;
} }
return context->size(context); return context->iface.size(context->userdata);
} }
Sint64 SDL_RWseek(SDL_RWops *context, Sint64 offset, int whence) Sint64 SDL_RWseek(SDL_RWops *context, Sint64 offset, int whence)
{ {
if (!context) { if (!context) {
return SDL_InvalidParamError("context"); return SDL_InvalidParamError("context");
} } else if (!context->iface.seek) {
if (!context->seek) {
return SDL_Unsupported(); return SDL_Unsupported();
} }
return context->seek(context, offset, whence); return context->iface.seek(context->userdata, offset, whence);
} }
Sint64 SDL_RWtell(SDL_RWops *context) Sint64 SDL_RWtell(SDL_RWops *context)
@ -784,8 +862,7 @@ size_t SDL_RWread(SDL_RWops *context, void *ptr, size_t size)
if (!context) { if (!context) {
SDL_InvalidParamError("context"); SDL_InvalidParamError("context");
return 0; return 0;
} } else if (!context->iface.read) {
if (!context->read) {
context->status = SDL_RWOPS_STATUS_WRITEONLY; context->status = SDL_RWOPS_STATUS_WRITEONLY;
SDL_Unsupported(); SDL_Unsupported();
return 0; return 0;
@ -798,7 +875,7 @@ size_t SDL_RWread(SDL_RWops *context, void *ptr, size_t size)
return 0; return 0;
} }
bytes = context->read(context, ptr, size); bytes = context->iface.read(context->userdata, ptr, size);
if (bytes == 0 && context->status == SDL_RWOPS_STATUS_READY) { if (bytes == 0 && context->status == SDL_RWOPS_STATUS_READY) {
if (*SDL_GetError()) { if (*SDL_GetError()) {
context->status = SDL_RWOPS_STATUS_ERROR; context->status = SDL_RWOPS_STATUS_ERROR;
@ -816,8 +893,7 @@ size_t SDL_RWwrite(SDL_RWops *context, const void *ptr, size_t size)
if (!context) { if (!context) {
SDL_InvalidParamError("context"); SDL_InvalidParamError("context");
return 0; return 0;
} } else if (!context->iface.write) {
if (!context->write) {
context->status = SDL_RWOPS_STATUS_READONLY; context->status = SDL_RWOPS_STATUS_READONLY;
SDL_Unsupported(); SDL_Unsupported();
return 0; return 0;
@ -830,8 +906,8 @@ size_t SDL_RWwrite(SDL_RWops *context, const void *ptr, size_t size)
return 0; return 0;
} }
bytes = context->write(context, ptr, size); bytes = context->iface.write(context->userdata, ptr, size);
if (bytes == 0 && context->status == SDL_RWOPS_STATUS_READY) { if ((bytes == 0) && (context->status == SDL_RWOPS_STATUS_READY)) {
context->status = SDL_RWOPS_STATUS_ERROR; context->status = SDL_RWOPS_STATUS_ERROR;
} }
return bytes; return bytes;
@ -872,18 +948,6 @@ size_t SDL_RWvprintf(SDL_RWops *context, SDL_PRINTF_FORMAT_STRING const char *fm
return bytes; return bytes;
} }
int SDL_RWclose(SDL_RWops *context)
{
if (!context) {
return SDL_InvalidParamError("context");
}
if (!context->close) {
SDL_DestroyRW(context);
return 0;
}
return context->close(context);
}
/* Functions for dynamically reading and writing endian-specific values */ /* Functions for dynamically reading and writing endian-specific values */
SDL_bool SDL_ReadU8(SDL_RWops *src, Uint8 *value) SDL_bool SDL_ReadU8(SDL_RWops *src, Uint8 *value)

View file

@ -1913,7 +1913,7 @@ static const void *SDLTest_ScreenShotClipboardProvider(void *context, const char
image = NULL; image = NULL;
} }
} }
SDL_RWclose(file); SDL_DestroyRW(file);
if (image) { if (image) {
data->image = image; data->image = image;
@ -1984,7 +1984,7 @@ static void SDLTest_PasteScreenShot(void)
if (file) { if (file) {
SDL_Log("Writing clipboard image to %s", filename); SDL_Log("Writing clipboard image to %s", filename);
SDL_RWwrite(file, data, size); SDL_RWwrite(file, data, size);
SDL_RWclose(file); SDL_DestroyRW(file);
} }
SDL_free(data); SDL_free(data);
return; return;

View file

@ -578,7 +578,7 @@ done:
surface = NULL; surface = NULL;
} }
if (freesrc && src) { if (freesrc && src) {
SDL_RWclose(src); SDL_DestroyRW(src);
} }
return surface; return surface;
} }
@ -857,7 +857,7 @@ done:
SDL_DestroySurface(intermediate_surface); SDL_DestroySurface(intermediate_surface);
} }
if (freedst && dst) { if (freedst && dst) {
if (SDL_RWclose(dst) < 0) { if (SDL_DestroyRW(dst) < 0) {
was_error = SDL_TRUE; was_error = SDL_TRUE;
} }
} }

View file

@ -269,15 +269,12 @@ static int rwops_testMem(void *arg)
return TEST_ABORTED; return TEST_ABORTED;
} }
/* Check type */
SDLTest_AssertCheck(rw->type == SDL_RWOPS_MEMORY, "Verify RWops type is SDL_RWOPS_MEMORY; expected: %d, got: %" SDL_PRIu32, SDL_RWOPS_MEMORY, rw->type);
/* Run generic tests */ /* Run generic tests */
testGenericRWopsValidations(rw, SDL_TRUE); testGenericRWopsValidations(rw, SDL_TRUE);
/* Close */ /* Close */
result = SDL_RWclose(rw); result = SDL_DestroyRW(rw);
SDLTest_AssertPass("Call to SDL_RWclose() succeeded"); SDLTest_AssertPass("Call to SDL_DestroyRW() succeeded");
SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result); SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
return TEST_COMPLETED; return TEST_COMPLETED;
@ -304,15 +301,12 @@ static int rwops_testConstMem(void *arg)
return TEST_ABORTED; return TEST_ABORTED;
} }
/* Check type */
SDLTest_AssertCheck(rw->type == SDL_RWOPS_MEMORY_RO, "Verify RWops type is SDL_RWOPS_MEMORY_RO; expected: %d, got: %" SDL_PRIu32, SDL_RWOPS_MEMORY_RO, rw->type);
/* Run generic tests */ /* Run generic tests */
testGenericRWopsValidations(rw, SDL_FALSE); testGenericRWopsValidations(rw, SDL_FALSE);
/* Close handle */ /* Close handle */
result = SDL_RWclose(rw); result = SDL_DestroyRW(rw);
SDLTest_AssertPass("Call to SDL_RWclose() succeeded"); SDLTest_AssertPass("Call to SDL_DestroyRW() succeeded");
SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result); SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
return TEST_COMPLETED; return TEST_COMPLETED;
@ -339,27 +333,12 @@ static int rwops_testFileRead(void *arg)
return TEST_ABORTED; return TEST_ABORTED;
} }
/* Check type */
#ifdef SDL_PLATFORM_ANDROID
SDLTest_AssertCheck(
rw->type == SDL_RWOPS_STDFILE || rw->type == SDL_RWOPS_JNIFILE,
"Verify RWops type is SDL_RWOPS_STDFILE or SDL_RWOPS_JNIFILE; expected: %d|%d, got: %d", SDL_RWOPS_STDFILE, SDL_RWOPS_JNIFILE, rw->type);
#elif defined(SDL_PLATFORM_WIN32)
SDLTest_AssertCheck(
rw->type == SDL_RWOPS_WINFILE,
"Verify RWops type is SDL_RWOPS_WINFILE; expected: %d, got: %d", SDL_RWOPS_WINFILE, rw->type);
#else
SDLTest_AssertCheck(
rw->type == SDL_RWOPS_STDFILE,
"Verify RWops type is SDL_RWOPS_STDFILE; expected: %d, got: %" SDL_PRIu32, SDL_RWOPS_STDFILE, rw->type);
#endif
/* Run generic tests */ /* Run generic tests */
testGenericRWopsValidations(rw, SDL_FALSE); testGenericRWopsValidations(rw, SDL_FALSE);
/* Close handle */ /* Close handle */
result = SDL_RWclose(rw); result = SDL_DestroyRW(rw);
SDLTest_AssertPass("Call to SDL_RWclose() succeeded"); SDLTest_AssertPass("Call to SDL_DestroyRW() succeeded");
SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result); SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
return TEST_COMPLETED; return TEST_COMPLETED;
@ -386,27 +365,12 @@ static int rwops_testFileWrite(void *arg)
return TEST_ABORTED; return TEST_ABORTED;
} }
/* Check type */
#ifdef SDL_PLATFORM_ANDROID
SDLTest_AssertCheck(
rw->type == SDL_RWOPS_STDFILE || rw->type == SDL_RWOPS_JNIFILE,
"Verify RWops type is SDL_RWOPS_STDFILE or SDL_RWOPS_JNIFILE; expected: %d|%d, got: %d", SDL_RWOPS_STDFILE, SDL_RWOPS_JNIFILE, rw->type);
#elif defined(SDL_PLATFORM_WIN32)
SDLTest_AssertCheck(
rw->type == SDL_RWOPS_WINFILE,
"Verify RWops type is SDL_RWOPS_WINFILE; expected: %d, got: %d", SDL_RWOPS_WINFILE, rw->type);
#else
SDLTest_AssertCheck(
rw->type == SDL_RWOPS_STDFILE,
"Verify RWops type is SDL_RWOPS_STDFILE; expected: %d, got: %" SDL_PRIu32, SDL_RWOPS_STDFILE, rw->type);
#endif
/* Run generic tests */ /* Run generic tests */
testGenericRWopsValidations(rw, SDL_TRUE); testGenericRWopsValidations(rw, SDL_TRUE);
/* Close handle */ /* Close handle */
result = SDL_RWclose(rw); result = SDL_DestroyRW(rw);
SDLTest_AssertPass("Call to SDL_RWclose() succeeded"); SDLTest_AssertPass("Call to SDL_DestroyRW() succeeded");
SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result); SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
return TEST_COMPLETED; return TEST_COMPLETED;
@ -421,18 +385,17 @@ static int rwops_testFileWrite(void *arg)
static int rwops_testAllocFree(void *arg) static int rwops_testAllocFree(void *arg)
{ {
/* Allocate context */ /* Allocate context */
SDL_RWops *rw = SDL_CreateRW(); SDL_RWopsInterface iface;
SDL_RWops *rw;
SDL_zero(iface);
rw = SDL_CreateRW(&iface, NULL);
SDLTest_AssertPass("Call to SDL_CreateRW() succeeded"); SDLTest_AssertPass("Call to SDL_CreateRW() succeeded");
SDLTest_AssertCheck(rw != NULL, "Validate result from SDL_CreateRW() is not NULL"); SDLTest_AssertCheck(rw != NULL, "Validate result from SDL_CreateRW() is not NULL");
if (rw == NULL) { if (rw == NULL) {
return TEST_ABORTED; return TEST_ABORTED;
} }
/* Check type */
SDLTest_AssertCheck(
rw->type == SDL_RWOPS_UNKNOWN,
"Verify RWops type is SDL_RWOPS_UNKNOWN; expected: %d, got: %" SDL_PRIu32, SDL_RWOPS_UNKNOWN, rw->type);
/* Free context again */ /* Free context again */
SDL_DestroyRW(rw); SDL_DestroyRW(rw);
SDLTest_AssertPass("Call to SDL_DestroyRW() succeeded"); SDLTest_AssertPass("Call to SDL_DestroyRW() succeeded");
@ -472,8 +435,8 @@ static int rwops_testCompareRWFromMemWithRWFromFile(void *arg)
SDLTest_AssertPass("Call to SDL_RWread(mem, size=%d)", size * 6); SDLTest_AssertPass("Call to SDL_RWread(mem, size=%d)", size * 6);
sv_mem = SDL_RWseek(rwops_mem, 0, SEEK_END); sv_mem = SDL_RWseek(rwops_mem, 0, SEEK_END);
SDLTest_AssertPass("Call to SDL_RWseek(mem,SEEK_END)"); SDLTest_AssertPass("Call to SDL_RWseek(mem,SEEK_END)");
result = SDL_RWclose(rwops_mem); result = SDL_DestroyRW(rwops_mem);
SDLTest_AssertPass("Call to SDL_RWclose(mem)"); SDLTest_AssertPass("Call to SDL_DestroyRW(mem)");
SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result); SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
/* Read/see from file */ /* Read/see from file */
@ -483,8 +446,8 @@ static int rwops_testCompareRWFromMemWithRWFromFile(void *arg)
SDLTest_AssertPass("Call to SDL_RWread(file, size=%d)", size * 6); SDLTest_AssertPass("Call to SDL_RWread(file, size=%d)", size * 6);
sv_file = SDL_RWseek(rwops_file, 0, SEEK_END); sv_file = SDL_RWseek(rwops_file, 0, SEEK_END);
SDLTest_AssertPass("Call to SDL_RWseek(file,SEEK_END)"); SDLTest_AssertPass("Call to SDL_RWseek(file,SEEK_END)");
result = SDL_RWclose(rwops_file); result = SDL_DestroyRW(rwops_file);
SDLTest_AssertPass("Call to SDL_RWclose(file)"); SDLTest_AssertPass("Call to SDL_DestroyRW(file)");
SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result); SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result);
/* Compare */ /* Compare */
@ -627,8 +590,8 @@ static int rwops_testFileWriteReadEndian(void *arg)
SDLTest_AssertCheck(LE64test == LE64value, "Validate object read from SDL_ReadU64LE, expected: %" SDL_PRIu64 ", got: %" SDL_PRIu64, LE64value, LE64test); SDLTest_AssertCheck(LE64test == LE64value, "Validate object read from SDL_ReadU64LE, expected: %" SDL_PRIu64 ", got: %" SDL_PRIu64, LE64value, LE64test);
/* Close handle */ /* Close handle */
cresult = SDL_RWclose(rw); cresult = SDL_DestroyRW(rw);
SDLTest_AssertPass("Call to SDL_RWclose() succeeded"); SDLTest_AssertPass("Call to SDL_DestroyRW() succeeded");
SDLTest_AssertCheck(cresult == 0, "Verify result value is 0; got: %d", cresult); SDLTest_AssertCheck(cresult == 0, "Verify result value is 0; got: %d", cresult);
} }

View file

@ -55,7 +55,7 @@ rwops_error_quit(unsigned line, SDL_RWops *rwops)
{ {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "testfile.c(%d): failed\n", line); SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "testfile.c(%d): failed\n", line);
if (rwops) { if (rwops) {
SDL_RWclose(rwops); /* This calls SDL_DestroyRW(rwops); */ SDL_DestroyRW(rwops);
} }
cleanup(); cleanup();
SDLTest_CommonDestroyState(state); SDLTest_CommonDestroyState(state);
@ -126,25 +126,25 @@ int main(int argc, char *argv[])
if (!rwops) { if (!rwops) {
RWOP_ERR_QUIT(rwops); RWOP_ERR_QUIT(rwops);
} }
SDL_RWclose(rwops); SDL_DestroyRW(rwops);
unlink(FBASENAME2); unlink(FBASENAME2);
rwops = SDL_RWFromFile(FBASENAME2, "wb+"); rwops = SDL_RWFromFile(FBASENAME2, "wb+");
if (!rwops) { if (!rwops) {
RWOP_ERR_QUIT(rwops); RWOP_ERR_QUIT(rwops);
} }
SDL_RWclose(rwops); SDL_DestroyRW(rwops);
unlink(FBASENAME2); unlink(FBASENAME2);
rwops = SDL_RWFromFile(FBASENAME2, "ab"); rwops = SDL_RWFromFile(FBASENAME2, "ab");
if (!rwops) { if (!rwops) {
RWOP_ERR_QUIT(rwops); RWOP_ERR_QUIT(rwops);
} }
SDL_RWclose(rwops); SDL_DestroyRW(rwops);
unlink(FBASENAME2); unlink(FBASENAME2);
rwops = SDL_RWFromFile(FBASENAME2, "ab+"); rwops = SDL_RWFromFile(FBASENAME2, "ab+");
if (!rwops) { if (!rwops) {
RWOP_ERR_QUIT(rwops); RWOP_ERR_QUIT(rwops);
} }
SDL_RWclose(rwops); SDL_DestroyRW(rwops);
unlink(FBASENAME2); unlink(FBASENAME2);
SDL_Log("test2 OK\n"); SDL_Log("test2 OK\n");
@ -171,7 +171,7 @@ int main(int argc, char *argv[])
RWOP_ERR_QUIT(rwops); /* we are in write only mode */ RWOP_ERR_QUIT(rwops); /* we are in write only mode */
} }
SDL_RWclose(rwops); SDL_DestroyRW(rwops);
rwops = SDL_RWFromFile(FBASENAME1, "rb"); /* read mode, file must exist */ rwops = SDL_RWFromFile(FBASENAME1, "rb"); /* read mode, file must exist */
if (!rwops) { if (!rwops) {
@ -208,7 +208,7 @@ int main(int argc, char *argv[])
RWOP_ERR_QUIT(rwops); /* readonly mode */ RWOP_ERR_QUIT(rwops); /* readonly mode */
} }
SDL_RWclose(rwops); SDL_DestroyRW(rwops);
/* test 3: same with w+ mode */ /* test 3: same with w+ mode */
rwops = SDL_RWFromFile(FBASENAME1, "wb+"); /* write + read + truncation */ rwops = SDL_RWFromFile(FBASENAME1, "wb+"); /* write + read + truncation */
@ -258,7 +258,7 @@ int main(int argc, char *argv[])
if (SDL_memcmp(test_buf, "12345678901234567890", 20) != 0) { if (SDL_memcmp(test_buf, "12345678901234567890", 20) != 0) {
RWOP_ERR_QUIT(rwops); RWOP_ERR_QUIT(rwops);
} }
SDL_RWclose(rwops); SDL_DestroyRW(rwops);
SDL_Log("test3 OK\n"); SDL_Log("test3 OK\n");
/* test 4: same in r+ mode */ /* test 4: same in r+ mode */
@ -309,7 +309,7 @@ int main(int argc, char *argv[])
if (SDL_memcmp(test_buf, "12345678901234567890", 20) != 0) { if (SDL_memcmp(test_buf, "12345678901234567890", 20) != 0) {
RWOP_ERR_QUIT(rwops); RWOP_ERR_QUIT(rwops);
} }
SDL_RWclose(rwops); SDL_DestroyRW(rwops);
SDL_Log("test4 OK\n"); SDL_Log("test4 OK\n");
/* test5 : append mode */ /* test5 : append mode */
@ -366,7 +366,7 @@ int main(int argc, char *argv[])
if (SDL_memcmp(test_buf, "123456789012345678901234567123", 30) != 0) { if (SDL_memcmp(test_buf, "123456789012345678901234567123", 30) != 0) {
RWOP_ERR_QUIT(rwops); RWOP_ERR_QUIT(rwops);
} }
SDL_RWclose(rwops); SDL_DestroyRW(rwops);
SDL_Log("test5 OK\n"); SDL_Log("test5 OK\n");
cleanup(); cleanup();
SDL_Quit(); SDL_Quit();

View file

@ -223,7 +223,7 @@ static int unifont_init(const char *fontname)
lineNumber++; lineNumber++;
} while (bytesRead > 0); } while (bytesRead > 0);
SDL_RWclose(hexFile); SDL_DestroyRW(hexFile);
SDL_Log("unifont: Loaded %" SDL_PRIu32 " glyphs.\n", numGlyphs); SDL_Log("unifont: Loaded %" SDL_PRIu32 " glyphs.\n", numGlyphs);
return 0; return 0;
} }

View file

@ -452,7 +452,7 @@ int main(int argc, char **argv)
SDL_RWread(handle, RawMooseData, MOOSEFRAME_SIZE * MOOSEFRAMES_COUNT); SDL_RWread(handle, RawMooseData, MOOSEFRAME_SIZE * MOOSEFRAMES_COUNT);
SDL_RWclose(handle); SDL_DestroyRW(handle);
/* Create the window and renderer */ /* Create the window and renderer */
window_w = MOOSEPIC_W * scale; window_w = MOOSEPIC_W * scale;

View file

@ -141,7 +141,7 @@ int main(int argc, char **argv)
SDL_WriteU32LE(io, dst_len); /* size */ SDL_WriteU32LE(io, dst_len); /* size */
SDL_RWwrite(io, dst_buf, dst_len); SDL_RWwrite(io, dst_buf, dst_len);
if (SDL_RWclose(io) == -1) { if (SDL_DestroyRW(io) == -1) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "fclose('%s') failed: %s\n", file_out, SDL_GetError()); SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "fclose('%s') failed: %s\n", file_out, SDL_GetError());
ret = 6; ret = 6;
goto end; goto end;

View file

@ -171,7 +171,7 @@ int main(int argc, char **argv)
quit(2); quit(2);
} }
SDL_RWread(handle, MooseFrames, MOOSEFRAME_SIZE * MOOSEFRAMES_COUNT); SDL_RWread(handle, MooseFrames, MOOSEFRAME_SIZE * MOOSEFRAMES_COUNT);
SDL_RWclose(handle); SDL_DestroyRW(handle);
/* Create the window and renderer */ /* Create the window and renderer */
window = SDL_CreateWindow("Happy Moose", MOOSEPIC_W * 4, MOOSEPIC_H * 4, SDL_WINDOW_RESIZABLE); window = SDL_CreateWindow("Happy Moose", MOOSEPIC_W * 4, MOOSEPIC_H * 4, SDL_WINDOW_RESIZABLE);

View file

@ -44,7 +44,7 @@ GetNearbyFilename(const char *file)
rw = SDL_RWFromFile(path, "rb"); rw = SDL_RWFromFile(path, "rb");
if (rw) { if (rw) {
SDL_RWclose(rw); SDL_DestroyRW(rw);
return path; return path;
} }