test/testiconv.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 04 Jul 2007 07:08:16 +0000
changeset 2142 ca80c942e69c
parent 1895 c121d94672cb
child 2143 e906da4414a3
permissions -rw-r--r--
Fixed bug #349

Solaris doesn't support the LATIN1 character set alias.

Merged from 1.2 svn revision 3133f
     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 (!test[1] || SDL_memcmp(test[1], ucs4, len) != 0) {
    61                 fprintf(stderr, "FAIL: %s\n", formats[i]);
    62                 ++errors;
    63             }
    64             if(test[0]) {
    65                 SDL_free(test[0]);
    66             }
    67             if(test[1]) {
    68                 SDL_free(test[1]);
    69             }
    70         }
    71         test[0] = SDL_iconv_string("UTF-8", "UCS-4", ucs4, len);
    72         SDL_free(ucs4);
    73         fputs(test[0], stdout);
    74         SDL_free(test[0]);
    75     }
    76     return (errors ? errors + 1 : 0);
    77 }