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