- make shell folder functions unicode aware

This commit is contained in:
Rachael Alexanderson 2022-08-16 22:10:09 -04:00
parent ee132372d8
commit 64824430fa

View file

@ -54,6 +54,7 @@
#include <process.h> #include <process.h>
#include <time.h> #include <time.h>
#include <map> #include <map>
#include <codecvt>
#include <stdarg.h> #include <stdarg.h>
@ -66,8 +67,6 @@
#include <shellapi.h> #include <shellapi.h>
#include <direct.h>
#include "hardware.h" #include "hardware.h"
#include "printf.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]; LPWSTR curdir = new wchar_t[MAX_PATH];
if (!getcwd (curdir, countof(curdir))) if (!GetCurrentDirectoryW(MAX_PATH, curdir))
{ {
Printf ("Current path too long\n"); Printf ("Current path too long\n");
return; return;
} }
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
chdir(folder); std::wstring folder = converter.from_bytes(infolder);
Printf("Opening folder: %s\n", folder); SetCurrentDirectoryW(folder.c_str());
std::system("explorer ."); Printf("Opening folder: %s\n", infolder);
chdir(curdir); ShellExecuteW(NULL, L"open", L"explorer.exe", L".", NULL, SW_SHOWNORMAL);
SetCurrentDirectoryW(curdir);
delete curdir;
} }
void I_OpenShellFile(const char* file) void I_OpenShellFile(const char* file)
{ {
char curdir[256]; LPWSTR curdir = new wchar_t[MAX_PATH];
if (!getcwd (curdir, countof(curdir))) if (!GetCurrentDirectoryW(MAX_PATH, curdir))
{ {
Printf ("Current path too long\n"); Printf ("Current path too long\n");
return; return;
} }
std::string folder = file; std::string infolder = file;
folder.erase(folder.find_last_of('/'), std::string::npos); std::string toreplace = "\\";
chdir(folder.c_str()); std::string replacewith = "/";
Printf("Opening folder: %s\n", folder.c_str()); std::size_t pos = infolder.find(toreplace);
std::system("explorer ."); while(pos != std::string::npos)
chdir(curdir); {
infolder.replace(pos, toreplace.length(), replacewith);
pos = infolder.find(toreplace);
}
infolder.erase(infolder.find_last_of('/'), std::string::npos);
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> 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;
} }