src/SDL_error.c
author Ozkan Sezer <sezeroz@gmail.com>
Sat, 24 Mar 2018 22:41:17 +0300
branchSDL-1.2
changeset 11972 0b5ac2b90ab1
parent 11971 70dd6a38cd76
child 13010 a355b476255e
permissions -rw-r--r--
backfort fix for bug #3739: handle %lu, %li and %ld in SDL_SetError.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2012 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 const char *SDL_LookupString(const char *key)
    43 {
    44 	/* FIXME: Add code to lookup key in language string hash-table */
    45 	return key;
    46 }
    47 
    48 /* Public functions */
    49 
    50 void SDL_SetError (const char *fmt, ...)
    51 {
    52 	va_list ap;
    53 	SDL_error *error;
    54 
    55 	/* Copy in the key, mark error as valid */
    56 	error = SDL_GetErrBuf();
    57 	error->error = 1;
    58 	SDL_strlcpy((char *)error->key, fmt, sizeof(error->key));
    59 
    60 	va_start(ap, fmt);
    61 	error->argc = 0;
    62 	while ( *fmt ) {
    63 		if ( *fmt++ == '%' ) {
    64 			while ( *fmt == '.' || (*fmt >= '0' && *fmt <= '9') ) {
    65 				++fmt;
    66 			}
    67 			switch (*fmt++) {
    68 			    case 0:  /* Malformed format string.. */
    69 				--fmt;
    70 				break;
    71 			    case 'l':
    72 				switch (*fmt++) {
    73 				case 0:  /* Malformed format string.. */
    74 					--fmt;
    75 					break;
    76 				case 'i': case 'd': case 'u':
    77 					error->args[error->argc++].value_l =
    78 								va_arg(ap, long);
    79 					break;
    80 				}
    81 				break;
    82 			    case 'c':
    83 			    case 'i':
    84 			    case 'd':
    85 			    case 'u':
    86 			    case 'o':
    87 			    case 'x':
    88 			    case 'X':
    89 				error->args[error->argc++].value_i =
    90 							va_arg(ap, int);
    91 				break;
    92 			    case 'f':
    93 				error->args[error->argc++].value_f =
    94 							va_arg(ap, double);
    95 				break;
    96 			    case 'p':
    97 				error->args[error->argc++].value_ptr =
    98 							va_arg(ap, void *);
    99 				break;
   100 			    case 's':
   101 				{
   102 				  int i = error->argc;
   103 				  const char *str = va_arg(ap, const char *);
   104 				  if (str == NULL)
   105 				      str = "(null)";
   106 				  SDL_strlcpy((char *)error->args[i].buf, str, ERR_MAX_STRLEN);
   107 				  error->argc++;
   108 				}
   109 				break;
   110 			    default:
   111 				break;
   112 			}
   113 			if ( error->argc >= ERR_MAX_ARGS ) {
   114 				break;
   115 			}
   116 		}
   117 	}
   118 	va_end(ap);
   119 
   120 	/* If we are in debug mode, print out an error message */
   121 #ifdef DEBUG_ERROR
   122 	fprintf(stderr, "SDL_SetError: %s\n", SDL_GetError());
   123 #endif
   124 }
   125 
   126 /* This function has a bit more overhead than most error functions
   127    so that it supports internationalization and thread-safe errors.
   128 */
   129 char *SDL_GetErrorMsg(char *errstr, int maxlen)
   130 {
   131 	SDL_error *error;
   132 
   133 	/* Clear the error string */
   134 	*errstr = '\0'; --maxlen;
   135 
   136 	/* Get the thread-safe error, and print it out */
   137 	error = SDL_GetErrBuf();
   138 	if ( error->error ) {
   139 		const char *fmt;
   140 		char *msg = errstr;
   141 		int len;
   142 		int argi;
   143 
   144 		fmt = SDL_LookupString(error->key);
   145 		argi = 0;
   146 		while ( *fmt && (maxlen > 0) ) {
   147 			if ( *fmt == '%' ) {
   148 				char tmp[32], *spot = tmp;
   149 				*spot++ = *fmt++;
   150 				while ( (*fmt == '.' || (*fmt >= '0' && *fmt <= '9')) && spot < (tmp+SDL_arraysize(tmp)-2) ) {
   151 					*spot++ = *fmt++;
   152 				}
   153 				if (*fmt == 'l') {
   154 					*spot++ = *fmt++;
   155 					*spot++ = *fmt++;
   156 					*spot++ = '\0';
   157 					switch (spot[-2]) {
   158 					case 'i': case 'd': case 'u':
   159 						len = SDL_snprintf(msg, maxlen, tmp, error->args[argi++].value_l);
   160 						if (len > 0) {
   161 						    msg += len;
   162 						    maxlen -= len;
   163 						}
   164 						break;
   165 					}
   166 					continue;
   167 				}
   168 				*spot++ = *fmt++;
   169 				*spot++ = '\0';
   170 				switch (spot[-2]) {
   171 				    case '%':
   172 					*msg++ = '%';
   173 					maxlen -= 1;
   174 					break;
   175 				    case 'c':
   176 				    case 'i':
   177 			            case 'd':
   178 			            case 'u':
   179 			            case 'o':
   180 				    case 'x':
   181 				    case 'X':
   182 					len = SDL_snprintf(msg, maxlen, tmp, error->args[argi++].value_i);
   183 					if (len > 0) {
   184 						msg += len;
   185 						maxlen -= len;
   186 					}
   187 					break;
   188 				    case 'f':
   189 					len = SDL_snprintf(msg, maxlen, tmp, error->args[argi++].value_f);
   190 					if (len > 0) {
   191 						msg += len;
   192 						maxlen -= len;
   193 					}
   194 					break;
   195 				    case 'p':
   196 					len = SDL_snprintf(msg, maxlen, tmp, error->args[argi++].value_ptr);
   197 					if (len > 0) {
   198 						msg += len;
   199 						maxlen -= len;
   200 					}
   201 					break;
   202 				    case 's':
   203 					len = SDL_snprintf(msg, maxlen, tmp, SDL_LookupString(error->args[argi++].buf));
   204 					if (len > 0) {
   205 						msg += len;
   206 						maxlen -= len;
   207 					}
   208 					break;
   209 				}
   210 			} else {
   211 				*msg++ = *fmt++;
   212 				maxlen -= 1;
   213 			}
   214 		}
   215 
   216 		/* slide back if we've overshot the end of our buffer. */
   217 		if (maxlen < 0) {
   218 			msg -= (-maxlen) + 1;
   219 		}
   220 
   221 		*msg = 0;	/* NULL terminate the string */
   222 	}
   223 	return(errstr);
   224 }
   225 
   226 /* Available for backwards compatibility */
   227 char *SDL_GetError (void)
   228 {
   229 	static char errmsg[SDL_ERRBUFIZE];
   230 
   231 	return((char *)SDL_GetErrorMsg(errmsg, SDL_ERRBUFIZE));
   232 }
   233 
   234 void SDL_ClearError(void)
   235 {
   236 	SDL_error *error;
   237 
   238 	error = SDL_GetErrBuf();
   239 	error->error = 0;
   240 }
   241 
   242 /* Very common errors go here */
   243 void SDL_Error(SDL_errorcode code)
   244 {
   245 	switch (code) {
   246 		case SDL_ENOMEM:
   247 			SDL_SetError("Out of memory");
   248 			break;
   249 		case SDL_EFREAD:
   250 			SDL_SetError("Error reading from datastream");
   251 			break;
   252 		case SDL_EFWRITE:
   253 			SDL_SetError("Error writing to datastream");
   254 			break;
   255 		case SDL_EFSEEK:
   256 			SDL_SetError("Error seeking in datastream");
   257 			break;
   258 		default:
   259 			SDL_SetError("Unknown SDL error");
   260 			break;
   261 	}
   262 }
   263 
   264 #ifdef TEST_ERROR
   265 int main(int argc, char *argv[])
   266 {
   267 	char buffer[BUFSIZ+1];
   268 
   269 	SDL_SetError("Hi there!");
   270 	printf("Error 1: %s\n", SDL_GetError());
   271 	SDL_ClearError();
   272 	SDL_memset(buffer, '1', BUFSIZ);
   273 	buffer[BUFSIZ] = 0;
   274 	SDL_SetError("This is the error: %s (%f)", buffer, 1.0);
   275 	printf("Error 2: %s\n", SDL_GetError());
   276 	exit(0);
   277 }
   278 #endif