feat: basically finish parser helper.

- basically finish parser helper, including TryParse, Parse and ToString.
- only Parse has been tested. other test should be filled in future.
This commit is contained in:
2024-05-28 19:59:41 +08:00
parent d437ecc140
commit 72eb79ce1a
9 changed files with 141 additions and 17 deletions

View File

@ -6,19 +6,29 @@
#include <iostream>
#include <string>
#include "WinImportPrefix.hpp"
#include <Windows.h>
#include <io.h>
#include <fcntl.h>
#include "WinImportSuffix.hpp"
namespace YYCC::IOHelper {
FILE* FOpen(const char* u8_filepath, const char* u8_mode) {
std::wstring wmode, wpath;
bool suc = YYCC::EncodingHelper::CharToWchar(u8_mode, wmode, CP_UTF8);
suc = suc && YYCC::EncodingHelper::CharToWchar(u8_filepath, wpath, CP_UTF8);
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;
}
if (suc) {
return _wfopen(wpath.c_str(), wmode.c_str());
} else {
// fallback
return std::fopen(u8_filepath, u8_mode);
}
FILE* fopen(const char* u8_filepath, const char* u8_mode) {
// convert mode and file path to wchar
std::wstring wmode, wpath;
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());
}
}