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