Last active
January 20, 2017 10:14
-
-
Save minoki/0b257ef1b38cb3605311bce525b77d6e to your computer and use it in GitHub Desktop.
Test programs for wide I/O functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <wchar.h> | |
#include <locale.h> | |
#include <string.h> | |
#include <io.h> | |
#include <fcntl.h> | |
#if defined(_MSC_VER) | |
void handler(const wchar_t *expression, const wchar_t *function, const wchar_t *file, unsigned int line, uintptr_t reserved) | |
{ | |
printf("invalid parameter handler: expression=%ls, function=%ls, file=%ls, line=%u\n", expression, function, file, line); | |
} | |
#endif | |
int main(int argc, char *argv[]) { | |
#if defined(_MSC_VER) | |
_set_invalid_parameter_handler(handler); | |
#endif | |
if (argc > 1) { | |
setlocale(LC_CTYPE, argv[1]); | |
} | |
printf("LC_CTYPE = %s\n", setlocale(LC_CTYPE, NULL)); | |
printf("LC_ALL = %s\n", setlocale(LC_ALL, NULL)); | |
FILE *f = fopen("out.txt", "w"); | |
int result = _setmode(_fileno(f), _O_U8TEXT); | |
if (result == -1) { | |
printf("_setmode failed: errno=%d (%s)\n", errno, strerror(errno)); | |
} | |
result = fputws(L"Caf\u00e9 au lait\n", f); | |
printf("fputws %s. [%d]\n", result >= 0 ? "successful" : "failed", result); | |
fflush(f); | |
result = _setmode(_fileno(f), _O_U16TEXT); | |
if (result == -1) { | |
printf("_setmode failed: errno=%d (%s)\n", errno, strerror(errno)); | |
} | |
result = fputws(L"Caf\u00e9 au lait\n", f); | |
printf("fputws %s. [%d]\n", result >= 0 ? "successful" : "failed", result); | |
fflush(f); | |
result = _setmode(_fileno(f), _O_TEXT); | |
if (result == -1) { | |
printf("_setmode failed: errno=%d (%s)\n", errno, strerror(errno)); | |
} | |
result = fputs("Caf\xe9 au lait\n", f); | |
printf("fputs %s. [%d]\n", result >= 0 ? "successful" : "failed", result); | |
fflush(f); | |
result = _setmode(_fileno(f), _O_BINARY); | |
if (result == -1) { | |
printf("_setmode failed: errno=%d (%s)\n", errno, strerror(errno)); | |
} | |
result = fputs("Caf\xe9 au lait\n", f); | |
printf("fputs %s. [%d]\n", result >= 0 ? "successful" : "failed", result); | |
fclose(f); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <limits.h> | |
#include <wchar.h> | |
#include <locale.h> | |
int main(int argc, char *argv[]) { | |
if (argc > 1) { | |
setlocale(LC_CTYPE, argv[1]); | |
} | |
printf("LC_CTYPE = %s\n", setlocale(LC_CTYPE, NULL)); | |
printf("LC_ALL = %s\n", setlocale(LC_ALL, NULL)); | |
#ifdef __STDC_ISO_10646__ | |
puts("__STDC_ISO_10646__ is defined."); | |
#else | |
puts("__STDC_ISO_10646__ is not defined."); | |
#endif | |
printf("MB_LEN_MAX = %d\n", MB_LEN_MAX); | |
{ | |
wchar_t buf[100] = {0}; | |
const char str[] = "\xe7\x8c\xab"; | |
int result = mbtowc(buf, str, 3); | |
printf("mbtowc returned %d.\n", result); | |
for (size_t i = 0; i < wcslen(buf); ++i) { | |
printf("%04x ", (unsigned int)buf[i]); | |
} | |
puts(""); | |
} | |
{ | |
char buf[100] = {0}; | |
int result = wctomb(buf, L'\xe7'); | |
printf("wctomb returned %d.\n", result); | |
for (size_t i = 0; i < strlen(buf); ++i) { | |
printf("%02x ", (unsigned char)buf[i]); | |
} | |
puts(""); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <wchar.h> | |
#include <locale.h> | |
int main(int argc, char *argv[]) { | |
if (argc > 1) { | |
setlocale(LC_CTYPE, argv[1]); | |
} | |
printf("LC_CTYPE = %s\n", setlocale(LC_CTYPE, NULL)); | |
printf("LC_ALL = %s\n", setlocale(LC_ALL, NULL)); | |
FILE *f = fopen("out.txt", "w"); | |
int orientation = fwide(f, 0); | |
printf("orientation is %s. [%d]\n", orientation > 0 ? "wide" : orientation < 0 ? "byte" : "not set", orientation); | |
int result = fputws(L"Caf\u00e9 au lait\n", f); // U+00E9: LATIN SMALL LETTER E WITH ACUTE | |
printf("fputws %s. [%d]\n", result >= 0 ? "successful" : "failed", result); | |
result = fputws(L"\u03B6\n", f); // U+03B6: GREEK SMALL LETTER ZETA | |
printf("fputws %s. [%d]\n", result >= 0 ? "successful" : "failed", result); | |
result = fputws(L"\u3042\n", f); // U+3042: HIRAGANA LETTER A | |
printf("fputws %s. [%d]\n", result >= 0 ? "successful" : "failed", result); | |
orientation = fwide(f, -1); | |
printf("orientation is %s. [%d]\n", orientation > 0 ? "wide" : orientation < 0 ? "byte" : "not set", orientation); | |
result = fputs("Caf\xe9 au lait\n", f); | |
printf("fputs %s. [%d]\n", result >= 0 ? "successful" : "failed", result); | |
fclose(f); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <wchar.h> | |
#include <locale.h> | |
#if defined(_MSC_VER) | |
void handler(const wchar_t *expression, const wchar_t *function, const wchar_t *file, unsigned int line, uintptr_t reserved) | |
{ | |
printf("invalid parameter handler: expression=%ls, function=%ls, file=%ls, line=%u\n", expression, function, file, line); | |
} | |
#endif | |
int main(int argc, char *argv[]) { | |
#if defined(_MSC_VER) | |
_set_invalid_parameter_handler(handler); | |
#endif | |
if (argc > 1) { | |
setlocale(LC_CTYPE, argv[1]); | |
} | |
printf("LC_CTYPE = %s\n", setlocale(LC_CTYPE, NULL)); | |
printf("LC_ALL = %s\n", setlocale(LC_ALL, NULL)); | |
FILE *f = fopen("out.txt", "w,ccs=UTF-8"); | |
int orientation = fwide(f, 0); | |
printf("orientation is %s. [%d]\n", orientation > 0 ? "wide" : orientation < 0 ? "byte" : "not set", orientation); | |
int result = fputws(L"Caf\u00e9 au lait\n", f); | |
printf("fputws %s. [%d]\n", result >= 0 ? "successful" : "failed", result); | |
result = fputws(L"\u03B6\n", f); | |
printf("fputws %s. [%d]\n", result >= 0 ? "successful" : "failed", result); | |
result = fputws(L"\u3042\n", f); | |
printf("fputws %s. [%d]\n", result >= 0 ? "successful" : "failed", result); | |
orientation = fwide(f, 0); | |
printf("orientation is %s. [%d]\n", orientation > 0 ? "wide" : orientation < 0 ? "byte" : "not set", orientation); | |
result = fputs("Caf\xe9 au lait\n", f); | |
printf("fputs %s. [%d]\n", result >= 0 ? "successful" : "failed", result); | |
fclose(f); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <wchar.h> | |
#include <locale.h> | |
#if defined(_MSC_VER) | |
void handler(const wchar_t *expression, const wchar_t *function, const wchar_t *file, unsigned int line, uintptr_t reserved) | |
{ | |
printf("invalid parameter handler: expression=%ls, function=%ls, file=%ls, line=%u\n", expression, function, file, line); | |
} | |
#endif | |
int main(int argc, char *argv[]) { | |
#if defined(_MSC_VER) | |
_set_invalid_parameter_handler(handler); | |
#endif | |
if (argc > 1) { | |
setlocale(LC_CTYPE, argv[1]); | |
} | |
printf("LC_CTYPE = %s\n", setlocale(LC_CTYPE, NULL)); | |
printf("LC_ALL = %s\n", setlocale(LC_ALL, NULL)); | |
FILE *f = fopen("out.txt", "wb,ccs=UTF-8"); | |
int orientation = fwide(f, 0); | |
printf("orientation is %s. [%d]\n", orientation > 0 ? "wide" : orientation < 0 ? "byte" : "not set", orientation); | |
int result = fputws(L"Caf\u00e9 au lait\n", f); | |
printf("fputws %s. [%d]\n", result >= 0 ? "successful" : "failed", result); | |
result = fputws(L"\u03B6\n", f); | |
printf("fputws %s. [%d]\n", result >= 0 ? "successful" : "failed", result); | |
result = fputws(L"\u3042\n", f); | |
printf("fputws %s. [%d]\n", result >= 0 ? "successful" : "failed", result); | |
orientation = fwide(f, 0); | |
printf("orientation is %s. [%d]\n", orientation > 0 ? "wide" : orientation < 0 ? "byte" : "not set", orientation); | |
result = fputs("Caf\xe9 au lait\n", f); | |
printf("fputs %s. [%d]\n", result >= 0 ? "successful" : "failed", result); | |
fclose(f); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <wchar.h> | |
int main(int argc, char *argv[]) { | |
FILE *f = fopen("out.txt", "w,ccs=UTF-8"); | |
wchar_t str[] = L"Caf\u00e9 au lait\n"; | |
size_t result = fwrite(str, sizeof(wchar_t), wcslen(str), f); | |
printf("fwrite %s. [%u]\n", result == wcslen(str) ? "successful" : "failed", (unsigned int)result); | |
fclose(f); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <wchar.h> | |
#include <locale.h> | |
#if defined(_MSC_VER) | |
static void handler(const wchar_t *expression, const wchar_t *function, const wchar_t *file, unsigned int line, uintptr_t reserved) | |
{ | |
} | |
#endif | |
int main(int argc, char *argv[]) | |
{ | |
#if defined(_MSC_VER) | |
_set_invalid_parameter_handler(handler); | |
#endif | |
if (argc > 1) { | |
setlocale(LC_CTYPE, argv[1]); | |
} | |
printf("LC_CTYPE = %s\n", setlocale(LC_CTYPE, NULL)); | |
printf("LC_ALL = %s\n", setlocale(LC_ALL, NULL)); | |
{ | |
FILE *f = fopen("inout-u8.txt", "wb"); | |
const char data[] = u8"\u00e9\u03b6\u3042\U0001f376\r\n"; | |
fwrite(data, 1, strlen(data), f); | |
fclose(f); | |
} | |
{ | |
FILE *f = fopen("inout-u8bom.txt", "wb"); | |
const char data[] = u8"\ufeff\u00e9\u03B6\u3042\U0001f376\r\n"; | |
fwrite(data, 1, strlen(data), f); | |
fclose(f); | |
} | |
{ | |
FILE *f = fopen("inout-u16le.txt", "wb"); | |
const char data[] = {0xff, 0xfe, 0xe9, 0x00, 0xb6, 0x03, 0x42, 0x30, 0x3c, 0xd8, 0x76, 0xdf, 0x0d, 0x00, 0x0a, 0x00}; | |
fwrite(data, 1, sizeof(data), f); | |
fclose(f); | |
} | |
{ | |
FILE *f = fopen("inout-u16be.txt", "wb"); | |
const char data[] = {0xfe, 0xff, 0x00, 0xe9, 0x03, 0xb6, 0x30, 0x42, 0xd8, 0x3c, 0xdf, 0x76, 0x00, 0x0d, 0x00, 0x0a}; | |
fwrite(data, 1, sizeof(data), f); | |
fclose(f); | |
} | |
const char *files[][2] = { | |
{"inout-u8.txt", "UTF-8"}, | |
{"inout-u8bom.txt", "UTF-8 (with BOM)"}, | |
{"inout-u16le.txt", "UTF-16LE (with BOM)"}, | |
{"inout-u16be.txt", "UTF-16BE (with BOM)"}, | |
}; | |
const char *ccs_string[] = {"UTF-8", "UNICODE", "UTF-16", "UTF-16LE"}; | |
for (size_t i = 0; i < sizeof(files) / sizeof(files[0]); ++i) { | |
for (size_t j = 0; j < sizeof(ccs_string) / sizeof(ccs_string[0]); ++j) { | |
const char *filename = files[i][0]; | |
const char *fileencoding = files[i][1]; | |
const char *readencoding = ccs_string[j]; | |
char modestring[32]; | |
sprintf(modestring, "r,ccs=%s", readencoding); | |
FILE *f = fopen(filename, modestring); | |
if (f == NULL) { | |
fprintf(stdout, "fopen(%s, %s) failed.\n", filename, modestring); | |
} else { | |
wchar_t buf[10]; | |
if (fgetws(buf, sizeof(buf) / sizeof(buf[0]), f) == NULL) { | |
fprintf(stdout, "Reading %s-encoded file with ccs=%s failed.\n", fileencoding, readencoding); | |
} else { | |
fprintf(stdout, "Read from %s-encoded file (with ccs=%s): ", fileencoding, readencoding); | |
for (size_t i = 0; i < wcslen(buf); ++i) { | |
fprintf(stdout, "%04x ", (unsigned int)buf[i]); | |
} | |
fputs("\n", stdout); | |
} | |
fclose(f); | |
} | |
} | |
} | |
{ | |
FILE *f = fopen("inout-u8.txt", "r,ccs=UTF-8"); | |
if (f != NULL) { | |
wchar_t buf[6]; | |
size_t result = fread(buf, sizeof(buf[0]), sizeof(buf) / sizeof(buf[0]), f); | |
if (result < sizeof(buf) / sizeof(buf[0])) { | |
fprintf(stdout, "fread on file handle with ccs=UTF-8 failed.\n"); | |
} | |
if (result > 0) { | |
fprintf(stdout, "Read from UTF-8-encoded file with fread: "); | |
for (size_t i = 0; i < result; ++i) { | |
fprintf(stdout, "%04x ", (unsigned int)buf[i]); | |
} | |
fputs("\n", stdout); | |
} | |
fclose(f); | |
} | |
} | |
{ | |
FILE *f = fopen("inout-u8.txt", "r,ccs=UTF-8"); | |
if (f != NULL) { | |
unsigned char buf[12]; | |
size_t result = fread(buf, sizeof(buf[0]), sizeof(buf) / sizeof(buf[0]), f); | |
if (result < sizeof(buf) / sizeof(buf[0])) { | |
fprintf(stdout, "fread on file handle with ccs=UTF-8 failed.\n"); | |
} | |
if (result > 0) { | |
fprintf(stdout, "Read from UTF-8-encoded file with fread: "); | |
for (size_t i = 0; i < result; ++i) { | |
fprintf(stdout, "%02x ", (unsigned int)buf[i]); | |
} | |
fputs("\n", stdout); | |
} | |
fclose(f); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment