test/testiconv.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 13 Mar 2006 01:08:00 +0000
changeset 1501 73dc5d39bbf8
child 1502 d403a39389da
permissions -rw-r--r--
Added UTF-8 <-> UTF-16 <-> UTF-32 <-> UCS-2 <-> UCS-4 conversion capability
slouken@1501
     1
slouken@1501
     2
#include <stdio.h>
slouken@1501
     3
slouken@1501
     4
#include "SDL.h"
slouken@1501
     5
slouken@1501
     6
static SDL_bool testutf16(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@1501
    47
	int i, j, 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@1501
    62
		ucs4 = SDL_iconv_string("UCS-4", "UTF-8", buffer, SDL_strlen(buffer)+1);
slouken@1501
    63
		size_t len = (widelen(ucs4)+1)*4;
slouken@1501
    64
		for ( i = 0; i < SDL_arraysize(formats); ++i ) {
slouken@1501
    65
			if ( (SDL_strncasecmp(formats[i], "UTF16", 5) == 0 ||
slouken@1501
    66
			      SDL_strncasecmp(formats[i], "UTF-16", 6) == 0) &&
slouken@1501
    67
			      !testutf16(ucs4) ) {
slouken@1501
    68
				continue;
slouken@1501
    69
			}
slouken@1501
    70
			test[0] = SDL_iconv_string(formats[i], "UCS-4", ucs4, len);
slouken@1501
    71
			test[1] = SDL_iconv_string("UCS-4", formats[i], test[0], len);
slouken@1501
    72
			if ( SDL_memcmp(test[1], ucs4, len) != 0 ) {
slouken@1501
    73
				fprintf(stderr, "FAIL: %s\n", formats[i]);
slouken@1501
    74
				++errors;
slouken@1501
    75
			}
slouken@1501
    76
			SDL_free(test[0]);
slouken@1501
    77
			SDL_free(test[1]);
slouken@1501
    78
		}
slouken@1501
    79
		test[0] = SDL_iconv_string("UTF-8", "UCS-4", ucs4, len);
slouken@1501
    80
		SDL_free(ucs4);
slouken@1501
    81
		fputs(test[0], stdout);
slouken@1501
    82
		SDL_free(test[0]);
slouken@1501
    83
	}
slouken@1501
    84
	return (errors ? errors + 1 : 0);
slouken@1501
    85
}