src/SDL_error.c
author Sam Lantinga
Wed, 12 Jan 2011 21:21:08 -0800
changeset 4992 b12d76ef05e2
parent 4979 be4ba07d9867
child 5221 2ee8112bfc6b
permissions -rw-r--r--
Added an extra line for consistency
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2010 Sam Lantinga
     4 
     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.
     9 
    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.
    14 
    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
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 /* Simple error handling in SDL */
    25 
    26 #ifdef __ANDROID__
    27 #include <android/log.h>
    28 #endif
    29 
    30 #include "SDL_error.h"
    31 #include "SDL_error_c.h"
    32 
    33 /*#define DEBUG_ERROR*/
    34 
    35 /* Routine to get the thread-specific error variable */
    36 #if SDL_THREADS_DISABLED
    37 /* The default (non-thread-safe) global error variable */
    38 static SDL_error SDL_global_error;
    39 #define SDL_GetErrBuf()	(&SDL_global_error)
    40 #else
    41 extern SDL_error *SDL_GetErrBuf(void);
    42 #endif /* SDL_THREADS_DISABLED */
    43 
    44 #define SDL_ERRBUFIZE	1024
    45 
    46 /* Private functions */
    47 
    48 static const char *
    49 SDL_LookupString(const char *key)
    50 {
    51     /* FIXME: Add code to lookup key in language string hash-table */
    52     return key;
    53 }
    54 
    55 /* Public functions */
    56 
    57 void
    58 SDL_SetError(const char *fmt, ...)
    59 {
    60     va_list ap;
    61     SDL_error *error;
    62 
    63     /* Copy in the key, mark error as valid */
    64     error = SDL_GetErrBuf();
    65     error->error = 1;
    66     SDL_strlcpy((char *) error->key, fmt, sizeof(error->key));
    67 
    68     va_start(ap, fmt);
    69     error->argc = 0;
    70     while (*fmt) {
    71         if (*fmt++ == '%') {
    72             while (*fmt == '.' || (*fmt >= '0' && *fmt <= '9')) {
    73                 ++fmt;
    74             }
    75             switch (*fmt++) {
    76             case 0:            /* Malformed format string.. */
    77                 --fmt;
    78                 break;
    79             case 'c':
    80             case 'i':
    81             case 'd':
    82             case 'u':
    83             case 'o':
    84             case 'x':
    85             case 'X':
    86                 error->args[error->argc++].value_i = va_arg(ap, int);
    87                 break;
    88             case 'f':
    89                 error->args[error->argc++].value_f = va_arg(ap, double);
    90                 break;
    91             case 'p':
    92                 error->args[error->argc++].value_ptr = va_arg(ap, void *);
    93                 break;
    94             case 's':
    95                 {
    96                     int i = error->argc;
    97                     const char *str = va_arg(ap, const char *);
    98                     if (str == NULL)
    99                         str = "(null)";
   100                     SDL_strlcpy((char *) error->args[i].buf, str,
   101                                 ERR_MAX_STRLEN);
   102                     error->argc++;
   103                 }
   104                 break;
   105             default:
   106                 break;
   107             }
   108             if (error->argc >= ERR_MAX_ARGS) {
   109                 break;
   110             }
   111         }
   112     }
   113     va_end(ap);
   114 
   115     /* If we are in debug mode, print out an error message */
   116 #ifdef DEBUG_ERROR
   117     fprintf(stderr, "SDL_SetError: %s\n", SDL_GetError());
   118 #ifdef __ANDROID__
   119     __android_log_print(ANDROID_LOG_INFO, "SDL", "ERROR: %s", SDL_GetError());
   120 #endif
   121 #endif /* DEBUG_ERROR */
   122 }
   123 
   124 /* This function has a bit more overhead than most error functions
   125    so that it supports internationalization and thread-safe errors.
   126 */
   127 static char *
   128 SDL_GetErrorMsg(char *errstr, unsigned int maxlen)
   129 {
   130     SDL_error *error;
   131 
   132     /* Clear the error string */
   133     *errstr = '\0';
   134     --maxlen;
   135 
   136     /* Get the thread-safe error, and print it out */
   137     error = SDL_GetErrBuf();
   138     if (error->error) {
   139         const char *fmt;
   140         char *msg = errstr;
   141         int len;
   142         int argi;
   143 
   144         fmt = SDL_LookupString(error->key);
   145         argi = 0;
   146         while (*fmt && (maxlen > 0)) {
   147             if (*fmt == '%') {
   148                 char tmp[32], *spot = tmp;
   149                 *spot++ = *fmt++;
   150                 while ((*fmt == '.' || (*fmt >= '0' && *fmt <= '9'))
   151                        && spot < (tmp + SDL_arraysize(tmp) - 2)) {
   152                     *spot++ = *fmt++;
   153                 }
   154                 *spot++ = *fmt++;
   155                 *spot++ = '\0';
   156                 switch (spot[-2]) {
   157                 case '%':
   158                     *msg++ = '%';
   159                     maxlen -= 1;
   160                     break;
   161                 case 'c':
   162                 case 'i':
   163                 case 'd':
   164                 case 'u':
   165                 case 'o':
   166                 case 'x':
   167                 case 'X':
   168                     len =
   169                         SDL_snprintf(msg, maxlen, tmp,
   170                                      error->args[argi++].value_i);
   171                     msg += len;
   172                     maxlen -= len;
   173                     break;
   174                 case 'f':
   175                     len =
   176                         SDL_snprintf(msg, maxlen, tmp,
   177                                      error->args[argi++].value_f);
   178                     msg += len;
   179                     maxlen -= len;
   180                     break;
   181                 case 'p':
   182                     len =
   183                         SDL_snprintf(msg, maxlen, tmp,
   184                                      error->args[argi++].value_ptr);
   185                     msg += len;
   186                     maxlen -= len;
   187                     break;
   188                 case 's':
   189                     len =
   190                         SDL_snprintf(msg, maxlen, tmp,
   191                                      SDL_LookupString(error->args[argi++].
   192                                                       buf));
   193                     msg += len;
   194                     maxlen -= len;
   195                     break;
   196                 }
   197             } else {
   198                 *msg++ = *fmt++;
   199                 maxlen -= 1;
   200             }
   201         }
   202         *msg = 0;               /* NULL terminate the string */
   203     }
   204     return (errstr);
   205 }
   206 
   207 /* Available for backwards compatibility */
   208 const char *
   209 SDL_GetError(void)
   210 {
   211     static char errmsg[SDL_ERRBUFIZE];
   212 
   213     return SDL_GetErrorMsg(errmsg, SDL_ERRBUFIZE);
   214 }
   215 
   216 void
   217 SDL_ClearError(void)
   218 {
   219     SDL_error *error;
   220 
   221     error = SDL_GetErrBuf();
   222     error->error = 0;
   223 }
   224 
   225 /* Very common errors go here */
   226 void
   227 SDL_Error(SDL_errorcode code)
   228 {
   229     switch (code) {
   230     case SDL_ENOMEM:
   231         SDL_SetError("Out of memory");
   232         break;
   233     case SDL_EFREAD:
   234         SDL_SetError("Error reading from datastream");
   235         break;
   236     case SDL_EFWRITE:
   237         SDL_SetError("Error writing to datastream");
   238         break;
   239     case SDL_EFSEEK:
   240         SDL_SetError("Error seeking in datastream");
   241         break;
   242     case SDL_UNSUPPORTED:
   243         SDL_SetError("That operation is not supported");
   244         break;
   245     default:
   246         SDL_SetError("Unknown SDL error");
   247         break;
   248     }
   249 }
   250 
   251 #ifdef TEST_ERROR
   252 int
   253 main(int argc, char *argv[])
   254 {
   255     char buffer[BUFSIZ + 1];
   256 
   257     SDL_SetError("Hi there!");
   258     printf("Error 1: %s\n", SDL_GetError());
   259     SDL_ClearError();
   260     SDL_memset(buffer, '1', BUFSIZ);
   261     buffer[BUFSIZ] = 0;
   262     SDL_SetError("This is the error: %s (%f)", buffer, 1.0);
   263     printf("Error 2: %s\n", SDL_GetError());
   264     exit(0);
   265 }
   266 #endif
   267 
   268 /* vi: set ts=4 sw=4 expandtab: */