src/SDL_error.c
branchSDL-1.3
changeset 1659 14717b52abc0
parent 1612 97d0966f4bf7
child 1662 782fd950bd46
     1.1 --- a/src/SDL_error.c	Mon May 01 06:58:33 2006 +0000
     1.2 +++ b/src/SDL_error.c	Wed May 17 08:18:28 2006 +0000
     1.3 @@ -39,15 +39,10 @@
     1.4  
     1.5  /* Private functions */
     1.6  
     1.7 -static void SDL_LookupString(const Uint8 *key, Uint16 *buf, int buflen)
     1.8 +static const char *SDL_LookupString(const char *key)
     1.9  {
    1.10  	/* FIXME: Add code to lookup key in language string hash-table */
    1.11 -
    1.12 -	/* Key not found in language string hash-table */
    1.13 -	while ( *key && (--buflen > 0) ) {
    1.14 -		*buf++ = *key++;
    1.15 -	}
    1.16 -	*buf = 0;	/* NULL terminate string */
    1.17 +	return key;
    1.18  }
    1.19  
    1.20  /* Public functions */
    1.21 @@ -66,17 +61,20 @@
    1.22  	error->argc = 0;
    1.23  	while ( *fmt ) {
    1.24  		if ( *fmt++ == '%' ) {
    1.25 +			while ( *fmt == '.' || (*fmt >= '0' && *fmt <= '9') ) {
    1.26 +				++fmt;
    1.27 +			}
    1.28  			switch (*fmt++) {
    1.29  			    case 0:  /* Malformed format string.. */
    1.30  				--fmt;
    1.31  				break;
    1.32 -#if 0	/* What is a character anyway?  (UNICODE issues) */
    1.33  			    case 'c':
    1.34 -				error->args[error->argc++].value_c =
    1.35 -						va_arg(ap, unsigned char);
    1.36 -				break;
    1.37 -#endif
    1.38 +			    case 'i':
    1.39  			    case 'd':
    1.40 +			    case 'u':
    1.41 +			    case 'o':
    1.42 +			    case 'x':
    1.43 +			    case 'X':
    1.44  				error->args[error->argc++].value_i =
    1.45  							va_arg(ap, int);
    1.46  				break;
    1.47 @@ -114,124 +112,67 @@
    1.48  #endif
    1.49  }
    1.50  
    1.51 -/* Print out an integer value to a UNICODE buffer */
    1.52 -static int PrintInt(Uint16 *str, unsigned int maxlen, int value)
    1.53 -{
    1.54 -	char tmp[128];
    1.55 -	int len, i;
    1.56 -
    1.57 -	SDL_snprintf(tmp, SDL_arraysize(tmp), "%d", value);
    1.58 -	len = 0;
    1.59 -	if ( SDL_strlen(tmp) < maxlen ) {
    1.60 -		for ( i=0; tmp[i]; ++i ) {
    1.61 -			*str++ = tmp[i];
    1.62 -			++len;
    1.63 -		}
    1.64 -	}
    1.65 -	return(len);
    1.66 -}
    1.67 -/* Print out a double value to a UNICODE buffer */
    1.68 -static int PrintDouble(Uint16 *str, unsigned int maxlen, double value)
    1.69 -{
    1.70 -	char tmp[128];
    1.71 -	int len, i;
    1.72 -
    1.73 -	SDL_snprintf(tmp, SDL_arraysize(tmp), "%f", value);
    1.74 -	len = 0;
    1.75 -	if ( SDL_strlen(tmp) < maxlen ) {
    1.76 -		for ( i=0; tmp[i]; ++i ) {
    1.77 -			*str++ = tmp[i];
    1.78 -			++len;
    1.79 -		}
    1.80 -	}
    1.81 -	return(len);
    1.82 -}
    1.83 -/* Print out a pointer value to a UNICODE buffer */
    1.84 -static int PrintPointer(Uint16 *str, unsigned int maxlen, void *value)
    1.85 -{
    1.86 -	char tmp[128];
    1.87 -	int len, i;
    1.88 -
    1.89 -	SDL_snprintf(tmp, SDL_arraysize(tmp), "%p", value);
    1.90 -	len = 0;
    1.91 -	if ( SDL_strlen(tmp) < maxlen ) {
    1.92 -		for ( i=0; tmp[i]; ++i ) {
    1.93 -			*str++ = tmp[i];
    1.94 -			++len;
    1.95 -		}
    1.96 -	}
    1.97 -	return(len);
    1.98 -}
    1.99 -
   1.100  /* This function has a bit more overhead than most error functions
   1.101     so that it supports internationalization and thread-safe errors.
   1.102  */
   1.103 -Uint16 *SDL_GetErrorMsgUNICODE(Uint16 *errstr, unsigned int maxlen)
   1.104 +char *SDL_GetErrorMsg(char *errstr, unsigned int maxlen)
   1.105  {
   1.106  	SDL_error *error;
   1.107  
   1.108  	/* Clear the error string */
   1.109 -	*errstr = 0; --maxlen;
   1.110 +	*errstr = '\0'; --maxlen;
   1.111  
   1.112  	/* Get the thread-safe error, and print it out */
   1.113  	error = SDL_GetErrBuf();
   1.114  	if ( error->error ) {
   1.115 -		Uint16 translated[ERR_MAX_STRLEN], *fmt, *msg;
   1.116 +		const char *fmt;
   1.117 +		char *msg = errstr;
   1.118  		int len;
   1.119  		int argi;
   1.120  
   1.121 -		/* Print out the UNICODE error message */
   1.122 -		SDL_LookupString(error->key, translated, sizeof(translated));
   1.123 -		msg = errstr;
   1.124 +		fmt = SDL_LookupString(error->key);
   1.125  		argi = 0;
   1.126 -		for ( fmt=translated; *fmt && (maxlen > 0); ) {
   1.127 +		while ( *fmt && (maxlen > 0) ) {
   1.128  			if ( *fmt == '%' ) {
   1.129 -				switch (fmt[1]) {
   1.130 -				    case 'S':	/* Special SKIP operand */
   1.131 -					argi += (fmt[2] - '0');
   1.132 -					++fmt;
   1.133 -					break;
   1.134 +				char tmp[32], *spot = tmp;
   1.135 +				*spot++ = *fmt++;
   1.136 +				while ( (*fmt == '.' || (*fmt >= '0' && *fmt <= '9')) && spot < (tmp+SDL_arraysize(tmp)-2) ) {
   1.137 +					*spot++ = *fmt++;
   1.138 +				}
   1.139 +				*spot++ = *fmt++;
   1.140 +				*spot++ = '\0';
   1.141 +				switch (spot[-2]) {
   1.142  				    case '%':
   1.143  					*msg++ = '%';
   1.144  					maxlen -= 1;
   1.145  					break;
   1.146 -#if 0	/* What is a character anyway?  (UNICODE issues) */
   1.147  				    case 'c':
   1.148 -                                        *msg++ = (unsigned char)
   1.149 -					         error->args[argi++].value_c;
   1.150 -					maxlen -= 1;
   1.151 -					break;
   1.152 -#endif
   1.153 -				    case 'd':
   1.154 -					len = PrintInt(msg, maxlen,
   1.155 -						error->args[argi++].value_i);
   1.156 +				    case 'i':
   1.157 +			            case 'd':
   1.158 +			            case 'u':
   1.159 +			            case 'o':
   1.160 +				    case 'x':
   1.161 +				    case 'X':
   1.162 +					len = SDL_snprintf(msg, maxlen, tmp, error->args[argi++].value_i);
   1.163  					msg += len;
   1.164  					maxlen -= len;
   1.165  					break;
   1.166  				    case 'f':
   1.167 -					len = PrintDouble(msg, maxlen,
   1.168 -						error->args[argi++].value_f);
   1.169 +					len = SDL_snprintf(msg, maxlen, tmp, error->args[argi++].value_f);
   1.170  					msg += len;
   1.171  					maxlen -= len;
   1.172  					break;
   1.173  				    case 'p':
   1.174 -					len = PrintPointer(msg, maxlen,
   1.175 -						error->args[argi++].value_ptr);
   1.176 +					len = SDL_snprintf(msg, maxlen, tmp, error->args[argi++].value_ptr);
   1.177  					msg += len;
   1.178  					maxlen -= len;
   1.179  					break;
   1.180 -				    case 's': /* UNICODE string */
   1.181 -					{ Uint16 buf[ERR_MAX_STRLEN], *str;
   1.182 -					  SDL_LookupString(error->args[argi++].buf, buf, sizeof(buf));
   1.183 -					  str = buf;
   1.184 -					  while ( *str && (maxlen > 0) ) {
   1.185 -						*msg++ = *str++;
   1.186 -						maxlen -= 1;
   1.187 -					  }
   1.188 -					}
   1.189 +				    case 's':
   1.190 +					len = SDL_snprintf(msg, maxlen, tmp, SDL_LookupString(error->args[argi++].buf));
   1.191 +					msg += len;
   1.192 +					maxlen -= len;
   1.193  					break;
   1.194  				}
   1.195 -				fmt += 2;
   1.196  			} else {
   1.197  				*msg++ = *fmt++;
   1.198  				maxlen -= 1;
   1.199 @@ -242,38 +183,12 @@
   1.200  	return(errstr);
   1.201  }
   1.202  
   1.203 -Uint8 *SDL_GetErrorMsg(Uint8 *errstr, unsigned int maxlen)
   1.204 -{
   1.205 -	Uint16 *errstr16;
   1.206 -	unsigned int i;
   1.207 -
   1.208 -	/* Allocate the UNICODE buffer */
   1.209 -	errstr16 = (Uint16 *)SDL_malloc(maxlen * (sizeof *errstr16));
   1.210 -	if ( ! errstr16 ) {
   1.211 -		SDL_strlcpy((char *)errstr, "Out of memory", maxlen);
   1.212 -		return(errstr);
   1.213 -	}
   1.214 -
   1.215 -	/* Get the error message */
   1.216 -	SDL_GetErrorMsgUNICODE(errstr16, maxlen);
   1.217 -
   1.218 -	/* Convert from UNICODE to Latin1 encoding */
   1.219 -	for ( i=0; i<maxlen; ++i ) {
   1.220 -		errstr[i] = (Uint8)errstr16[i];
   1.221 -	}
   1.222 -
   1.223 -	/* Free UNICODE buffer (if necessary) */
   1.224 -	SDL_free(errstr16);
   1.225 -
   1.226 -	return(errstr);
   1.227 -}
   1.228 -
   1.229  /* Available for backwards compatibility */
   1.230  char *SDL_GetError (void)
   1.231  {
   1.232  	static char errmsg[SDL_ERRBUFIZE];
   1.233  
   1.234 -	return((char *)SDL_GetErrorMsg((unsigned char *)errmsg, SDL_ERRBUFIZE));
   1.235 +	return((char *)SDL_GetErrorMsg(errmsg, SDL_ERRBUFIZE));
   1.236  }
   1.237  
   1.238  void SDL_ClearError(void)