| 1 |
/* |
| 2 |
Copyright (C) 1997-2026 Sam Lantinga <[email protected]> |
| 3 |
|
| 4 |
This software is provided 'as-is', without any express or implied |
| 5 |
warranty. In no event will the authors be held liable for any damages |
| 6 |
arising from the use of this software. |
| 7 |
|
| 8 |
Permission is granted to anyone to use this software for any purpose, |
| 9 |
including commercial applications, and to alter it and redistribute it |
| 10 |
freely. |
| 11 |
*/ |
| 12 |
|
| 13 |
/* quiet windows compiler warnings */ |
| 14 |
#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS) |
| 15 |
#define _CRT_SECURE_NO_WARNINGS |
| 16 |
#endif |
| 17 |
|
| 18 |
#include <stdio.h> |
| 19 |
|
| 20 |
#include "SDL.h" |
| 21 |
#include "testutils.h" |
| 22 |
|
| 23 |
static size_t |
| 24 |
widelen(char *data) |
| 25 |
{ |
| 26 |
size_t len = 0; |
| 27 |
Uint32 *p = (Uint32 *)data; |
| 28 |
while (*p++) { |
| 29 |
++len; |
| 30 |
} |
| 31 |
return len; |
| 32 |
} |
| 33 |
|
| 34 |
int main(int argc, char *argv[]) |
| 35 |
{ |
| 36 |
const char *formats[] = { |
| 37 |
"UTF8", |
| 38 |
"UTF-8", |
| 39 |
"UTF16BE", |
| 40 |
"UTF-16BE", |
| 41 |
"UTF16LE", |
| 42 |
"UTF-16LE", |
| 43 |
"UTF32BE", |
| 44 |
"UTF-32BE", |
| 45 |
"UTF32LE", |
| 46 |
"UTF-32LE", |
| 47 |
"UCS4", |
| 48 |
"UCS-4", |
| 49 |
}; |
| 50 |
|
| 51 |
char *fname; |
| 52 |
char buffer[BUFSIZ]; |
| 53 |
char *ucs4; |
| 54 |
char *test[2]; |
| 55 |
int i; |
| 56 |
FILE *file; |
| 57 |
int errors = 0; |
| 58 |
|
| 59 |
/* Enable standard application logging */ |
| 60 |
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); |
| 61 |
|
| 62 |
fname = GetResourceFilename(argc > 1 ? argv[1] : NULL, "utf8.txt"); |
| 63 |
file = fopen(fname, "rb"); |
| 64 |
if (!file) { |
| 65 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to open %s\n", fname); |
| 66 |
return 1; |
| 67 |
} |
| 68 |
SDL_free(fname); |
| 69 |
|
| 70 |
while (fgets(buffer, sizeof(buffer), file)) { |
| 71 |
/* Convert to UCS-4 */ |
| 72 |
size_t len; |
| 73 |
ucs4 = |
| 74 |
SDL_iconv_string("UCS-4", "UTF-8", buffer, |
| 75 |
SDL_strlen(buffer) + 1); |
| 76 |
len = (widelen(ucs4) + 1) * 4; |
| 77 |
for (i = 0; i < SDL_arraysize(formats); ++i) { |
| 78 |
test[0] = SDL_iconv_string(formats[i], "UCS-4", ucs4, len); |
| 79 |
test[1] = SDL_iconv_string("UCS-4", formats[i], test[0], len); |
| 80 |
if (!test[1] || SDL_memcmp(test[1], ucs4, len) != 0) { |
| 81 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "FAIL: %s\n", formats[i]); |
| 82 |
++errors; |
| 83 |
} |
| 84 |
SDL_free(test[0]); |
| 85 |
SDL_free(test[1]); |
| 86 |
} |
| 87 |
test[0] = SDL_iconv_string("UTF-8", "UCS-4", ucs4, len); |
| 88 |
SDL_free(ucs4); |
| 89 |
(void)fputs(test[0], stdout); |
| 90 |
SDL_free(test[0]); |
| 91 |
} |
| 92 |
(void)fclose(file); |
| 93 |
|
| 94 |
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Total errors: %d\n", errors); |
| 95 |
return errors ? errors + 1 : 0; |
| 96 |
} |