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