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