src/SDL_error.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 23 Nov 2016 17:20:28 -0500
changeset 10644 594c17c446f6
parent 10643 930938c7ca82
child 10737 3406a0f8b041
permissions -rw-r--r--
Still more compiler warning fixes for various platforms.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "./SDL_internal.h"
    22 
    23 /* Simple error handling in SDL */
    24 
    25 #include "SDL_log.h"
    26 #include "SDL_error.h"
    27 #include "SDL_error_c.h"
    28 
    29 
    30 /* Routine to get the thread-specific error variable */
    31 #if SDL_THREADS_DISABLED
    32 /* The default (non-thread-safe) global error variable */
    33 static SDL_error SDL_global_error;
    34 #define SDL_GetErrBuf() (&SDL_global_error)
    35 #else
    36 extern SDL_error *SDL_GetErrBuf(void);
    37 #endif /* SDL_THREADS_DISABLED */
    38 
    39 #define SDL_ERRBUFIZE   1024
    40 
    41 /* Private functions */
    42 
    43 static const char *
    44 SDL_LookupString(const char *key)
    45 {
    46     /* FIXME: Add code to lookup key in language string hash-table */
    47     return key;
    48 }
    49 
    50 /* Public functions */
    51 
    52 int
    53 SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
    54 {
    55     va_list ap;
    56     SDL_error *error;
    57 
    58     /* Ignore call if invalid format pointer was passed */
    59     if (fmt == NULL) return -1;
    60 
    61     /* Copy in the key, mark error as valid */
    62     error = SDL_GetErrBuf();
    63     error->error = 1;
    64     SDL_strlcpy((char *) error->key, fmt, sizeof(error->key));
    65 
    66     va_start(ap, fmt);
    67     error->argc = 0;
    68     while (*fmt) {
    69         if (*fmt++ == '%') {
    70             while (*fmt == '.' || (*fmt >= '0' && *fmt <= '9')) {
    71                 ++fmt;
    72             }
    73             switch (*fmt++) {
    74             case 0:            /* Malformed format string.. */
    75                 --fmt;
    76                 break;
    77             case 'c':
    78             case 'i':
    79             case 'd':
    80             case 'u':
    81             case 'o':
    82             case 'x':
    83             case 'X':
    84                 error->args[error->argc++].value_i = va_arg(ap, int);
    85                 break;
    86             case 'f':
    87                 error->args[error->argc++].value_f = va_arg(ap, double);
    88                 break;
    89             case 'p':
    90                 error->args[error->argc++].value_ptr = va_arg(ap, void *);
    91                 break;
    92             case 's':
    93                 {
    94                     int i = error->argc;
    95                     const char *str = va_arg(ap, const char *);
    96                     if (str == NULL)
    97                         str = "(null)";
    98                     SDL_strlcpy((char *) error->args[i].buf, str,
    99                                 ERR_MAX_STRLEN);
   100                     error->argc++;
   101                 }
   102                 break;
   103             default:
   104                 break;
   105             }
   106             if (error->argc >= ERR_MAX_ARGS) {
   107                 break;
   108             }
   109         }
   110     }
   111     va_end(ap);
   112 
   113     /* If we are in debug mode, print out an error message */
   114     SDL_LogDebug(SDL_LOG_CATEGORY_ERROR, "%s", SDL_GetError());
   115 
   116     return -1;
   117 }
   118 
   119 static char *SDL_GetErrorMsg(char *errstr, int maxlen);
   120 
   121 /* Available for backwards compatibility */
   122 const char *
   123 SDL_GetError(void)
   124 {
   125     static char errmsg[SDL_ERRBUFIZE];
   126 
   127     return SDL_GetErrorMsg(errmsg, SDL_ERRBUFIZE);
   128 }
   129 
   130 void
   131 SDL_ClearError(void)
   132 {
   133     SDL_error *error;
   134 
   135     error = SDL_GetErrBuf();
   136     error->error = 0;
   137 }
   138 
   139 /* Very common errors go here */
   140 int
   141 SDL_Error(SDL_errorcode code)
   142 {
   143     switch (code) {
   144     case SDL_ENOMEM:
   145         return SDL_SetError("Out of memory");
   146     case SDL_EFREAD:
   147         return SDL_SetError("Error reading from datastream");
   148     case SDL_EFWRITE:
   149         return SDL_SetError("Error writing to datastream");
   150     case SDL_EFSEEK:
   151         return SDL_SetError("Error seeking in datastream");
   152     case SDL_UNSUPPORTED:
   153         return SDL_SetError("That operation is not supported");
   154     default:
   155         return SDL_SetError("Unknown SDL error");
   156     }
   157 }
   158 
   159 #ifdef TEST_ERROR
   160 int
   161 main(int argc, char *argv[])
   162 {
   163     char buffer[BUFSIZ + 1];
   164 
   165     SDL_SetError("Hi there!");
   166     printf("Error 1: %s\n", SDL_GetError());
   167     SDL_ClearError();
   168     SDL_memset(buffer, '1', BUFSIZ);
   169     buffer[BUFSIZ] = 0;
   170     SDL_SetError("This is the error: %s (%f)", buffer, 1.0);
   171     printf("Error 2: %s\n", SDL_GetError());
   172     exit(0);
   173 }
   174 #endif
   175 
   176 
   177 /* keep this at the end of the file so it works with GCC builds that don't
   178    support "#pragma GCC diagnostic push" ... we'll just leave the warning
   179    disabled after this. */
   180 /* this pragma arrived in GCC 4.2 and causes a warning on older GCCs! Sigh. */
   181 #if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && (__GNUC_MINOR__ >= 2))))
   182 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
   183 #endif
   184 
   185 /* This function has a bit more overhead than most error functions
   186    so that it supports internationalization and thread-safe errors.
   187 */
   188 static char *
   189 SDL_GetErrorMsg(char *errstr, int maxlen)
   190 {
   191     SDL_error *error;
   192 
   193     /* Clear the error string */
   194     *errstr = '\0';
   195     --maxlen;
   196 
   197     /* Get the thread-safe error, and print it out */
   198     error = SDL_GetErrBuf();
   199     if (error->error) {
   200         const char *fmt;
   201         char *msg = errstr;
   202         int len;
   203         int argi;
   204 
   205         fmt = SDL_LookupString(error->key);
   206         argi = 0;
   207         while (*fmt && (maxlen > 0)) {
   208             if (*fmt == '%') {
   209                 char tmp[32], *spot = tmp;
   210                 *spot++ = *fmt++;
   211                 while ((*fmt == '.' || (*fmt >= '0' && *fmt <= '9'))
   212                        && spot < (tmp + SDL_arraysize(tmp) - 2)) {
   213                     *spot++ = *fmt++;
   214                 }
   215                 *spot++ = *fmt++;
   216                 *spot++ = '\0';
   217                 switch (spot[-2]) {
   218                 case '%':
   219                     *msg++ = '%';
   220                     maxlen -= 1;
   221                     break;
   222                 case 'c':
   223                 case 'i':
   224                 case 'd':
   225                 case 'u':
   226                 case 'o':
   227                 case 'x':
   228                 case 'X':
   229                     len =
   230                         SDL_snprintf(msg, maxlen, tmp,
   231                                      error->args[argi++].value_i);
   232                     if (len > 0) {
   233                         msg += len;
   234                         maxlen -= len;
   235                     }
   236                     break;
   237 
   238                 case 'f':
   239                     len =
   240                         SDL_snprintf(msg, maxlen, tmp,
   241                                      error->args[argi++].value_f);
   242                     if (len > 0) {
   243                         msg += len;
   244                         maxlen -= len;
   245                     }
   246                     break;
   247 
   248                 case 'p':
   249                     len =
   250                         SDL_snprintf(msg, maxlen, tmp,
   251                                      error->args[argi++].value_ptr);
   252                     if (len > 0) {
   253                         msg += len;
   254                         maxlen -= len;
   255                     }
   256                     break;
   257 
   258                 case 's':
   259                     len =
   260                         SDL_snprintf(msg, maxlen, tmp,
   261                                      SDL_LookupString(error->args[argi++].
   262                                                       buf));
   263                     if (len > 0) {
   264                         msg += len;
   265                         maxlen -= len;
   266                     }
   267                     break;
   268 
   269                 }
   270             } else {
   271                 *msg++ = *fmt++;
   272                 maxlen -= 1;
   273             }
   274         }
   275 
   276         /* slide back if we've overshot the end of our buffer. */
   277         if (maxlen < 0) {
   278             msg -= (-maxlen) + 1;
   279         }
   280 
   281         *msg = 0;               /* NULL terminate the string */
   282     }
   283     return (errstr);
   284 }
   285 
   286 /* vi: set ts=4 sw=4 expandtab: */