test/testiconv.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 13 Mar 2006 05:36:42 +0000
changeset 1516 4d241ea8a1cd
parent 1502 d403a39389da
child 1528 ff3322d66771
permissions -rw-r--r--
Updated MacOS Classic build
slouken@1501
     1
slouken@1501
     2
#include <stdio.h>
slouken@1501
     3
slouken@1501
     4
#include "SDL.h"
slouken@1501
     5
slouken@1502
     6
static SDL_bool testutf1632(char *data)
slouken@1501
     7
{
slouken@1501
     8
	Uint32 *p = (Uint32 *)data;
slouken@1501
     9
	while(*p) {
slouken@1501
    10
		if ( *p > 0x10FFFF ) {
slouken@1501
    11
			return SDL_FALSE;
slouken@1501
    12
		}
slouken@1501
    13
		++p;
slouken@1501
    14
	}
slouken@1501
    15
	return SDL_TRUE;
slouken@1501
    16
}
slouken@1501
    17
slouken@1501
    18
static size_t widelen(char *data)
slouken@1501
    19
{
slouken@1501
    20
	size_t len = 0;
slouken@1501
    21
	Uint32 *p = (Uint32 *)data;
slouken@1501
    22
	while(*p++) {
slouken@1501
    23
		++len;
slouken@1501
    24
	}
slouken@1501
    25
	return len;
slouken@1501
    26
}
slouken@1501
    27
slouken@1501
    28
int main(int argc, char *argv[])
slouken@1501
    29
{
slouken@1501
    30
	const char * formats[] = {
slouken@1501
    31
		"UTF8",
slouken@1501
    32
		"UTF-8",
slouken@1501
    33
		"UTF16BE",
slouken@1501
    34
		"UTF-16BE",
slouken@1501
    35
		"UTF16LE",
slouken@1501
    36
		"UTF-16LE",
slouken@1501
    37
		"UTF32BE",
slouken@1501
    38
		"UTF-32BE",
slouken@1501
    39
		"UTF32LE",
slouken@1501
    40
		"UTF-32LE",
slouken@1501
    41
		"UCS4",
slouken@1501
    42
		"UCS-4",
slouken@1501
    43
	};
slouken@1501
    44
	char buffer[BUFSIZ];
slouken@1501
    45
	char *ucs4;
slouken@1501
    46
	char *test[2];
slouken@1516
    47
	int i, index = 0;
slouken@1501
    48
	FILE *file;
slouken@1501
    49
	int errors = 0;
slouken@1501
    50
slouken@1501
    51
	if ( !argv[1] ) {
slouken@1501
    52
		argv[1] = "utf8.txt";
slouken@1501
    53
	}
slouken@1501
    54
	file = fopen(argv[1], "rb");
slouken@1501
    55
	if ( !file ) {
slouken@1501
    56
		fprintf(stderr, "Unable to open %s\n", argv[1]);
slouken@1501
    57
		return (1);
slouken@1501
    58
	}
slouken@1501
    59
slouken@1501
    60
	while ( fgets(buffer, sizeof(buffer), file) ) {
slouken@1501
    61
		/* Convert to UCS-4 */
slouken@1516
    62
		size_t len;
slouken@1501
    63
		ucs4 = SDL_iconv_string("UCS-4", "UTF-8", buffer, SDL_strlen(buffer)+1);
slouken@1516
    64
		len = (widelen(ucs4)+1)*4;
slouken@1501
    65
		for ( i = 0; i < SDL_arraysize(formats); ++i ) {
slouken@1501
    66
			if ( (SDL_strncasecmp(formats[i], "UTF16", 5) == 0 ||
slouken@1502
    67
			      SDL_strncasecmp(formats[i], "UTF-16", 6) == 0 ||
slouken@1502
    68
			      SDL_strncasecmp(formats[i], "UTF32", 5) == 0 ||
slouken@1502
    69
			      SDL_strncasecmp(formats[i], "UTF-32", 6) == 0) &&
slouken@1502
    70
			      !testutf1632(ucs4) ) {
slouken@1501
    71
				continue;
slouken@1501
    72
			}
slouken@1501
    73
			test[0] = SDL_iconv_string(formats[i], "UCS-4", ucs4, len);
slouken@1501
    74
			test[1] = SDL_iconv_string("UCS-4", formats[i], test[0], len);
slouken@1501
    75
			if ( SDL_memcmp(test[1], ucs4, len) != 0 ) {
slouken@1501
    76
				fprintf(stderr, "FAIL: %s\n", formats[i]);
slouken@1501
    77
				++errors;
slouken@1501
    78
			}
slouken@1501
    79
			SDL_free(test[0]);
slouken@1501
    80
			SDL_free(test[1]);
slouken@1501
    81
		}
slouken@1501
    82
		test[0] = SDL_iconv_string("UTF-8", "UCS-4", ucs4, len);
slouken@1501
    83
		SDL_free(ucs4);
slouken@1501
    84
		fputs(test[0], stdout);
slouken@1501
    85
		SDL_free(test[0]);
slouken@1501
    86
	}
slouken@1501
    87
	return (errors ? errors + 1 : 0);
slouken@1501
    88
}