test/testiconv.c
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
equal deleted inserted replaced
1667:1fddae038bc8 1668:4da1ee79c9af
     2 #include <stdio.h>
     2 #include <stdio.h>
     3 
     3 
     4 #include "SDL.h"
     4 #include "SDL.h"
     5 
     5 
     6 static size_t
     6 static size_t
     7 widelen (char *data)
     7 widelen(char *data)
     8 {
     8 {
     9     size_t len = 0;
     9     size_t len = 0;
    10     Uint32 *p = (Uint32 *) data;
    10     Uint32 *p = (Uint32 *) data;
    11     while (*p++) {
    11     while (*p++) {
    12         ++len;
    12         ++len;
    13     }
    13     }
    14     return len;
    14     return len;
    15 }
    15 }
    16 
    16 
    17 int
    17 int
    18 main (int argc, char *argv[])
    18 main(int argc, char *argv[])
    19 {
    19 {
    20     const char *formats[] = {
    20     const char *formats[] = {
    21         "UTF8",
    21         "UTF8",
    22         "UTF-8",
    22         "UTF-8",
    23         "UTF16BE",
    23         "UTF16BE",
    39     int errors = 0;
    39     int errors = 0;
    40 
    40 
    41     if (!argv[1]) {
    41     if (!argv[1]) {
    42         argv[1] = "utf8.txt";
    42         argv[1] = "utf8.txt";
    43     }
    43     }
    44     file = fopen (argv[1], "rb");
    44     file = fopen(argv[1], "rb");
    45     if (!file) {
    45     if (!file) {
    46         fprintf (stderr, "Unable to open %s\n", argv[1]);
    46         fprintf(stderr, "Unable to open %s\n", argv[1]);
    47         return (1);
    47         return (1);
    48     }
    48     }
    49 
    49 
    50     while (fgets (buffer, sizeof (buffer), file)) {
    50     while (fgets(buffer, sizeof(buffer), file)) {
    51         /* Convert to UCS-4 */
    51         /* Convert to UCS-4 */
    52         size_t len;
    52         size_t len;
    53         ucs4 =
    53         ucs4 =
    54             SDL_iconv_string ("UCS-4", "UTF-8", buffer,
    54             SDL_iconv_string("UCS-4", "UTF-8", buffer,
    55                               SDL_strlen (buffer) + 1);
    55                              SDL_strlen(buffer) + 1);
    56         len = (widelen (ucs4) + 1) * 4;
    56         len = (widelen(ucs4) + 1) * 4;
    57         for (i = 0; i < SDL_arraysize (formats); ++i) {
    57         for (i = 0; i < SDL_arraysize(formats); ++i) {
    58             test[0] = SDL_iconv_string (formats[i], "UCS-4", ucs4, len);
    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);
    59             test[1] = SDL_iconv_string("UCS-4", formats[i], test[0], len);
    60             if (SDL_memcmp (test[1], ucs4, len) != 0) {
    60             if (SDL_memcmp(test[1], ucs4, len) != 0) {
    61                 fprintf (stderr, "FAIL: %s\n", formats[i]);
    61                 fprintf(stderr, "FAIL: %s\n", formats[i]);
    62                 ++errors;
    62                 ++errors;
    63             }
    63             }
    64             SDL_free (test[0]);
    64             SDL_free(test[0]);
    65             SDL_free (test[1]);
    65             SDL_free(test[1]);
    66         }
    66         }
    67         test[0] = SDL_iconv_string ("UTF-8", "UCS-4", ucs4, len);
    67         test[0] = SDL_iconv_string("UTF-8", "UCS-4", ucs4, len);
    68         SDL_free (ucs4);
    68         SDL_free(ucs4);
    69         fputs (test[0], stdout);
    69         fputs(test[0], stdout);
    70         SDL_free (test[0]);
    70         SDL_free(test[0]);
    71     }
    71     }
    72     return (errors ? errors + 1 : 0);
    72     return (errors ? errors + 1 : 0);
    73 }
    73 }