From 68602eca98d142d13fb888d02eb73141bf6dd236 Mon Sep 17 00:00:00 2001 From: Jenny Bryan Date: Sun, 8 May 2022 15:35:48 -0700 Subject: [PATCH] Re-encode to native just prior to fopen() --- src/unicode_fopen.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/unicode_fopen.h b/src/unicode_fopen.h index b3a735d0..e5f79320 100644 --- a/src/unicode_fopen.h +++ b/src/unicode_fopen.h @@ -12,9 +12,12 @@ #endif // clang-format on -#ifdef _WIN32 #include + +#ifdef _WIN32 #include +#else +#include "cpp11/r_string.hpp" #endif // This is needed to support wide character paths on windows @@ -40,7 +43,10 @@ inline FILE* unicode_fopen(const char* path, const char* mode) { MultiByteToWideChar(CP_UTF8, 0, path, -1, buf, len); out = _wfopen(buf, mode_w); #else - out = fopen(path, mode); + // cpp11 will have converted the user's path to UTF-8 by now + // but we need to pass the path to fopen() in the native encoding + const char* native_path = Rf_translateChar(cpp11::r_string(path)); + out = fopen(native_path, mode); #endif return out;