test/testiconv.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 28 May 2006 13:04:16 +0000
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1528 ff3322d66771
child 1668 4da1ee79c9af
permissions -rw-r--r--
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.

WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.

The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce

The headers are being converted to automatically generate doxygen documentation.
slouken@1501
     1
slouken@1501
     2
#include <stdio.h>
slouken@1501
     3
slouken@1501
     4
#include "SDL.h"
slouken@1501
     5
slouken@1662
     6
static size_t
slouken@1662
     7
widelen (char *data)
slouken@1501
     8
{
slouken@1662
     9
    size_t len = 0;
slouken@1662
    10
    Uint32 *p = (Uint32 *) data;
slouken@1662
    11
    while (*p++) {
slouken@1662
    12
        ++len;
slouken@1662
    13
    }
slouken@1662
    14
    return len;
slouken@1501
    15
}
slouken@1501
    16
slouken@1662
    17
int
slouken@1662
    18
main (int argc, char *argv[])
slouken@1501
    19
{
slouken@1662
    20
    const char *formats[] = {
slouken@1662
    21
        "UTF8",
slouken@1662
    22
        "UTF-8",
slouken@1662
    23
        "UTF16BE",
slouken@1662
    24
        "UTF-16BE",
slouken@1662
    25
        "UTF16LE",
slouken@1662
    26
        "UTF-16LE",
slouken@1662
    27
        "UTF32BE",
slouken@1662
    28
        "UTF-32BE",
slouken@1662
    29
        "UTF32LE",
slouken@1662
    30
        "UTF-32LE",
slouken@1662
    31
        "UCS4",
slouken@1662
    32
        "UCS-4",
slouken@1662
    33
    };
slouken@1662
    34
    char buffer[BUFSIZ];
slouken@1662
    35
    char *ucs4;
slouken@1662
    36
    char *test[2];
slouken@1662
    37
    int i, index = 0;
slouken@1662
    38
    FILE *file;
slouken@1662
    39
    int errors = 0;
slouken@1501
    40
slouken@1662
    41
    if (!argv[1]) {
slouken@1662
    42
        argv[1] = "utf8.txt";
slouken@1662
    43
    }
slouken@1662
    44
    file = fopen (argv[1], "rb");
slouken@1662
    45
    if (!file) {
slouken@1662
    46
        fprintf (stderr, "Unable to open %s\n", argv[1]);
slouken@1662
    47
        return (1);
slouken@1662
    48
    }
slouken@1501
    49
slouken@1662
    50
    while (fgets (buffer, sizeof (buffer), file)) {
slouken@1662
    51
        /* Convert to UCS-4 */
slouken@1662
    52
        size_t len;
slouken@1662
    53
        ucs4 =
slouken@1662
    54
            SDL_iconv_string ("UCS-4", "UTF-8", buffer,
slouken@1662
    55
                              SDL_strlen (buffer) + 1);
slouken@1662
    56
        len = (widelen (ucs4) + 1) * 4;
slouken@1662
    57
        for (i = 0; i < SDL_arraysize (formats); ++i) {
slouken@1662
    58
            test[0] = SDL_iconv_string (formats[i], "UCS-4", ucs4, len);
slouken@1662
    59
            test[1] = SDL_iconv_string ("UCS-4", formats[i], test[0], len);
slouken@1662
    60
            if (SDL_memcmp (test[1], ucs4, len) != 0) {
slouken@1662
    61
                fprintf (stderr, "FAIL: %s\n", formats[i]);
slouken@1662
    62
                ++errors;
slouken@1662
    63
            }
slouken@1662
    64
            SDL_free (test[0]);
slouken@1662
    65
            SDL_free (test[1]);
slouken@1662
    66
        }
slouken@1662
    67
        test[0] = SDL_iconv_string ("UTF-8", "UCS-4", ucs4, len);
slouken@1662
    68
        SDL_free (ucs4);
slouken@1662
    69
        fputs (test[0], stdout);
slouken@1662
    70
        SDL_free (test[0]);
slouken@1662
    71
    }
slouken@1662
    72
    return (errors ? errors + 1 : 0);
slouken@1501
    73
}