2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2006 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
24 /* Simple error handling in SDL */
26 #include "SDL_error.h"
27 #include "SDL_error_c.h"
29 /* Routine to get the thread-specific error variable */
30 #if SDL_THREADS_DISABLED
31 /* The SDL_arraysize(The ),default (non-thread-safe) global error variable */
32 static SDL_error SDL_global_error
;
33 #define SDL_GetErrBuf() (&SDL_global_error)
35 extern SDL_error
*SDL_GetErrBuf(void);
36 #endif /* SDL_THREADS_DISABLED */
38 #define SDL_ERRBUFIZE 1024
40 /* Private functions */
42 static const char *SDL_LookupString(const char *key
)
44 /* FIXME: Add code to lookup key in language string hash-table */
48 /* Public functions */
50 void SDL_SetError (const char *fmt
, ...)
55 /* Copy in the key, mark error as valid */
56 error
= SDL_GetErrBuf();
58 SDL_strlcpy((char *)error
->key
, fmt
, sizeof(error
->key
));
63 if ( *fmt
++ == '%' ) {
64 while ( *fmt
== '.' || (*fmt
>= '0' && *fmt
<= '9') ) {
68 case 0: /* Malformed format string.. */
78 error
->args
[error
->argc
++].value_i
=
82 error
->args
[error
->argc
++].value_f
=
86 error
->args
[error
->argc
++].value_ptr
=
92 const char *str
= va_arg(ap
, const char *);
95 SDL_strlcpy((char *)error
->args
[i
].buf
, str
, ERR_MAX_STRLEN
);
102 if ( error
->argc
>= ERR_MAX_ARGS
) {
109 /* If we are in debug mode, print out an error message */
111 fprintf(stderr
, "SDL_SetError: %s\n", SDL_GetError());
115 /* This function has a bit more overhead than most error functions
116 so that it supports internationalization and thread-safe errors.
118 char *SDL_GetErrorMsg(char *errstr
, unsigned int maxlen
)
122 /* Clear the error string */
123 *errstr
= '\0'; --maxlen
;
125 /* Get the thread-safe error, and print it out */
126 error
= SDL_GetErrBuf();
127 if ( error
->error
) {
133 fmt
= SDL_LookupString(error
->key
);
135 while ( *fmt
&& (maxlen
> 0) ) {
137 char tmp
[32], *spot
= tmp
;
139 while ( (*fmt
== '.' || (*fmt
>= '0' && *fmt
<= '9')) && spot
< (tmp
+SDL_arraysize(tmp
)-2) ) {
156 len
= SDL_snprintf(msg
, maxlen
, tmp
, error
->args
[argi
++].value_i
);
161 len
= SDL_snprintf(msg
, maxlen
, tmp
, error
->args
[argi
++].value_f
);
166 len
= SDL_snprintf(msg
, maxlen
, tmp
, error
->args
[argi
++].value_ptr
);
171 len
= SDL_snprintf(msg
, maxlen
, tmp
, SDL_LookupString(error
->args
[argi
++].buf
));
181 *msg
= 0; /* NULL terminate the string */
186 /* Available for backwards compatibility */
187 char *SDL_GetError (void)
189 static char errmsg
[SDL_ERRBUFIZE
];
191 return((char *)SDL_GetErrorMsg(errmsg
, SDL_ERRBUFIZE
));
194 void SDL_ClearError(void)
198 error
= SDL_GetErrBuf();
202 /* Very common errors go here */
203 void SDL_Error(SDL_errorcode code
)
207 SDL_SetError("Out of memory");
210 SDL_SetError("Error reading from datastream");
213 SDL_SetError("Error writing to datastream");
216 SDL_SetError("Error seeking in datastream");
219 SDL_SetError("Unknown SDL error");
225 int main(int argc
, char *argv
[])
227 char buffer
[BUFSIZ
+1];
229 SDL_SetError("Hi there!");
230 printf("Error 1: %s\n", SDL_GetError());
232 SDL_memset(buffer
, '1', BUFSIZ
);
234 SDL_SetError("This is the error: %s (%f)", buffer
, 1.0);
235 printf("Error 2: %s\n", SDL_GetError());