SDL_AddGamepadMappingsFromFile() has been made into a real function

This commit is contained in:
Sam Lantinga 2023-07-18 12:05:04 -07:00
parent dfc6e8825e
commit 75e7a6fcfa
5 changed files with 43 additions and 17 deletions

View file

@ -175,8 +175,8 @@ extern DECLSPEC int SDLCALL SDL_AddGamepadMapping(const char *mapping);
* processing it, so take this into consideration if you are in a memory * processing it, so take this into consideration if you are in a memory
* constrained environment. * constrained environment.
* *
* \param rw the data stream for the mappings to be added * \param src the data stream for the mappings to be added
* \param freerw non-zero to close the stream after being read * \param freesrc non-zero to close the stream after being read
* \returns the number of mappings added or -1 on error; call SDL_GetError() * \returns the number of mappings added or -1 on error; call SDL_GetError()
* for more information. * for more information.
* *
@ -186,14 +186,32 @@ extern DECLSPEC int SDLCALL SDL_AddGamepadMapping(const char *mapping);
* \sa SDL_AddGamepadMappingsFromFile * \sa SDL_AddGamepadMappingsFromFile
* \sa SDL_GetGamepadMappingForGUID * \sa SDL_GetGamepadMappingForGUID
*/ */
extern DECLSPEC int SDLCALL SDL_AddGamepadMappingsFromRW(SDL_RWops *rw, int freerw); extern DECLSPEC int SDLCALL SDL_AddGamepadMappingsFromRW(SDL_RWops *src, int freesrc);
/** /**
* Load a set of mappings from a file, filtered by the current SDL_GetPlatform() * Load a set of gamepad mappings from a file.
* *
* Convenience macro. * You can call this function several times, if needed, to load different
* database files.
*
* If a new mapping is loaded for an already known gamepad GUID, the later
* version will overwrite the one currently loaded.
*
* Mappings not belonging to the current platform or with no platform field
* specified will be ignored (i.e. mappings for Linux will be ignored in
* Windows, etc).
*
* \param file the mappings file to load
* \returns the number of mappings added or -1 on error; call SDL_GetError()
* for more information.
*
* \since This function is available since SDL 3.0.0.
*
* \sa SDL_AddGamepadMapping
* \sa SDL_AddGamepadMappingsFromRW
* \sa SDL_GetGamepadMappingForGUID
*/ */
#define SDL_AddGamepadMappingsFromFile(file) SDL_AddGamepadMappingsFromRW(SDL_RWFromFile(file, "rb"), 1) extern DECLSPEC int SDLCALL SDL_AddGamepadMappingsFromFile(const char *file);
/** /**
* Get the number of mappings installed. * Get the number of mappings installed.

View file

@ -877,6 +877,7 @@ SDL3_0.0.0 {
SDL_GetRealGamepadType; SDL_GetRealGamepadType;
SDL_wcsnlen; SDL_wcsnlen;
SDL_strnlen; SDL_strnlen;
SDL_AddGamepadMappingsFromFile;
# extra symbols go here (don't modify this line) # extra symbols go here (don't modify this line)
local: *; local: *;
}; };

View file

@ -903,3 +903,4 @@
#define SDL_GetRealGamepadType SDL_GetRealGamepadType_REAL #define SDL_GetRealGamepadType SDL_GetRealGamepadType_REAL
#define SDL_wcsnlen SDL_wcsnlen_REAL #define SDL_wcsnlen SDL_wcsnlen_REAL
#define SDL_strnlen SDL_strnlen_REAL #define SDL_strnlen SDL_strnlen_REAL
#define SDL_AddGamepadMappingsFromFile SDL_AddGamepadMappingsFromFile_REAL

View file

@ -948,3 +948,4 @@ SDL_DYNAPI_PROC(SDL_GamepadType,SDL_GetRealGamepadInstanceType,(SDL_JoystickID a
SDL_DYNAPI_PROC(SDL_GamepadType,SDL_GetRealGamepadType,(SDL_Gamepad *a),(a),return) SDL_DYNAPI_PROC(SDL_GamepadType,SDL_GetRealGamepadType,(SDL_Gamepad *a),(a),return)
SDL_DYNAPI_PROC(size_t,SDL_wcsnlen,(const wchar_t *a, size_t b),(a,b),return) SDL_DYNAPI_PROC(size_t,SDL_wcsnlen,(const wchar_t *a, size_t b),(a,b),return)
SDL_DYNAPI_PROC(size_t,SDL_strnlen,(const char *a, size_t b),(a,b),return) SDL_DYNAPI_PROC(size_t,SDL_strnlen,(const char *a, size_t b),(a,b),return)
SDL_DYNAPI_PROC(int,SDL_AddGamepadMappingsFromFile,(const char *a),(a),return)

View file

@ -1661,7 +1661,7 @@ static GamepadMapping_t *SDL_PrivateGetGamepadMapping(SDL_JoystickID instance_id
/* /*
* Add or update an entry into the Mappings Database * Add or update an entry into the Mappings Database
*/ */
int SDL_AddGamepadMappingsFromRW(SDL_RWops *rw, int freerw) int SDL_AddGamepadMappingsFromRW(SDL_RWops *src, int freesrc)
{ {
const char *platform = SDL_GetPlatform(); const char *platform = SDL_GetPlatform();
int gamepads = 0; int gamepads = 0;
@ -1669,29 +1669,29 @@ int SDL_AddGamepadMappingsFromRW(SDL_RWops *rw, int freerw)
Sint64 db_size; Sint64 db_size;
size_t platform_len; size_t platform_len;
if (rw == NULL) { if (src == NULL) {
return SDL_SetError("Invalid RWops"); return SDL_InvalidParamError("src");
} }
db_size = SDL_RWsize(rw); db_size = SDL_RWsize(src);
buf = (char *)SDL_malloc((size_t)db_size + 1); buf = (char *)SDL_malloc((size_t)db_size + 1);
if (buf == NULL) { if (buf == NULL) {
if (freerw) { if (freesrc) {
SDL_RWclose(rw); SDL_RWclose(src);
} }
return SDL_SetError("Could not allocate space to read DB into memory"); return SDL_SetError("Could not allocate space to read DB into memory");
} }
if (SDL_RWread(rw, buf, db_size) != db_size) { if (SDL_RWread(src, buf, db_size) != db_size) {
if (freerw) { if (freesrc) {
SDL_RWclose(rw); SDL_RWclose(src);
} }
SDL_free(buf); SDL_free(buf);
return SDL_SetError("Could not read DB"); return SDL_SetError("Could not read DB");
} }
if (freerw) { if (freesrc) {
SDL_RWclose(rw); SDL_RWclose(src);
} }
buf[db_size] = '\0'; buf[db_size] = '\0';
@ -1733,6 +1733,11 @@ int SDL_AddGamepadMappingsFromRW(SDL_RWops *rw, int freerw)
return gamepads; return gamepads;
} }
int SDL_AddGamepadMappingsFromFile(const char *file)
{
return SDL_AddGamepadMappingsFromRW(SDL_RWFromFile(file, "rb"), 1);
}
/* /*
* Add or update an entry into the Mappings Database with a priority * Add or update an entry into the Mappings Database with a priority
*/ */