2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2010 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 /*#define DEBUG_ERROR*/
31 /* Routine to get the thread-specific error variable */
32 #if SDL_THREADS_DISABLED
33 /* !!! FIXME: what does this comment mean? Victim of Search and Replace? */
34 /* The SDL_arraysize(The ),default (non-thread-safe) global error variable */
35 static SDL_error SDL_global_error;
36 #define SDL_GetErrBuf() (&SDL_global_error)
38 extern SDL_error *SDL_GetErrBuf(void);
39 #endif /* SDL_THREADS_DISABLED */
41 #define SDL_ERRBUFIZE 1024
43 /* Private functions */
46 SDL_LookupString(const char *key)
48 /* FIXME: Add code to lookup key in language string hash-table */
52 /* Public functions */
55 SDL_SetError(const char *fmt, ...)
60 /* Copy in the key, mark error as valid */
61 error = SDL_GetErrBuf();
63 SDL_strlcpy((char *) error->key, fmt, sizeof(error->key));
69 while (*fmt == '.' || (*fmt >= '0' && *fmt <= '9')) {
73 case 0: /* Malformed format string.. */
83 error->args[error->argc++].value_i = va_arg(ap, int);
86 error->args[error->argc++].value_f = va_arg(ap, double);
89 error->args[error->argc++].value_ptr = va_arg(ap, void *);
94 const char *str = va_arg(ap, const char *);
97 SDL_strlcpy((char *) error->args[i].buf, str,
105 if (error->argc >= ERR_MAX_ARGS) {
112 /* If we are in debug mode, print out an error message */
114 fprintf(stderr, "SDL_SetError: %s\n", SDL_GetError());
118 /* This function has a bit more overhead than most error functions
119 so that it supports internationalization and thread-safe errors.
122 SDL_GetErrorMsg(char *errstr, unsigned int maxlen)
126 /* Clear the error string */
130 /* Get the thread-safe error, and print it out */
131 error = SDL_GetErrBuf();
138 fmt = SDL_LookupString(error->key);
140 while (*fmt && (maxlen > 0)) {
142 char tmp[32], *spot = tmp;
144 while ((*fmt == '.' || (*fmt >= '0' && *fmt <= '9'))
145 && spot < (tmp + SDL_arraysize(tmp) - 2)) {
163 SDL_snprintf(msg, maxlen, tmp,
164 error->args[argi++].value_i);
170 SDL_snprintf(msg, maxlen, tmp,
171 error->args[argi++].value_f);
177 SDL_snprintf(msg, maxlen, tmp,
178 error->args[argi++].value_ptr);
184 SDL_snprintf(msg, maxlen, tmp,
185 SDL_LookupString(error->args[argi++].
196 *msg = 0; /* NULL terminate the string */
201 /* Available for backwards compatibility */
205 static char errmsg[SDL_ERRBUFIZE];
207 return ((char *) SDL_GetErrorMsg(errmsg, SDL_ERRBUFIZE));
215 error = SDL_GetErrBuf();
219 /* Very common errors go here */
221 SDL_Error(SDL_errorcode code)
225 SDL_SetError("Out of memory");
228 SDL_SetError("Error reading from datastream");
231 SDL_SetError("Error writing to datastream");
234 SDL_SetError("Error seeking in datastream");
236 case SDL_UNSUPPORTED:
237 SDL_SetError("That operation is not supported");
240 SDL_SetError("Unknown SDL error");
247 main(int argc, char *argv[])
249 char buffer[BUFSIZ + 1];
251 SDL_SetError("Hi there!");
252 printf("Error 1: %s\n", SDL_GetError());
254 SDL_memset(buffer, '1', BUFSIZ);
256 SDL_SetError("This is the error: %s (%f)", buffer, 1.0);
257 printf("Error 2: %s\n", SDL_GetError());
261 /* vi: set ts=4 sw=4 expandtab: */