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 /* Routine to get the thread-specific error variable */
30 #if SDL_THREADS_DISABLED
31 /* !!! FIXME: what does this comment mean? Victim of Search and Replace? */
32 /* The SDL_arraysize(The ),default (non-thread-safe) global error variable */
33 static SDL_error SDL_global_error;
34 #define SDL_GetErrBuf() (&SDL_global_error)
36 extern SDL_error *SDL_GetErrBuf(void);
37 #endif /* SDL_THREADS_DISABLED */
39 #define SDL_ERRBUFIZE 1024
41 /* Private functions */
44 SDL_LookupString(const char *key)
46 /* FIXME: Add code to lookup key in language string hash-table */
50 /* Public functions */
53 SDL_SetError(const char *fmt, ...)
58 /* Copy in the key, mark error as valid */
59 error = SDL_GetErrBuf();
61 SDL_strlcpy((char *) error->key, fmt, sizeof(error->key));
67 while (*fmt == '.' || (*fmt >= '0' && *fmt <= '9')) {
71 case 0: /* Malformed format string.. */
81 error->args[error->argc++].value_i = va_arg(ap, int);
84 error->args[error->argc++].value_f = va_arg(ap, double);
87 error->args[error->argc++].value_ptr = va_arg(ap, void *);
92 const char *str = va_arg(ap, const char *);
95 SDL_strlcpy((char *) error->args[i].buf, str,
103 if (error->argc >= ERR_MAX_ARGS) {
110 /* If we are in debug mode, print out an error message */
112 fprintf(stderr, "SDL_SetError: %s\n", SDL_GetError());
116 /* This function has a bit more overhead than most error functions
117 so that it supports internationalization and thread-safe errors.
120 SDL_GetErrorMsg(char *errstr, unsigned int maxlen)
124 /* Clear the error string */
128 /* Get the thread-safe error, and print it out */
129 error = SDL_GetErrBuf();
136 fmt = SDL_LookupString(error->key);
138 while (*fmt && (maxlen > 0)) {
140 char tmp[32], *spot = tmp;
142 while ((*fmt == '.' || (*fmt >= '0' && *fmt <= '9'))
143 && spot < (tmp + SDL_arraysize(tmp) - 2)) {
161 SDL_snprintf(msg, maxlen, tmp,
162 error->args[argi++].value_i);
168 SDL_snprintf(msg, maxlen, tmp,
169 error->args[argi++].value_f);
175 SDL_snprintf(msg, maxlen, tmp,
176 error->args[argi++].value_ptr);
182 SDL_snprintf(msg, maxlen, tmp,
183 SDL_LookupString(error->args[argi++].
194 *msg = 0; /* NULL terminate the string */
199 /* Available for backwards compatibility */
203 static char errmsg[SDL_ERRBUFIZE];
205 return ((char *) SDL_GetErrorMsg(errmsg, SDL_ERRBUFIZE));
213 error = SDL_GetErrBuf();
217 /* Very common errors go here */
219 SDL_Error(SDL_errorcode code)
223 SDL_SetError("Out of memory");
226 SDL_SetError("Error reading from datastream");
229 SDL_SetError("Error writing to datastream");
232 SDL_SetError("Error seeking in datastream");
234 case SDL_UNSUPPORTED:
235 SDL_SetError("That operation is not supported");
238 SDL_SetError("Unknown SDL error");
245 main(int argc, char *argv[])
247 char buffer[BUFSIZ + 1];
249 SDL_SetError("Hi there!");
250 printf("Error 1: %s\n", SDL_GetError());
252 SDL_memset(buffer, '1', BUFSIZ);
254 SDL_SetError("This is the error: %s (%f)", buffer, 1.0);
255 printf("Error 2: %s\n", SDL_GetError());
259 /* vi: set ts=4 sw=4 expandtab: */