src/SDL_error.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 19 Feb 2006 23:46:34 +0000
changeset 1379 c0a74f199ecf
parent 1361 19418e4422cb
child 1402 d910939febfa
permissions -rw-r--r--
Use only safe string functions
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@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@1361
    28
/* Routine to get the thread-specific error variable */
slouken@1361
    29
#if SDL_THREADS_DISABLED
slouken@1361
    30
/* The  SDL_arraysize(The ),default (non-thread-safe) global error variable */
slouken@0
    31
static SDL_error SDL_global_error;
slouken@0
    32
#define SDL_GetErrBuf()	(&SDL_global_error)
slouken@1361
    33
#else
slouken@1361
    34
extern SDL_error *SDL_GetErrBuf(void);
slouken@1361
    35
#endif /* SDL_THREADS_DISABLED */
slouken@0
    36
slouken@0
    37
#define SDL_ERRBUFIZE	1024
slouken@0
    38
slouken@0
    39
/* Private functions */
slouken@0
    40
slouken@0
    41
static void SDL_LookupString(const Uint8 *key, Uint16 *buf, int buflen)
slouken@0
    42
{
slouken@0
    43
	/* FIXME: Add code to lookup key in language string hash-table */
slouken@0
    44
slouken@0
    45
	/* Key not found in language string hash-table */
slouken@0
    46
	while ( *key && (--buflen > 0) ) {
slouken@0
    47
		*buf++ = *key++;
slouken@0
    48
	}
slouken@0
    49
	*buf = 0;	/* NULL terminate string */
slouken@0
    50
}
slouken@0
    51
slouken@0
    52
/* Public functions */
slouken@0
    53
slouken@0
    54
void SDL_SetError (const char *fmt, ...)
slouken@0
    55
{
slouken@0
    56
	va_list ap;
slouken@0
    57
	SDL_error *error;
slouken@0
    58
slouken@0
    59
	/* Copy in the key, mark error as valid */
slouken@0
    60
	error = SDL_GetErrBuf();
slouken@0
    61
	error->error = 1;
slouken@1379
    62
	SDL_strlcpy((char *)error->key, fmt, sizeof(error->key));
slouken@0
    63
slouken@0
    64
	va_start(ap, fmt);
slouken@0
    65
	error->argc = 0;
slouken@0
    66
	while ( *fmt ) {
slouken@0
    67
		if ( *fmt++ == '%' ) {
slouken@0
    68
			switch (*fmt++) {
slouken@0
    69
			    case 0:  /* Malformed format string.. */
slouken@0
    70
				--fmt;
slouken@0
    71
				break;
slouken@0
    72
#if 0	/* What is a character anyway?  (UNICODE issues) */
slouken@0
    73
			    case 'c':
slouken@0
    74
				error->args[error->argc++].value_c =
slouken@0
    75
						va_arg(ap, unsigned char);
slouken@0
    76
				break;
slouken@0
    77
#endif
slouken@0
    78
			    case 'd':
slouken@0
    79
				error->args[error->argc++].value_i =
slouken@0
    80
							va_arg(ap, int);
slouken@0
    81
				break;
slouken@0
    82
			    case 'f':
slouken@0
    83
				error->args[error->argc++].value_f =
slouken@0
    84
							va_arg(ap, double);
slouken@0
    85
				break;
slouken@0
    86
			    case 'p':
slouken@0
    87
				error->args[error->argc++].value_ptr =
slouken@0
    88
							va_arg(ap, void *);
slouken@0
    89
				break;
slouken@0
    90
			    case 's':
slouken@0
    91
				{
slouken@0
    92
				  int index = error->argc;
icculus@1172
    93
				  char *str = va_arg(ap, char *);
icculus@1172
    94
				  if (str == NULL)
icculus@1172
    95
				      str = "(null)";
slouken@1379
    96
				  SDL_strlcpy((char *)error->args[index].buf, str, ERR_MAX_STRLEN);
slouken@0
    97
				  error->argc++;
slouken@0
    98
				}
slouken@0
    99
				break;
slouken@0
   100
			    default:
slouken@0
   101
				break;
slouken@0
   102
			}
slouken@0
   103
			if ( error->argc >= ERR_MAX_ARGS ) {
slouken@0
   104
				break;
slouken@0
   105
			}
slouken@0
   106
		}
slouken@0
   107
	}
slouken@0
   108
	va_end(ap);
slouken@0
   109
slouken@0
   110
	/* If we are in debug mode, print out an error message */
slouken@0
   111
#ifdef DEBUG_ERROR
slouken@0
   112
	fprintf(stderr, "SDL_SetError: %s\n", SDL_GetError());
slouken@0
   113
#endif
slouken@0
   114
}
slouken@0
   115
slouken@0
   116
/* Print out an integer value to a UNICODE buffer */
slouken@0
   117
