From 64824430fa9b9f6f43ef6000a250a93fd0bbd03e Mon Sep 17 00:00:00 2001 From: Rachael Alexanderson Date: Tue, 16 Aug 2022 22:10:09 -0400 Subject: [PATCH] - make shell folder functions unicode aware --- src/common/platform/win32/i_system.cpp | 49 ++++++++++++++++---------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/src/common/platform/win32/i_system.cpp b/src/common/platform/win32/i_system.cpp index 48005a428..120f9818c 100644 --- a/src/common/platform/win32/i_system.cpp +++ b/src/common/platform/win32/i_system.cpp @@ -54,6 +54,7 @@ #include #include #include +#include #include @@ -66,8 +67,6 @@ #include -#include - #include "hardware.h" #include "printf.h" @@ -962,35 +961,49 @@ void I_SetThreadNumaNode(std::thread &thread, int numaNode) } } -void I_OpenShellFolder(const char* folder) +void I_OpenShellFolder(const char* infolder) { - char curdir[256]; - if (!getcwd (curdir, countof(curdir))) + LPWSTR curdir = new wchar_t[MAX_PATH]; + if (!GetCurrentDirectoryW(MAX_PATH, curdir)) { Printf ("Current path too long\n"); return; } - - chdir(folder); - Printf("Opening folder: %s\n", folder); - std::system("explorer ."); - chdir(curdir); + std::wstring_convert> converter; + std::wstring folder = converter.from_bytes(infolder); + SetCurrentDirectoryW(folder.c_str()); + Printf("Opening folder: %s\n", infolder); + ShellExecuteW(NULL, L"open", L"explorer.exe", L".", NULL, SW_SHOWNORMAL); + SetCurrentDirectoryW(curdir); + delete curdir; } void I_OpenShellFile(const char* file) { - char curdir[256]; - if (!getcwd (curdir, countof(curdir))) + LPWSTR curdir = new wchar_t[MAX_PATH]; + if (!GetCurrentDirectoryW(MAX_PATH, curdir)) { Printf ("Current path too long\n"); return; } - std::string folder = file; - folder.erase(folder.find_last_of('/'), std::string::npos); - chdir(folder.c_str()); - Printf("Opening folder: %s\n", folder.c_str()); - std::system("explorer ."); - chdir(curdir); + std::string infolder = file; + std::string toreplace = "\\"; + std::string replacewith = "/"; + std::size_t pos = infolder.find(toreplace); + while(pos != std::string::npos) + { + infolder.replace(pos, toreplace.length(), replacewith); + pos = infolder.find(toreplace); + } + infolder.erase(infolder.find_last_of('/'), std::string::npos); + + std::wstring_convert> converter; + std::wstring folder = converter.from_bytes(infolder); + SetCurrentDirectoryW(folder.c_str()); + Printf("Opening folder: %s\n", infolder.c_str()); + ShellExecuteW(NULL, L"open", L"explorer.exe", L".", NULL, SW_SHOWNORMAL); + SetCurrentDirectoryW(curdir); + delete curdir; }