src/SDL_error.c
author Ozkan Sezer <sezeroz@gmail.com>
Sat, 24 Mar 2018 22:41:17 +0300
branchSDL-1.2
changeset 11972 0b5ac2b90ab1
parent 11971 70dd6a38cd76
child 13010 a355b476255e
permissions -rw-r--r--
backfort fix for bug #3739: handle %lu, %li and %ld in SDL_SetError.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@6137
     3
    Copyright (C) 1997-2012 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@1818
    42
static const char *SDL_LookupString(const char *key)
slouken@0
    43
{
slouken@0
    44
	/* FIXME: Add code to lookup key in language string hash-table */
slouken@1818
    45
	return key;
slouken@0
    46
}
slouken@0
    47
slouken@0
    48
/* Public functions */
slouken@0
    49
slouken@0
    50
void SDL_SetError (const char *fmt, ...)
slouken@0
    51
{
slouken@0
    52
	va_list ap;
slouken@0
    53
	SDL_error *error;
slouken@0
    54
slouken@0
    55
	/* Copy in the key, mark error as valid */
slouken@0
    56
	error = SDL_GetErrBuf();
slouken@0
    57
	error->error = 1;
slouken@1379
    58
	SDL_strlcpy((char *)error->key, fmt, sizeof(error->key));
slouken@0
    59
slouken@0
    60
	va_start(ap, fmt);
slouken@0
    61
	error->argc = 0;
slouken@0
    62
	while ( *fmt ) {
slouken@0
    63
		if ( *fmt++ == '%' ) {
slouken@1818
    64
			while ( *fmt == '.' || (*fmt >= '0' && *fmt <= '9') ) {
slouken@1818
    65
				++fmt;
slouken@1818
    66
			}
slouken@0
    67
			switch (*fmt++) {
slouken@0
    68
			    case 0:  /* Malformed format string.. */
slouken@0
    69
				--fmt;
slouken@0
    70
				break;
sezeroz@11972
    71
			    case 'l':
sezeroz@11972
    72
				switch (*fmt++) {
sezeroz@11972
    73
				case 0:  /* Malformed format string.. */
sezeroz@11972
    74
					--fmt;
sezeroz@11972
    75
					break;
sezeroz@11972
    76
				case 'i': case 'd': case 'u':
sezeroz@11972
    77
					error->args[error->argc++].value_l =
sezeroz@11972
    78
								va_arg(ap, long);
sezeroz@11972
    79
					break;
sezeroz@11972
    80
				}
sezeroz@11972
    81
				break;
slouken@0
    82
			    case 'c':
slouken@1818
    83
			    case 'i':
slouken@0
    84
			    case 'd':
slouken@1818
    85
			    case 'u':
slouken@1818
    86
			    case 'o':
slouken@1818
    87
			    case 'x':
slouken@1818
    88
			    case 'X':
slouken@0
    89
				error->args[error->argc++].value_i =
slouken@0
    90
							va_arg(ap, int);
slouken@0
    91
				break;
slouken@0
    92
			    case 'f':
slouken@0
    93
				error->args[error->argc++].value_f =
slouken@0
    94
							va_arg(ap, double);
slouken@0
    95
				break;
slouken@0
    96
			    case 'p':
slouken@0
    97
				error->args[error->argc++].value_ptr =
slouken@0
    98
							va_arg(ap, void *);
slouken@0
    99
				break;
slouken@0
   100
			    case 's':
slouken@0
   101
				{
slouken@1612
   102
				  int i = error->argc;
slouken@1612
   103
				  const char *str = va_arg(ap, const char *);
icculus@1172
   104
				  if (str == NULL)
icculus@1172
   105
				      str = "(null)";
slouken@1612
   106
				  SDL_strlcpy((char *)error->args[i].buf, str, ERR_MAX_STRLEN);
slouken@0
   107
				  error->argc++;
slouken@0
   108
				}
slouken@0
   109
				break;
slouken@0
   110
			    default:
slouken@0
   111
				break;
slouken@0
   112
			}
slouken@0
   113
			if ( error->argc >= ERR_MAX_ARGS ) {
slouken@0
   114
				break;
slouken@0
   115
			}
slouken@0
   116
		}
slouken@0
   117
	}
slouken@0
   118
	va_end(ap);
slouken@0
   119
slouken@0
   120
	/* If we are in debug mode, print out an error message */
slouken@0
   121
#ifdef DEBUG_ERROR
slouken@0
   122
	fprintf(stderr, "SDL_SetError: %s\n", SDL_GetError());
slouken@0
   123
#endif
slouken@0
   124
}
slouken@0
   125
