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