src/SDL_error.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 21 Sep 2009 08:58:51 +0000
branchSDL-1.2
changeset 4214 4250beeb5ad1
parent 4159 a1b03ba2fcd0
child 6137 4720145f848b
permissions -rw-r--r--
Oh yeah, we have GLX support too.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@4159
     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
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;
slouken@0
    71
			    case 'c':
slouken@1818
    72
			    case 'i':
slouken@0
    73
			    case 'd':
slouken@1818
    74
			    case 'u':
slouken@1818
    75
			    case 'o':
slouken@1818
    76
			    case 'x':
slouken@1818
    77
			    case 'X':
slouken@0
    78
				error->args[error->argc++].value_i =
slouken@0
    79
							va_arg(ap, int);
slouken@0
    80
				break;
slouken@0
    81
			    case 'f':
slouken@0
    82
				error->args[error->argc++].value_f =
slouken@0
    83
							va_arg(ap, double);
slouken@0
    84
				break;
slouken@0
    85
			    case 'p':
slouken@0
    86
				error->args[error->argc++].value_ptr =
slouken@0
    87
							va_arg(ap, void *);
slouken@0
    88
				break;
slouken@0
    89
			    case 's':
slouken@0
    90
				{
slouken@1612
    91
				  int i = error->argc;
slouken@1612
    92
				  const char *str = va_arg(ap, const char *);
icculus@1172
    93
				  if (str == NULL)
icculus@1172
    94
				      str = "(null)";
slouken@1612
    95
				  SDL_strlcpy((char *)error->args[i].buf, str, ERR_MAX_STRLEN);
slouken@0
    96
				  error->argc++;
slouken@0
    97
				}
slouken@0
    98
				break;
slouken@0
    99
			    default:
slouken@0
   100
				break;
slouken@0
   101
			}
slouken@0
   102
			if ( error->argc >= ERR_MAX_ARGS ) {
slouken@0
   103
				break;
slouken@0
   104
			}
slouken@0
   105
		}
slouken@0
   106
	}
slouken@0
   107
	va_end(ap);
slouken@0
   108
slouken@0
   109
	/* If we are in debug mode, print out an error message */
slouken@0
   110
#ifdef DEBUG_ERROR
slouken@0
   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@1818
   118
char *SDL_GetErrorMsg(char *errstr, unsigned int maxlen)
slouken@0
   119
{
slouken@0
   120
	SDL_error *error;
slouken@0
   121
slouken@0
   122
	/* Clear the error string */
slouken@1818
   123
	*errstr = '\0'; --maxlen;
slouken@0
   124
slouken@0
   125
	/* Get the thread-safe error, and print it out */
slouken@0
   126
	error = SDL_GetErrBuf();
slouken@0
   127
	if ( error->error ) {
slouken@1818
   128
		const char *fmt;
slouken@1818
   129
		char *msg = errstr;
slouken@0
   130
		int len;
slouken@0
   131
		int argi;
slouken@0
   132
slouken@1818
   133
		fmt = SDL_LookupString(error->key);
slouken@0
   134
		argi = 0;
slouken@1818
   135
		while ( *fmt && (maxlen > 0) ) {
slouken@0
   136
			if ( *fmt == '%' ) {
slouken@1818
   137
				char tmp[32], *spot = tmp;
slouken@1818
   138
				*spot++ = *fmt++;
slouken@1847
   139
				while ( (*fmt == '.' || (*fmt >= '0' && *fmt <= '9')) && spot < (tmp+SDL_arraysize(tmp)-2) ) {
slouken@1818
   140
					*spot++ = *fmt++;
slouken@1818
   141
				}
slouken@1818
   142
				*spot++ = *fmt++;
slouken@1818
   143
				*spot++ = '\0';
slouken@1818
   144
				switch (spot[-2]) {
slouken@0
   145
				    case '%':
slouken@0
   146
					*msg++ = '%';
slouken@0
   147
					maxlen -= 1;
slouken@0
   148
					break;
slouken@0
   149
				    case 'c':
slouken@1818
   150
				    case 'i':
slouken@1818
   151
			            case 'd':
slouken@1818
   152
			            case 'u':
slouken@1818
   153
			            case 'o':
slouken@1818
   154
				    case 'x':
slouken@1818
   155
				    case 'X':
slouken@1818
   156
					len = SDL_snprintf(msg, maxlen, tmp, error->args[argi++].value_i);
slouken@0
   157
					msg += len;
slouken@0
   158
					maxlen -= len;
slouken@0
   159
					break;
slouken@0
   160
				    case 'f':
slouken@1818
   161
					len = SDL_snprintf(msg, maxlen, tmp, error->args[argi++].value_f);
slouken@0
   162
					msg += len;
slouken@0
   163
					maxlen -= len;
slouken@0
   164
					break;
slouken@0
   165
				    case 'p':
slouken@1818
   166
					len = SDL_snprintf(msg, maxlen, tmp, error->args[argi++].value_ptr);
slouken@0
   167
					msg += len;
slouken@0
   168
					maxlen -= len;
slouken@0
   169
					break;
slouken@1818
   170
				    case 's':
slouken@1818
   171
					len = SDL_snprintf(msg, maxlen, tmp, SDL_LookupString(error->args[argi++].buf));
slouken@1818
   172
					msg += len;
slouken@1818
   173
					maxlen -= len;
slouken@0
   174
					break;
slouken@0
   175
				}
slouken@0
   176
			} else {
slouken@0
   177
				*msg++ = *fmt++;
slouken@0
   178
				maxlen -= 1;
slouken@0
   179
			}
slouken@0
   180
		}
slouken@0
   181
		*msg = 0;	/* NULL terminate the string */
slouken@0
   182
	}
slouken@0
   183
	return(errstr);
slouken@0
   184
}
slouken@0
   185