static int PrintInt(Uint16 *str, unsigned int maxlen, int value)
slouken@0
   118
{
slouken@0
   119
	char tmp[128];
slouken@0
   120
	int len, i;
slouken@0
   121
slouken@1336
   122
	SDL_snprintf(tmp, SDL_arraysize(tmp), "%d", value);
slouken@0
   123
	len = 0;
slouken@1336
   124
	if ( SDL_strlen(tmp) < maxlen ) {
slouken@0
   125
		for ( i=0; tmp[i]; ++i ) {
slouken@0
   126
			*str++ = tmp[i];
slouken@0
   127
			++len;
slouken@0
   128
		}
slouken@0
   129
	}
slouken@0
   130
	return(len);
slouken@0
   131
}
slouken@0
   132
/* Print out a double value to a UNICODE buffer */
slouken@0
   133
static int PrintDouble(Uint16 *str, unsigned int maxlen, double value)
slouken@0
   134
{
slouken@0
   135
	char tmp[128];
slouken@0
   136
	int len, i;
slouken@0
   137
slouken@1336
   138
	SDL_snprintf(tmp, SDL_arraysize(tmp), "%f", value);
slouken@0
   139
	len = 0;
slouken@1336
   140
	if ( SDL_strlen(tmp) < maxlen ) {
slouken@0
   141
		for ( i=0; tmp[i]; ++i ) {
slouken@0
   142
			*str++ = tmp[i];
slouken@0
   143
			++len;
slouken@0
   144
		}
slouken@0
   145
	}
slouken@0
   146
	return(len);
slouken@0
   147
}
slouken@0
   148
/* Print out a pointer value to a UNICODE buffer */
slouken@0
   149
static int PrintPointer(Uint16 *str, unsigned int maxlen, void *value)
slouken@0
   150
{
slouken@0
   151
	char tmp[128];
slouken@0
   152
	int len, i;
slouken@0
   153
slouken@1336
   154
	SDL_snprintf(tmp, SDL_arraysize(tmp), "%p", value);
slouken@0
   155
	len = 0;
slouken@1336
   156
	if ( SDL_strlen(tmp) < maxlen ) {
slouken@0
   157
		for ( i=0; tmp[i]; ++i ) {
slouken@0
   158
			*str++ = tmp[i];
slouken@0
   159
			++len;
slouken@0
   160
		}
slouken@0
   161
	}
slouken@0
   162
	return(len);
slouken@0
   163
}
slouken@0
   164
slouken@0
   165
/* This function has a bit more overhead than most error functions
slouken@0
   166
   so that it supports internationalization and thread-safe errors.
slouken@0
   167
*/
slouken@0
   168
Uint16 *SDL_GetErrorMsgUNICODE(Uint16 *errstr, unsigned int maxlen)
slouken@0
   169
{
slouken@0
   170
	SDL_error *error;
slouken@0
   171
slouken@0
   172
	/* Clear the error string */
slouken@0
   173
	*errstr = 0; --maxlen;
slouken@0
   174
slouken@0
   175
	/* Get the thread-safe error, and print it out */
slouken@0
   176
	error = SDL_GetErrBuf();
slouken@0
   177
	if ( error->error ) {
slouken@0
   178
		Uint16 translated[ERR_MAX_STRLEN], *fmt, *msg;
slouken@0
   179
		int len;
slouken@0
   180
		int argi;
slouken@0
   181
slouken@0
   182
		/* Print out the UNICODE error message */
slouken@0
   183
		SDL_LookupString(error->key, translated, sizeof(translated));
slouken@0
   184
		msg = errstr;
slouken@0
   185
		argi = 0;
slouken@0
   186
		for ( fmt=translated; *fmt && (maxlen > 0); ) {
slouken@0
   187
			if ( *fmt == '%' ) {
slouken@0
   188
				switch (fmt[1]) {
slouken@0
   189
				    case 'S':	/* Special SKIP operand */
slouken@0
   190
					argi += (fmt[2] - '0');
slouken@0
   191
					++fmt;
slouken@0
   192
					break;
slouken@0
   193
				    case '%':
slouken@0
   194
					*msg++ = '%';
slouken@0
   195
					maxlen -= 1;
slouken@0
   196
					break;
slouken@0
   197
#if 0	/* What is a character anyway?  (UNICODE issues) */
slouken@0
   198
				    case 'c':
slouken@0
   199
                                        *msg++ = (unsigned char)
slouken@0
   200
					         error->args[argi++].value_c;
slouken@0
   201
					maxlen -= 1;
slouken@0
   202
					break;
slouken@0
   203
#endif
slouken@0
   204
				    case 'd':
slouken@0
   205
					len = PrintInt(msg, maxlen,
slouken@0
   206
						error->args[argi++].value_i);
slouken@0
   207
					msg += len;
slouken@0
   208
					maxlen -= len;
slouken@0
   209
					break;
slouken@0
   210
				    case 'f':
slouken@0
   211
					len = PrintDouble(msg, maxlen,
slouken@0
   212
						error->args[argi++].value_f);
slouken@0
   213
					msg += len;
slouken@0
   214
					maxlen -= len;
slouken@0
   215
					break;
slouken@0
   216
				    case 'p':
slouken@0
   217
					len = PrintPointer(msg, maxlen,
slouken@0
   218
						error->args[argi++].value_ptr);
slouken@0
   219
					msg += len;
slouken@0
   220
					maxlen -= len;
slouken@0
   221
					break;
slouken@0
   222
				    case 's': /* UNICODE string */
slouken@0
   223
					{ Uint16 buf[ERR_MAX_STRLEN], *str;
slouken@0
   224
					  SDL_LookupString(error->args[argi++].buf, buf, sizeof(buf));
slouken@0
   225
					  str = buf;
slouken@0
   226
					  while ( *str && (maxlen > 0) ) {
slouken@0
   227
						*msg++ = *str++;
slouken@0
   228
						maxlen -= 1;
slouken@0
   229
					  }
slouken@0
   230
					}
slouken@0
   231
					break;
slouken@0
   232
				}
slouken@0
   233
				fmt += 2;
slouken@0
   234
			} else {
slouken@0
   235
				*msg++ = *fmt++;
slouken@0
   236
				maxlen -= 1;
slouken@0
   237
			}
slouken@0
   238
		}
slouken@0
   239
		*msg = 0;	/* NULL terminate the string */
slouken@0
   240
	}
slouken@0
   241
	return(errstr);
slouken@0
   242
}
slouken@0
   243
