Fixed bug 4526 - replace SDL_RW* macros with functions for using in bindings
authorSam Lantinga <slouken@libsdl.org>
Sat, 08 Jun 2019 17:43:23 -0700
changeset 12799302904fa669a
parent 12798 92061a78e730
child 12802 f9a9d6c76b21
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
src/dynapi/SDL_dynapi_overrides.h
src/dynapi/SDL_dynapi_procs.h
src/file/SDL_rwops.c
     1.1 --- a/include/SDL_rwops.h	Sat Jun 08 15:10:20 2019 -0700
     1.2 +++ b/include/SDL_rwops.h	Sat Jun 08 17:43:23 2019 -0700
     1.3 @@ -176,19 +176,48 @@
     1.4  #define RW_SEEK_END 2       /**< Seek relative to the end of data */
     1.5  
     1.6  /**
     1.7 - *  \name Read/write macros
     1.8 + *  Return the size of the file in this rwops, or -1 if unknown
     1.9 + */
    1.10 +extern DECLSPEC Sint64 SDLCALL SDL_RWsize(SDL_RWops *context);
    1.11 +
    1.12 +/**
    1.13 + *  Seek to \c offset relative to \c whence, one of stdio's whence values:
    1.14 + *  RW_SEEK_SET, RW_SEEK_CUR, RW_SEEK_END
    1.15   *
    1.16 - *  Macros to easily read and write from an SDL_RWops structure.
    1.17 + *  \return the final offset in the data stream, or -1 on error.
    1.18   */
    1.19 -/* @{ */
    1.20 -#define SDL_RWsize(ctx)         (ctx)->size(ctx)
    1.21 -#define SDL_RWseek(ctx, offset, whence) (ctx)->seek(ctx, offset, whence)
    1.22 -#define SDL_RWtell(ctx)         (ctx)->seek(ctx, 0, RW_SEEK_CUR)
    1.23 -#define SDL_RWread(ctx, ptr, size, n)   (ctx)->read(ctx, ptr, size, n)
    1.24 -#define SDL_RWwrite(ctx, ptr, size, n)  (ctx)->write(ctx, ptr, size, n)
    1.25 -#define SDL_RWclose(ctx)        (ctx)->close(ctx)
    1.26 -/* @} *//* Read/write macros */
    1.27 +extern DECLSPEC Sint64 SDLCALL SDL_RWseek(SDL_RWops *context,
    1.28 +                                          Sint64 offset, int whence);
    1.29  
    1.30 +/**
    1.31 + *  Return the current offset in the data stream, or -1 on error.
    1.32 + */
    1.33 +extern DECLSPEC Sint64 SDLCALL SDL_RWtell(SDL_RWops *context);
    1.34 +
    1.35 +/**
    1.36 + *  Read up to \c maxnum objects each of size \c size from the data
    1.37 + *  stream to the area pointed at by \c ptr.
    1.38 + *
    1.39 + *  \return the number of objects read, or 0 at error or end of file.
    1.40 + */
    1.41 +extern DECLSPEC size_t SDLCALL SDL_RWread(SDL_RWops *context,
    1.42 +                                          void *ptr, size_t size, size_t maxnum);
    1.43 +
    1.44 +/**
    1.45 + *  Write exactly \c num objects each of size \c size from the area
    1.46 + *  pointed at by \c ptr to data stream.
    1.47 + *
    1.48 + *  \return the number of objects written, or 0 at error or end of file.
    1.49 + */
    1.50 +extern DECLSPEC size_t SDLCALL SDL_RWwrite(SDL_RWops *context,
    1.51 +                                           const void *ptr, size_t size, size_t num);
    1.52 +
    1.53 +/**
    1.54 + *  Close and free an allocated SDL_RWops structure.
    1.55 + *
    1.56 + *  \return 0 if successful or -1 on write error when flushing data.
    1.57 + */
    1.58 +extern DECLSPEC int SDLCALL SDL_RWclose(SDL_RWops *context);
    1.59  
    1.60  /**
    1.61   *  Load all the data from an SDL data stream.
    1.62 @@ -209,9 +238,17 @@
    1.63  /**
    1.64   *  Load an entire file.
    1.65   *
    1.66 - *  Convenience macro.
    1.67 + *  The data is allocated with a zero byte at the end (null terminated)
    1.68 + *
    1.69 + *  If \c datasize is not NULL, it is filled with the size of the data read.
    1.70 + *
    1.71 + *  If \c freesrc is non-zero, the stream will be closed after being read.
    1.72 + *
    1.73 + *  The data should be freed with SDL_free().
    1.74 + *
    1.75 + *  \return the data, or NULL if there was an error.
    1.76   */
    1.77 -#define SDL_LoadFile(file, datasize)   SDL_LoadFile_RW(SDL_RWFromFile(file, "rb"), datasize, 1)
    1.78 +extern DECLSPEC void *SDLCALL SDL_LoadFile(const char *file, size_t *datasize);
    1.79  
    1.80  /**
    1.81   *  \name Read endian functions
     2.1 --- a/src/dynapi/SDL_dynapi_overrides.h	Sat Jun 08 15:10:20 2019 -0700
     2.2 +++ b/src/dynapi/SDL_dynapi_overrides.h	Sat Jun 08 17:43:23 2019 -0700
     2.3 @@ -717,3 +717,10 @@
     2.4  #define SDL_SIMDGetAlignment SDL_SIMDGetAlignment_REAL
     2.5  #define SDL_SIMDAlloc SDL_SIMDAlloc_REAL
     2.6  #define SDL_SIMDFree SDL_SIMDFree_REAL
     2.7 +#define SDL_RWsize SDL_RWsize_REAL
     2.8 +#define SDL_RWseek SDL_RWseek_REAL
     2.9 +#define SDL_RWtell SDL_RWtell_REAL
    2.10 +#define SDL_RWread SDL_RWread_REAL
    2.11 +#define SDL_RWwrite SDL_RWwrite_REAL
    2.12 +#define SDL_RWclose SDL_RWclose_REAL
    2.13 +#define SDL_LoadFile SDL_LoadFile_REAL
     3.1 --- a/src/dynapi/SDL_dynapi_procs.h	Sat Jun 08 15:10:20 2019 -0700
     3.2 +++ b/src/dynapi/SDL_dynapi_procs.h	Sat Jun 08 17:43:23 2019 -0700
     3.3 @@ -773,3 +773,10 @@
     3.4  SDL_DYNAPI_PROC(size_t,SDL_SIMDGetAlignment,(void),(),return)
     3.5  SDL_DYNAPI_PROC(void*,SDL_SIMDAlloc,(const size_t a),(a),return)
     3.6  SDL_DYNAPI_PROC(void,SDL_SIMDFree,(void *a),(a),)
     3.7 +SDL_DYNAPI_PROC(Sint64,SDL_RWsize,(SDL_RWops *a),(a),return)
     3.8 +SDL_DYNAPI_PROC(Sint64,SDL_RWseek,(SDL_RWops *a, Sint64 b, int c),(a,b,c),return)
     3.9 +SDL_DYNAPI_PROC(Sint64,SDL_RWtell,(SDL_RWops *a),(a),return)
    3.10 +SDL_DYNAPI_PROC(size_t,SDL_RWread,(SDL_RWops *a, void *b, size_t c, size_t d),(a,b,c,d),return)
    3.11 +SDL_DYNAPI_PROC(size_t,SDL_RWwrite,(SDL_RWops *a, const void *b, size_t c, size_t d),(a,b,c,d),return)
    3.12 +SDL_DYNAPI_PROC(int,SDL_RWclose,(SDL_RWops *a),(a),return)
    3.13 +SDL_DYNAPI_PROC(void*,SDL_LoadFile,(const char *a, size_t *b),(a,b),return)
     4.1 --- a/src/file/SDL_rwops.c	Sat Jun 08 15:10:20 2019 -0700
     4.2 +++ b/src/file/SDL_rwops.c	Sat Jun 08 17:43:23 2019 -0700
     4.3 @@ -752,6 +752,48 @@
     4.4      return data;
     4.5  }
     4.6  
     4.7 +void *
     4.8 +SDL_LoadFile(const char *file, size_t *datasize)
     4.9 +{
    4.10 +   return SDL_LoadFile_RW(SDL_RWFromFile(file, "rb"), datasize, 1);
    4.11 +}
    4.12 +
    4.13 +Sint64
    4.14 +SDL_RWsize(SDL_RWops *context)
    4.15 +{
    4.16 +    return context->size(context);
    4.17 +}
    4.18 +
    4.19 +Sint64
    4.20 +SDL_RWseek(SDL_RWops *context, Sint64 offset, int whence)
    4.21 +{
    4.22 +    return context->seek(context, offset, whence);
    4.23 +}
    4.24 +
    4.25 +Sint64
    4.26 +SDL_RWtell(SDL_RWops *context)
    4.27 +{
    4.28 +    return context->seek(context, 0, RW_SEEK_CUR);
    4.29 +}
    4.30 +
    4.31 +size_t
    4.32 +SDL_RWread(SDL_RWops *context, void *ptr, size_t size, size_t maxnum)
    4.33 +{
    4.34 +    return context->read(context, ptr, size, maxnum);
    4.35 +}
    4.36 +
    4.37 +size_t
    4.38 +SDL_RWwrite(SDL_RWops *context, const void *ptr, size_t size, size_t num)
    4.39 +{
    4.40 +    return context->write(context, ptr, size, num);
    4.41 +}
    4.42 +
    4.43 +int
    4.44 +SDL_RWclose(SDL_RWops *context)
    4.45 +{
    4.46 +    return context->close(context);
    4.47 +}
    4.48 +
    4.49  /* Functions for dynamically reading and writing endian-specific values */
    4.50  
    4.51  Uint8