test/testiconv.c
author Patrice Mandin <patmandin@gmail.com>
Wed, 13 Aug 2014 11:02:11 +0200
branchSDL-1.2
changeset 9059 9a9f56143400
parent 3997 6a4f3a32c2e6
permissions -rw-r--r--
atari:gem: Remove unused variables.
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@3997
    56
			if ( !test[1] || 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@3997
    60
			if ( test[0] ) {
slouken@3997
    61
				SDL_free(test[0]);
slouken@3997
    62
			}
slouken@3997
    63
			if ( test[1] ) {
slouken@3997
    64
				SDL_free(test[1]);
slouken@3997
    65
			}
slouken@1501
    66
		}
slouken@1501
    67
		test[0] = SDL_iconv_string("UTF-8", "UCS-4", ucs4, len);
slouken@1501
    68
		SDL_free(ucs4);
slouken@1501
    69
		fputs(test[0], stdout);
slouken@1501
    70
		SDL_free(test[0]);
slouken@1501
    71
	}
slouken@1501
    72
	return (errors ? errors + 1 : 0);
slouken@1501
    73
}