From 316ff3847b4d9d87d9b0aab15321461db0e8ae0b Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Sat, 8 Jun 2019 17:43:23 -0700 Subject: [PATCH] Fixed bug 4526 - replace SDL_RW* macros with functions for using in bindings ace I got this bug in SDL_ttf: https://bugzilla.libsdl.org/show_bug.cgi?id=4524 Sylvain proposed solution: SDL_RWseek(RWops, 0, RW_SEEK_SET); And it works, but i can use it my project, because it written in C# with SDL2-CS wrapper and there not export for macroses: #define SDL_RWsize(ctx) (ctx)->size(ctx) #define SDL_RWseek(ctx, offset, whence) (ctx)->seek(ctx, offset, whence) #define SDL_RWtell(ctx) (ctx)->seek(ctx, 0, RW_SEEK_CUR) #define SDL_RWread(ctx, ptr, size, n) (ctx)->read(ctx, ptr, size, n) #define SDL_RWwrite(ctx, ptr, size, n) (ctx)->write(ctx, ptr, size, n) #define SDL_RWclose(ctx) (ctx)->close(ctx) Therefore, I suggest replacing this macros with functions so that they can be exported and used in bindings --- include/SDL_rwops.h | 61 +++++++++++++++++++++++++------ src/dynapi/SDL_dynapi_overrides.h | 7 ++++ src/dynapi/SDL_dynapi_procs.h | 7 ++++ src/file/SDL_rwops.c | 42 +++++++++++++++++++++ 4 files changed, 105 insertions(+), 12 deletions(-) diff --git a/include/SDL_rwops.h b/include/SDL_rwops.h index d26fff9bce4cb..f66119fb02249 100644 --- a/include/SDL_rwops.h +++ b/include/SDL_rwops.h @@ -176,19 +176,48 @@ extern DECLSPEC void SDLCALL SDL_FreeRW(SDL_RWops * area); #define RW_SEEK_END 2 /**< Seek relative to the end of data */ /** - * \name Read/write macros + * Return the size of the file in this rwops, or -1 if unknown + */ +extern DECLSPEC Sint64 SDLCALL SDL_RWsize(SDL_RWops *context); + +/** + * Seek to \c offset relative to \c whence, one of stdio's whence values: + * RW_SEEK_SET, RW_SEEK_CUR, RW_SEEK_END * - * Macros to easily read and write from an SDL_RWops structure. + * \return the final offset in the data stream, or -1 on error. */ -/* @{ */ -#define SDL_RWsize(ctx) (ctx)->size(ctx) -#define SDL_RWseek(ctx, offset, whence) (ctx)->seek(ctx, offset, whence) -#define SDL_RWtell(ctx) (ctx)->seek(ctx, 0, RW_SEEK_CUR) -#define SDL_RWread(ctx, ptr, size, n) (ctx)->read(ctx, ptr, size, n) -#define SDL_RWwrite(ctx, ptr, size, n) (ctx)->write(ctx, ptr, size, n) -#define SDL_RWclose(ctx) (ctx)->close(ctx) -/* @} *//* Read/write macros */ +extern DECLSPEC Sint64 SDLCALL SDL_RWseek(SDL_RWops *context, + Sint64 offset, int whence); + +/** + * Return the current offset in the data stream, or -1 on error. + */ +extern DECLSPEC Sint64 SDLCALL SDL_RWtell(SDL_RWops *context); +/** + * Read up to \c maxnum objects each of size \c size from the data + * stream to the area pointed at by \c ptr. + * + * \return the number of objects read, or 0 at error or end of file. + */ +extern DECLSPEC size_t SDLCALL SDL_RWread(SDL_RWops *context, + void *ptr, size_t size, size_t maxnum); + +/** + * Write exactly \c num objects each of size \c size from the area + * pointed at by \c ptr to data stream. + * + * \return the number of objects written, or 0 at error or end of file. + */ +extern DECLSPEC size_t SDLCALL SDL_RWwrite(SDL_RWops *context, + const void *ptr, size_t size, size_t num); + +/** + * Close and free an allocated SDL_RWops structure. + * + * \return 0 if successful or -1 on write error when flushing data. + */ +extern DECLSPEC int SDLCALL SDL_RWclose(SDL_RWops *context); /** * Load all the data from an SDL data stream. @@ -209,9 +238,17 @@ extern DECLSPEC void *SDLCALL SDL_LoadFile_RW(SDL_RWops * src, size_t *datasize, /** * Load an entire file. * - * Convenience macro. + * The data is allocated with a zero byte at the end (null terminated) + * + * If \c datasize is not NULL, it is filled with the size of the data read. + * + * If \c freesrc is non-zero, the stream will be closed after being read. + * + * The data should be freed with SDL_free(). + * + * \return the data, or NULL if there was an error. */ -#define SDL_LoadFile(file, datasize) SDL_LoadFile_RW(SDL_RWFromFile(file, "rb"), datasize, 1) +extern DECLSPEC void *SDLCALL SDL_LoadFile(const char *file, size_t *datasize); /** * \name Read endian functions diff --git a/src/dynapi/SDL_dynapi_overrides.h b/src/dynapi/SDL_dynapi_overrides.h index 5a604e8886cb6..f8c54334808af 100644 --- a/src/dynapi/SDL_dynapi_overrides.h +++ b/src/dynapi/SDL_dynapi_overrides.h @@ -717,3 +717,10 @@ #define SDL_SIMDGetAlignment SDL_SIMDGetAlignment_REAL #define SDL_SIMDAlloc SDL_SIMDAlloc_REAL #define SDL_SIMDFree SDL_SIMDFree_REAL +#define SDL_RWsize SDL_RWsize_REAL +#define SDL_RWseek SDL_RWseek_REAL +#define SDL_RWtell SDL_RWtell_REAL +#define SDL_RWread SDL_RWread_REAL +#define SDL_RWwrite SDL_RWwrite_REAL +#define SDL_RWclose SDL_RWclose_REAL +#define SDL_LoadFile SDL_LoadFile_REAL diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h index bab4c29043b9b..5a227369507a5 100644 --- a/src/dynapi/SDL_dynapi_procs.h +++ b/src/dynapi/SDL_dynapi_procs.h @@ -773,3 +773,10 @@ SDL_DYNAPI_PROC(int,SDL_UIKitRunApp,(int a, char *b, SDL_main_func c),(a,b,c),re SDL_DYNAPI_PROC(size_t,SDL_SIMDGetAlignment,(void),(),return) SDL_DYNAPI_PROC(void*,SDL_SIMDAlloc,(const size_t a),(a),return) SDL_DYNAPI_PROC(void,SDL_SIMDFree,(void *a),(a),) +SDL_DYNAPI_PROC(Sint64,SDL_RWsize,(SDL_RWops *a),(a),return) +SDL_DYNAPI_PROC(Sint64,SDL_RWseek,(SDL_RWops *a, Sint64 b, int c),(a,b,c),return) +SDL_DYNAPI_PROC(Sint64,SDL_RWtell,(SDL_RWops *a),(a),return) +SDL_DYNAPI_PROC(size_t,SDL_RWread,(SDL_RWops *a, void *b, size_t c, size_t d),(a,b,c,d),return) +SDL_DYNAPI_PROC(size_t,SDL_RWwrite,(SDL_RWops *a, const void *b, size_t c, size_t d),(a,b,c,d),return) +SDL_DYNAPI_PROC(int,SDL_RWclose,(SDL_RWops *a),(a),return) +SDL_DYNAPI_PROC(void*,SDL_LoadFile,(const char *a, size_t *b),(a,b),return) diff --git a/src/file/SDL_rwops.c b/src/file/SDL_rwops.c index 41f7b0fbb1a12..56ffb730c5232 100644 --- a/src/file/SDL_rwops.c +++ b/src/file/SDL_rwops.c @@ -752,6 +752,48 @@ SDL_LoadFile_RW(SDL_RWops * src, size_t *datasize, int freesrc) return data; } +void * +SDL_LoadFile(const char *file, size_t *datasize) +{ + return SDL_LoadFile_RW(SDL_RWFromFile(file, "rb"), datasize, 1); +} + +Sint64 +SDL_RWsize(SDL_RWops *context) +{ + return context->size(context); +} + +Sint64 +SDL_RWseek(SDL_RWops *context, Sint64 offset, int whence) +{ + return context->seek(context, offset, whence); +} + +Sint64 +SDL_RWtell(SDL_RWops *context) +{ + return context->seek(context, 0, RW_SEEK_CUR); +} + +size_t +SDL_RWread(SDL_RWops *context, void *ptr, size_t size, size_t maxnum) +{ + return context->read(context, ptr, size, maxnum); +} + +size_t +SDL_RWwrite(SDL_RWops *context, const void *ptr, size_t size, size_t num) +{ + return context->write(context, ptr, size, num); +} + +int +SDL_RWclose(SDL_RWops *context) +{ + return context->close(context); +} + /* Functions for dynamically reading and writing endian-specific values */ Uint8