Skip to content

Commit

Permalink
Re-encode to native just prior to fopen() or mio::make_mmap_source()
Browse files Browse the repository at this point in the history
  • Loading branch information
jennybc committed May 8, 2022
1 parent 8e821c0 commit 4317c8a
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/unicode_fopen.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
#endif
// clang-format on

#ifdef _WIN32
#include <Rinternals.h>

#ifdef _WIN32
#include <windows.h>
#else
#include "cpp11/r_string.hpp"
#endif

// This is needed to support wide character paths on windows
Expand All @@ -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;
Expand All @@ -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
}

0 comments on commit 4317c8a

Please sign in to comment.