Skip to content

Commit

Permalink
Add properties to SDL_IOStreams returned by IOFromMem
Browse files Browse the repository at this point in the history
  • Loading branch information
flibitijibibo authored and slouken committed Oct 3, 2024
1 parent 1bb7e2b commit a0de6c4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
17 changes: 17 additions & 0 deletions include/SDL3/SDL_iostream.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,13 @@ extern SDL_DECLSPEC SDL_IOStream * SDLCALL SDL_IOFromFile(const char *file, cons
* buffer, you should use SDL_IOFromConstMem() with a read-only buffer of
* memory instead.
*
* The following properties will be set at creation time by SDL:
*
* - `SDL_PROP_IOSTREAM_MEMORY_POINTER`: this will be the `mem` parameter that
* was passed to this function.
* - `SDL_PROP_IOSTREAM_MEMORY_SIZE_NUMBER`: this will be the `size` parameter
* that was passed to this function.
*
* \param mem a pointer to a buffer to feed an SDL_IOStream stream.
* \param size the buffer size, in bytes.
* \returns a pointer to a new SDL_IOStream structure or NULL on failure; call
Expand All @@ -308,6 +315,9 @@ extern SDL_DECLSPEC SDL_IOStream * SDLCALL SDL_IOFromFile(const char *file, cons
*/
extern SDL_DECLSPEC SDL_IOStream * SDLCALL SDL_IOFromMem(void *mem, size_t size);

#define SDL_PROP_IOSTREAM_MEMORY_POINTER "SDL.iostream.memory.base"
#define SDL_PROP_IOSTREAM_MEMORY_SIZE_NUMBER "SDL.iostream.memory.size"

/**
* Use this function to prepare a read-only memory buffer for use with
* SDL_IOStream.
Expand All @@ -325,6 +335,13 @@ extern SDL_DECLSPEC SDL_IOStream * SDLCALL SDL_IOFromMem(void *mem, size_t size)
* If you need to write to a memory buffer, you should use SDL_IOFromMem()
* with a writable buffer of memory instead.
*
* The following properties will be set at creation time by SDL:
*
* - `SDL_PROP_IOSTREAM_MEMORY_POINTER`: this will be the `mem` parameter that
* was passed to this function.
* - `SDL_PROP_IOSTREAM_MEMORY_SIZE_NUMBER`: this will be the `size` parameter
* that was passed to this function.
*
* \param mem a pointer to a read-only buffer to feed an SDL_IOStream stream.
* \param size the buffer size, in bytes.
* \returns a pointer to a new SDL_IOStream structure or NULL on failure; call
Expand Down
12 changes: 12 additions & 0 deletions src/file/SDL_iostream.c
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,12 @@ SDL_IOStream *SDL_IOFromMem(void *mem, size_t size)
SDL_IOStream *iostr = SDL_OpenIO(&iface, iodata);
if (!iostr) {
SDL_free(iodata);
} else {
const SDL_PropertiesID props = SDL_GetIOProperties(iostr);
if (props) {
SDL_SetPointerProperty(props, SDL_PROP_IOSTREAM_MEMORY_POINTER, mem);
SDL_SetNumberProperty(props, SDL_PROP_IOSTREAM_MEMORY_SIZE_NUMBER, size);
}
}
return iostr;
}
Expand Down Expand Up @@ -827,6 +833,12 @@ SDL_IOStream *SDL_IOFromConstMem(const void *mem, size_t size)
SDL_IOStream *iostr = SDL_OpenIO(&iface, iodata);
if (!iostr) {
SDL_free(iodata);
} else {
const SDL_PropertiesID props = SDL_GetIOProperties(iostr);
if (props) {
SDL_SetPointerProperty(props, SDL_PROP_IOSTREAM_MEMORY_POINTER, (void *)mem);
SDL_SetNumberProperty(props, SDL_PROP_IOSTREAM_MEMORY_SIZE_NUMBER, size);
}
}
return iostr;
}
Expand Down

0 comments on commit a0de6c4

Please sign in to comment.