From c58d4b47c2c711d45d5bb0e5fa7c4875b9c97f23 Mon Sep 17 00:00:00 2001
From: Sean McBride <sean@rogue-research.com>
Date: Tue, 21 Apr 2020 21:05:04 -0400
Subject: [PATCH] SystemTools: On Windows, strip 'e' from Fopen mode

The MSVC runtime does not support the 'e' mode specifier.
---
 SystemTools.cxx    | 7 ++++++-
 SystemTools.hxx.in | 3 ++-
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/SystemTools.cxx b/SystemTools.cxx
index 3a6ceec9..c2fdab56 100644
--- a/SystemTools.cxx
+++ b/SystemTools.cxx
@@ -24,6 +24,7 @@
 #include KWSYS_HEADER(Encoding.h)
 #include KWSYS_HEADER(Encoding.hxx)
 
+#include <algorithm>
 #include <fstream>
 #include <iostream>
 #include <set>
@@ -892,8 +893,12 @@ const char* SystemTools::GetExecutableExtension()
 FILE* SystemTools::Fopen(const std::string& file, const char* mode)
 {
 #ifdef _WIN32
+  // Remove any 'e', which is supported on UNIX, but not Windows.
+  std::wstring trimmedMode = Encoding::ToWide(mode);
+  trimmedMode.erase(std::remove(trimmedMode.begin(), trimmedMode.end(), L'e'),
+                    trimmedMode.end());
   return _wfopen(Encoding::ToWindowsExtendedPath(file).c_str(),
-                 Encoding::ToWide(mode).c_str());
+                 trimmedMode.c_str());
 #else
   return fopen(file.c_str(), mode);
 #endif
diff --git a/SystemTools.hxx.in b/SystemTools.hxx.in
index cd7b728e..ae08e573 100644
--- a/SystemTools.hxx.in
+++ b/SystemTools.hxx.in
@@ -549,7 +549,8 @@ public:
    */
 
   /**
-   * Open a file considering unicode.
+   * Open a file considering unicode. On Windows, if 'e' is present in
+   * mode it is first discarded.
    */
   static FILE* Fopen(const std::string& file, const char* mode);
 
-- 
GitLab