test/testiconv.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 04 Jul 2007 08:01:04 +0000
changeset 2143 e906da4414a3
parent 2142 ca80c942e69c
child 5535 96594ac5fd1a
permissions -rw-r--r--
Fix for bug #447 merged from SDL 1.2
slouken@1501
     1
slouken@1501
     2
#include <stdio.h>
slouken@1501
     3
slouken@1501
     4
#include "SDL.h"
slouken@1501
     5
slouken@1895
     6
static size_t
slouken@1895
     7
widelen(char *data)
slouken@1501
     8
{
slouken@1895
     9
    size_t len = 0;
slouken@1895
    10
    Uint32 *p = (Uint32 *) data;
slouken@1895
    11
    while (*p++) {
slouken@1895
    12
        ++len;
slouken@1895
    13
    }
slouken@1895
    14
    return len;
slouken@1501
    15
}
slouken@1501
    16
slouken@1895
    17
int
slouken@1895
    18
main(int argc, char *argv[])
slouken@1501
    19
{
slouken@1895
    20
    const char *formats[] = {
slouken@1895
    21
        "UTF8",
slouken@1895
    22
        "UTF-8",
slouken@1895
    23
        "UTF16BE",
slouken@1895
    24
        "UTF-16BE",
slouken@1895
    25
        "UTF16LE",
slouken@1895
    26
        "UTF-16LE",
slouken@1895
    27
        "UTF32BE",
slouken@1895
    28
        "UTF-32BE",
slouken@1895
    29
        "UTF32LE",
slouken@1895
    30
        "UTF-32LE",
slouken@1895
    31
        "UCS4",
slouken@1895
    32
        "UCS-4",
slouken@1895
    33
    };
slouken@1895
    34
    char buffer[BUFSIZ];
slouken@1895
    35
    char *ucs4;
slouken@1895
    36
    char *test[2];
slouken@1895
    37
    int i, index = 0;
slouken@1895
    38
    FILE *file;
slouken@1895
    39
    int errors = 0;
slouken@1501
    40
slouken@1895
    41
    if (!argv[1]) {
slouken@1895
    42
        argv[1] = "utf8.txt";
slouken@1895
    43
    }
slouken@1895
    44
    file = fopen(argv[1], "rb");
slouken@1895
    45
    if (!file) {
slouken@1895
    46
        fprintf(stderr, "Unable to open %s\n", argv[1]);
slouken@1895
    47
        return (1);
slouken@1895
    48
    }
slouken@1501
    49
slouken@1895
    50
    while (fgets(buffer, sizeof(buffer), file)) {
slouken@1895
    51
        /* Convert to UCS-4 */
slouken@1895
    52
        size_t len;
slouken@1895
    53
        ucs4 =
slouken@1895
    54
            SDL_iconv_string("UCS-4", "UTF-8", buffer,
slouken@1895
    55
                             SDL_strlen(buffer) + 1);
slouken@1895
    56
        len = (widelen(ucs4) + 1) * 4;
slouken@1895
    57
        for (i = 0; i < SDL_arraysize(formats); ++i) {
slouken@1895
    58
            test[0] = SDL_iconv_string(formats[i], "UCS-4", ucs4, len);
slouken@1895
    59
            test[1] = SDL_iconv_string("UCS-4", formats[i], test[0], len);
slouken@2142
    60
            if (!test[1] || SDL_memcmp(test[1], ucs4, len) != 0) {
slouken@1895
    61
                fprintf(stderr, "FAIL: %s\n", formats[i]);
slouken@1895
    62
                ++errors;
slouken@1895
    63
            }
slouken@2143
    64
            if (test[0]) {
slouken@2142
    65
                SDL_free(test[0]);
slouken@2142
    66
            }
slouken@2143
    67
            if (test[1]) {
slouken@2142
    68
                SDL_free(test[1]);
slouken@2142
    69
            }
slouken@1895
    70
        }
slouken@1895
    71
        test[0] = SDL_iconv_string("UTF-8", "UCS-4", ucs4, len);
slouken@1895
    72
        SDL_free(ucs4);
slouken@1895
    73
        fputs(test[0], stdout);
slouken@1895
    74
        SDL_free(test[0]);
slouken@1895
    75
    }
slouken@1895
    76
    return (errors ? errors + 1 : 0);
slouken@1501
    77
}