- cleanup of Windows's I_OpenShell* functions

* do not use deprecated library features (wstring_convert was deorecated in C++17) - instead use the existing WideString utility.
* do proper calculation of the current directory's path length.
* remove the mostly redundant I_OpenShellFile function and instead use ExtractFilePath to get the config's path.
This commit is contained in:
Christoph Oelckers 2022-08-17 00:31:22 +02:00
parent 64824430fa
commit c5f4967871
6 changed files with 15 additions and 77 deletions

View file

@ -186,20 +186,3 @@ void I_OpenShellFolder(const char* folder)
chdir(curdir);
}
void I_OpenShellFile(const char* file)
{
char curdir[256];
if (!getcwd (curdir, countof(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("open .");
chdir(curdir);
}

View file

@ -70,6 +70,5 @@ inline int I_GetNumaNodeThreadCount(int numaNode) { return std::max<int>(std::th
inline void I_SetThreadNumaNode(std::thread &thread, int numaNode) { }
void I_OpenShellFolder(const char*);
void I_OpenShellFile(const char*);
#endif

View file

@ -447,20 +447,3 @@ void I_OpenShellFolder(const char* folder)
chdir(curdir);
}
void I_OpenShellFile(const char* file)
{
char curdir[256];
if (!getcwd (curdir, countof(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("xdg-open .");
chdir(curdir);
}

View file

@ -963,47 +963,21 @@ void I_SetThreadNumaNode(std::thread &thread, int numaNode)
void I_OpenShellFolder(const char* infolder)
{
LPWSTR curdir = new wchar_t[MAX_PATH];
if (!GetCurrentDirectoryW(MAX_PATH, curdir))
auto len = GetCurrentDirectoryW(0, nullptr);
TArray<wchar_t> curdir(len + 1, true);
if (!GetCurrentDirectoryW(len + 1, curdir.Data()))
{
Printf ("Current path too long\n");
return;
Printf("Unable to retrieve current directory\n");
}
else if (SetCurrentDirectoryW(WideString(infolder).c_str()))
{
Printf("Opening folder: %s\n", infolder);
ShellExecuteW(NULL, L"open", L"explorer.exe", L".", NULL, SW_SHOWNORMAL);
SetCurrentDirectoryW(curdir.Data());
}
else
{
Printf("Unable to open directory '%s\n", infolder);
}
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);
ShellExecuteW(NULL, L"open", L"explorer.exe", L".", NULL, SW_SHOWNORMAL);
SetCurrentDirectoryW(curdir);
delete curdir;
}
void I_OpenShellFile(const char* file)
{
LPWSTR curdir = new wchar_t[MAX_PATH];
if (!GetCurrentDirectoryW(MAX_PATH, curdir))
{
Printf ("Current path too long\n");
return;
}
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<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;
}

View file

@ -80,6 +80,5 @@ int I_GetNumaNodeThreadCount(int numaNode);
void I_SetThreadNumaNode(std::thread &thread, int numaNode);
void I_OpenShellFolder(const char*);
void I_OpenShellFile(const char*);
#endif

View file

@ -316,7 +316,7 @@ UNSAFE_CCMD (writeini)
CCMD(openconfig)
{
M_SaveDefaults(nullptr);
I_OpenShellFile(GameConfig->GetPathName());
I_OpenShellFolder(ExtractFilePath(GameConfig->GetPathName()));
}
//