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