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