/*1Simple DirectMedia Layer2Copyright (C) 1997-2025 Sam Lantinga <[email protected]>34This software is provided 'as-is', without any express or implied5warranty. In no event will the authors be held liable for any damages6arising from the use of this software.78Permission is granted to anyone to use this software for any purpose,9including commercial applications, and to alter it and redistribute it10freely, subject to the following restrictions:11121. The origin of this software must not be misrepresented; you must not13claim that you wrote the original software. If you use this software14in a product, an acknowledgment in the product documentation would be15appreciated but is not required.162. Altered source versions must be plainly marked as such, and must not be17misrepresented as being the original software.183. This notice may not be removed or altered from any source distribution.19*/20#include "SDL_internal.h"2122// Simple error handling in SDL2324#include "SDL_error_c.h"2526bool SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)27{28va_list ap;29bool result;3031va_start(ap, fmt);32result = SDL_SetErrorV(fmt, ap);33va_end(ap);34return result;35}3637bool SDL_SetErrorV(SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap)38{39// Ignore call if invalid format pointer was passed40if (fmt) {41int result;42SDL_error *error = SDL_GetErrBuf(true);43va_list ap2;4445error->error = SDL_ErrorCodeGeneric;4647va_copy(ap2, ap);48result = SDL_vsnprintf(error->str, error->len, fmt, ap2);49va_end(ap2);5051if (result >= 0 && (size_t)result >= error->len && error->realloc_func) {52size_t len = (size_t)result + 1;53char *str = (char *)error->realloc_func(error->str, len);54if (str) {55error->str = str;56error->len = len;57va_copy(ap2, ap);58(void)SDL_vsnprintf(error->str, error->len, fmt, ap2);59va_end(ap2);60}61}6263// Enable this if you want to see all errors printed as they occur.64// Note that there are many recoverable errors that may happen internally and65// can be safely ignored if the public API doesn't return an error code.66#if 067SDL_LogError(SDL_LOG_CATEGORY_ERROR, "%s", error->str);68#endif69}7071return false;72}7374const char *SDL_GetError(void)75{76const SDL_error *error = SDL_GetErrBuf(false);7778if (!error) {79return "";80}8182switch (error->error) {83case SDL_ErrorCodeGeneric:84return error->str;85case SDL_ErrorCodeOutOfMemory:86return "Out of memory";87default:88return "";89}90}9192bool SDL_ClearError(void)93{94SDL_error *error = SDL_GetErrBuf(false);9596if (error) {97error->error = SDL_ErrorCodeNone;98}99return true;100}101102bool SDL_OutOfMemory(void)103{104SDL_error *error = SDL_GetErrBuf(true);105106if (error) {107error->error = SDL_ErrorCodeOutOfMemory;108}109return false;110}111112113114