]> git.lyx.org Git - lyx.git/blobdiff - src/support/os_win32.cpp
Need this for std::endl now, according to Eugene
[lyx.git] / src / support / os_win32.cpp
index c50fa05b1a8370983d6f6c58fd5737f1d109e44c..e4f9ebd0fcbd9a15405f082545bd3b2f108a3e7a 100644 (file)
 #include "support/lassert.h"
 
 #include <cstdlib>
+#include <iostream>
 #include <vector>
 
 #include <QString>
 
 #include <io.h>
 #include <direct.h> // _getdrive
+#include <fileapi.h> // GetFinalPathNameByHandle
 #include <shlobj.h>  // SHGetFolderPath
 #include <windef.h>
 #include <shellapi.h>
 #include <stdio.h>
 #endif
 
-
+#if defined(_MSC_VER) && (_MSC_VER >= 1900)
+#else
 extern "C" {
 extern void __wgetmainargs(int * argc, wchar_t *** argv, wchar_t *** envp,
                           int expand_wildcards, int * new_mode);
 }
+#endif
 
 using namespace std;
 
@@ -95,9 +99,9 @@ BOOL terminate_handler(DWORD event)
        return FALSE;
 }
 
-} // namespace anon
+} // namespace
 
-void init(int argc, char * argv[])
+void init(int argc, char ** argv[])
 {
        /* Note from Angus, 17 Jan 2005:
         *
@@ -156,10 +160,35 @@ void init(int argc, char * argv[])
         */
 
 
+       // Remove PYTHONPATH from the environment as it may point to an
+       // external python installation and cause reconfigure failures.
+       unsetEnv("PYTHONPATH");
+
+
+#if defined(_MSC_VER) && (_MSC_VER >= 1900)
+       // Removing an argument from argv leads to an assertion on Windows
+       // when compiling with MSVC 2015 in debug mode (see bug #10440).
+       // To avoid this we make a copy of the array of pointers.
+       char ** newargv = (char **) malloc((argc + 1) * sizeof(char *));
+       if (newargv) {
+               memcpy(newargv, *argv, (argc + 1) * sizeof(char *));
+               *argv = newargv;
+       } else {
+               lyxerr << "LyX warning: Cannot make a copy of "
+                         "command line arguments!"
+                      << endl;
+       }
+#endif
+
+
        // Get the wide program arguments array
+#if defined(_MSC_VER) && (_MSC_VER >= 1900)
+       argv_ = CommandLineToArgvW(GetCommandLineW(), &argc_);
+#else
        wchar_t ** envp = 0;
        int newmode = 0;
        __wgetmainargs(&argc_, &argv_, &envp, -1, &newmode);
+#endif
        LATTEST(argc == argc_);
 
        // If Cygwin is detected, query the cygdrive prefix.
@@ -420,23 +449,6 @@ string latex_path_list(string const & p)
 }
 
 
-bool is_valid_strftime(string const & p)
-{
-       string::size_type pos = p.find_first_of('%');
-       while (pos != string::npos) {
-               if (pos + 1 == string::npos)
-                       break;
-               if (!containsOnly(p.substr(pos + 1, 1),
-                       "aAbBcdfHIjmMpSUwWxXyYzZ%"))
-                       return false;
-               if (pos + 2 == string::npos)
-                     break;
-               pos = p.find_first_of('%', pos + 2);
-       }
-       return true;
-}
-
-
 // returns a string suitable to be passed to popen when
 // reading a pipe
 char const * popen_read_mode()
@@ -561,10 +573,13 @@ bool autoOpenFile(string const & filename, auto_open_mode const mode,
                setEnv("TEXFONTS", newtexfonts);
        }
 
+       QString const wname = toqstr(filename);
+
        // reference: http://msdn.microsoft.com/en-us/library/bb762153.aspx
-       char const * action = (mode == VIEW) ? "open" : "edit";
-       bool success = reinterpret_cast<int>(ShellExecute(NULL, action,
-               to_local8bit(from_utf8(filename)).c_str(), NULL, NULL, 1)) > 32;
+       wchar_t const * action = (mode == VIEW) ? L"open" : L"edit";
+       bool success = reinterpret_cast<intptr_t>(ShellExecuteW(NULL, action,
+                       reinterpret_cast<wchar_t const *>(wname.utf16()),
+                       NULL, NULL, 1)) > 32;
 
        if (!path.empty() && !lyxrc.texinputs_prefix.empty()) {
                setEnv("TEXINPUTS", oldtexinputs);
@@ -581,45 +596,18 @@ string real_path(string const & path)
        // See http://msdn.microsoft.com/en-us/library/aa366789(VS.85).aspx
        QString const qpath = get_long_path(toqstr(path));
        HANDLE hpath = CreateFileW((wchar_t *) qpath.utf16(), GENERIC_READ,
-                               FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
+                               FILE_SHARE_READ, NULL, OPEN_EXISTING,
+                               FILE_FLAG_BACKUP_SEMANTICS, NULL);
 
        if (hpath == INVALID_HANDLE_VALUE) {
                // The file cannot be accessed.
                return path;
        }
 
-       // Get the file size.
-       DWORD size_hi = 0;
-       DWORD size_lo = GetFileSize(hpath, &size_hi);
-
-       if (size_lo == 0 && size_hi == 0) {
-               // A zero-length file cannot be mapped.
-               CloseHandle(hpath);
-               return path;
-       }
-
-       // Create a file mapping object.
-       HANDLE hmap = CreateFileMapping(hpath, NULL, PAGE_READONLY, 0, 1, NULL);
-
-       if (!hmap) {
-               CloseHandle(hpath);
-               return path;
-       }
-
-       // Create a file mapping to get the file name.
-       void * pmem = MapViewOfFile(hmap, FILE_MAP_READ, 0, 0, 1);
-
-       if (!pmem) {
-               CloseHandle(hmap);
-               CloseHandle(hpath);
-               return path;
-       }
-
        TCHAR realpath[MAX_PATH + 1];
 
-       if (!GetMappedFileName(GetCurrentProcess(), pmem, realpath, MAX_PATH)) {
-               UnmapViewOfFile(pmem);
-               CloseHandle(hmap);
+       DWORD size = GetFinalPathNameByHandle(hpath, realpath, MAX_PATH, VOLUME_NAME_NT);
+       if (size > MAX_PATH) {
                CloseHandle(hpath);
                return path;
        }
@@ -665,8 +653,6 @@ string real_path(string const & path)
                        while (*p++) ;
                } while (!found && *p);
        }
-       UnmapViewOfFile(pmem);
-       CloseHandle(hmap);
        CloseHandle(hpath);
        string const retpath = subst(string(realpath), '\\', '/');
        return FileName::fromFilesystemEncoding(retpath).absFileName();