src/SDL_error.c
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1659 14717b52abc0
child 1668 4da1ee79c9af
     1.1 --- a/src/SDL_error.c	Sun May 21 17:27:13 2006 +0000
     1.2 +++ b/src/SDL_error.c	Sun May 28 13:04:16 2006 +0000
     1.3 @@ -32,207 +32,224 @@
     1.4  static SDL_error SDL_global_error;
     1.5  #define SDL_GetErrBuf()	(&SDL_global_error)
     1.6  #else
     1.7 -extern SDL_error *SDL_GetErrBuf(void);
     1.8 +extern SDL_error *SDL_GetErrBuf (void);
     1.9  #endif /* SDL_THREADS_DISABLED */
    1.10  
    1.11  #define SDL_ERRBUFIZE	1024
    1.12  
    1.13  /* Private functions */
    1.14  
    1.15 -static const char *SDL_LookupString(const char *key)
    1.16 +static const char *
    1.17 +SDL_LookupString (const char *key)
    1.18  {
    1.19 -	/* FIXME: Add code to lookup key in language string hash-table */
    1.20 -	return key;
    1.21 +    /* FIXME: Add code to lookup key in language string hash-table */
    1.22 +    return key;
    1.23  }
    1.24  
    1.25  /* Public functions */
    1.26  
    1.27 -void SDL_SetError (const char *fmt, ...)
    1.28 +void
    1.29 +SDL_SetError (const char *fmt, ...)
    1.30  {
    1.31 -	va_list ap;
    1.32 -	SDL_error *error;
    1.33 +    va_list ap;
    1.34 +    SDL_error *error;
    1.35  
    1.36 -	/* Copy in the key, mark error as valid */
    1.37 -	error = SDL_GetErrBuf();
    1.38 -	error->error = 1;
    1.39 -	SDL_strlcpy((char *)error->key, fmt, sizeof(error->key));
    1.40 +    /* Copy in the key, mark error as valid */
    1.41 +    error = SDL_GetErrBuf ();
    1.42 +    error->error = 1;
    1.43 +    SDL_strlcpy ((char *) error->key, fmt, sizeof (error->key));
    1.44  
    1.45 -	va_start(ap, fmt);
    1.46 -	error->argc = 0;
    1.47 -	while ( *fmt ) {
    1.48 -		if ( *fmt++ == '%' ) {
    1.49 -			while ( *fmt == '.' || (*fmt >= '0' && *fmt <= '9') ) {
    1.50 -				++fmt;
    1.51 -			}
    1.52 -			switch (*fmt++) {
    1.53 -			    case 0:  /* Malformed format string.. */
    1.54 -				--fmt;
    1.55 -				break;
    1.56 -			    case 'c':
    1.57 -			    case 'i':
    1.58 -			    case 'd':
    1.59 -			    case 'u':
    1.60 -			    case 'o':
    1.61 -			    case 'x':
    1.62 -			    case 'X':
    1.63 -				error->args[error->argc++].value_i =
    1.64 -							va_arg(ap, int);
    1.65 -				break;
    1.66 -			    case 'f':
    1.67 -				error->args[error->argc++].value_f =
    1.68 -							va_arg(ap, double);
    1.69 -				break;
    1.70 -			    case 'p':
    1.71 -				error->args[error->argc++].value_ptr =
    1.72 -							va_arg(ap, void *);
    1.73 -				break;
    1.74 -			    case 's':
    1.75 -				{
    1.76 -				  int i = error->argc;
    1.77 -				  const char *str = va_arg(ap, const char *);
    1.78 -				  if (str == NULL)
    1.79 -				      str = "(null)";
    1.80 -				  SDL_strlcpy((char *)error->args[i].buf, str, ERR_MAX_STRLEN);
    1.81 -				  error->argc++;
    1.82 -				}
    1.83 -				break;
    1.84 -			    default:
    1.85 -				break;
    1.86 -			}
    1.87 -			if ( error->argc >= ERR_MAX_ARGS ) {
    1.88 -				break;
    1.89 -			}
    1.90 -		}
    1.91 -	}
    1.92 -	va_end(ap);
    1.93 +    va_start (ap, fmt);
    1.94 +    error->argc = 0;
    1.95 +    while (*fmt) {
    1.96 +        if (*fmt++ == '%') {
    1.97 +            while (*fmt == '.' || (*fmt >= '0' && *fmt <= '9')) {
    1.98 +                ++fmt;
    1.99 +            }
   1.100 +            switch (*fmt++) {
   1.101 +            case 0:            /* Malformed format string.. */
   1.102 +                --fmt;
   1.103 +                break;
   1.104 +            case 'c':
   1.105 +            case 'i':
   1.106 +            case 'd':
   1.107 +            case 'u':
   1.108 +            case 'o':
   1.109 +            case 'x':
   1.110 +            case 'X':
   1.111 +                error->args[error->argc++].value_i = va_arg (ap, int);
   1.112 +                break;
   1.113 +            case 'f':
   1.114 +                error->args[error->argc++].value_f = va_arg (ap, double);
   1.115 +                break;
   1.116 +            case 'p':
   1.117 +                error->args[error->argc++].value_ptr = va_arg (ap, void *);
   1.118 +                break;
   1.119 +            case 's':
   1.120 +                {
   1.121 +                    int i = error->argc;
   1.122 +                    const char *str = va_arg (ap, const char *);
   1.123 +                    if (str == NULL)
   1.124 +                        str = "(null)";
   1.125 +                    SDL_strlcpy ((char *) error->args[i].buf, str,
   1.126 +                                 ERR_MAX_STRLEN);
   1.127 +                    error->argc++;
   1.128 +                }
   1.129 +                break;
   1.130 +            default:
   1.131 +                break;
   1.132 +            }
   1.133 +            if (error->argc >= ERR_MAX_ARGS) {
   1.134 +                break;
   1.135 +            }
   1.136 +        }
   1.137 +    }
   1.138 +    va_end (ap);
   1.139  
   1.140 -	/* If we are in debug mode, print out an error message */
   1.141 +    /* If we are in debug mode, print out an error message */
   1.142  #ifdef DEBUG_ERROR
   1.143 -	fprintf(stderr, "SDL_SetError: %s\n", SDL_GetError());
   1.144 +    fprintf (stderr, "SDL_SetError: %s\n", SDL_GetError ());
   1.145  #endif
   1.146  }
   1.147  
   1.148  /* This function has a bit more overhead than most error functions
   1.149     so that it supports internationalization and thread-safe errors.
   1.150  */
   1.151 -char *SDL_GetErrorMsg(char *errstr, unsigned int maxlen)
   1.152 +char *
   1.153 +SDL_GetErrorMsg (char *errstr, unsigned int maxlen)
   1.154  {
   1.155 -	SDL_error *error;
   1.156 +    SDL_error *error;
   1.157  
   1.158 -	/* Clear the error string */
   1.159 -	*errstr = '\0'; --maxlen;
   1.160 +    /* Clear the error string */
   1.161 +    *errstr = '\0';
   1.162 +    --maxlen;
   1.163  
   1.164 -	/* Get the thread-safe error, and print it out */
   1.165 -	error = SDL_GetErrBuf();
   1.166 -	if ( error->error ) {
   1.167 -		const char *fmt;
   1.168 -		char *msg = errstr;
   1.169 -		int len;
   1.170 -		int argi;
   1.171 +    /* Get the thread-safe error, and print it out */
   1.172 +    error = SDL_GetErrBuf ();
   1.173 +    if (error->error) {
   1.174 +        const char *fmt;
   1.175 +        char *msg = errstr;
   1.176 +        int len;
   1.177 +        int argi;
   1.178  
   1.179 -		fmt = SDL_LookupString(error->key);
   1.180 -		argi = 0;
   1.181 -		while ( *fmt && (maxlen > 0) ) {
   1.182 -			if ( *fmt == '%' ) {
   1.183 -				char tmp[32], *spot = tmp;
   1.184 -				*spot++ = *fmt++;
   1.185 -				while ( (*fmt == '.' || (*fmt >= '0' && *fmt <= '9')) && spot < (tmp+SDL_arraysize(tmp)-2) ) {
   1.186 -					*spot++ = *fmt++;
   1.187 -				}
   1.188 -				*spot++ = *fmt++;
   1.189 -				*spot++ = '\0';
   1.190 -				switch (spot[-2]) {
   1.191 -				    case '%':
   1.192 -					*msg++ = '%';
   1.193 -					maxlen -= 1;
   1.194 -					break;
   1.195 -				    case 'c':
   1.196 -				    case 'i':
   1.197 -			            case 'd':
   1.198 -			            case 'u':
   1.199 -			            case 'o':
   1.200 -				    case 'x':
   1.201 -				    case 'X':
   1.202 -					len = SDL_snprintf(msg, maxlen, tmp, error->args[argi++].value_i);
   1.203 -					msg += len;
   1.204 -					maxlen -= len;
   1.205 -					break;
   1.206 -				    case 'f':
   1.207 -					len = SDL_snprintf(msg, maxlen, tmp, error->args[argi++].value_f);
   1.208 -					msg += len;
   1.209 -					maxlen -= len;
   1.210 -					break;
   1.211 -				    case 'p':
   1.212 -					len = SDL_snprintf(msg, maxlen, tmp, error->args[argi++].value_ptr);
   1.213 -					msg += len;
   1.214 -					maxlen -= len;
   1.215 -					break;
   1.216 -				    case 's':
   1.217 -					len = SDL_snprintf(msg, maxlen, tmp, SDL_LookupString(error->args[argi++].buf));
   1.218 -					msg += len;
   1.219 -					maxlen -= len;
   1.220 -					break;
   1.221 -				}
   1.222 -			} else {
   1.223 -				*msg++ = *fmt++;
   1.224 -				maxlen -= 1;
   1.225 -			}
   1.226 -		}
   1.227 -		*msg = 0;	/* NULL terminate the string */
   1.228 -	}
   1.229 -	return(errstr);
   1.230 +        fmt = SDL_LookupString (error->key);
   1.231 +        argi = 0;
   1.232 +        while (*fmt && (maxlen > 0)) {
   1.233 +            if (*fmt == '%') {
   1.234 +                char tmp[32], *spot = tmp;
   1.235 +                *spot++ = *fmt++;
   1.236 +                while ((*fmt == '.' || (*fmt >= '0' && *fmt <= '9'))
   1.237 +                       && spot < (tmp + SDL_arraysize (tmp) - 2)) {
   1.238 +                    *spot++ = *fmt++;
   1.239 +                }
   1.240 +                *spot++ = *fmt++;
   1.241 +                *spot++ = '\0';
   1.242 +                switch (spot[-2]) {
   1.243 +                case '%':
   1.244 +                    *msg++ = '%';
   1.245 +                    maxlen -= 1;
   1.246 +                    break;
   1.247 +                case 'c':
   1.248 +                case 'i':
   1.249 +                case 'd':
   1.250 +                case 'u':
   1.251 +                case 'o':
   1.252 +                case 'x':
   1.253 +                case 'X':
   1.254 +                    len =
   1.255 +                        SDL_snprintf (msg, maxlen, tmp,
   1.256 +                                      error->args[argi++].value_i);
   1.257 +                    msg += len;
   1.258 +                    maxlen -= len;
   1.259 +                    break;
   1.260 +                case 'f':
   1.261 +                    len =
   1.262 +                        SDL_snprintf (msg, maxlen, tmp,
   1.263 +                                      error->args[argi++].value_f);
   1.264 +                    msg += len;
   1.265 +                    maxlen -= len;
   1.266 +                    break;
   1.267 +                case 'p':
   1.268 +                    len =
   1.269 +                        SDL_snprintf (msg, maxlen, tmp,
   1.270 +                                      error->args[argi++].value_ptr);
   1.271 +                    msg += len;
   1.272 +                    maxlen -= len;
   1.273 +                    break;
   1.274 +                case 's':
   1.275 +                    len =
   1.276 +                        SDL_snprintf (msg, maxlen, tmp,
   1.277 +                                      SDL_LookupString (error->
   1.278 +                                                        args[argi++].buf));
   1.279 +                    msg += len;
   1.280 +                    maxlen -= len;
   1.281 +                    break;
   1.282 +                }
   1.283 +            } else {
   1.284 +                *msg++ = *fmt++;
   1.285 +                maxlen -= 1;
   1.286 +            }
   1.287 +        }
   1.288 +        *msg = 0;               /* NULL terminate the string */
   1.289 +    }
   1.290 +    return (errstr);
   1.291  }
   1.292  
   1.293  /* Available for backwards compatibility */
   1.294 -char *SDL_GetError (void)
   1.295 +char *
   1.296 +SDL_GetError (void)
   1.297  {
   1.298 -	static char errmsg[SDL_ERRBUFIZE];
   1.299 +    static char errmsg[SDL_ERRBUFIZE];
   1.300  
   1.301 -	return((char *)SDL_GetErrorMsg(errmsg, SDL_ERRBUFIZE));
   1.302 +    return ((char *) SDL_GetErrorMsg (errmsg, SDL_ERRBUFIZE));
   1.303  }
   1.304  
   1.305 -void SDL_ClearError(void)
   1.306 +void
   1.307 +SDL_ClearError (void)
   1.308  {
   1.309 -	SDL_error *error;
   1.310 +    SDL_error *error;
   1.311  
   1.312 -	error = SDL_GetErrBuf();
   1.313 -	error->error = 0;
   1.314 +    error = SDL_GetErrBuf ();
   1.315 +    error->error = 0;
   1.316  }
   1.317  
   1.318  /* Very common errors go here */
   1.319 -void SDL_Error(SDL_errorcode code)
   1.320 +void
   1.321 +SDL_Error (SDL_errorcode code)
   1.322  {
   1.323 -	switch (code) {
   1.324 -		case SDL_ENOMEM:
   1.325 -			SDL_SetError("Out of memory");
   1.326 -			break;
   1.327 -		case SDL_EFREAD:
   1.328 -			SDL_SetError("Error reading from datastream");
   1.329 -			break;
   1.330 -		case SDL_EFWRITE:
   1.331 -			SDL_SetError("Error writing to datastream");
   1.332 -			break;
   1.333 -		case SDL_EFSEEK:
   1.334 -			SDL_SetError("Error seeking in datastream");
   1.335 -			break;
   1.336 -		default:
   1.337 -			SDL_SetError("Unknown SDL error");
   1.338 -			break;
   1.339 -	}
   1.340 +    switch (code) {
   1.341 +    case SDL_ENOMEM:
   1.342 +        SDL_SetError ("Out of memory");
   1.343 +        break;
   1.344 +    case SDL_EFREAD:
   1.345 +        SDL_SetError ("Error reading from datastream");
   1.346 +        break;
   1.347 +    case SDL_EFWRITE:
   1.348 +        SDL_SetError ("Error writing to datastream");
   1.349 +        break;
   1.350 +    case SDL_EFSEEK:
   1.351 +        SDL_SetError ("Error seeking in datastream");
   1.352 +        break;
   1.353 +    default:
   1.354 +        SDL_SetError ("Unknown SDL error");
   1.355 +        break;
   1.356 +    }
   1.357  }
   1.358  
   1.359  #ifdef TEST_ERROR
   1.360 -int main(int argc, char *argv[])
   1.361 +int
   1.362 +main (int argc, char *argv[])
   1.363  {
   1.364 -	char buffer[BUFSIZ+1];
   1.365 +    char buffer[BUFSIZ + 1];
   1.366  
   1.367 -	SDL_SetError("Hi there!");
   1.368 -	printf("Error 1: %s\n", SDL_GetError());
   1.369 -	SDL_ClearError();
   1.370 -	SDL_memset(buffer, '1', BUFSIZ);
   1.371 -	buffer[BUFSIZ] = 0;
   1.372 -	SDL_SetError("This is the error: %s (%f)", buffer, 1.0);
   1.373 -	printf("Error 2: %s\n", SDL_GetError());
   1.374 -	exit(0);
   1.375 +    SDL_SetError ("Hi there!");
   1.376 +    printf ("Error 1: %s\n", SDL_GetError ());
   1.377 +    SDL_ClearError ();
   1.378 +    SDL_memset (buffer, '1', BUFSIZ);
   1.379 +    buffer[BUFSIZ] = 0;
   1.380 +    SDL_SetError ("This is the error: %s (%f)", buffer, 1.0);
   1.381 +    printf ("Error 2: %s\n", SDL_GetError ());
   1.382 +    exit (0);
   1.383  }
   1.384  #endif
   1.385 +/* vi: set ts=4 sw=4 expandtab: */