test/testiconv.c
changeset 1895 c121d94672cb
parent 1528 ff3322d66771
child 2142 ca80c942e69c
     1.1 --- a/test/testiconv.c	Thu Jul 06 18:01:37 2006 +0000
     1.2 +++ b/test/testiconv.c	Mon Jul 10 21:04:37 2006 +0000
     1.3 @@ -3,67 +3,71 @@
     1.4  
     1.5  #include "SDL.h"
     1.6  
     1.7 -static size_t widelen(char *data)
     1.8 +static size_t
     1.9 +widelen(char *data)
    1.10  {
    1.11 -	size_t len = 0;
    1.12 -	Uint32 *p = (Uint32 *)data;
    1.13 -	while(*p++) {
    1.14 -		++len;
    1.15 -	}
    1.16 -	return len;
    1.17 +    size_t len = 0;
    1.18 +    Uint32 *p = (Uint32 *) data;
    1.19 +    while (*p++) {
    1.20 +        ++len;
    1.21 +    }
    1.22 +    return len;
    1.23  }
    1.24  
    1.25 -int main(int argc, char *argv[])
    1.26 +int
    1.27 +main(int argc, char *argv[])
    1.28  {
    1.29 -	const char * formats[] = {
    1.30 -		"UTF8",
    1.31 -		"UTF-8",
    1.32 -		"UTF16BE",
    1.33 -		"UTF-16BE",
    1.34 -		"UTF16LE",
    1.35 -		"UTF-16LE",
    1.36 -		"UTF32BE",
    1.37 -		"UTF-32BE",
    1.38 -		"UTF32LE",
    1.39 -		"UTF-32LE",
    1.40 -		"UCS4",
    1.41 -		"UCS-4",
    1.42 -	};
    1.43 -	char buffer[BUFSIZ];
    1.44 -	char *ucs4;
    1.45 -	char *test[2];
    1.46 -	int i, index = 0;
    1.47 -	FILE *file;
    1.48 -	int errors = 0;
    1.49 +    const char *formats[] = {
    1.50 +        "UTF8",
    1.51 +        "UTF-8",
    1.52 +        "UTF16BE",
    1.53 +        "UTF-16BE",
    1.54 +        "UTF16LE",
    1.55 +        "UTF-16LE",
    1.56 +        "UTF32BE",
    1.57 +        "UTF-32BE",
    1.58 +        "UTF32LE",
    1.59 +        "UTF-32LE",
    1.60 +        "UCS4",
    1.61 +        "UCS-4",
    1.62 +    };
    1.63 +    char buffer[BUFSIZ];
    1.64 +    char *ucs4;
    1.65 +    char *test[2];
    1.66 +    int i, index = 0;
    1.67 +    FILE *file;
    1.68 +    int errors = 0;
    1.69  
    1.70 -	if ( !argv[1] ) {
    1.71 -		argv[1] = "utf8.txt";
    1.72 -	}
    1.73 -	file = fopen(argv[1], "rb");
    1.74 -	if ( !file ) {
    1.75 -		fprintf(stderr, "Unable to open %s\n", argv[1]);
    1.76 -		return (1);
    1.77 -	}
    1.78 +    if (!argv[1]) {
    1.79 +        argv[1] = "utf8.txt";
    1.80 +    }
    1.81 +    file = fopen(argv[1], "rb");
    1.82 +    if (!file) {
    1.83 +        fprintf(stderr, "Unable to open %s\n", argv[1]);
    1.84 +        return (1);
    1.85 +    }
    1.86  
    1.87 -	while ( fgets(buffer, sizeof(buffer), file) ) {
    1.88 -		/* Convert to UCS-4 */
    1.89 -		size_t len;
    1.90 -		ucs4 = SDL_iconv_string("UCS-4", "UTF-8", buffer, SDL_strlen(buffer)+1);
    1.91 -		len = (widelen(ucs4)+1)*4;
    1.92 -		for ( i = 0; i < SDL_arraysize(formats); ++i ) {
    1.93 -			test[0] = SDL_iconv_string(formats[i], "UCS-4", ucs4, len);
    1.94 -			test[1] = SDL_iconv_string("UCS-4", formats[i], test[0], len);
    1.95 -			if ( SDL_memcmp(test[1], ucs4, len) != 0 ) {
    1.96 -				fprintf(stderr, "FAIL: %s\n", formats[i]);
    1.97 -				++errors;
    1.98 -			}
    1.99 -			SDL_free(test[0]);
   1.100 -			SDL_free(test[1]);
   1.101 -		}
   1.102 -		test[0] = SDL_iconv_string("UTF-8", "UCS-4", ucs4, len);
   1.103 -		SDL_free(ucs4);
   1.104 -		fputs(test[0], stdout);
   1.105 -		SDL_free(test[0]);
   1.106 -	}
   1.107 -	return (errors ? errors + 1 : 0);
   1.108 +    while (fgets(buffer, sizeof(buffer), file)) {
   1.109 +        /* Convert to UCS-4 */
   1.110 +        size_t len;
   1.111 +        ucs4 =
   1.112 +            SDL_iconv_string("UCS-4", "UTF-8", buffer,
   1.113 +                             SDL_strlen(buffer) + 1);
   1.114 +        len = (widelen(ucs4) + 1) * 4;
   1.115 +        for (i = 0; i < SDL_arraysize(formats); ++i) {
   1.116 +            test[0] = SDL_iconv_string(formats[i], "UCS-4", ucs4, len);
   1.117 +            test[1] = SDL_iconv_string("UCS-4", formats[i], test[0], len);
   1.118 +            if (SDL_memcmp(test[1], ucs4, len) != 0) {
   1.119 +                fprintf(stderr, "FAIL: %s\n", formats[i]);
   1.120 +                ++errors;
   1.121 +            }
   1.122 +            SDL_free(test[0]);
   1.123 +            SDL_free(test[1]);
   1.124 +        }
   1.125 +        test[0] = SDL_iconv_string("UTF-8", "UCS-4", ucs4, len);
   1.126 +        SDL_free(ucs4);
   1.127 +        fputs(test[0], stdout);
   1.128 +        SDL_free(test[0]);
   1.129 +    }
   1.130 +    return (errors ? errors + 1 : 0);
   1.131  }