slouken@0
   126
/* This function has a bit more overhead than most error functions
slouken@0
   127
   so that it supports internationalization and thread-safe errors.
slouken@0
   128
*/
sezeroz@11971
   129
char *SDL_GetErrorMsg(char *errstr, int maxlen)
slouken@0
   130
{
slouken@0
   131
	SDL_error *error;
slouken@0
   132
slouken@0
   133
	/* Clear the error string */
slouken@1818
   134
	*errstr = '\0'; --maxlen;
slouken@0
   135
slouken@0
   136
	/* Get the thread-safe error, and print it out */
slouken@0
   137
	error = SDL_GetErrBuf();
slouken@0
   138
	if ( error->error ) {
slouken@1818
   139
		const char *fmt;
slouken@1818
   140
		char *msg = errstr;
slouken@0
   141
		int len;
slouken@0
   142
		int argi;
slouken@0
   143
slouken@1818
   144
		fmt = SDL_LookupString(error->key);
slouken@0
   145
		argi = 0;
slouken@1818
   146
		while ( *fmt && (maxlen > 0) ) {
slouken@0
   147
			if ( *fmt == '%' ) {
slouken@1818
   148
				char tmp[32], *spot = tmp;
slouken@1818
   149
				*spot++ = *fmt++;
slouken@1847
   150
				while ( (*fmt == '.' || (*fmt >= '0' && *fmt <= '9')) && spot < (tmp+SDL_arraysize(tmp)-2) ) {
slouken@1818
   151
					*spot++ = *fmt++;
slouken@1818
   152
				}
sezeroz@11972
   153
				if (*fmt == 'l') {
sezeroz@11972
   154
					*spot++ = *fmt++;
sezeroz@11972
   155
					*spot++ = *fmt++;
sezeroz@11972
   156
					*spot++ = '\0';
sezeroz@11972
   157
					switch (spot[-2]) {
sezeroz@11972
   158
					case 'i': case 'd': case 'u':
sezeroz@11972
   159
						len = SDL_snprintf(msg, maxlen, tmp, error->args[argi++].value_l);
sezeroz@11972
   160
						if (len > 0) {
sezeroz@11972
   161
						    msg += len;
sezeroz@11972
   162
						    maxlen -= len;
sezeroz@11972
   163
						}
sezeroz@11972
   164
						break;
sezeroz@11972
   165
					}
sezeroz@11972
   166
					continue;
sezeroz@11972
   167
				}
slouken@1818
   168
				*spot++ = *fmt++;
slouken@1818
   169
				*spot++ = '\0';
slouken@1818
   170
				switch (spot[-2]) {
slouken@0
   171
				    case '%':
slouken@0
   172
					*msg++ = '%';
slouken@0
   173
					maxlen -= 1;
slouken@0
   174
					break;
slouken@0
   175
				    case 'c':
slouken@1818
   176
				    case 'i':
slouken@1818
   177
			            case 'd':
slouken@1818
   178
			            case 'u':
slouken@1818
   179
			            case 'o':
slouken@1818
   180
				    case 'x':
slouken@1818
   181
				    case 'X':
slouken@1818
   182
					len = SDL_snprintf(msg, maxlen, tmp, error->args[argi++].value_i);
sezeroz@11971
   183
					if (len > 0) {
sezeroz@11971
   184
						msg += len;
sezeroz@11971
   185
						maxlen -= len;
sezeroz@11971
   186
					}
slouken@0
   187
					break;
slouken@0
   188
				    case 'f':
slouken@1818
   189
					len = SDL_snprintf(msg, maxlen, tmp, error->args[argi++].value_f);
sezeroz@11971
   190
					if (len > 0) {
sezeroz@11971
   191
						msg += len;
sezeroz@11971
   192
						maxlen -= len;
sezeroz@11971
   193
					}
slouken@0
   194
					break;
slouken@0
   195
				    case 'p':
slouken@1818
   196
					len = SDL_snprintf(msg, maxlen, tmp, error->args[argi++].value_ptr);
sezeroz@11971
   197
					if (len > 0) {
sezeroz@11971
   198
						msg += len;
sezeroz@11971
   199
						maxlen -= len;
sezeroz@11971
   200
					}
slouken@0
   201
					break;
slouken@1818
   202
				    case 's':
slouken@1818
   203
					len = SDL_snprintf(msg, maxlen, tmp, SDL_LookupString(error->args[argi++].buf));
sezeroz@11971
   204
					if (len > 0) {
sezeroz@11971
   205
						msg += len;
sezeroz@11971
   206
						maxlen -= len;
sezeroz@11971
   207
					}
slouken@0
   208
					break;
slouken@0
   209
				}
slouken@0
   210
			} else {
slouken@0
   211
				*msg++ = *fmt++;
slouken@0
   212
				maxlen -= 1;
slouken@0
   213
			}
slouken@0
   214
		}
sezeroz@11971
   215
sezeroz@11971
   216
		/* slide back if we've overshot the end of our buffer. */
sezeroz@11971
   217
		if (maxlen < 0) {
sezeroz@11971
   218
			msg -= (-maxlen) + 1;
sezeroz@11971
   219
		}
sezeroz@11971
   220
slouken@0
   221
		*msg = 0;	/* NULL terminate the string */
slouken@0
   222
	}
slouken@0
   223
	return(errstr);
slouken@0
   224
}
slouken@0
   225
