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