Add fopen to utils

This commit is contained in:
2025-03-19 09:01:14 +08:00
parent 5fde5e6e52
commit 55d00feb3c
2 changed files with 22 additions and 0 deletions

View File

@@ -45,6 +45,10 @@ bool exists_internal(wchar_t* fn) {
return !_waccess(fn, 0); return !_waccess(fn, 0);
} }
FILE* fopen_internal(wchar_t* fn, const wchar_t* mode) {
return _wfopen(fn, mode);
}
bool remove_internal(wchar_t* fn, bool print_error) { bool remove_internal(wchar_t* fn, bool print_error) {
int ret = _wremove(fn); int ret = _wremove(fn);
if (ret && print_error && errno != ENOENT) { if (ret && print_error && errno != ENOENT) {
@@ -667,3 +671,20 @@ std::string fileop::relpath(std::string path, std::string start) {
return result; return result;
} }
FILE* fileop::fopen(std::string path, std::string mode) {
#if _WIN32
std::wstring wMode;
if (wchar_util::str_to_wstr(wMode, mode, CP_UTF8)) {
UINT cp[] = { CP_UTF8, CP_OEMCP, CP_ACP };
int i;
for (i = 0; i < 3; i++) {
auto f = fileop_internal<FILE*>(path.c_str(), cp[i], &fopen_internal, nullptr, wMode.c_str());
if (f) return f;
}
}
return ::fopen(path.c_str(), mode.c_str());
#else
return ::fopen(path.c_str(), mode.c_str());
#endif
}

View File

@@ -182,5 +182,6 @@ namespace fileop {
* @return Result * @return Result
*/ */
std::string relpath(std::string path, std::string start = ""); std::string relpath(std::string path, std::string start = "");
FILE* fopen(std::string path, std::string mode);
} }
#endif #endif