From 77046f9b432279f010aff10ce46ec571c1bb69d2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?David=20Tschumperl=C3=A9?= <David.Tschumperle@gmail.com>
Date: Tue, 2 Oct 2018 07:53:32 +0200
Subject: [PATCH] .

---
 CImg.h | 31 +++++++++++++++++--------------
 1 file changed, 17 insertions(+), 14 deletions(-)

diff --git a/CImg.h b/CImg.h
index bff229c..22a7165 100644
--- a/CImg.h
+++ b/CImg.h
@@ -59816,21 +59816,24 @@ namespace cimg {
   // Open a file (with wide character support on Windows).
   inline std::FILE *win_fopen(const char *const path, const char *const mode) {
 #if cimg_OS==2
-    // Convert 'path' to a wide-character string.
+    std::FILE *const res = std::fopen(path,mode);
+    if (res) return res;
+
+    // Try alternative method, with wide-character string.
     int err = MultiByteToWideChar(CP_UTF8,0,path,-1,0,0);
-    if (!err) return std::fopen(path,mode);
-    CImg<wchar_t> wpath(err);
-    err = MultiByteToWideChar(CP_UTF8,0,path,-1,wpath,err);
-    if (!err) return std::fopen(path,mode);
-
-    // Convert 'mode' to a wide-character string.
-    err = MultiByteToWideChar(CP_UTF8,0,mode,-1,0,0);
-    if (!err) return std_fopen(path,mode);
-    CImg<wchar_t> wmode(err);
-    err = MultiByteToWideChar(CP_UTF8,0,mode,-1,wmode,err);
-    if (!err) return std::fopen(path,mode);
-    std::FILE *const res = _wfopen(wpath,wmode);
-    return res?res:std::fopen(path,mode);
+    if (err) {
+      CImg<wchar_t> wpath(err);
+      err = MultiByteToWideChar(CP_UTF8,0,path,-1,wpath,err);
+      if (err) { // Convert 'mode' to a wide-character string.
+        err = MultiByteToWideChar(CP_UTF8,0,mode,-1,0,0);
+        if (err) {
+          CImg<wchar_t> wmode(err);
+          if (MultiByteToWideChar(CP_UTF8,0,mode,-1,wmode,err))
+            return _wfopen(wpath,wmode);
+        }
+      }
+    }
+    return 0;
 #else
     return std::fopen(path,mode);
 #endif
-- 
GitLab