src/SDL_error.c
author Ryan C. Gordon
Tue, 07 Apr 2020 14:03:13 -0400
changeset 13704 25edf3df6e51
parent 13696 ea20a7434b98
child 13772 6a1ff60e03fb
permissions -rw-r--r--
emscripten: support KaiOS's Left Soft Key and Right Soft Key (thanks, pelya!).

Fixes Bugzilla #5027.
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@13422
     3
  Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@0
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
icculus@8093
    21
#include "./SDL_internal.h"
slouken@0
    22
slouken@0
    23
/* Simple error handling in SDL */
slouken@0
    24
slouken@0
    25
#include "SDL_error.h"
slouken@0
    26
#include "SDL_error_c.h"
slouken@0
    27
slouken@7191
    28
#define SDL_ERRBUFIZE   1024
slouken@0
    29
slouken@0
    30
/* Private functions */
slouken@0
    31
slouken@1895
    32
static const char *
slouken@1895
    33
SDL_LookupString(const char *key)
slouken@0
    34
{
slouken@1895
    35
    /* FIXME: Add code to lookup key in language string hash-table */
slouken@1895
    36
    return key;
slouken@0
    37
}
slouken@0
    38
slouken@0
    39
/* Public functions */
slouken@0
    40
icculus@7037
    41
int
slouken@8820
    42
SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
slouken@0
    43
{
slouken@1895
    44
    va_list ap;
slouken@1895
    45
    SDL_error *error;
slouken@0
    46
aschiffler@5766
    47
    /* Ignore call if invalid format pointer was passed */
icculus@7037
    48
    if (fmt == NULL) return -1;
slouken@7191
    49
slouken@1895
    50
    /* Copy in the key, mark error as valid */
slouken@1895
    51
    error = SDL_GetErrBuf();
slouken@1895
    52
    error->error = 1;
slouken@1895
    53
    SDL_strlcpy((char *) error->key, fmt, sizeof(error->key));
slouken@0
    54
slouken@1895
    55
    va_start(ap, fmt);
slouken@1895
    56
    error->argc = 0;
slouken@1895
    57
    while (*fmt) {
slouken@1895
    58
        if (*fmt++ == '%') {
slouken@1895
    59
            while (*fmt == '.' || (*fmt >= '0' && *fmt <= '9')) {
slouken@1895
    60
                ++fmt;
slouken@1895
    61
            }
slouken@1895
    62
            switch (*fmt++) {
slouken@1895
    63
            case 0:            /* Malformed format string.. */
slouken@1895
    64
                --fmt;
slouken@1895
    65
                break;
sezero@11875
    66
            case 'l':
sezero@11875
    67
                switch (*fmt++) {
sezero@11875
    68
                case 0:        /* Malformed format string.. */
sezero@11875
    69
                    --fmt;
sezero@11875
    70
                    break;
wasteland@12642
    71
                case 'i': case 'd': case 'u': case 'x': case 'X':
sezero@11875
    72
                    error->args[error->argc++].value_l = va_arg(ap, long);
sezero@11875
    73
                    break;
sezero@11875
    74
                }
sezero@11875
    75
                break;
slouken@1895
    76
            case 'c':
slouken@1895
    77
            case 'i':
slouken@1895
    78
            case 'd':
slouken@1895
    79
            case 'u':
slouken@1895
    80
            case 'o':
slouken@1895
    81
            case 'x':
slouken@1895
    82
            case 'X':
slouken@1895
    83
                error->args[error->argc++].value_i = va_arg(ap, int);
slouken@1895
    84
                break;
slouken@1895
    85
            case 'f':
slouken@1895
    86
                error->args[error->argc++].value_f = va_arg(ap, double);
slouken@1895
    87
                break;
slouken@1895
    88
            case 'p':
slouken@1895
    89
                error->args[error->argc++].value_ptr = va_arg(ap, void *);
slouken@1895
    90
                break;
slouken@1895
    91
            case 's':
slouken@1895
    92
                {
slouken@1895
    93
                    int i = error->argc;
slouken@1895
    94
                    const char *str = va_arg(ap, const char *);
slouken@1895
    95
                    if (str == NULL)
slouken@1895
    96
                        str = "(null)";
slouken@1895
    97
                    SDL_strlcpy((char *) error->args[i].buf, str,
slouken@1895
    98
                                ERR_MAX_STRLEN);
slouken@1895
    99
                    error->argc++;
slouken@1895
   100
                }
slouken@1895
   101
                break;
slouken@1895
   102
            default:
slouken@1895
   103
                break;
slouken@1895
   104
            }
slouken@1895
   105
            if (error->argc >= ERR_MAX_ARGS) {
slouken@1895
   106
                break;
slouken@1895
   107
            }
slouken@1895
   108
        }
slouken@1895
   109
    }
slouken@1895
   110
    va_end(ap);
slouken@0
   111
slouken@11137
   112
    if (SDL_LogGetPriority(SDL_LOG_CATEGORY_ERROR) <= SDL_LOG_PRIORITY_DEBUG) {
slouken@11137
   113
        /* If we are in debug mode, print out an error message
slouken@11137
   114
         * Avoid stomping on the static buffer in GetError, just
slouken@11137
   115
         * in case this is called while processing a ShowMessageBox to
slouken@11137
   116
         * show an error already in that static buffer.
slouken@11137
   117
         */
slouken@11137
   118
        char errmsg[SDL_ERRBUFIZE];
slouken@11137
   119
        SDL_GetErrorMsg(errmsg, sizeof(errmsg));
slouken@11137
   120
        SDL_LogDebug(SDL_LOG_CATEGORY_ERROR, "%s", errmsg);
slouken@11137
   121
    }
icculus@7037
   122
    return -1;
slouken@0
   123
}
slouken@0
   124
