Atlas - SDL_error.c

Home / ext / SDL / src Lines: 1 | Size: 3072 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 4 5 This software is provided 'as-is', without any express or implied 6 warranty. In no event will the authors be held liable for any damages 7 arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, 10 including commercial applications, and to alter it and redistribute it 11 freely, subject to the following restrictions: 12 13 1. The origin of this software must not be misrepresented; you must not 14 claim that you wrote the original software. If you use this software 15 in a product, an acknowledgment in the product documentation would be 16 appreciated but is not required. 17 2. Altered source versions must be plainly marked as such, and must not be 18 misrepresented as being the original software. 19 3. This notice may not be removed or altered from any source distribution. 20*/ 21#include "SDL_internal.h" 22 23#include "stdlib/SDL_vacopy.h" 24 25// Simple error handling in SDL 26 27#include "SDL_error_c.h" 28 29bool SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) 30{ 31 va_list ap; 32 bool result; 33 34 va_start(ap, fmt); 35 result = SDL_SetErrorV(fmt, ap); 36 va_end(ap); 37 return result; 38} 39 40bool SDL_SetErrorV(SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) 41{ 42 // Ignore call if invalid format pointer was passed 43 if (fmt) { 44 int result; 45 SDL_error *error = SDL_GetErrBuf(true); 46 va_list ap2; 47 48 error->error = SDL_ErrorCodeGeneric; 49 50 va_copy(ap2, ap); 51 result = SDL_vsnprintf(error->str, error->len, fmt, ap2); 52 va_end(ap2); 53 54 if (result >= 0 && (size_t)result >= error->len && error->realloc_func) { 55 size_t len = (size_t)result + 1; 56 char *str = (char *)error->realloc_func(error->str, len); 57 if (str) { 58 error->str = str; 59 error->len = len; 60 va_copy(ap2, ap); 61 (void)SDL_vsnprintf(error->str, error->len, fmt, ap2); 62 va_end(ap2); 63 } 64 } 65 66// Enable this if you want to see all errors printed as they occur. 67// Note that there are many recoverable errors that may happen internally and 68// can be safely ignored if the public API doesn't return an error code. 69#if 0 70 SDL_LogError(SDL_LOG_CATEGORY_ERROR, "%s", error->str); 71#endif 72 } 73 74 return false; 75} 76 77const char *SDL_GetError(void) 78{ 79 const SDL_error *error = SDL_GetErrBuf(false); 80 81 if (!error) { 82 return ""; 83 } 84 85 switch (error->error) { 86 case SDL_ErrorCodeGeneric: 87 return error->str; 88 case SDL_ErrorCodeOutOfMemory: 89 return "Out of memory"; 90 default: 91 return ""; 92 } 93} 94 95bool SDL_ClearError(void) 96{ 97 SDL_error *error = SDL_GetErrBuf(false); 98 99 if (error) { 100 error->error = SDL_ErrorCodeNone; 101 } 102 return true; 103} 104 105bool SDL_OutOfMemory(void) 106{ 107 SDL_error *error = SDL_GetErrBuf(true); 108 109 if (error) { 110 error->error = SDL_ErrorCodeOutOfMemory; 111 } 112 return false; 113} 114 115
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.