feat: update IO helper.

- add utf8 path to std::filesystem::path convertion function.
- add left-padding zero format string for printing pointer.
- update exception handler, but not finished because console helper need update.
This commit is contained in:
2024-06-13 15:24:12 +08:00
parent 015ff874f8
commit 0ef1939e6e
4 changed files with 78 additions and 30 deletions

View File

@ -1,23 +1,48 @@
#pragma once
#include "YYCCInternal.hpp"
#if YYCC_OS == YYCC_OS_WINDOWS
#include <cstdio>
#include <filesystem>
namespace YYCC::IOHelper {
#if UINTPTR_MAX == UINT32_MAX
#define PRI_XPTR_LEFT_PADDING "08"
#elif UINTPTR_MAX == UINT64_MAX
/**
* @brief Set given FILE* as UTF8 mode.
* @param fs[in] The FILE* need to be set as UTF8 mode.
* @return True if success, otherwise false.
* @warning Once this function success, you can not use any narrow char function on this FILE*,
* such as std::fputs, std::fprintf and etc. You only can use wide char function on it,
* or use the functions provided in this namespace by providing UTF8 string as their argument.
* @brief The left-padding zero format string of HEX-printed pointer type.
* @details
* When printing a pointer with HEX style, we always hope it can be left-padded with some zero for easy reading.
* In different architecture, the size of this padding is differnet too so we create this macro.
* \n
* In 32-bit environment, it will be "08" meaning left pad zero until 8 number position.
* In 64-bit environment, it will be "016" meaning left pad zero until 16 number position.
*/
bool futf8(FILE* fs);
#define PRI_XPTR_LEFT_PADDING "016"
#else
#error "Not supported pointer size."
#endif
FILE* fopen(const char* u8_filepath, const char* u8_mode);
/**
* @brief The UTF8 version of std::fopen.
* @param u8_filepath[in] The UTF8 encoded path to the file to be opened.
* @param u8_mode[in] UTF8 encoded mode string of the file to be opened.
* @remarks
* This function is suit for Windows because std::fopen do not support UTF8 on Windows.
* On other platforms, this function will delegate request directly to std::fopen.
* @return FILE* of the file to be opened, or nullptr if failed.
*/
FILE* UTF8FOpen(const char* u8_filepath, const char* u8_mode);
/**
* @brief Build std::filesystem::path from UTF8 string.
* @param u8_path[in] UTF8 path string for building this std::filesystem::path.
* @return std::filesystem::path instance.
* @exception std::invalid_argument Fail to parse given UTF8 string (maybe invalid?).
* @remarks
* This function is suit for Windows.
* On other platforms, it will simply call the constructor of std::filesystem::path.
*/
std::filesystem::path UTF8Path(const char* u8_path);
}
#endif