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