diff --git a/src/dci/private/ddcifileengine.cpp b/src/dci/private/ddcifileengine.cpp index 50472f2..14b2a0a 100644 --- a/src/dci/private/ddcifileengine.cpp +++ b/src/dci/private/ddcifileengine.cpp @@ -44,6 +44,7 @@ QAbstractFileEngine *DDciFileEngineHandler::create(const QString &fileName) cons #else DDciFileEngine *engine = new DDciFileEngine(fileName); #endif + if (!engine->isValid()) { #if QT_VERSION < QT_VERSION_CHECK(6, 8, 0) delete engine; @@ -56,8 +57,7 @@ QAbstractFileEngine *DDciFileEngineHandler::create(const QString &fileName) cons // 共享同个线程内的同个 DDciFile static thread_local QHash> sharedDciFile; -static void doDeleteSharedDciFile(const QString &path, DDciFile *file) -{ +static void doDeleteSharedDciFile(const QString &path, DDciFile *file) { int count = sharedDciFile.remove(path); Q_ASSERT(count > 0); delete file; @@ -70,7 +70,8 @@ static DDciFileShared getDciFile(const QString &dciFilePath, bool usePath = true } DDciFileShared shared(usePath ? new DDciFile(dciFilePath) : new DDciFile(), - std::bind(doDeleteSharedDciFile, dciFilePath, std::placeholders::_1)); + std::bind(doDeleteSharedDciFile, dciFilePath, + std::placeholders::_1)); sharedDciFile[dciFilePath] = shared.toWeakRef(); return shared; } @@ -82,6 +83,7 @@ DDciFileEngineIterator::DDciFileEngineIterator(QDir::Filters filters, const QStr : QAbstractFileEngineIterator(filters, nameFilters) #endif { + } #if QT_VERSION < QT_VERSION_CHECK(6, 8, 0) @@ -90,17 +92,16 @@ QString DDciFileEngineIterator::next() current = nextValid; return DDciFileEngineIterator::currentFileName(); } -#endif -#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0) -bool DDciFileEngineIterator::advance() -#else bool DDciFileEngineIterator::hasNext() const +#else +bool DDciFileEngineIterator::advance() #endif { if (!file) { const auto paths = DDciFileEngine::resolvePath(path()); - if (paths.first.isEmpty() || paths.second.isEmpty()) + if (paths.first.isEmpty() + || paths.second.isEmpty()) return false; file = getDciFile(paths.first); @@ -120,7 +121,7 @@ bool DDciFileEngineIterator::hasNext() const } else if (fileType == DDciFile::Symlink) { if (filters.testFlag(QDir::NoSymLinks)) continue; - } else { // DDciFile::UnknowFile + } else { // DDciFile::UnknowFile continue; } @@ -198,7 +199,8 @@ bool DDciFileEngine::open(QIODevice::OpenMode openMode) } } - if ((openMode & QIODevice::ExistingOnly) || !(openMode & QIODevice::WriteOnly)) { + if ((openMode & QIODevice::ExistingOnly) + || !(openMode & QIODevice::WriteOnly)) { if (!file->exists(subfilePath)) { setError(QFile::OpenError, "The file is not exists"); return false; @@ -219,7 +221,8 @@ bool DDciFileEngine::open(QIODevice::OpenMode openMode) #endif // 不存在时尝试新建 - if (!file->exists(subfilePath) && !file->writeFile(subfilePath, QByteArray())) { + if (!file->exists(subfilePath) + && !file->writeFile(subfilePath, QByteArray())) { return false; } } @@ -509,22 +512,24 @@ QAbstractFileEngine::FileFlags DDciFileEngine::fileFlags(QAbstractFileEngine::Fi QString DDciFileEngine::fileName(QAbstractFileEngine::FileName file) const { switch (file) { - case AbsoluteName: - case CanonicalName: - case DefaultName: - return QDir::cleanPath(DCI_FILE_SCHEME + dciFilePath + subfilePath); - case AbsolutePathName: - return QDir::cleanPath(DCI_FILE_SCHEME + dciFilePath); - case BaseName: - return QFileInfo(subfilePath).baseName(); + case AbsoluteName: + case CanonicalName: + case DefaultName: + return QDir::cleanPath(DCI_FILE_SCHEME + dciFilePath + subfilePath); + case AbsolutePathName: + return QDir::cleanPath(DCI_FILE_SCHEME + dciFilePath); + case BaseName: + return QFileInfo(subfilePath).baseName(); #if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0) - case AbsoluteLinkTarget: + case AbsoluteLinkTarget: #else - case LinkName: + case LinkName: #endif - return this->file->type(subfilePath) == DDciFile::Symlink ? this->file->symlinkTarget(subfilePath) : QString(); - default: - break; + return this->file->type(subfilePath) == DDciFile::Symlink + ? this->file->symlinkTarget(subfilePath) + : QString(); + default: + break; } return QString(); @@ -539,7 +544,8 @@ void DDciFileEngine::setFileName(const QString &fullPath) subfilePath.clear(); const auto paths = resolvePath(fullPath, QString(), false); - if (paths.first.isEmpty() || paths.second.isEmpty()) + if (paths.first.isEmpty() + || paths.second.isEmpty()) return; dciFilePath = paths.first; @@ -560,17 +566,17 @@ QDateTime DDciFileEngine::fileTime(QAbstractFileEngine::FileTime time) const #endif #if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0) -QAbstractFileEngine::IteratorUniquePtr -DDciFileEngine::beginEntryList(const QString &path, QDir::Filters filters, const QStringList &filterNames) -{ - return QAbstractFileEngine::IteratorUniquePtr(new DDciFileEngineIterator(filters, filterNames)); -} +QAbstractFileEngine::IteratorUniquePtr DDciFileEngine::beginEntryList(const QString &path, QDir::Filters filters, const QStringList &filterNames) #else DDciFileEngine::Iterator *DDciFileEngine::beginEntryList(QDir::Filters filters, const QStringList &filterNames) +#endif { +#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0) + return QAbstractFileEngine::IteratorUniquePtr(new DDciFileEngineIterator(filters, filterNames)); +#else return new DDciFileEngineIterator(filters, filterNames); -} #endif +} #if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0) QAbstractFileEngine::IteratorUniquePtr DDciFileEngine::endEntryList() @@ -621,7 +627,9 @@ bool DDciFileEngine::forceSave(bool writeFile) const return flushToFile(&file, writeFile); } -QPair DDciFileEngine::resolvePath(const QString &fullPath, const QString &realFilePath, bool needRealFileExists) +QPair DDciFileEngine::resolvePath(const QString &fullPath, + const QString &realFilePath, + bool needRealFileExists) { if (!fullPath.startsWith(QStringLiteral(DCI_FILE_SCHEME) + realFilePath)) return {}; @@ -663,8 +671,8 @@ QPair DDciFileEngine::resolvePath(const QString &fullPath, con return {}; subfilePath = QDir::cleanPath(formatFullPath.mid(schemeLength + dciFilePath.length())); - qCDebug( - logFE(), "The DCI file path is: \"%s\", the subfile path is: \"%s\"", qPrintable(dciFilePath), qPrintable(subfilePath)); + qCDebug(logFE(), "The DCI file path is: \"%s\", the subfile path is: \"%s\"", + qPrintable(dciFilePath), qPrintable(subfilePath)); Q_ASSERT(!subfilePath.isEmpty()); return qMakePair(dciFilePath, subfilePath); diff --git a/src/dci/private/ddcifileengine_p.h b/src/dci/private/ddcifileengine_p.h index a1ea33b..118b971 100644 --- a/src/dci/private/ddcifileengine_p.h +++ b/src/dci/private/ddcifileengine_p.h @@ -36,15 +36,14 @@ using DDciFileShared = QSharedPointer; class DDciFileEngineIterator : public QAbstractFileEngineIterator { friend class DDciFileEngine; - public: DDciFileEngineIterator(QDir::Filters filters, const QStringList &nameFilters); -#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0) - bool advance() override; -#else +#if QT_VERSION < QT_VERSION_CHECK(6, 8, 0) QString next() override; bool hasNext() const override; +#else + bool advance() override; #endif QString currentFileName() const override; @@ -59,7 +58,6 @@ class DDciFileEngineIterator : public QAbstractFileEngineIterator class DDciFileEngine : public QAbstractFileEngine { friend class DDciFileEngineIterator; - public: explicit DDciFileEngine(const QString &fullPath); ~DDciFileEngine(); @@ -112,6 +110,7 @@ class DDciFileEngine : public QAbstractFileEngine #endif typedef DDciFileEngineIterator Iterator; + #if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0) IteratorUniquePtr beginEntryList(const QString &path, QDir::Filters filters, const QStringList &filterNames) override; IteratorUniquePtr endEntryList() override; @@ -123,7 +122,8 @@ class DDciFileEngine : public QAbstractFileEngine qint64 read(char *data, qint64 maxlen) override; qint64 write(const char *data, qint64 len) override; - bool extension(Extension extension, const ExtensionOption *option = 0, ExtensionReturn *output = 0) override; + bool extension(Extension extension, const ExtensionOption *option = 0, + ExtensionReturn *output = 0) override; bool supportsExtension(Extension extension) const override; bool cloneTo(QAbstractFileEngine *target) override; @@ -138,8 +138,9 @@ class DDciFileEngine : public QAbstractFileEngine * 是 DCI 文件的内部路径。 * 函数返回的第一个数据是"真实文件路径"。 */ - static QPair - resolvePath(const QString &fullPath, const QString &realFilePath = QString(), bool needRealFileExists = true); + static QPair resolvePath(const QString &fullPath, + const QString &realFilePath = QString(), + bool needRealFileExists = true); DDciFileShared file; QString dciFilePath; @@ -150,4 +151,5 @@ class DDciFileEngine : public QAbstractFileEngine QBuffer *fileBuffer = nullptr; }; + DCORE_END_NAMESPACE