1 | /* |
2 | Simple DirectMedia Layer |
3 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> |
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 | // Simple error handling in SDL |
24 | |
25 | #include "SDL_error_c.h" |
26 | |
27 | bool SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...) |
28 | { |
29 | va_list ap; |
30 | bool result; |
31 | |
32 | va_start(ap, fmt); |
33 | result = SDL_SetErrorV(fmt, ap); |
34 | va_end(ap); |
35 | return result; |
36 | } |
37 | |
38 | bool SDL_SetErrorV(SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap) |
39 | { |
40 | // Ignore call if invalid format pointer was passed |
41 | if (fmt) { |
42 | int result; |
43 | SDL_error *error = SDL_GetErrBuf(true); |
44 | va_list ap2; |
45 | |
46 | error->error = SDL_ErrorCodeGeneric; |
47 | |
48 | va_copy(ap2, ap); |
49 | result = SDL_vsnprintf(error->str, error->len, fmt, ap2); |
50 | va_end(ap2); |
51 | |
52 | if (result >= 0 && (size_t)result >= error->len && error->realloc_func) { |
53 | size_t len = (size_t)result + 1; |
54 | char *str = (char *)error->realloc_func(error->str, len); |
55 | if (str) { |
56 | error->str = str; |
57 | error->len = len; |
58 | va_copy(ap2, ap); |
59 | (void)SDL_vsnprintf(error->str, error->len, fmt, ap2); |
60 | va_end(ap2); |
61 | } |
62 | } |
63 | |
64 | // Enable this if you want to see all errors printed as they occur. |
65 | // Note that there are many recoverable errors that may happen internally and |
66 | // can be safely ignored if the public API doesn't return an error code. |
67 | #if 0 |
68 | SDL_LogError(SDL_LOG_CATEGORY_ERROR, "%s" , error->str); |
69 | #endif |
70 | } |
71 | |
72 | return false; |
73 | } |
74 | |
75 | const char *SDL_GetError(void) |
76 | { |
77 | const SDL_error *error = SDL_GetErrBuf(false); |
78 | |
79 | if (!error) { |
80 | return "" ; |
81 | } |
82 | |
83 | switch (error->error) { |
84 | case SDL_ErrorCodeGeneric: |
85 | return error->str; |
86 | case SDL_ErrorCodeOutOfMemory: |
87 | return "Out of memory" ; |
88 | default: |
89 | return "" ; |
90 | } |
91 | } |
92 | |
93 | bool SDL_ClearError(void) |
94 | { |
95 | SDL_error *error = SDL_GetErrBuf(false); |
96 | |
97 | if (error) { |
98 | error->error = SDL_ErrorCodeNone; |
99 | } |
100 | return true; |
101 | } |
102 | |
103 | bool SDL_OutOfMemory(void) |
104 | { |
105 | SDL_error *error = SDL_GetErrBuf(true); |
106 | |
107 | if (error) { |
108 | error->error = SDL_ErrorCodeOutOfMemory; |
109 | } |
110 | return false; |
111 | } |
112 | |
113 | |