slouken@0
   186
/* Available for backwards compatibility */
slouken@0
   187
char *SDL_GetError (void)
slouken@0
   188
{
slouken@0
   189
	static char errmsg[SDL_ERRBUFIZE];
slouken@0
   190
slouken@1824
   191
	return((char *)SDL_GetErrorMsg(errmsg, SDL_ERRBUFIZE));
slouken@0
   192
}
slouken@0
   193
slouken@0
   194
void SDL_ClearError(void)
slouken@0
   195
{
slouken@0
   196
	SDL_error *error;
slouken@0
   197
slouken@0
   198
	error = SDL_GetErrBuf();
slouken@0
   199
	error->error = 0;
slouken@0
   200
}
slouken@0
   201
slouken@0
   202
/* Very common errors go here */
slouken@0
   203
void SDL_Error(SDL_errorcode code)
slouken@0
   204
{
slouken@0
   205
	switch (code) {
slouken@0
   206
		case SDL_ENOMEM:
slouken@0
   207
			SDL_SetError("Out of memory");
slouken@0
   208
			break;
slouken@0
   209
		case SDL_EFREAD:
slouken@0
   210
			SDL_SetError("Error reading from datastream");
slouken@0
   211
			break;
slouken@0
   212
		case SDL_EFWRITE:
slouken@0
   213
			SDL_SetError("Error writing to datastream");
slouken@0
   214
			break;
slouken@0
   215
		case SDL_EFSEEK:
slouken@0
   216
			SDL_SetError("Error seeking in datastream");
slouken@0
   217
			break;
slouken@0
   218
		default:
slouken@0
   219
			SDL_SetError("Unknown SDL error");
slouken@0
   220
			break;
slouken@0
   221
	}
slouken@0
   222
}
slouken@0
   223
slouken@0
   224
#ifdef TEST_ERROR
slouken@0
   225
int main(int argc, char *argv[])
slouken@0
   226
{
slouken@0
   227
	char buffer[BUFSIZ+1];
slouken@0
   228
slouken@0
   229
	SDL_SetError("Hi there!");
slouken@0
   230
	printf("Error 1: %s\n", SDL_GetError());
slouken@0
   231
	SDL_ClearError();
slouken@1336
   232
	SDL_memset(buffer, '1', BUFSIZ);
slouken@0
   233
	buffer[BUFSIZ] = 0;
slouken@0
   234
	SDL_SetError("This is the error: %s (%f)", buffer, 1.0);
slouken@0
   235
	printf("Error 2: %s\n", SDL_GetError());
slouken@0
   236
	exit(0);
slouken@0
   237
}
slouken@0
   238
#endif