src/SDL_error.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 01 Feb 2006 06:32:25 +0000
changeset 1312 c9b51268668f
parent 1172 f69f4d25fb20
child 1330 450721ad5436
permissions -rw-r--r--
Updated copyright information and removed rcs id lines (problematic in branch merges)
I batch edited these files, so please let me know if I've accidentally removed anybody's
credit here.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 /* Simple error handling in SDL */
    24 
    25 #include <stdio.h>
    26 #include <stdlib.h>
    27 #include <stdarg.h>
    28 #include <string.h>
    29 
    30 #include "SDL_types.h"
    31 #include "SDL_getenv.h"
    32 #include "SDL_error.h"
    33 #include "SDL_error_c.h"
    34 #ifndef DISABLE_THREADS
    35 #include "SDL_thread_c.h"
    36 #endif
    37 
    38 #ifdef DISABLE_THREADS
    39 /* The default (non-thread-safe) global error variable */
    40 static SDL_error SDL_global_error;
    41 
    42 #define SDL_GetErrBuf()	(&SDL_global_error)
    43 #endif /* DISABLE_THREADS */
    44 
    45 #ifdef __CYGWIN__
    46 #define DISABLE_STDIO
    47 #endif
    48 
    49 #define SDL_ERRBUFIZE	1024
    50 
    51 /* Private functions */
    52 
    53 static void SDL_LookupString(const Uint8 *key, Uint16 *buf, int buflen)
    54 {
    55 	/* FIXME: Add code to lookup key in language string hash-table */
    56 
    57 	/* Key not found in language string hash-table */
    58 	while ( *key && (--buflen > 0) ) {
    59 		*buf++ = *key++;
    60 	}
    61 	*buf = 0;	/* NULL terminate string */
    62 }
    63 
    64 /* Public functions */
    65 
    66 void SDL_SetError (const char *fmt, ...)
    67 {
    68 	va_list ap;
    69 	SDL_error *error;
    70 
    71 	/* Copy in the key, mark error as valid */
    72 	error = SDL_GetErrBuf();
    73 	error->error = 1;
    74 	strncpy((char *)error->key, fmt, sizeof(error->key));
    75 	error->key[sizeof(error->key)-1] = '\0';
    76 
    77 	va_start(ap, fmt);
    78 	error->argc = 0;
    79 	while ( *fmt ) {
    80 		if ( *fmt++ == '%' ) {
    81 			switch (*fmt++) {
    82 			    case 0:  /* Malformed format string.. */
    83 				--fmt;
    84 				break;
    85 #if 0	/* What is a character anyway?  (UNICODE issues) */
    86 			    case 'c':
    87 				error->args[error->argc++].value_c =
    88 						va_arg(ap, unsigned char);
    89 				break;
    90 #endif
    91 			    case 'd':
    92 				error->args[error->argc++].value_i =
    93 							va_arg(ap, int);
    94 				break;
    95 			    case 'f':
    96 				error->args[error->argc++].value_f =
    97 							va_arg(ap, double);
    98 				break;
    99 			    case 'p':
   100 				error->args[error->argc++].value_ptr =
   101 							va_arg(ap, void *);
   102 				break;
   103 			    case 's':
   104 				{
   105 				  int index = error->argc;
   106 				  char *str = va_arg(ap, char *);
   107 				  if (str == NULL)
   108 				      str = "(null)";
   109 				  strncpy((char *)error->args[index].buf, str, ERR_MAX_STRLEN);
   110 				  error->args[index].buf[ERR_MAX_STRLEN-1] = 0;
   111 				  error->argc++;
   112 				}
   113 				break;
   114 			    default:
   115 				break;
   116 			}
   117 			if ( error->argc >= ERR_MAX_ARGS ) {
   118 				break;
   119 			}
   120 		}
   121 	}
   122 	va_end(ap);
   123 
   124 #ifndef DISABLE_STDIO
   125 	/* If we are in debug mode, print out an error message */
   126 #ifdef DEBUG_ERROR
   127 	fprintf(stderr, "SDL_SetError: %s\n", SDL_GetError());
   128 #else
   129 	if ( getenv("SDL_DEBUG") ) {
   130 		fprintf(stderr, "SDL_SetError: %s\n", SDL_GetError());
   131 	}
   132 #endif
   133 #endif /* !DISABLE_STDIO */
   134 }
   135 
   136 /* Print out an integer value to a UNICODE buffer */
   137 static int PrintInt(Uint16 *str, unsigned int maxlen, int value)
   138 {
   139 	char tmp[128];
   140 	int len, i;
   141 
   142 	sprintf(tmp, "%d", value);
   143 	len = 0;
   144 	if ( strlen(tmp) < maxlen ) {
   145 		for ( i=0; tmp[i]; ++i ) {
   146 			*str++ = tmp[i];
   147 			++len;
   148 		}
   149 	}
   150 	return(len);
   151 }
   152 /* Print out a double value to a UNICODE buffer */
   153 static int PrintDouble(Uint16 *str, unsigned int maxlen, double value)
   154 {
   155 	char tmp[128];
   156 	int len, i;
   157 
   158 	sprintf(tmp, "%f", value);
   159 	len = 0;
   160 	if ( strlen(tmp) < maxlen ) {
   161 		for ( i=0; tmp[i]; ++i ) {
   162 			*str++ = tmp[i];
   163 			++len;
   164 		}
   165 	}
   166 	return(len);
   167 }
   168 /* Print out a pointer value to a UNICODE buffer */
   169 static int PrintPointer(Uint16 *str, unsigned int maxlen, void *value)
   170 {
   171 	char tmp[128];
   172 	int len, i;
   173 
   174 	sprintf(tmp, "%p", value);
   175 	len = 0;
   176 	if ( strlen(tmp) < maxlen ) {
   177 		for ( i=0; tmp[i]; ++i ) {
   178 			*str++ = tmp[i];
   179 			++len;
   180 		}
   181 	}
   182 	return(len);
   183 }
   184 
   185 /* This function has a bit more overhead than most error functions
   186    so that it supports internationalization and thread-safe errors.
   187 */
   188 Uint16 *SDL_GetErrorMsgUNICODE(Uint16 *errstr, unsigned int maxlen)
   189 {
   190 	SDL_error *error;
   191 
   192 	/* Clear the error string */
   193 	*errstr = 0; --maxlen;
   194 
   195 	/* Get the thread-safe error, and print it out */
   196 	error = SDL_GetErrBuf();
   197 	if ( error->error ) {
   198 		Uint16 translated[ERR_MAX_STRLEN], *fmt, *msg;
   199 		int len;
   200 		int argi;
   201 
   202 		/* Print out the UNICODE error message */
   203 		SDL_LookupString(error->key, translated, sizeof(translated));
   204 		msg = errstr;
   205 		argi = 0;
   206 		for ( fmt=translated; *fmt && (maxlen > 0); ) {
   207 			if ( *fmt == '%' ) {
   208 				switch (fmt[1]) {
   209 				    case 'S':	/* Special SKIP operand */
   210 					argi += (fmt[2] - '0');
   211 					++fmt;
   212 					break;
   213 				    case '%':
   214 					*msg++ = '%';
   215 					maxlen -= 1;
   216 					break;
   217 #if 0	/* What is a character anyway?  (UNICODE issues) */
   218 				    case 'c':
   219                                         *msg++ = (unsigned char)
   220 					         error->args[argi++].value_c;
   221 					maxlen -= 1;
   222 					break;
   223 #endif
   224 				    case 'd':
   225 					len = PrintInt(msg, maxlen,
   226 						error->args[argi++].value_i);
   227 					msg += len;
   228 					maxlen -= len;
   229 					break;
   230 				    case 'f':
   231 					len = PrintDouble(msg, maxlen,
   232 						error->args[argi++].value_f);
   233 					msg += len;
   234 					maxlen -= len;
   235 					break;
   236 				    case 'p':
   237 					len = PrintPointer(msg, maxlen,
   238 						error->args[argi++].value_ptr);
   239 					msg += len;
   240 					maxlen -= len;
   241 					break;
   242 				    case 's': /* UNICODE string */
   243 					{ Uint16 buf[ERR_MAX_STRLEN], *str;
   244 					  SDL_LookupString(error->args[argi++].buf, buf, sizeof(buf));
   245 					  str = buf;
   246 					  while ( *str && (maxlen > 0) ) {
   247 						*msg++ = *str++;
   248 						maxlen -= 1;
   249 					  }
   250 					}
   251 					break;
   252 				}
   253 				fmt += 2;
   254 			} else {
   255 				*msg++ = *fmt++;
   256 				maxlen -= 1;
   257 			}
   258 		}
   259 		*msg = 0;	/* NULL terminate the string */
   260 	}
   261 	return(errstr);
   262 }
   263 
   264 Uint8 *SDL_GetErrorMsg(Uint8 *errstr, unsigned int maxlen)
   265 {
   266 	Uint16 *errstr16;
   267 	unsigned int i;
   268 
   269 	/* Allocate the UNICODE buffer */
   270 	errstr16 = (Uint16 *)malloc(maxlen * (sizeof *errstr16));
   271 	if ( ! errstr16 ) {
   272 		strncpy((char *)errstr, "Out of memory", maxlen);
   273 		errstr[maxlen-1] = '\0';
   274 		return(errstr);
   275 	}
   276 
   277 	/* Get the error message */
   278 	SDL_GetErrorMsgUNICODE(errstr16, maxlen);
   279 
   280 	/* Convert from UNICODE to Latin1 encoding */
   281 	for ( i=0; i<maxlen; ++i ) {
   282 		errstr[i] = (Uint8)errstr16[i];
   283 	}
   284 
   285 	/* Free UNICODE buffer (if necessary) */
   286 	free(errstr16);
   287 
   288 	return(errstr);
   289 }
   290 
   291 /* Available for backwards compatibility */
   292 char *SDL_GetError (void)
   293 {
   294 	static char errmsg[SDL_ERRBUFIZE];
   295 
   296 	return((char *)SDL_GetErrorMsg((unsigned char *)errmsg, SDL_ERRBUFIZE));
   297 }
   298 
   299 void SDL_ClearError(void)
   300 {
   301 	SDL_error *error;
   302 
   303 	error = SDL_GetErrBuf();
   304 	error->error = 0;
   305 }
   306 
   307 /* Very common errors go here */
   308 void SDL_Error(SDL_errorcode code)
   309 {
   310 	switch (code) {
   311 		case SDL_ENOMEM:
   312 			SDL_SetError("Out of memory");
   313 			break;
   314 		case SDL_EFREAD:
   315 			SDL_SetError("Error reading from datastream");
   316 			break;
   317 		case SDL_EFWRITE:
   318 			SDL_SetError("Error writing to datastream");
   319 			break;
   320 		case SDL_EFSEEK:
   321 			SDL_SetError("Error seeking in datastream");
   322 			break;
   323 		default:
   324 			SDL_SetError("Unknown SDL error");
   325 			break;
   326 	}
   327 }
   328 
   329 #ifdef TEST_ERROR
   330 int main(int argc, char *argv[])
   331 {
   332 	char buffer[BUFSIZ+1];
   333 
   334 	SDL_SetError("Hi there!");
   335 	printf("Error 1: %s\n", SDL_GetError());
   336 	SDL_ClearError();
   337 	memset(buffer, '1', BUFSIZ);
   338 	buffer[BUFSIZ] = 0;
   339 	SDL_SetError("This is the error: %s (%f)", buffer, 1.0);
   340 	printf("Error 2: %s\n", SDL_GetError());
   341 	exit(0);
   342 }
   343 #endif