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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
diff --git a/include/SDL_rwops.h b/include/SDL_rwops.h
index 00d82a6..7f0cbdf 100644
--- a/include/SDL_rwops.h
+++ b/include/SDL_rwops.h
@@ -191,6 +191,29 @@ extern DECLSPEC void SDLCALL SDL_FreeRW(SDL_RWops * area);
/**
+ * Load all the data from an SDL data stream.
+ *
+ * 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.
+ */
+extern DECLSPEC void *SDLCALL SDL_LoadFile_RW(SDL_RWops * src, size_t *datasize,
+ int freesrc);
+
+/**
+ * Load an entire file.
+ *
+ * Convenience macro.
+ */
+#define SDL_LoadFile(file, datasize) SDL_LoadFile_RW(SDL_RWFromFile(file, "rb"), datasize, 1)
+
+/**
* \name Read endian functions
*
* Read an item of the specified endianness and return in native format.
diff --git a/src/dynapi/SDL_dynapi_overrides.h b/src/dynapi/SDL_dynapi_overrides.h
index c848a7f..819e410 100644
--- a/src/dynapi/SDL_dynapi_overrides.h
+++ b/src/dynapi/SDL_dynapi_overrides.h
@@ -625,3 +625,4 @@
#define SDL_MemoryBarrierAcquireFunction SDL_MemoryBarrierAcquireFunction_REAL
#define SDL_JoystickGetDeviceInstanceID SDL_JoystickGetDeviceInstanceID_REAL
#define SDL_utf8strlen SDL_utf8strlen_REAL
+#define SDL_LoadFile_RW SDL_LoadFile_RW_REAL
diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h
index 9583a34..4db5288 100644
--- a/src/dynapi/SDL_dynapi_procs.h
+++ b/src/dynapi/SDL_dynapi_procs.h
@@ -657,3 +657,4 @@ SDL_DYNAPI_PROC(void,SDL_MemoryBarrierReleaseFunction,(void),(),)
SDL_DYNAPI_PROC(void,SDL_MemoryBarrierAcquireFunction,(void),(),)
SDL_DYNAPI_PROC(SDL_JoystickID,SDL_JoystickGetDeviceInstanceID,(int a),(a),return)
SDL_DYNAPI_PROC(size_t,SDL_utf8strlen,(const char *a),(a),return)
+SDL_DYNAPI_PROC(void*,SDL_LoadFile_RW,(SDL_RWops *a, size_t *b, int c),(a,b,c),return)
diff --git a/src/file/SDL_rwops.c b/src/file/SDL_rwops.c
index 2d3bff6..a3ca213 100644
--- a/src/file/SDL_rwops.c
+++ b/src/file/SDL_rwops.c
@@ -653,6 +653,59 @@ SDL_FreeRW(SDL_RWops * area)
SDL_free(area);
}
+/* Load all the data from an SDL data stream */
+void *
+SDL_LoadFile_RW(SDL_RWops * src, size_t *datasize, int freesrc)
+{
+ const int FILE_CHUNK_SIZE = 1024;
+ Sint64 size;
+ size_t size_read, size_total;
+ void *data = NULL, *newdata;
+
+ if (!src) {
+ SDL_InvalidParamError("src");
+ return NULL;
+ }
+
+ size = SDL_RWsize(src);
+ if (size < 0) {
+ size = FILE_CHUNK_SIZE;
+ }
+ data = SDL_malloc(size+1);
+
+ size_total = 0;
+ for (;;) {
+ if ((size_total + FILE_CHUNK_SIZE) > size) {
+ size = (size_total + FILE_CHUNK_SIZE);
+ newdata = SDL_realloc(data, size + 1);
+ if (!newdata) {
+ SDL_free(data);
+ data = NULL;
+ SDL_OutOfMemory();
+ goto done;
+ }
+ data = newdata;
+ }
+
+ size_read = SDL_RWread(src, (char *)data+size_total, 1, (size_t)(size-size_total));
+ if (size_read == 0) {
+ break;
+ }
+ size_total += size_read;
+ }
+
+ if (datasize) {
+ *datasize = size_total;
+ }
+ ((char *)data)[size_total] = '\0';
+
+done:
+ if (freesrc && src) {
+ SDL_RWclose(src);
+ }
+ return data;
+}
+
/* Functions for dynamically reading and writing endian-specific values */
Uint8