src/SDL_error.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 28 May 2006 13:04:16 +0000
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1659 14717b52abc0
child 1668 4da1ee79c9af
permissions -rw-r--r--
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.

WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.

The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce

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