comparison test/testiconv.c @ 1501:73dc5d39bbf8

Added UTF-8 <-> UTF-16 <-> UTF-32 <-> UCS-2 <-> UCS-4 conversion capability
author Sam Lantinga <slouken@libsdl.org>
date Mon, 13 Mar 2006 01:08:00 +0000
parents
children d403a39389da
comparison
equal deleted inserted replaced
1500:f58c88a4dff5 1501:73dc5d39bbf8
1
2 #include <stdio.h>
3
4 #include "SDL.h"
5
6 static SDL_bool testutf16(char *data)
7 {
8 Uint32 *p = (Uint32 *)data;
9 while(*p) {
10 if ( *p > 0x10FFFF ) {
11 return SDL_FALSE;
12 }
13 ++p;
14 }
15 return SDL_TRUE;
16 }
17
18 static size_t widelen(char *data)
19 {
20 size_t len = 0;
21 Uint32 *p = (Uint32 *)data;
22 while(*p++) {
23 ++len;
24 }
25 return len;
26 }
27
28 int main(int argc, char *argv[])
29 {
30 const char * formats[] = {
31 "UTF8",
32 "UTF-8",
33 "UTF16BE",
34 "UTF-16BE",
35 "UTF16LE",
36 "UTF-16LE",
37 "UTF32BE",
38 "UTF-32BE",
39 "UTF32LE",
40 "UTF-32LE",
41 "UCS4",
42 "UCS-4",
43 };
44 char buffer[BUFSIZ];
45 char *ucs4;
46 char *test[2];
47 int i, j, index = 0;
48 FILE *file;
49 int errors = 0;
50
51 if ( !argv[1] ) {
52 argv[1] = "utf8.txt";
53 }
54 file = fopen(argv[1], "rb");
55 if ( !file ) {
56 fprintf(stderr, "Unable to open %s\n", argv[1]);
57 return (1);
58 }
59
60 while ( fgets(buffer, sizeof(buffer), file) ) {
61 /* Convert to UCS-4 */
62 ucs4 = SDL_iconv_string("UCS-4", "UTF-8", buffer, SDL_strlen(buffer)+1);
63 size_t len = (widelen(ucs4)+1)*4;
64 for ( i = 0; i < SDL_arraysize(formats); ++i ) {
65 if ( (SDL_strncasecmp(formats[i], "UTF16", 5) == 0 ||
66 SDL_strncasecmp(formats[i], "UTF-16", 6) == 0) &&
67 !testutf16(ucs4) ) {
68 continue;
69 }
70 test[0] = SDL_iconv_string(formats[i], "UCS-4", ucs4, len);
71 test[1] = SDL_iconv_string("UCS-4", formats[i], test[0], len);
72 if ( SDL_memcmp(test[1], ucs4, len) != 0 ) {
73 fprintf(stderr, "FAIL: %s\n", formats[i]);
74 ++errors;
75 }
76 SDL_free(test[0]);
77 SDL_free(test[1]);
78 }
79 test[0] = SDL_iconv_string("UTF-8", "UCS-4", ucs4, len);
80 SDL_free(ucs4);
81 fputs(test[0], stdout);
82 SDL_free(test[0]);
83 }
84 return (errors ? errors + 1 : 0);
85 }