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