icculus@10643
   125
/* Available for backwards compatibility */
icculus@10643
   126
const char *
icculus@10643
   127
SDL_GetError(void)
icculus@10643
   128
{
icculus@10643
   129
    static char errmsg[SDL_ERRBUFIZE];
icculus@10643
   130
icculus@10643
   131
    return SDL_GetErrorMsg(errmsg, SDL_ERRBUFIZE);
icculus@10643
   132
}
icculus@10643
   133
icculus@10643
   134
void
icculus@10643
   135
SDL_ClearError(void)
icculus@10643
   136
{
icculus@10643
   137
    SDL_error *error;
icculus@10643
   138
icculus@10643
   139
    error = SDL_GetErrBuf();
icculus@10643
   140
    error->error = 0;
icculus@10643
   141
}
icculus@10643
   142
icculus@10643
   143
/* Very common errors go here */
icculus@10643
   144
int
icculus@10643
   145
SDL_Error(SDL_errorcode code)
icculus@10643
   146
{
icculus@10643
   147
    switch (code) {
icculus@10643
   148
    case SDL_ENOMEM:
icculus@10643
   149
        return SDL_SetError("Out of memory");
icculus@10643
   150
    case SDL_EFREAD:
icculus@10643
   151
        return SDL_SetError("Error reading from datastream");
icculus@10643
   152
    case SDL_EFWRITE:
icculus@10643
   153
        return SDL_SetError("Error writing to datastream");
icculus@10643
   154
    case SDL_EFSEEK:
icculus@10643
   155
        return SDL_SetError("Error seeking in datastream");
icculus@10643
   156
    case SDL_UNSUPPORTED:
icculus@10643
   157
        return SDL_SetError("That operation is not supported");
icculus@10643
   158
    default:
icculus@10643
   159
        return SDL_SetError("Unknown SDL error");
icculus@10643
   160
    }
icculus@10643
   161
}
icculus@10643
   162
icculus@10643
   163
#ifdef TEST_ERROR
icculus@10643
   164
int
icculus@10643
   165
main(int argc, char *argv[])
icculus@10643
   166
{
icculus@10643
   167
    char buffer[BUFSIZ + 1];
icculus@10643
   168
icculus@10643
   169
    SDL_SetError("Hi there!");
icculus@10643
   170
    printf("Error 1: %s\n", SDL_GetError());
icculus@10643
   171
    SDL_ClearError();
icculus@10643
   172
    SDL_memset(buffer, '1', BUFSIZ);
icculus@10643
   173
    buffer[BUFSIZ] = 0;
icculus@10643
   174
    SDL_SetError("This is the error: %s (%f)", buffer, 1.0);
icculus@10643
   175
    printf("Error 2: %s\n", SDL_GetError());
icculus@10643
   176
    exit(0);
icculus@10643
   177
}
icculus@10643
   178
#endif
icculus@10643
   179
icculus@10643
   180
icculus@10643
   181
/* keep this at the end of the file so it works with GCC builds that don't
icculus@10643
   182
   support "#pragma GCC diagnostic push" ... we'll just leave the warning
icculus@10643
   183
   disabled after this. */
icculus@10644
   184
/* this pragma arrived in GCC 4.2 and causes a warning on older GCCs! Sigh. */
icculus@10644
   185
#if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && (__GNUC_MINOR__ >= 2))))
hadess@10396
   186
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
slouken@10403
   187
#endif
icculus@10644
   188
slouken@0
   189
/* This function has a bit more overhead than most error functions
slouken@0
   190
   so that it supports internationalization and thread-safe errors.
slouken@0
   191
*/
slouken@13624
   192
char *
icculus@9430
   193
