2024-04-25 10:38:13 +08:00
|
|
|
#include "IOHelper.hpp"
|
2024-04-26 15:37:28 +08:00
|
|
|
#if YYCC_OS == YYCC_OS_WINDOWS
|
|
|
|
|
2024-04-25 10:38:13 +08:00
|
|
|
#include "EncodingHelper.hpp"
|
|
|
|
#include <cstdio>
|
|
|
|
#include <iostream>
|
2024-05-22 13:42:43 +08:00
|
|
|
#include <string>
|
2024-04-25 10:38:13 +08:00
|
|
|
|
2024-05-28 19:59:41 +08:00
|
|
|
#include "WinImportPrefix.hpp"
|
|
|
|
#include <Windows.h>
|
|
|
|
#include <io.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include "WinImportSuffix.hpp"
|
|
|
|
|
2024-04-25 10:38:13 +08:00
|
|
|
namespace YYCC::IOHelper {
|
|
|
|
|
2024-05-28 19:59:41 +08:00
|
|
|
bool futf8(FILE* fs) {
|
|
|
|
// https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/setmode?view=msvc-170
|
|
|
|
return _setmode(_fileno(fs), _O_U8TEXT) != -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE* fopen(const char* u8_filepath, const char* u8_mode) {
|
|
|
|
// convert mode and file path to wchar
|
2024-04-25 10:38:13 +08:00
|
|
|
std::wstring wmode, wpath;
|
2024-05-28 19:59:41 +08:00
|
|
|
if (!YYCC::EncodingHelper::UTF8ToWchar(u8_mode, wmode))
|
|
|
|
return nullptr;
|
|
|
|
if (!YYCC::EncodingHelper::UTF8ToWchar(u8_filepath, wpath))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// call microsoft specified fopen which support wchar as argument.
|
|
|
|
return _wfopen(wpath.c_str(), wmode.c_str());
|
2024-04-25 10:38:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2024-04-26 15:37:28 +08:00
|
|
|
|
|
|
|
#endif
|