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