SDL_GetErrorMsg(char *errstr, int maxlen)
slouken@0
   194
{
slouken@1895
   195
    SDL_error *error;
slouken@0
   196
slouken@1895
   197
    /* Clear the error string */
slouken@1895
   198
    *errstr = '\0';
slouken@1895
   199
    --maxlen;
slouken@1895
   200
slouken@1895
   201
    /* Get the thread-safe error, and print it out */
slouken@1895
   202
    error = SDL_GetErrBuf();
slouken@1895
   203
    if (error->error) {
slouken@1895
   204
        const char *fmt;
slouken@1895
   205
        char *msg = errstr;
slouken@1895
   206
        int len;
slouken@1895
   207
        int argi;
slouken@0
   208
slouken@1895
   209
        fmt = SDL_LookupString(error->key);
slouken@1895
   210
        argi = 0;
slouken@1895
   211
        while (*fmt && (maxlen > 0)) {
slouken@1895
   212
            if (*fmt == '%') {
slouken@1895
   213
                char tmp[32], *spot = tmp;
slouken@1895
   214
                *spot++ = *fmt++;
slouken@1895
   215
                while ((*fmt == '.' || (*fmt >= '0' && *fmt <= '9'))
slouken@1895
   216
                       && spot < (tmp + SDL_arraysize(tmp) - 2)) {
slouken@1895
   217
                    *spot++ = *fmt++;
slouken@1895
   218
                }
sezero@11875
   219
                if (*fmt == 'l') {
sezero@11875
   220
                    *spot++ = *fmt++;
sezero@11875
   221
                    *spot++ = *fmt++;
sezero@11875
   222
                    *spot++ = '\0';
sezero@11875
   223
                    switch (spot[-2]) {
wasteland@12642
   224
                    case 'i': case 'd': case 'u': case 'x': case 'X':
sezero@11875
   225
                      len = SDL_snprintf(msg, maxlen, tmp,
sezero@11875
   226
                                         error->args[argi++].value_l);
sezero@11875
   227
                      if (len > 0) {
sezero@11875
   228
                          msg += len;
sezero@11875
   229
                          maxlen -= len;
sezero@11875
   230
                      }
sezero@11875
   231
                      break;
sezero@11875
   232
                    }
sezero@11875
   233
                    continue;
sezero@11875
   234
                }
slouken@1895
   235
                *spot++ = *fmt++;
slouken@1895
   236
                *spot++ = '\0';
slouken@1895
   237
                switch (spot[-2]) {
slouken@1895
   238
                case '%':
slouken@1895
   239
                    *msg++ = '%';
slouken@1895
   240
                    maxlen -= 1;
slouken@1895
   241
                    break;
slouken@1895
   242
                case 'c':
slouken@1895
   243
                case 'i':
slouken@1895
   244
                case 'd':
slouken@1895
   245
                case 'u':
slouken@1895
   246
                case 'o':
slouken@1895
   247
                case 'x':
slouken@1895
   248
                case 'X':
slouken@1895
   249
                    len =
slouken@1895
   250
                        SDL_snprintf(msg, maxlen, tmp,
slouken@1895
   251
                                     error->args[argi++].value_i);
icculus@9430
   252
                    if (len > 0) {
icculus@9430
   253
                        msg += len;
icculus@9430
   254
                        maxlen -= len;
icculus@9430
   255
                    }
slouken@1895
   256
                    break;
icculus@9430
   257
slouken@1895
   258
                case 'f':
slouken@1895
   259
                    len =
slouken@1895
   260
                        SDL_snprintf(msg, maxlen, tmp,
slouken@1895
   261
                                     error->args[argi++].value_f);
icculus@9430
   262
                    if (len > 0) {
icculus@9430
   263
                        msg += len;
icculus@9430
   264
                        maxlen -= len;
icculus@9430
   265
                    }
slouken@1895
   266
                    break;
icculus@9430
   267
slouken@1895
   268
                case 'p':
slouken@1895
   269
                    len =
slouken@1895
   270
                        SDL_snprintf(msg, maxlen, tmp,
slouken@1895
   271
                                     error->args[argi++].value_ptr);
icculus@9430
   272
                    if (len > 0) {
icculus@9430
   273
                        msg += len;
icculus@9430
   274
                        maxlen -= len;
icculus@9430
   275
                    }
slouken@1895
   276
                    break;
icculus@9430
   277
slouken@1895
   278
                case 's':
slouken@1895
   279
                    len =
slouken@1895
   280
                        SDL_snprintf(msg, maxlen, tmp,
slouken@3013
   281
                                     SDL_LookupString(error->args[argi++].
slouken@3013
   282
                                                      buf));
icculus@9430
   283
                    if (len > 0) {
icculus@9430
   284
                        msg += len;
icculus@9430
   285
                        maxlen -= len;
icculus@9430
   286
                    }
slouken@1895
   287
                    break;
icculus@9430
   288
slouken@1895
   289
                }
slouken@1895
   290
            } else {
slouken@1895
   291
                *msg++ = *fmt++;
slouken@1895
   292
                maxlen -= 1;
slouken@1895
   293
            }
slouken@1895
   294
        }
icculus@9430
   295
icculus@9430
   296
        /* slide back if we've overshot the end of our buffer. */
icculus@9430
   297
        if (maxlen < 0) {
icculus@9430
   298
            msg -= (-maxlen) + 1;
icculus@9430
   299
        }
icculus@9430
   300
slouken@1895
   301
        *msg = 0;               /* NULL terminate the string */
slouken@1895
   302
    }
slouken@1895
   303
    return (errstr);
slouken@0
   304
}
slouken@4992
   305
slouken@1895
   306
/* vi: set ts=4 sw=4 expandtab: */