This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
testiconv.c
73 lines (67 loc) · 1.64 KB
1
2
3
4
5
#include <stdio.h>
#include "SDL.h"
7
widelen(char *data)
9
10
11
12
13
14
size_t len = 0;
Uint32 *p = (Uint32 *) data;
while (*p++) {
++len;
}
return len;
18
main(int argc, char *argv[])
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const char *formats[] = {
"UTF8",
"UTF-8",
"UTF16BE",
"UTF-16BE",
"UTF16LE",
"UTF-16LE",
"UTF32BE",
"UTF-32BE",
"UTF32LE",
"UTF-32LE",
"UCS4",
"UCS-4",
};
char buffer[BUFSIZ];
char *ucs4;
char *test[2];
int i, index = 0;
FILE *file;
int errors = 0;
41
42
43
if (!argv[1]) {
argv[1] = "utf8.txt";
}
44
file = fopen(argv[1], "rb");
46
fprintf(stderr, "Unable to open %s\n", argv[1]);
47
48
return (1);
}
50
while (fgets(buffer, sizeof(buffer), file)) {
51
52
53
/* Convert to UCS-4 */
size_t len;
ucs4 =
54
55
56
57
58
59
60
61
SDL_iconv_string("UCS-4", "UTF-8", buffer,
SDL_strlen(buffer) + 1);
len = (widelen(ucs4) + 1) * 4;
for (i = 0; i < SDL_arraysize(formats); ++i) {
test[0] = SDL_iconv_string(formats[i], "UCS-4", ucs4, len);
test[1] = SDL_iconv_string("UCS-4", formats[i], test[0], len);
if (SDL_memcmp(test[1], ucs4, len) != 0) {
fprintf(stderr, "FAIL: %s\n", formats[i]);
64
65
SDL_free(test[0]);
SDL_free(test[1]);
67
68
69
70
test[0] = SDL_iconv_string("UTF-8", "UCS-4", ucs4, len);
SDL_free(ucs4);
fputs(test[0], stdout);
SDL_free(test[0]);
71
72
}
return (errors ? errors + 1 : 0);