test/testiconv.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Fri, 04 Apr 2014 23:56:15 +0200
changeset 8692 715d401184a2
parent 8149 681eb46b8ac4
child 9619 b94b6d0bff0f
permissions -rw-r--r--
Removed unused variable from test program.
slouken@5535
     1
/*
slouken@8149
     2
  Copyright (C) 1997-2014 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
aschiffler@7639
    52
    /* Enable standard application logging */
aschiffler@7639
    53
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
aschiffler@7639
    54
slouken@1895
    55
    if (!argv[1]) {
slouken@1895
    56
        argv[1] = "utf8.txt";
slouken@1895
    57
    }
slouken@1895
    58
    file = fopen(argv[1], "rb");
slouken@1895
    59
    if (!file) {
aschiffler@7639
    60
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to open %s\n", argv[1]);
slouken@1895
    61
        return (1);
slouken@1895
    62
    }
slouken@1501
    63
slouken@1895
    64
    while (fgets(buffer, sizeof(buffer), file)) {
slouken@1895
    65
        /* Convert to UCS-4 */
slouken@1895
    66
        size_t len;
slouken@1895
    67
        ucs4 =
slouken@1895
    68
            SDL_iconv_string("UCS-4", "UTF-8", buffer,
slouken@1895
    69
                             SDL_strlen(buffer) + 1);
slouken@1895
    70
        len = (widelen(ucs4) + 1) * 4;
slouken@1895
    71
        for (i = 0; i < SDL_arraysize(formats); ++i) {
slouken@1895
    72
            test[0] = SDL_iconv_string(formats[i], "UCS-4", ucs4, len);
slouken@1895
    73
            test[1] = SDL_iconv_string("UCS-4", formats[i], test[0], len);
slouken@2142
    74
            if (!test[1] || SDL_memcmp(test[1], ucs4, len) != 0) {
aschiffler@7639
    75
                SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "FAIL: %s\n", formats[i]);
slouken@1895
    76
                ++errors;
slouken@1895
    77
            }
slouken@7719
    78
            SDL_free(test[0]);
slouken@7719
    79
            SDL_free(test[1]);
slouken@1895
    80
        }
slouken@1895
    81
        test[0] = SDL_iconv_string("UTF-8", "UCS-4", ucs4, len);
slouken@1895
    82
        SDL_free(ucs4);
slouken@1895
    83
        fputs(test[0], stdout);
slouken@1895
    84
        SDL_free(test[0]);
slouken@1895
    85
    }
philipp@7436
    86
    fclose(file);
slouken@1895
    87
    return (errors ? errors + 1 : 0);
slouken@1501
    88
}