QFileSystemEngine/Win: simplify code

We don't need two or even three buffers for the current directory. In
the worst case, we had a 260-byte buffer on the stack, a larger one on
the heap (new[]/delete[]) and then a copy of it in QString.

Now, we shall have only one and it could be "gifted" to QFileSystemEntry
via std::move() (requires separate patch to take the QString by rvalue-
ref).

Pick-to: 6.3
Change-Id: Ibcde9b9795ad42ac9978fffd16f2bb2a443697d6
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Reviewed-by: Kai Koehne <kai.koehne@qt.io>
This commit is contained in:
Thiago Macieira 2022-05-26 11:30:03 -07:00
parent e21522b8bf
commit e390ff050a

View File

@ -1640,23 +1640,20 @@ bool QFileSystemEngine::setCurrentPath(const QFileSystemEntry &entry)
QFileSystemEntry QFileSystemEngine::currentPath() QFileSystemEntry QFileSystemEngine::currentPath()
{ {
QString ret; QString ret(PATH_MAX, Qt::Uninitialized);
DWORD size = 0; DWORD size = GetCurrentDirectoryW(PATH_MAX, reinterpret_cast<wchar_t *>(ret.data()));
wchar_t currentName[PATH_MAX]; if (size > PATH_MAX) {
size = ::GetCurrentDirectory(PATH_MAX, currentName); // try again after enlarging the buffer
if (size != 0) { ret.resize(size);
if (size > PATH_MAX) { size = GetCurrentDirectoryW(size, reinterpret_cast<wchar_t *>(ret.data()));
wchar_t *newCurrentName = new wchar_t[size];
if (::GetCurrentDirectory(size, newCurrentName) != 0) // note: the current directory may have changed underneath us; if the
ret = QString::fromWCharArray(newCurrentName, size); // new one is even bigger, we may return a truncated string!
delete [] newCurrentName;
} else {
ret = QString::fromWCharArray(currentName, size);
}
} }
if (ret.length() >= 2 && ret[1] == u':') if (size >= 2 && ret.at(1) == u':')
ret[0] = ret.at(0).toUpper(); // Force uppercase drive letters. ret[0] = ret.at(0).toUpper(); // Force uppercase drive letters.
return QFileSystemEntry(ret, QFileSystemEntry::FromNativePath()); ret.resize(size);
return QFileSystemEntry(std::move(ret), QFileSystemEntry::FromNativePath());
} }
//static //static