From 050fa9f6cc1145f5a835420b9974a6b0e261b062 Mon Sep 17 00:00:00 2001 From: Thomas Zander Date: Tue, 7 Sep 2010 16:51:55 +0200 Subject: Add baseName and completeBaseName getters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Using the definition of the concepts from QFileInfo, allow us to use the already calculated indexes to fetch the baseName and the completeBaseName on a QFileSystemEntry. Reviewed-by: João Abecasis --- src/corelib/io/qfilesystementry.cpp | 26 +++++++++++--- src/corelib/io/qfilesystementry_p.h | 2 ++ .../auto/qfilesystementry/tst_qfilesystementry.cpp | 40 +++++++++++++++------- 3 files changed, 52 insertions(+), 16 deletions(-) diff --git a/src/corelib/io/qfilesystementry.cpp b/src/corelib/io/qfilesystementry.cpp index f5009b4..6f04c21 100644 --- a/src/corelib/io/qfilesystementry.cpp +++ b/src/corelib/io/qfilesystementry.cpp @@ -149,10 +149,8 @@ QString QFileSystemEntry::fileName() const { findLastSeparator(); #if defined(Q_OS_WIN) || defined(Q_OS_SYMBIAN) - if (m_lastSeparator == -1) { - if (m_filePath.length() >= 2 && m_filePath.at(1) == QLatin1Char(':')) - return m_filePath.mid(2); - } + if (m_lastSeparator == -1 && m_filePath.length() >= 2 && m_filePath.at(1) == QLatin1Char(':')) + return m_filePath.mid(2); #endif return m_filePath.mid(m_lastSeparator + 1); } @@ -176,6 +174,26 @@ QString QFileSystemEntry::path() const return m_filePath.left(m_lastSeparator); } +QString QFileSystemEntry::baseName() const +{ + findFileNameSeparators(); +#if defined(Q_OS_WIN) || defined(Q_OS_SYMBIAN) + if (m_lastSeparator == -1 && m_filePath.length() >= 2 && m_filePath.at(1) == QLatin1Char(':')) + return m_filePath.mid(2); +#endif + return m_filePath.mid(m_lastSeparator + 1, m_firstDotInFileName == -1 ?-1 : m_firstDotInFileName - 1); +} + +QString QFileSystemEntry::completeBaseName() const +{ + findFileNameSeparators(); +#if defined(Q_OS_WIN) || defined(Q_OS_SYMBIAN) + if (m_lastSeparator == -1 && m_filePath.length() >= 2 && m_filePath.at(1) == QLatin1Char(':')) + return m_filePath.mid(2); +#endif + return m_filePath.mid(m_lastSeparator + 1, m_firstDotInFileName == -1 ?-1 : m_firstDotInFileName + m_lastDotInFileName - 1); +} + QString QFileSystemEntry::suffix() const { findFileNameSeparators(); diff --git a/src/corelib/io/qfilesystementry_p.h b/src/corelib/io/qfilesystementry_p.h index 6b2cedd..5a41782 100644 --- a/src/corelib/io/qfilesystementry_p.h +++ b/src/corelib/io/qfilesystementry_p.h @@ -83,6 +83,8 @@ public: QString fileName() const; QString path() const; NativePath nativeFilePath() const; + QString baseName() const; + QString completeBaseName() const; QString suffix() const; QString completeSuffix() const; bool isAbsolute() const; diff --git a/tests/auto/qfilesystementry/tst_qfilesystementry.cpp b/tests/auto/qfilesystementry/tst_qfilesystementry.cpp index 5194c02..0fa63b5 100644 --- a/tests/auto/qfilesystementry/tst_qfilesystementry.cpp +++ b/tests/auto/qfilesystementry/tst_qfilesystementry.cpp @@ -65,6 +65,8 @@ void tst_QFileSystemEntry::getSetCheck_data() QTest::addColumn("internalnativeFilePath"); QTest::addColumn("filepath"); QTest::addColumn("filename"); + QTest::addColumn("baseName"); + QTest::addColumn("completeBasename"); QTest::addColumn("suffix"); QTest::addColumn("completeSuffix"); QTest::addColumn("absolute"); @@ -78,33 +80,33 @@ void tst_QFileSystemEntry::getSetCheck_data() << QString("A:\\home\\qt\\in\\a\\dir.tar.gz") << absPrefix + QString("A:\\home\\qt\\in\\a\\dir.tar.gz") << "A:/home/qt/in/a/dir.tar.gz" - << "dir.tar.gz" << "gz" << "tar.gz" << true; + << "dir.tar.gz" << "dir" << "dir.tar" << "gz" << "tar.gz" << true; QTest::newRow("relative") << QString("in\\a\\dir.tar.gz") << relPrefix + QString("in\\a\\dir.tar.gz") << "in/a/dir.tar.gz" - << "dir.tar.gz" << "gz" << "tar.gz" << false; + << "dir.tar.gz" << "dir" << "dir.tar" << "gz" << "tar.gz" << false; QTest::newRow("noSuffix") << QString("myDir\\myfile") << relPrefix + QString("myDir\\myfile") - << "myDir/myfile" << "myfile" << "" << "" << false; + << "myDir/myfile" << "myfile" << "myfile" << "myfile" << "" << "" << false; QTest::newRow("noLongSuffix") << QString("myDir\\myfile.txt") << relPrefix + QString("myDir\\myfile.txt") - << "myDir/myfile.txt" << "myfile.txt" << "txt" << "txt" << false; + << "myDir/myfile.txt" << "myfile.txt" << "myfile" << "myfile" << "txt" << "txt" << false; QTest::newRow("endingSlash") << QString("myDir\\myfile.bla\\") << relPrefix + QString("myDir\\myfile.bla\\") - << "myDir/myfile.bla/" << "" << "" << "" << false; + << "myDir/myfile.bla/" << "" << "" << "" << "" << "" << false; QTest::newRow("absolutePath") << QString("A:dir\\without\\leading\\backslash.bat") << absPrefix + QString("A:\\dir\\without\\leading\\backslash.bat") - << "A:dir/without/leading/backslash.bat" << "backslash.bat" << "bat" << "bat" << true; + << "A:dir/without/leading/backslash.bat" << "backslash.bat" << << "backslash" << "backslash" << "bat" << "bat" << true; } void tst_QFileSystemEntry::getSetCheck() @@ -113,6 +115,8 @@ void tst_QFileSystemEntry::getSetCheck() QFETCH(QString, internalnativeFilePath); QFETCH(QString, filepath); QFETCH(QString, filename); + QFETCH(QString, basename); + QFETCH(QString, completeBasename); QFETCH(QString, suffix); QFETCH(QString, completeSuffix); QFETCH(bool, absolute); @@ -125,6 +129,8 @@ void tst_QFileSystemEntry::getSetCheck() QCOMPARE(entry1.completeSuffix(), completeSuffix); QCOMPARE(entry1.isAbsolute(), absolute); QCOMPARE(entry1.isRelative(), !absolute); + QCOMPARE(entry1.baseName(), basename); + QCOMPARE(entry1.completeBaseName(), completeBasename); QFileSystemEntry entry2(nativeFilePath, QFileSystemEntry::FromNativePath()); QCOMPARE(entry2.suffix(), suffix); @@ -136,6 +142,8 @@ void tst_QFileSystemEntry::getSetCheck() // the object shouldnot change nativeFilePath. QCOMPARE(entry2.nativeFilePath(), nativeFilePath); QCOMPARE(entry2.fileName(), filename); + QCOMPARE(entry2.baseName(), basename); + QCOMPARE(entry2.completeBaseName(), completeBasename); } #else @@ -145,6 +153,8 @@ void tst_QFileSystemEntry::getSetCheck_data() QTest::addColumn("nativeFilePath"); QTest::addColumn("filepath"); QTest::addColumn("filename"); + QTest::addColumn("basename"); + QTest::addColumn("completeBasename"); QTest::addColumn("suffix"); QTest::addColumn("completeSuffix"); QTest::addColumn("absolute"); @@ -152,27 +162,27 @@ void tst_QFileSystemEntry::getSetCheck_data() QTest::newRow("simple") << QByteArray("/home/qt/in/a/dir.tar.gz") << "/home/qt/in/a/dir.tar.gz" - << "dir.tar.gz" << "gz" << "tar.gz" << true; + << "dir.tar.gz" << "dir" << "dir.tar" << "gz" << "tar.gz" << true; QTest::newRow("relative") << QByteArray("in/a/dir.tar.gz") << "in/a/dir.tar.gz" - << "dir.tar.gz" << "gz" << "tar.gz" << false; + << "dir.tar.gz" << "dir" << "dir.tar" << "gz" << "tar.gz" << false; QTest::newRow("noSuffix") << QByteArray("myDir/myfile") - << "myDir/myfile" << "myfile" << "" << "" << false; + << "myDir/myfile" << "myfile" << "myfile" << "myfile" << "" << "" << false; QTest::newRow("noLongSuffix") << QByteArray("myDir/myfile.txt") - << "myDir/myfile.txt" << "myfile.txt" << "txt" << "txt" << false; + << "myDir/myfile.txt" << "myfile.txt" << "myfile" << "myfile" << "txt" << "txt" << false; QTest::newRow("endingSlash") << QByteArray("myDir/myfile.bla/") - << "myDir/myfile.bla/" << "" << "" << "" << false; + << "myDir/myfile.bla/" << "" << "" << "" << "" << "" << false; QTest::newRow("relativePath") << QByteArray("A:dir/without/leading/backslash.bat") - << "A:dir/without/leading/backslash.bat" << "backslash.bat" << "bat" << "bat" << false; + << "A:dir/without/leading/backslash.bat" << "backslash.bat" << "backslash" << "backslash" << "bat" << "bat" << false; } void tst_QFileSystemEntry::getSetCheck() @@ -180,6 +190,8 @@ void tst_QFileSystemEntry::getSetCheck() QFETCH(QByteArray, nativeFilePath); QFETCH(QString, filepath); QFETCH(QString, filename); + QFETCH(QString, basename); + QFETCH(QString, completeBasename); QFETCH(QString, suffix); QFETCH(QString, completeSuffix); QFETCH(bool, absolute); @@ -192,6 +204,8 @@ void tst_QFileSystemEntry::getSetCheck() QCOMPARE(entry1.completeSuffix(), completeSuffix); QCOMPARE(entry1.isAbsolute(), absolute); QCOMPARE(entry1.isRelative(), !absolute); + QCOMPARE(entry1.baseName(), basename); + QCOMPARE(entry1.completeBaseName(), completeBasename); QFileSystemEntry entry2(nativeFilePath, QFileSystemEntry::FromNativePath()); QCOMPARE(entry2.suffix(), suffix); @@ -201,6 +215,8 @@ void tst_QFileSystemEntry::getSetCheck() QCOMPARE(entry2.filePath(), filepath); QCOMPARE(entry2.nativeFilePath(), nativeFilePath); QCOMPARE(entry2.fileName(), filename); + QCOMPARE(entry2.baseName(), basename); + QCOMPARE(entry2.completeBaseName(), completeBasename); } #endif -- cgit v0.12