src/file/SDL_rwops.c
changeset 11200 8bcc1cc9ac4c
parent 10737 3406a0f8b041
child 11230 15b10ad902dd
     1.1 --- a/src/file/SDL_rwops.c	Wed Aug 09 01:01:41 2017 -0400
     1.2 +++ b/src/file/SDL_rwops.c	Wed Aug 09 11:58:38 2017 -0700
     1.3 @@ -653,6 +653,59 @@
     1.4      SDL_free(area);
     1.5  }
     1.6  
     1.7 +/* Load all the data from an SDL data stream */
     1.8 +void *
     1.9 +SDL_LoadFile_RW(SDL_RWops * src, size_t *datasize, int freesrc)
    1.10 +{
    1.11 +    const int FILE_CHUNK_SIZE = 1024;
    1.12 +    Sint64 size;
    1.13 +    size_t size_read, size_total;
    1.14 +    void *data = NULL, *newdata;
    1.15 +
    1.16 +    if (!src) {
    1.17 +        SDL_InvalidParamError("src");
    1.18 +        return NULL;
    1.19 +    }
    1.20 +
    1.21 +    size = SDL_RWsize(src);
    1.22 +    if (size < 0) {
    1.23 +        size = FILE_CHUNK_SIZE;
    1.24 +    }
    1.25 +    data = SDL_malloc(size+1);
    1.26 +
    1.27 +    size_total = 0;
    1.28 +    for (;;) {
    1.29 +        if ((size_total + FILE_CHUNK_SIZE) > size) {
    1.30 +            size = (size_total + FILE_CHUNK_SIZE);
    1.31 +            newdata = SDL_realloc(data, size + 1);
    1.32 +            if (!newdata) {
    1.33 +                SDL_free(data);
    1.34 +                data = NULL;
    1.35 +                SDL_OutOfMemory();
    1.36 +                goto done;
    1.37 +            }
    1.38 +            data = newdata;
    1.39 +        }
    1.40 +
    1.41 +        size_read = SDL_RWread(src, (char *)data+size_total, 1, (size_t)(size-size_total));
    1.42 +        if (size_read == 0) {
    1.43 +            break;
    1.44 +        }
    1.45 +        size_total += size_read;
    1.46 +    }
    1.47 +
    1.48 +    if (datasize) {
    1.49 +        *datasize = size_total;
    1.50 +    }
    1.51 +    ((char *)data)[size_total] = '\0';
    1.52 +
    1.53 +done:
    1.54 +    if (freesrc && src) {
    1.55 +        SDL_RWclose(src);
    1.56 +    }
    1.57 +    return data;
    1.58 +}
    1.59 +
    1.60  /* Functions for dynamically reading and writing endian-specific values */
    1.61  
    1.62  Uint8