test/testiconv.c
changeset 1501 73dc5d39bbf8
child 1502 d403a39389da
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testiconv.c	Mon Mar 13 01:08:00 2006 +0000
     1.3 @@ -0,0 +1,85 @@
     1.4 +
     1.5 +#include <stdio.h>
     1.6 +
     1.7 +#include "SDL.h"
     1.8 +
     1.9 +static SDL_bool testutf16(char *data)
    1.10 +{
    1.11 +	Uint32 *p = (Uint32 *)data;
    1.12 +	while(*p) {
    1.13 +		if ( *p > 0x10FFFF ) {
    1.14 +			return SDL_FALSE;
    1.15 +		}
    1.16 +		++p;
    1.17 +	}
    1.18 +	return SDL_TRUE;
    1.19 +}
    1.20 +
    1.21 +static size_t widelen(char *data)
    1.22 +{
    1.23 +	size_t len = 0;
    1.24 +	Uint32 *p = (Uint32 *)data;
    1.25 +	while(*p++) {
    1.26 +		++len;
    1.27 +	}
    1.28 +	return len;
    1.29 +}
    1.30 +
    1.31 +int main(int argc, char *argv[])
    1.32 +{
    1.33 +	const char * formats[] = {
    1.34 +		"UTF8",
    1.35 +		"UTF-8",
    1.36 +		"UTF16BE",
    1.37 +		"UTF-16BE",
    1.38 +		"UTF16LE",
    1.39 +		"UTF-16LE",
    1.40 +		"UTF32BE",
    1.41 +		"UTF-32BE",
    1.42 +		"UTF32LE",
    1.43 +		"UTF-32LE",
    1.44 +		"UCS4",
    1.45 +		"UCS-4",
    1.46 +	};
    1.47 +	char buffer[BUFSIZ];
    1.48 +	char *ucs4;
    1.49 +	char *test[2];
    1.50 +	int i, j, index = 0;
    1.51 +	FILE *file;
    1.52 +	int errors = 0;
    1.53 +
    1.54 +	if ( !argv[1] ) {
    1.55 +		argv[1] = "utf8.txt";
    1.56 +	}
    1.57 +	file = fopen(argv[1], "rb");
    1.58 +	if ( !file ) {
    1.59 +		fprintf(stderr, "Unable to open %s\n", argv[1]);
    1.60 +		return (1);
    1.61 +	}
    1.62 +
    1.63 +	while ( fgets(buffer, sizeof(buffer), file) ) {
    1.64 +		/* Convert to UCS-4 */
    1.65 +		ucs4 = SDL_iconv_string("UCS-4", "UTF-8", buffer, SDL_strlen(buffer)+1);
    1.66 +		size_t len = (widelen(ucs4)+1)*4;
    1.67 +		for ( i = 0; i < SDL_arraysize(formats); ++i ) {
    1.68 +			if ( (SDL_strncasecmp(formats[i], "UTF16", 5) == 0 ||
    1.69 +			      SDL_strncasecmp(formats[i], "UTF-16", 6) == 0) &&
    1.70 +			      !testutf16(ucs4) ) {
    1.71 +				continue;
    1.72 +			}
    1.73 +			test[0] = SDL_iconv_string(formats[i], "UCS-4", ucs4, len);
    1.74 +			test[1] = SDL_iconv_string("UCS-4", formats[i], test[0], len);
    1.75 +			if ( SDL_memcmp(test[1], ucs4, len) != 0 ) {
    1.76 +				fprintf(stderr, "FAIL: %s\n", formats[i]);
    1.77 +				++errors;
    1.78 +			}
    1.79 +			SDL_free(test[0]);
    1.80 +			SDL_free(test[1]);
    1.81 +		}
    1.82 +		test[0] = SDL_iconv_string("UTF-8", "UCS-4", ucs4, len);
    1.83 +		SDL_free(ucs4);
    1.84 +		fputs(test[0], stdout);
    1.85 +		SDL_free(test[0]);
    1.86 +	}
    1.87 +	return (errors ? errors + 1 : 0);
    1.88 +}