From 35095c542fde45f2617f81830cff41855f2a39db Mon Sep 17 00:00:00 2001 From: hjk Date: Thu, 9 Sep 2021 08:14:26 +0200 Subject: [PATCH] Utils: Re-order FilePath API a bit Somewhat more logical blocks. Also make clear which functions should not be used. Change-Id: Ia2f7b84347601dbd4d51f42c992a71ddcceb1e0b Reviewed-by: David Schulz --- src/libs/utils/filepath.h | 132 ++++++++++++++++++-------------------- 1 file changed, 63 insertions(+), 69 deletions(-) diff --git a/src/libs/utils/filepath.h b/src/libs/utils/filepath.h index 0c9e0e8b94e..c3060643bb6 100644 --- a/src/libs/utils/filepath.h +++ b/src/libs/utils/filepath.h @@ -57,30 +57,16 @@ public: template FilePath(const char (&literal)[N]) { setFromString(literal); } static FilePath fromString(const QString &filepath); - static FilePath fromFileInfo(const QFileInfo &info); static FilePath fromStringWithExtension(const QString &filepath, const QString &defaultExtension); static FilePath fromUserInput(const QString &filepath); static FilePath fromUtf8(const char *filepath, int filepathSize = -1); static FilePath fromVariant(const QVariant &variant); - - QString toString() const; - FilePath onDevice(const FilePath &deviceTemplate) const; - FilePath withNewPath(const QString &newPath) const; - - QFileInfo toFileInfo() const; - QVariant toVariant() const; - QDir toDir() const; + static FilePath fromUrl(const QUrl &url); QString toUserOutput() const; - QString shortNativePath() const; - - QString fileName() const; - QString fileNameWithPathComponents(int pathComponents) const; - - QString baseName() const; - QString completeBaseName() const; - QString suffix() const; - QString completeSuffix() const; + QString toString() const; + QVariant toVariant() const; + QUrl toUrl() const; QString scheme() const { return m_scheme; } void setScheme(const QString &scheme); @@ -91,8 +77,24 @@ public: QString path() const { return m_data; } void setPath(const QString &path) { m_data = path; } - bool needsDevice() const; + QString fileName() const; + QString fileNameWithPathComponents(int pathComponents) const; + + QString baseName() const; + QString completeBaseName() const; + QString suffix() const; + QString completeSuffix() const; + + FilePath pathAppended(const QString &str) const; + FilePath stringAppended(const QString &str) const; + bool startsWith(const QString &s) const; + bool endsWith(const QString &s) const; + bool exists() const; + bool needsDevice() const; + + FilePath parentDir() const; + bool isChildOf(const FilePath &s) const; bool isWritableDir() const; bool isWritableFile() const; @@ -105,38 +107,6 @@ public: bool isAbsolutePath() const { return !isRelativePath(); } bool isFile() const; bool isDir() const; - - bool createDir() const; - QList dirEntries(const QStringList &nameFilters, - QDir::Filters filters = QDir::NoFilter, - QDir::SortFlags sort = QDir::NoSort) const; - QList dirEntries(QDir::Filters filters) const; - QByteArray fileContents(qint64 maxSize = -1, qint64 offset = 0) const; - bool writeFileContents(const QByteArray &data) const; - - FilePath parentDir() const; - FilePath absolutePath() const; - FilePath absoluteFilePath() const; - - // makes sure that capitalization of directories is canonical - // on Windows and macOS. This is rarely needed. - FilePath normalizedPathName() const; - - bool operator==(const FilePath &other) const; - bool operator!=(const FilePath &other) const; - bool operator<(const FilePath &other) const; - bool operator<=(const FilePath &other) const; - bool operator>(const FilePath &other) const; - bool operator>=(const FilePath &other) const; - FilePath operator+(const QString &s) const; - FilePath operator/(const QString &str) const; - - bool isChildOf(const FilePath &s) const; - bool isChildOf(const QDir &dir) const; - bool startsWith(const QString &s) const; - bool endsWith(const QString &s) const; - bool startsWithDriveLetter() const; - bool isNewerThan(const QDateTime &timeStamp) const; QDateTime lastModified() const; QFile::Permissions permissions() const; @@ -147,35 +117,51 @@ public: bool copyFile(const FilePath &target) const; bool renameFile(const FilePath &target) const; qint64 fileSize() const; + bool createDir() const; + QList dirEntries(const QStringList &nameFilters, + QDir::Filters filters = QDir::NoFilter, + QDir::SortFlags sort = QDir::NoSort) const; + QList dirEntries(QDir::Filters filters) const; + QByteArray fileContents(qint64 maxSize = -1, qint64 offset = 0) const; + bool writeFileContents(const QByteArray &data) const; + + bool operator==(const FilePath &other) const; + bool operator!=(const FilePath &other) const; + bool operator<(const FilePath &other) const; + bool operator<=(const FilePath &other) const; + bool operator>(const FilePath &other) const; + bool operator>=(const FilePath &other) const; + FilePath operator+(const QString &s) const; + FilePath operator/(const QString &str) const; Qt::CaseSensitivity caseSensitivity() const; - FilePath relativeChildPath(const FilePath &parent) const; - FilePath relativePath(const FilePath &anchor) const; - FilePath pathAppended(const QString &str) const; - FilePath stringAppended(const QString &str) const; - FilePath resolvePath(const FilePath &tail) const; - FilePath resolvePath(const QString &tail) const; - FilePath cleanPath() const; - - FilePath canonicalPath() const; - FilePath symLinkTarget() const; - FilePath resolveSymlinks() const; - FilePath withExecutableSuffix() const; - void clear(); bool isEmpty() const; uint hash(uint seed) const; - // NOTE: Most FilePath operations on FilePath created from URL currently - // do not work. Among the working are .toVariant() and .toUrl(). - static FilePath fromUrl(const QUrl &url); - QUrl toUrl() const; - + FilePath resolvePath(const FilePath &tail) const; + FilePath resolvePath(const QString &tail) const; + FilePath cleanPath() const; + FilePath canonicalPath() const; + FilePath symLinkTarget() const; + FilePath resolveSymlinks() const; + FilePath withExecutableSuffix() const; + FilePath relativeChildPath(const FilePath &parent) const; + FilePath relativePath(const FilePath &anchor) const; FilePath searchInDirectories(const QList &dirs) const; FilePath searchInPath(const QList &additionalDirs = {}) const; Environment deviceEnvironment() const; + FilePath onDevice(const FilePath &deviceTemplate) const; + FilePath withNewPath(const QString &newPath) const; + + // makes sure that capitalization of directories is canonical + // on Windows and macOS. This is rarely needed. + FilePath normalizedPathName() const; + + QString shortNativePath() const; + bool startsWithDriveLetter() const; static QString formatFilePaths(const QList &files, const QString &separator); static void removeDuplicates(QList &files); @@ -188,6 +174,14 @@ public: qint64 maxSize = -1, qint64 offset = 0) const; void asyncWriteFileContents(const Continuation &cont, const QByteArray &data) const; + // Deprecated. + static FilePath fromFileInfo(const QFileInfo &info); // Avoid. + QFileInfo toFileInfo() const; // Avoid. + QDir toDir() const; // Avoid. + FilePath absolutePath() const; // Avoid. Use resolvePath(...)[.parent()] with proper base. + FilePath absoluteFilePath() const; // Avoid. Use resolvePath(...) with proper base. + bool isChildOf(const QDir &dir) const; // Avoid. Use isChildOf(FilePath). + private: friend class ::tst_fileutils; static QString calcRelativePath(const QString &absolutePath, const QString &absoluteAnchorPath);