From cf732aba37422898c43e5b1ab50c0d1969390320 Mon Sep 17 00:00:00 2001 From: Povilas Kanapickas Date: Mon, 1 Nov 2021 02:52:48 +0200 Subject: [PATCH] lib/io: Add a replacement for fopen() which works on Windows fopen() does not correctly handle non-ASCII paths on Windows. --- src/lib/io/fstream.cpp | 12 ++++++++++++ src/lib/io/fstream.h | 3 +++ 2 files changed, 15 insertions(+) diff --git a/src/lib/io/fstream.cpp b/src/lib/io/fstream.cpp index 4aef9073..ea91859d 100644 --- a/src/lib/io/fstream.cpp +++ b/src/lib/io/fstream.cpp @@ -54,4 +54,16 @@ void open_utf8_path(std::fstream& stream, const std::string& path, std::ios_base open_utf8_path_impl(stream, path, mode); } +std::FILE* fopen_utf8_path(const std::string& path, const std::string& mode) +{ +#if SYSAPI_WIN32 + auto wchar_path = utf8_to_win_char(path); + auto wchar_mode = utf8_to_win_char(mode); + return _wfopen(reinterpret_cast(wchar_path.data()), + reinterpret_cast(wchar_mode.data())); +#else + return std::fopen(path.c_str(), mode.c_str()); +#endif +} + } // namespace barrier diff --git a/src/lib/io/fstream.h b/src/lib/io/fstream.h index 26288373..2b327f18 100644 --- a/src/lib/io/fstream.h +++ b/src/lib/io/fstream.h @@ -18,6 +18,7 @@ #ifndef BARRIER_LIB_IO_FSTREAM_H #define BARRIER_LIB_IO_FSTREAM_H +#include #include #include @@ -30,6 +31,8 @@ void open_utf8_path(std::ofstream& stream, const std::string& path, void open_utf8_path(std::fstream& stream, const std::string& path, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out); +std::FILE* fopen_utf8_path(const std::string& path, const std::string& mode); + } // namespace barrier #endif