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