slouken@0
   226
/* Available for backwards compatibility */
slouken@0
   227
char *SDL_GetError (void)
slouken@0
   228
{
slouken@0
   229
	static char errmsg[SDL_ERRBUFIZE];
slouken@0
   230
slouken@1824
   231
	return((char *)SDL_GetErrorMsg(errmsg, SDL_ERRBUFIZE));
slouken@0
   232
}
slouken@0
   233
slouken@0
   234
void SDL_ClearError(void)
slouken@0
   235
{
slouken@0
   236
	SDL_error *error;
slouken@0
   237
slouken@0
   238
	error = SDL_GetErrBuf();
slouken@0
   239
	error->error = 0;
slouken@0
   240
}
slouken@0
   241
slouken@0
   242
/* Very common errors go here */
slouken@0
   243
void SDL_Error(SDL_errorcode code)
slouken@0
   244
{
slouken@0
   245
	switch (code) {
slouken@0
   246
		case SDL_ENOMEM:
slouken@0
   247
			SDL_SetError("Out of memory");
slouken@0
   248
			break;
slouken@0
   249
		case SDL_EFREAD:
slouken@0
   250
			SDL_SetError("Error reading from datastream");
slouken@0
   251
			break;
slouken@0
   252
		case SDL_EFWRITE:
slouken@0
   253
			SDL_SetError("Error writing to datastream");
slouken@0
   254
			break;
slouken@0
   255
		case SDL_EFSEEK:
slouken@0
   256
			SDL_SetError("Error seeking in datastream");
slouken@0
   257
			break;
slouken@0
   258
		default:
slouken@0
   259
			SDL_SetError("Unknown SDL error");
slouken@0
   260
			break;
slouken@0
   261
	}
slouken@0
   262
}
slouken@0
   263
slouken@0
   264
#ifdef TEST_ERROR
slouken@0
   265
int main(int argc, char *argv[])
slouken@0
   266
{
slouken@0
   267
	char buffer[BUFSIZ+1];
slouken@0
   268
slouken@0
   269
	SDL_SetError("Hi there!");
slouken@0
   270
	printf("Error 1: %s\n", SDL_GetError());
slouken@0
   271
	SDL_ClearError();
slouken@1336
   272
	SDL_memset(buffer, '1', BUFSIZ);
slouken@0
   273
	buffer[BUFSIZ] = 0;
slouken@0
   274
	SDL_SetError("This is the error: %s (%f)", buffer, 1.0);
slouken@0
   275
	printf("Error 2: %s\n", SDL_GetError());
slouken@0
   276
	exit(0);
slouken@0
   277
}
slouken@0
   278
#endif