Switch-ify QFSFileEngine::fileName and update implementation

Adapt to coding guidelines and use raw string literals.

Change-Id: Ice9a87cafb22e01a361ad44221d561a298e5af05
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Karsten Heimrich 2021-06-04 13:00:14 +02:00
parent b216b360ac
commit 8654192f8f

View File

@ -591,64 +591,72 @@ QByteArray QFSFileEngine::id() const
QString QFSFileEngine::fileName(FileName file) const QString QFSFileEngine::fileName(FileName file) const
{ {
Q_D(const QFSFileEngine); Q_D(const QFSFileEngine);
if (file == BaseName) { switch (file) {
case BaseName:
return d->fileEntry.fileName(); return d->fileEntry.fileName();
} else if (file == PathName) { case PathName:
return d->fileEntry.path(); return d->fileEntry.path();
} else if (file == AbsoluteName || file == AbsolutePathName) { case AbsoluteName:
QString ret; case AbsolutePathName: {
QString ret = d->fileEntry.filePath();
if (!isRelativePath()) { if (isRelativePath()) {
if (d->fileEntry.filePath().startsWith(QLatin1Char('/')) || // It's a absolute path to the current drive, so \a.txt -> Z:\a.txt ret = QDir::cleanPath(QDir::currentPath() + u'/' + ret);
d->fileEntry.filePath().size() == 2 || // It's a drive letter that needs to get a working dir appended } else if (ret.startsWith(u'/') // absolute path to the current drive, so \a.txt -> Z:\a.txt
(d->fileEntry.filePath().size() > 2 && d->fileEntry.filePath().at(2) != QLatin1Char('/')) || // It's a drive-relative path, so Z:a.txt -> Z:\currentpath\a.txt || ret.size() == 2 // or a drive letter that needs to get a working dir appended
d->fileEntry.filePath().contains(QLatin1String("/../")) || d->fileEntry.filePath().contains(QLatin1String("/./")) || // or a drive-relative path, so Z:a.txt -> Z:\currentpath\a.txt
d->fileEntry.filePath().endsWith(QLatin1String("/..")) || d->fileEntry.filePath().endsWith(QLatin1String("/."))) || (ret.size() > 2 && ret.at(2) != u'/')
{ || ret.contains(QStringView(u"/../"))
ret = QDir::fromNativeSeparators(QFileSystemEngine::nativeAbsoluteFilePath(d->fileEntry.filePath())); || ret.contains(QStringView(u"/./"))
} else { || ret.endsWith(QStringView(u"/.."))
ret = d->fileEntry.filePath(); || ret.endsWith(QStringView(u"/."))) {
} ret = QDir::fromNativeSeparators(QFileSystemEngine::nativeAbsoluteFilePath(ret));
} else {
ret = QDir::cleanPath(QDir::currentPath() + QLatin1Char('/') + d->fileEntry.filePath());
} }
// The path should be absolute at this point. // The path should be absolute at this point.
// From the docs : // From the docs :
// Absolute paths begin with the directory separator "/" // Absolute paths begin with the directory separator "/"
// (optionally preceded by a drive specification under Windows). // (optionally preceded by a drive specification under Windows).
if (ret.at(0) != QLatin1Char('/')) { if (ret.at(0) != u'/') {
Q_ASSERT(ret.length() >= 2); Q_ASSERT(ret.length() >= 2);
Q_ASSERT(ret.at(0).isLetter()); Q_ASSERT(ret.at(0).isLetter());
Q_ASSERT(ret.at(1) == QLatin1Char(':')); Q_ASSERT(ret.at(1) == u':');
// Force uppercase drive letters. // Force uppercase drive letters.
ret[0] = ret.at(0).toUpper(); ret[0] = ret.at(0).toUpper();
} }
if (file == AbsolutePathName) { if (file == AbsolutePathName) {
int slash = ret.lastIndexOf(QLatin1Char('/')); int slash = ret.lastIndexOf(u'/');
if (slash < 0) if (slash < 0)
return ret; return ret;
if (ret.at(0) != QLatin1Char('/') && slash == 2) if (ret.at(0) != u'/' && slash == 2)
return ret.left(3); // include the slash return ret.left(3); // include the slash
return ret.left(slash > 0 ? slash : 1); return ret.left(slash > 0 ? slash : 1);
} }
return ret; return ret;
} else if (file == CanonicalName || file == CanonicalPathName) { }
case CanonicalName:
case CanonicalPathName: {
if (!(fileFlags(ExistsFlag) & ExistsFlag)) if (!(fileFlags(ExistsFlag) & ExistsFlag))
return QString(); return QString();
QFileSystemEntry entry(QFileSystemEngine::canonicalName(QFileSystemEntry(fileName(AbsoluteName)), d->metaData)); const QFileSystemEntry entry =
QFileSystemEngine::canonicalName(QFileSystemEntry(fileName(AbsoluteName)), d->metaData);
if (file == CanonicalPathName) if (file == CanonicalPathName)
return entry.path(); return entry.path();
return entry.filePath(); return entry.filePath();
} else if (file == LinkName) { }
case LinkName:
return QFileSystemEngine::getLinkTarget(d->fileEntry, d->metaData).filePath(); return QFileSystemEngine::getLinkTarget(d->fileEntry, d->metaData).filePath();
} else if (file == BundleName) { case BundleName:
return QString(); return QString();
} else if (file == JunctionName) { case JunctionName:
return QFileSystemEngine::getJunctionTarget(d->fileEntry, d->metaData).filePath(); return QFileSystemEngine::getJunctionTarget(d->fileEntry, d->metaData).filePath();
case DefaultName:
break;
case NFileNames:
Q_ASSERT(false);
break;
} }
return d->fileEntry.filePath(); return d->fileEntry.filePath();
} }