test/testiconv.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 25 Jul 2013 09:51:21 -0700
changeset 7517 965d57022c01
parent 7437 964e8876ad3b
child 7639 9406b7dd2f2d
permissions -rw-r--r--
Updated the copyright year for the test programs
slouken@5535
     1
/*
slouken@7517
     2
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
slouken@5535
     3
slouken@5535
     4
  This software is provided 'as-is', without any express or implied
slouken@5535
     5
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     6
  arising from the use of this software.
slouken@5535
     7
slouken@5535
     8
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
     9
  including commercial applications, and to alter it and redistribute it
slouken@5535
    10
  freely.
slouken@5535
    11
*/
slouken@1501
    12
slouken@1501
    13
#include <stdio.h>
slouken@1501
    14
slouken@1501
    15
#include "SDL.h"
slouken@1501
    16
slouken@1895
    17
static size_t
slouken@1895
    18
widelen(char *data)
slouken@1501
    19
{
slouken@1895
    20
    size_t len = 0;
slouken@1895
    21
    Uint32 *p = (Uint32 *) data;
slouken@1895
    22
    while (*p++) {
slouken@1895
    23
        ++len;
slouken@1895
    24
    }
slouken@1895
    25
    return len;
slouken@1501
    26
}
slouken@1501
    27
slouken@1895
    28
int
slouken@1895
    29
main(int argc, char *argv[])
slouken@1501
    30
{
slouken@1895
    31
    const char *formats[] = {
slouken@1895
    32
        "UTF8",
slouken@1895
    33
        "UTF-8",
slouken@1895
    34
        "UTF16BE",
slouken@1895
    35
        "UTF-16BE",
slouken@1895
    36
        "UTF16LE",
slouken@1895
    37
        "UTF-16LE",
slouken@1895
    38
        "UTF32BE",
slouken@1895
    39
        "UTF-32BE",
slouken@1895
    40
        "UTF32LE",
slouken@1895
    41
        "UTF-32LE",
slouken@1895
    42
        "UCS4",
slouken@1895
    43
        "UCS-4",
slouken@1895
    44
    };
slouken@1895
    45
    char buffer[BUFSIZ];
slouken@1895
    46
    char *ucs4;
slouken@1895
    47
    char *test[2];
philipp@7437
    48
    int i;
slouken@1895
    49
    FILE *file;
slouken@1895
    50
    int errors = 0;
slouken@1501
    51
slouken@1895
    52
    if (!argv[1]) {
slouken@1895
    53
        argv[1] = "utf8.txt";
slouken@1895
    54
    }
slouken@1895
    55
    file = fopen(argv[1], "rb");
slouken@1895
    56
    if (!file) {
slouken@1895
    57
        fprintf(stderr, "Unable to open %s\n", argv[1]);
slouken@1895
    58
        return (1);
slouken@1895
    59
    }
slouken@1501
    60
slouken@1895
    61
    while (fgets(buffer, sizeof(buffer), file)) {
slouken@1895
    62
        /* Convert to UCS-4 */
slouken@1895
    63
        size_t len;
slouken@1895
    64
        ucs4 =
slouken@1895
    65
            SDL_iconv_string("UCS-4", "UTF-8", buffer,
slouken@1895
    66
                             SDL_strlen(buffer) + 1);
slouken@1895
    67
        len = (widelen(ucs4) + 1) * 4;
slouken@1895
    68
        for (i = 0; i < SDL_arraysize(formats); ++i) {
slouken@1895
    69
            test[0] = SDL_iconv_string(formats[i], "UCS-4", ucs4, len);
slouken@1895
    70
            test[1] = SDL_iconv_string("UCS-4", formats[i], test[0], len);
slouken@2142
    71
            if (!test[1] || SDL_memcmp(test[1], ucs4, len) != 0) {
slouken@1895
    72
                fprintf(stderr, "FAIL: %s\n", formats[i]);
slouken@1895
    73
                ++errors;
slouken@1895
    74
            }
slouken@2143
    75
            if (test[0]) {
slouken@2142
    76
                SDL_free(test[0]);
slouken@2142
    77
            }
slouken@2143
    78
            if (test[1]) {
slouken@2142
    79
                SDL_free(test[1]);
slouken@2142
    80
            }
slouken@1895
    81
        }
slouken@1895
    82
        test[0] = SDL_iconv_string("UTF-8", "UCS-4", ucs4, len);
slouken@1895
    83
        SDL_free(ucs4);
slouken@1895
    84
        fputs(test[0], stdout);
slouken@1895
    85
        SDL_free(test[0]);
slouken@1895
    86
    }
philipp@7436
    87
    fclose(file);
slouken@1895
    88
    return (errors ? errors + 1 : 0);
slouken@1501
    89
}