comparison test/testiconv.c @ 1895:c121d94672cb

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