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