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