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