Added an API SDL_LoadFile_RW() to load all the data from an SDL data stream, and a convenience macro SDL_LoadFile() to load all the data from a file.
authorSam Lantinga <slouken@libsdl.org>
Wed, 09 Aug 2017 11:58:38 -0700
changeset 112008bcc1cc9ac4c
parent 11199 714324c6dd1f
child 11201 813a8510bd0c
Added an API SDL_LoadFile_RW() to load all the data from an SDL data stream, and a convenience macro SDL_LoadFile() to load all the data from a file.
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	Wed Aug 09 01:01:41 2017 -0400
     1.2 +++ b/include/SDL_rwops.h	Wed Aug 09 11:58:38 2017 -0700
     1.3 @@ -191,6 +191,29 @@
     1.4  
     1.5  
     1.6  /**
     1.7 + *  Load all the data from an SDL data stream.
     1.8 + *
     1.9 + *  The data is allocated with a zero byte at the end (null terminated)
    1.10 + *
    1.11 + *  If \c datasize is not NULL, it is filled with the size of the data read.
    1.12 + *
    1.13 + *  If \c freesrc is non-zero, the stream will be closed after being read.
    1.14 + *
    1.15 + *  The data should be freed with SDL_free().
    1.16 + *
    1.17 + *  \return the data, or NULL if there was an error.
    1.18 + */
    1.19 +extern DECLSPEC void *SDLCALL SDL_LoadFile_RW(SDL_RWops * src, size_t *datasize,
    1.20 +                                                    int freesrc);
    1.21 +
    1.22 +/**
    1.23 + *  Load an entire file.
    1.24 + *
    1.25 + *  Convenience macro.
    1.26 + */
    1.27 +#define SDL_LoadFile(file, datasize)   SDL_LoadFile_RW(SDL_RWFromFile(file, "rb"), datasize, 1)
    1.28 +
    1.29 +/**
    1.30   *  \name Read endian functions
    1.31   *
    1.32   *  Read an item of the specified endianness and return in native format.
     2.1 --- a/src/dynapi/SDL_dynapi_overrides.h	Wed Aug 09 01:01:41 2017 -0400
     2.2 +++ b/src/dynapi/SDL_dynapi_overrides.h	Wed Aug 09 11:58:38 2017 -0700
     2.3 @@ -625,3 +625,4 @@
     2.4  #define SDL_MemoryBarrierAcquireFunction SDL_MemoryBarrierAcquireFunction_REAL
     2.5  #define SDL_JoystickGetDeviceInstanceID SDL_JoystickGetDeviceInstanceID_REAL
     2.6  #define SDL_utf8strlen SDL_utf8strlen_REAL
     2.7 +#define SDL_LoadFile_RW SDL_LoadFile_RW_REAL
     3.1 --- a/src/dynapi/SDL_dynapi_procs.h	Wed Aug 09 01:01:41 2017 -0400
     3.2 +++ b/src/dynapi/SDL_dynapi_procs.h	Wed Aug 09 11:58:38 2017 -0700
     3.3 @@ -657,3 +657,4 @@
     3.4  SDL_DYNAPI_PROC(void,SDL_MemoryBarrierAcquireFunction,(void),(),)
     3.5  SDL_DYNAPI_PROC(SDL_JoystickID,SDL_JoystickGetDeviceInstanceID,(int a),(a),return)
     3.6  SDL_DYNAPI_PROC(size_t,SDL_utf8strlen,(const char *a),(a),return)
     3.7 +SDL_DYNAPI_PROC(void*,SDL_LoadFile_RW,(SDL_RWops *a, size_t *b, int c),(a,b,c),return)
     4.1 --- a/src/file/SDL_rwops.c	Wed Aug 09 01:01:41 2017 -0400
     4.2 +++ b/src/file/SDL_rwops.c	Wed Aug 09 11:58:38 2017 -0700
     4.3 @@ -653,6 +653,59 @@
     4.4      SDL_free(area);
     4.5  }
     4.6  
     4.7 +/* Load all the data from an SDL data stream */
     4.8 +void *
     4.9 +SDL_LoadFile_RW(SDL_RWops * src, size_t *datasize, int freesrc)
    4.10 +{
    4.11 +    const int FILE_CHUNK_SIZE = 1024;
    4.12 +    Sint64 size;
    4.13 +    size_t size_read, size_total;
    4.14 +    void *data = NULL, *newdata;
    4.15 +
    4.16 +    if (!src) {
    4.17 +        SDL_InvalidParamError("src");
    4.18 +        return NULL;
    4.19 +    }
    4.20 +
    4.21 +    size = SDL_RWsize(src);
    4.22 +    if (size < 0) {
    4.23 +        size = FILE_CHUNK_SIZE;
    4.24 +    }
    4.25 +    data = SDL_malloc(size+1);
    4.26 +
    4.27 +    size_total = 0;
    4.28 +    for (;;) {
    4.29 +        if ((size_total + FILE_CHUNK_SIZE) > size) {
    4.30 +            size = (size_total + FILE_CHUNK_SIZE);
    4.31 +            newdata = SDL_realloc(data, size + 1);
    4.32 +            if (!newdata) {
    4.33 +                SDL_free(data);
    4.34 +                data = NULL;
    4.35 +                SDL_OutOfMemory();
    4.36 +                goto done;
    4.37 +            }
    4.38 +            data = newdata;
    4.39 +        }
    4.40 +
    4.41 +        size_read = SDL_RWread(src, (char *)data+size_total, 1, (size_t)(size-size_total));
    4.42 +        if (size_read == 0) {
    4.43 +            break;
    4.44 +        }
    4.45 +        size_total += size_read;
    4.46 +    }
    4.47 +
    4.48 +    if (datasize) {
    4.49 +        *datasize = size_total;
    4.50 +    }
    4.51 +    ((char *)data)[size_total] = '\0';
    4.52 +
    4.53 +done:
    4.54 +    if (freesrc && src) {
    4.55 +        SDL_RWclose(src);
    4.56 +    }
    4.57 +    return data;
    4.58 +}
    4.59 +
    4.60  /* Functions for dynamically reading and writing endian-specific values */
    4.61  
    4.62  Uint8