From 4317c8a285ecf8d9cd17a6285301b9ea2613c00b 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() or mio::make_mmap_source() --- src/unicode_fopen.h | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/unicode_fopen.h b/src/unicode_fopen.h index b3a735d0..049dbd20 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; @@ -64,6 +70,9 @@ make_mmap_source(const char* file, std::error_code& error) { free(buf); return out; #else - return mio::make_mmap_source(file, error); + // cpp11 will have converted the user's path to UTF-8 by now + // but we need to pass the path to mio in the native encoding + const char* native_path = Rf_translateChar(cpp11::r_string(file)); + return mio::make_mmap_source(native_path, error); #endif }