Fix handling of network path (UNC path) in CustomFileDialog

Use STL algorithms for path transformation.

Fix #9527, close #9551
This commit is contained in:
mere-human 2021-02-21 19:50:42 +02:00 committed by Don HO
parent 26dad277c9
commit 064d844a41
No known key found for this signature in database
GPG Key ID: 6C429F1D8D84F46E
1 changed files with 17 additions and 10 deletions

View File

@ -382,18 +382,25 @@ private:
if (fileName.empty()) if (fileName.empty())
return false; return false;
bool transformed = false; bool transformed = false;
// Transform to a Windows path. // Replace a forward-slash with a backslash.
size_t pos = 0; std::replace_if(fileName.begin(), fileName.end(),
while ((pos = fileName.find('/', pos)) != generic_string::npos) [&transformed](generic_string::value_type c)
{ {
fileName[pos] = '\\'; const bool eq = (c == '/');
++pos; transformed |= eq;
transformed = true; return eq;
} },
'\\');
// If there are two or more double backslash, then change it to single. // If there are two or more double backslash, then change it to single.
while (fileName.find(_T("\\\\")) != generic_string::npos) // Start from 2nd element to keep a path that starts from "\\".
auto last = std::unique(fileName.begin() + 1, fileName.end(),
[](generic_string::value_type a, generic_string::value_type b)
{ {
fileName.replace(fileName.find(_T("\\\\")), 2, _T("\\")); return a == b && a == '\\';
});
if (last != fileName.end())
{
fileName.erase(last, fileName.end());
transformed = true; transformed = true;
} }
return transformed; return transformed;