test/testiconv.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 18 Feb 2019 00:26:53 -0500
changeset 69 a2c5f6a01a55
parent 0 a6cb692b8939
permissions -rw-r--r--
Fill in a default palette for 8-bit screen surfaces.
icculus@0
     1
icculus@0
     2
#include <stdio.h>
icculus@0
     3
icculus@0
     4
#include "SDL.h"
icculus@0
     5
icculus@0
     6
static size_t widelen(char *data)
icculus@0
     7
{
icculus@0
     8
	size_t len = 0;
icculus@0
     9
	Uint32 *p = (Uint32 *)data;
icculus@0
    10
	while(*p++) {
icculus@0
    11
		++len;
icculus@0
    12
	}
icculus@0
    13
	return len;
icculus@0
    14
}
icculus@0
    15
icculus@0
    16
int main(int argc, char *argv[])
icculus@0
    17
{
icculus@0
    18
	const char * formats[] = {
icculus@0
    19
		"UTF8",
icculus@0
    20
		"UTF-8",
icculus@0
    21
		"UTF16BE",
icculus@0
    22
		"UTF-16BE",
icculus@0
    23
		"UTF16LE",
icculus@0
    24
		"UTF-16LE",
icculus@0
    25
		"UTF32BE",
icculus@0
    26
		"UTF-32BE",
icculus@0
    27
		"UTF32LE",
icculus@0
    28
		"UTF-32LE",
icculus@0
    29
		"UCS4",
icculus@0
    30
		"UCS-4",
icculus@0
    31
	};
icculus@0
    32
	char buffer[BUFSIZ];
icculus@0
    33
	char *ucs4;
icculus@0
    34
	char *test[2];
icculus@0
    35
	int i, index = 0;
icculus@0
    36
	FILE *file;
icculus@0
    37
	int errors = 0;
icculus@0
    38
icculus@0
    39
	if ( !argv[1] ) {
icculus@0
    40
		argv[1] = "utf8.txt";
icculus@0
    41
	}
icculus@0
    42
	file = fopen(argv[1], "rb");
icculus@0
    43
	if ( !file ) {
icculus@0
    44
		fprintf(stderr, "Unable to open %s\n", argv[1]);
icculus@0
    45
		return (1);
icculus@0
    46
	}
icculus@0
    47
icculus@0
    48
	while ( fgets(buffer, sizeof(buffer), file) ) {
icculus@0
    49
		/* Convert to UCS-4 */
icculus@0
    50
		size_t len;
icculus@0
    51
		ucs4 = SDL_iconv_string("UCS-4", "UTF-8", buffer, SDL_strlen(buffer)+1);
icculus@0
    52
		len = (widelen(ucs4)+1)*4;
icculus@0
    53
		for ( i = 0; i < SDL_arraysize(formats); ++i ) {
icculus@0
    54
			test[0] = SDL_iconv_string(formats[i], "UCS-4", ucs4, len);
icculus@0
    55
			test[1] = SDL_iconv_string("UCS-4", formats[i], test[0], len);
icculus@0
    56
			if ( !test[1] || SDL_memcmp(test[1], ucs4, len) != 0 ) {
icculus@0
    57
				fprintf(stderr, "FAIL: %s\n", formats[i]);
icculus@0
    58
				++errors;
icculus@0
    59
			}
icculus@0
    60
			if ( test[0] ) {
icculus@0
    61
				SDL_free(test[0]);
icculus@0
    62
			}
icculus@0
    63
			if ( test[1] ) {
icculus@0
    64
				SDL_free(test[1]);
icculus@0
    65
			}
icculus@0
    66
		}
icculus@0
    67
		test[0] = SDL_iconv_string("UTF-8", "UCS-4", ucs4, len);
icculus@0
    68
		SDL_free(ucs4);
icculus@0
    69
		fputs(test[0], stdout);
icculus@0
    70
		SDL_free(test[0]);
icculus@0
    71
	}
icculus@0
    72
	return (errors ? errors + 1 : 0);
icculus@0
    73
}