slouken@0
   244
Uint8 *SDL_GetErrorMsg(Uint8 *errstr, unsigned int maxlen)
slouken@0
   245
{
slouken@0
   246
	Uint16 *errstr16;
slouken@0
   247
	unsigned int i;
slouken@0
   248
slouken@0
   249
	/* Allocate the UNICODE buffer */
slouken@1336
   250
	errstr16 = (Uint16 *)SDL_malloc(maxlen * (sizeof *errstr16));
slouken@0
   251
	if ( ! errstr16 ) {
slouken@1379
   252
		SDL_strlcpy((char *)errstr, "Out of memory", maxlen);
slouken@0
   253
		return(errstr);
slouken@0
   254
	}
slouken@0
   255
slouken@0
   256
	/* Get the error message */
slouken@0
   257
	SDL_GetErrorMsgUNICODE(errstr16, maxlen);
slouken@0
   258
slouken@0
   259
	/* Convert from UNICODE to Latin1 encoding */
slouken@0
   260
	for ( i=0; i<maxlen; ++i ) {
slouken@0
   261
		errstr[i] = (Uint8)errstr16[i];
slouken@0
   262
	}
slouken@0
   263
slouken@0
   264
	/* Free UNICODE buffer (if necessary) */
slouken@1336
   265
	SDL_free(errstr16);
slouken@0
   266
slouken@0
   267
	return(errstr);
slouken@0
   268
}
slouken@0
   269
slouken@0
   270
/* Available for backwards compatibility */
slouken@0
   271
char *SDL_GetError (void)
slouken@0
   272
{
slouken@0
   273
	static char errmsg[SDL_ERRBUFIZE];
slouken@0
   274
slouken@0
   275
	return((char *)SDL_GetErrorMsg((unsigned char *)errmsg, SDL_ERRBUFIZE));
slouken@0
   276
}
slouken@0
   277
slouken@0
   278
void SDL_ClearError(void)
slouken@0
   279
{
slouken@0
   280
	SDL_error *error;
slouken@0
   281
slouken@0
   282
	error = SDL_GetErrBuf();
slouken@0
   283
	error->error = 0;
slouken@0
   284
}
slouken@0
   285
slouken@0
   286
/* Very common errors go here */
slouken@0
   287
void SDL_Error(SDL_errorcode code)
slouken@0
   288
{
slouken@0
   289
	switch (code) {
slouken@0
   290
		case SDL_ENOMEM:
slouken@0
   291
			SDL_SetError("Out of memory");
slouken@0
   292
			break;
slouken@0
   293
		case SDL_EFREAD:
slouken@0
   294
			SDL_SetError("Error reading from datastream");
slouken@0
   295
			break;
slouken@0
   296
		case SDL_EFWRITE:
slouken@0
   297
			SDL_SetError("Error writing to datastream");
slouken@0
   298
			break;
slouken@0
   299
		case SDL_EFSEEK:
slouken@0
   300
			SDL_SetError("Error seeking in datastream");
slouken@0
   301
			break;
slouken@0
   302
		default:
slouken@0
   303
			SDL_SetError("Unknown SDL error");
slouken@0
   304
			break;
slouken@0
   305
	}
slouken@0
   306
}
slouken@0
   307
slouken@0
   308
#ifdef TEST_ERROR
slouken@0
   309
int main(int argc, char *argv[])
slouken@0
   310
{
slouken@0
   311
	char buffer[BUFSIZ+1];
slouken@0
   312
slouken@0
   313
	SDL_SetError("Hi there!");
slouken@0
   314
	printf("Error 1: %s\n", SDL_GetError());
slouken@0
   315
	SDL_ClearError();
slouken@1336
   316
	SDL_memset(buffer, '1', BUFSIZ);
slouken@0
   317
	buffer[BUFSIZ] = 0;
slouken@0
   318
	SDL_SetError("This is the error: %s (%f)", buffer, 1.0);
slouken@0
   319
	printf("Error 2: %s\n", SDL_GetError());
slouken@0
   320
	exit(0);
slouken@0
   321
}
slouken@0
   322
#endif