From a5c215ac09e9ede853024fc69a0b74a7af820ea4 Mon Sep 17 00:00:00 2001 From: Thomas Zander Date: Tue, 24 Aug 2010 13:54:05 +0200 Subject: Add some more methods to the QFileSystemEntry --- src/corelib/io/qfilesystementry.cpp | 87 ++++++++++++++++++++++++++++++++++--- src/corelib/io/qfilesystementry_p.h | 15 +++++-- 2 files changed, 94 insertions(+), 8 deletions(-) diff --git a/src/corelib/io/qfilesystementry.cpp b/src/corelib/io/qfilesystementry.cpp index 1cc82ef..3e8b8b2 100644 --- a/src/corelib/io/qfilesystementry.cpp +++ b/src/corelib/io/qfilesystementry.cpp @@ -44,20 +44,26 @@ QFileSystemEntry::QFileSystemEntry(const QString &filePath) : m_filePath(filePath), - m_lastSeparator(-2) + m_lastSeparator(-2), + m_firstDotInFileName(-2), + m_lastDotInFileName(0) { } QFileSystemEntry::QFileSystemEntry(const QByteArray &nativeFilePath) : m_nativeFilePath(nativeFilePath), - m_lastSeparator(-2) + m_lastSeparator(-2), + m_firstDotInFileName(-2), + m_lastDotInFileName(0) { } QFileSystemEntry::QFileSystemEntry(const QByteArray &nativeFilePath, const QString &filePath) : m_filePath(filePath), m_nativeFilePath(nativeFilePath), - m_lastSeparator(-2) + m_lastSeparator(-2), + m_firstDotInFileName(-2), + m_lastDotInFileName(0) { } @@ -83,7 +89,7 @@ void QFileSystemEntry::resolveFilePath() const void QFileSystemEntry::resolveNativeFilePath() const { if (!m_filePath.isEmpty() && m_nativeFilePath.isEmpty()) { - m_nativeFilePath = m_filePath.toLocal8Bit(); + m_nativeFilePath = QDir::toNativeSeparators(m_filePath).toLocal8Bit(); } } @@ -93,10 +99,37 @@ QString QFileSystemEntry::fileName() const return m_filePath.mid(m_lastSeparator + 1); } -void QFileSystemEntry::findLastSeparator() const +QString QFileSystemEntry::suffix() const +{ + findFileNameSeparators(); + + if (m_lastDotInFileName == -1) + return QString(); + + return m_filePath.mid(m_lastSeparator + m_lastDotInFileName + 1); +} + +QString QFileSystemEntry::completeSuffix() const +{ + findFileNameSeparators(); + if (m_firstDotInFileName == -1) + return QString(); + + return m_filePath.mid(m_lastSeparator + m_firstDotInFileName + 1); +} + +bool QFileSystemEntry::isAbsolute() const { resolveFilePath(); + return !m_filePath.isEmpty() && (m_filePath[0].unicode() == '/' /*|| hasScheme()*/); +} + +// private methods + +void QFileSystemEntry::findLastSeparator() const +{ if (m_lastSeparator == -2) { + resolveFilePath(); m_lastSeparator = -1; for (int i = m_filePath.size() - 1; i >= 0; --i) { if (m_filePath[i].unicode() == '/') { @@ -106,3 +139,47 @@ void QFileSystemEntry::findLastSeparator() const } } } + +void QFileSystemEntry::findFileNameSeparators() const +{ + if (m_firstDotInFileName == -2) { + resolveFilePath(); + int firstDotInFileName = -1; + int lastDotInFileName = -1; + int lastSeparator = m_lastSeparator; + + int stop; + if (lastSeparator < 0) { + lastSeparator = -1; + stop = 0; + } else { + stop = lastSeparator; + } + + int i = m_filePath.size() - 1; + for (; i >= stop; --i) { + if (m_filePath[i].unicode() == '.') { + firstDotInFileName = lastDotInFileName = i; + break; + } else if (m_filePath[i].unicode() == '/') { + lastSeparator = i; + break; + } + } + + if (lastSeparator != i) { + for (--i; i >= stop; --i) { + if (m_filePath[i].unicode() == '.') + firstDotInFileName = i; + else if (m_filePath[i].unicode() == '/') { + lastSeparator = i; + break; + } + } + } + + m_lastSeparator = lastSeparator; + m_firstDotInFileName = firstDotInFileName == -1 ? -1 : firstDotInFileName - lastSeparator; + m_lastDotInFileName = lastDotInFileName == -1 ? -1 : lastDotInFileName - firstDotInFileName - lastSeparator; + } +} diff --git a/src/corelib/io/qfilesystementry_p.h b/src/corelib/io/qfilesystementry_p.h index 3e67174..1812b9f 100644 --- a/src/corelib/io/qfilesystementry_p.h +++ b/src/corelib/io/qfilesystementry_p.h @@ -67,20 +67,29 @@ class QFileSystemEntry QString filePath() const; QString fileName() const; QByteArray nativeFileName() const; + QString suffix() const; + QString completeSuffix() const; + bool isAbsolute() const; + bool isRelative() const { + return !isAbsolute(); + } private: // creates the QString version out of the bytearray version void resolveFilePath() const; // creates the bytearray version out of the QString version void resolveNativeFilePath() const; + // resolves the separator void findLastSeparator() const; + /// resolves the dots and the separator + void findFileNameSeparators() const; mutable QString m_filePath; // always has slashes as separator mutable QByteArray m_nativeFilePath; // native encoding and separators - mutable int m_lastSeparator : 16; - - int dummy : 16; + mutable int m_lastSeparator : 16; // index in m_filePath of last separator + mutable int m_firstDotInFileName : 11; // index after m_filePath for first dot (.) + mutable int m_lastDotInFileName : 5; // index after m_firstDotInFileName for last dot (.) }; QT_END_NAMESPACE -- cgit v0.12