Browse Source

closes https://github.com/assimp/assimp/issues/1612: make wstaring handling depend from encoding of the filename.

Kim Kulling 7 years ago
parent
commit
5a30bccdae
1 changed files with 49 additions and 15 deletions
  1. 49 15
      code/DefaultIOSystem.cpp

+ 49 - 15
code/DefaultIOSystem.cpp

@@ -74,10 +74,22 @@ bool DefaultIOSystem::Exists( const char* pFile) const
 {
 #ifdef _WIN32
     wchar_t fileName16[PATHLIMIT];
-    MultiByteToWideChar(CP_UTF8, MB_PRECOMPOSED, pFile, -1, fileName16, PATHLIMIT);
-    struct _stat64 filestat;
-    if (0 != _wstat64(fileName16, &filestat))
-        return false;
+
+    bool isUnicode = IsTextUnicode(pFile, strlen(pFile), NULL);
+    if (isUnicode) {
+
+        MultiByteToWideChar(CP_UTF8, MB_PRECOMPOSED, pFile, -1, fileName16, PATHLIMIT);
+        struct _stat64 filestat;
+        if (0 != _wstat64(fileName16, &filestat)) {
+            return false;
+        }
+    } else {
+        FILE* file = ::fopen(pFile, "rb");
+        if (!file)
+            return false;
+
+        ::fclose(file);
+    }
 #else
     FILE* file = ::fopen( pFile, "rb");
     if( !file)
@@ -97,9 +109,14 @@ IOStream* DefaultIOSystem::Open( const char* strFile, const char* strMode)
     FILE* file;
 #ifdef _WIN32
     wchar_t fileName16[PATHLIMIT];
-    MultiByteToWideChar(CP_UTF8, MB_PRECOMPOSED, strFile, -1, fileName16, PATHLIMIT);
-    std::string mode8(strMode);
-    file = ::_wfopen(fileName16, std::wstring(mode8.begin(), mode8.end()).c_str());
+    bool isUnicode = IsTextUnicode(strFile, strlen(strFile), NULL );
+    if (isUnicode) {
+        MultiByteToWideChar(CP_UTF8, MB_PRECOMPOSED, strFile, -1, fileName16, PATHLIMIT);
+        std::string mode8(strMode);
+        file = ::_wfopen(fileName16, std::wstring(mode8.begin(), mode8.end()).c_str());
+    } else {
+        file = ::fopen(strFile, strMode);
+    }
 #else
     file = ::fopen(strFile, strMode);
 #endif
@@ -140,24 +157,41 @@ inline static void MakeAbsolutePath (const char* in, char* _out)
 {
     ai_assert(in && _out);
 #if defined( _MSC_VER ) || defined( __MINGW32__ )
-    wchar_t out16[PATHLIMIT];
-    wchar_t in16[PATHLIMIT];
-    MultiByteToWideChar(CP_UTF8, MB_PRECOMPOSED, in, -1, out16, PATHLIMIT);
-    wchar_t* ret = ::_wfullpath( out16, in16, PATHLIMIT );
-    if (ret)
-    {
-        WideCharToMultiByte(CP_UTF8, MB_PRECOMPOSED, out16, -1, _out, PATHLIMIT, nullptr, nullptr);
+    bool isUnicode = IsTextUnicode(in, strlen(in), NULL);
+    if (isUnicode) {
+        wchar_t out16[PATHLIMIT];
+        wchar_t in16[PATHLIMIT];
+        MultiByteToWideChar(CP_UTF8, MB_PRECOMPOSED, in, -1, out16, PATHLIMIT);
+        wchar_t* ret = ::_wfullpath(out16, in16, PATHLIMIT);
+        if (ret) {
+            WideCharToMultiByte(CP_UTF8, MB_PRECOMPOSED, out16, -1, _out, PATHLIMIT, nullptr, nullptr);
+        }
+        if (!ret) {
+            // preserve the input path, maybe someone else is able to fix
+            // the path before it is accessed (e.g. our file system filter)
+            DefaultLogger::get()->warn("Invalid path: " + std::string(in));
+            strcpy(_out, in);
+        }
+
+    } else {
+        char* ret = :: _fullpath(_out, in, PATHLIMIT);
+        if (!ret) {
+            // preserve the input path, maybe someone else is able to fix
+            // the path before it is accessed (e.g. our file system filter)
+            DefaultLogger::get()->warn("Invalid path: " + std::string(in));
+            strcpy(_out, in);
+        }
     }
 #else
     // use realpath
     char* ret = realpath(in, _out);
-#endif
     if(!ret) {
         // preserve the input path, maybe someone else is able to fix
         // the path before it is accessed (e.g. our file system filter)
         DefaultLogger::get()->warn("Invalid path: "+std::string(in));
         strcpy(_out,in);
     }
+#endif
 }
 
 // ------------------------------------------------------------------------------------------------