test/testiconv.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 14 Mar 2006 07:45:15 +0000
changeset 1528 ff3322d66771
parent 1516 4d241ea8a1cd
child 1662 782fd950bd46
child 1895 c121d94672cb
child 3997 6a4f3a32c2e6
permissions -rw-r--r--
The check for UTF-* isn't needed anymore since UTF-8 has the same code range.
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 size_t widelen(char *data)
slouken@1501
     7
{
slouken@1501
     8
	size_t len = 0;
slouken@1501
     9
	Uint32 *p = (Uint32 *)data;
slouken@1501
    10
	while(*p++) {
slouken@1501
    11
		++len;
slouken@1501
    12
	}
slouken@1501
    13
	return len;
slouken@1501
    14
}
slouken@1501
    15
slouken@1501
    16
int main(int argc, char *argv[])
slouken@1501
    17
{
slouken@1501
    18
	const char * formats[] = {
slouken@1501
    19
		"UTF8",
slouken@1501
    20
		"UTF-8",
slouken@1501
    21
		"UTF16BE",
slouken@1501
    22
		"UTF-16BE",
slouken@1501
    23
		"UTF16LE",
slouken@1501
    24
		"UTF-16LE",
slouken@1501
    25
		"UTF32BE",
slouken@1501
    26
		"UTF-32BE",
slouken@1501
    27
		"UTF32LE",
slouken@1501
    28
		"UTF-32LE",
slouken@1501
    29
		"UCS4",
slouken@1501
    30
		"UCS-4",
slouken@1501
    31
	};
slouken@1501
    32
	char buffer[BUFSIZ];
slouken@1501
    33
	char *ucs4;
slouken@1501
    34
	char *test[2];
slouken@1516
    35
	int i, index = 0;
slouken@1501
    36
	FILE *file;
slouken@1501
    37
	int errors = 0;
slouken@1501
    38
slouken@1501
    39
	if ( !argv[1] ) {
slouken@1501
    40
		argv[1] = "utf8.txt";
slouken@1501
    41
	}
slouken@1501
    42
	file = fopen(argv[1], "rb");
slouken@1501
    43
	if ( !file ) {
slouken@1501
    44
		fprintf(stderr, "Unable to open %s\n", argv[1]);
slouken@1501
    45
		return (1);
slouken@1501
    46
	}
slouken@1501
    47
slouken@1501
    48
	while ( fgets(buffer, sizeof(buffer), file) ) {
slouken@1501
    49
		/* Convert to UCS-4 */
slouken@1516
    50
		size_t len;
slouken@1501
    51
		ucs4 = SDL_iconv_string("UCS-4", "UTF-8", buffer, SDL_strlen(buffer)+1);
slouken@1516
    52
		len = (widelen(ucs4)+1)*4;
slouken@1501
    53
		for ( i = 0; i < SDL_arraysize(formats); ++i ) {
slouken@1501
    54
			test[0] = SDL_iconv_string(formats[i], "UCS-4", ucs4, len);
slouken@1501
    55
			test[1] = SDL_iconv_string("UCS-4", formats[i], test[0], len);
slouken@1501
    56
			if ( SDL_memcmp(test[1], ucs4, len) != 0 ) {
slouken@1501
    57
				fprintf(stderr, "FAIL: %s\n", formats[i]);
slouken@1501
    58
				++errors;
slouken@1501
    59
			}
slouken@1501
    60
			SDL_free(test[0]);
slouken@1501
    61
			SDL_free(test[1]);
slouken@1501
    62
		}
slouken@1501
    63
		test[0] = SDL_iconv_string("UTF-8", "UCS-4", ucs4, len);
slouken@1501
    64
		SDL_free(ucs4);
slouken@1501
    65
		fputs(test[0], stdout);
slouken@1501
    66
		SDL_free(test[0]);
slouken@1501
    67
	}
slouken@1501
    68
	return (errors ? errors + 1 : 0);
slouken@1501
    69
}