Add FileName isChildOf/relativePath and endsWith and appendPath

Needed by qtversionmanager

Change-Id: I4d455298e809b744ae3663493914db6e31f372a6
Reviewed-by: Tobias Hunger <tobias.hunger@nokia.com>
Reviewed-by: Daniel Teske <daniel.teske@nokia.com>
This commit is contained in:
Daniel Teske
2011-11-25 13:17:52 +01:00
parent a082365d65
commit e9c079a648
3 changed files with 78 additions and 22 deletions

View File

@@ -407,6 +407,14 @@ TempFileSaver::~TempFileSaver()
QFile::remove(m_fileName); QFile::remove(m_fileName);
} }
/*! \class Utils::FileName
\brief A light-weight convenience class for filenames
On windows filenames are compared case insensitively.
*/
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
Qt::CaseSensitivity FileName::cs = Qt::CaseInsensitive; Qt::CaseSensitivity FileName::cs = Qt::CaseInsensitive;
#else #else
@@ -419,31 +427,62 @@ FileName::FileName()
} }
/// Constructs a FileName from \a info
FileName::FileName(const QFileInfo &info) FileName::FileName(const QFileInfo &info)
: QString(info.absoluteFilePath()) : QString(info.absoluteFilePath())
{ {
} }
/// \returns a QFileInfo
QFileInfo FileName::toFileInfo() const
{
return QFileInfo(*this);
}
/// \returns a QString for passing on to QString based APIs
QString FileName::toString() const QString FileName::toString() const
{ {
return QString(*this); return QString(*this);
} }
/// \returns a QString to display to the user
/// Converts the separators to the native format
QString FileName::toUserOutput() const
{
return QDir::toNativeSeparators(toString());
}
/// Constructs a FileName from \a fileName
/// \a fileName is not checked for validity.
FileName FileName::fromString(const QString &filename) FileName FileName::fromString(const QString &filename)
{ {
return FileName(filename); return FileName(filename);
} }
/// Constructs a FileName from \a fileName
/// \a fileName is only passed through QDir::cleanPath
/// and QDir::fromNativeSeparators
FileName FileName::fromUserInput(const QString &filename)
{
return FileName(QDir::cleanPath(QDir::fromNativeSeparators(filename)));
}
FileName::FileName(const QString &string) FileName::FileName(const QString &string)
: QString(string) : QString(string)
{ {
} }
bool FileName::operator==(const FileName &other) const bool FileName::operator==(const FileName &other) const
{ {
return QString::compare(*this, other, cs) == 0; return QString::compare(*this, other, cs) == 0;
} }
bool FileName::operator!=(const FileName &other) const
{
return !(*this == other);
}
bool FileName::operator<(const FileName &other) const bool FileName::operator<(const FileName &other) const
{ {
return QString::compare(*this, other, cs) < 0; return QString::compare(*this, other, cs) < 0;
@@ -464,29 +503,38 @@ bool FileName::operator>=(const FileName &other) const
return other <= *this; return other <= *this;
} }
bool FileName::startsWith(const QString &s) const /// \returns whether FileName is a child of \a s
bool FileName::isChildOf(const FileName &s) const
{ {
return QString::startsWith(s, cs); if (!QString::startsWith(s, cs))
return false;
if (size() <= s.size())
return false;
return at(s.size()) == '/';
} }
/// \returns whether FileName endsWith \a s
bool FileName::endsWith(const QString &s) const bool FileName::endsWith(const QString &s) const
{ {
return QString::endsWith(s, cs); return QString::endsWith(s, cs);
} }
FileName FileName::left(int n) const /// \returns the relativeChildPath of FileName to parent if FileName is a child of parent
/// \note returns a empty FileName if FileName is not a child of parent
/// That is, this never returns a path starting with "../"
FileName FileName::relativeChildPath(const FileName &parent) const
{ {
return FileName(QString::left(n)); if (!isChildOf(parent))
return Utils::FileName();
return FileName(QString::mid(parent.size() + 1, -1));
} }
FileName FileName::mid(int position, int n) const /// Appends \a s, ensuring a / between the parts
void FileName::appendPath(const QString &s)
{ {
return FileName(QString::mid(position, n)); if (QString::endsWith(QLatin1Char('/')))
} append(QLatin1Char('/'));
append(s);
FileName FileName::right(int n) const
{
return FileName(QString::right(n));
} }
} // namespace Utils } // namespace Utils

View File

@@ -39,6 +39,7 @@
#include <QtCore/QIODevice> #include <QtCore/QIODevice>
#include <QtCore/QXmlStreamWriter> // Mac. #include <QtCore/QXmlStreamWriter> // Mac.
#include <QtCore/QFileInfo> #include <QtCore/QFileInfo>
#include <QtCore/QMetaType>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QFile; class QFile;
@@ -145,26 +146,32 @@ class QTCREATOR_UTILS_EXPORT FileName : private QString
public: public:
FileName(); FileName();
explicit FileName(const QFileInfo &info); explicit FileName(const QFileInfo &info);
QString toString() const; QFileInfo toFileInfo() const;
static FileName fromString(const QString &filename); static FileName fromString(const QString &filename);
static FileName fromUserInput(const QString &filename);
QString toString() const;
QString toUserOutput() const;
bool operator==(const FileName &other) const; bool operator==(const FileName &other) const;
bool operator!=(const FileName &other) const;
bool operator<(const FileName &other) const; bool operator<(const FileName &other) const;
bool operator<=(const FileName &other) const; bool operator<=(const FileName &other) const;
bool operator>(const FileName &other) const; bool operator>(const FileName &other) const;
bool operator>=(const FileName &other) const; bool operator>=(const FileName &other) const;
bool startsWith(const QString &s) const; bool isChildOf(const FileName &s) const;
bool endsWith(const QString &s) const; bool endsWith(const QString &s) const;
FileName left(int n) const Q_REQUIRED_RESULT; Utils::FileName relativeChildPath(const FileName &parent) const;
FileName mid(int position, int n = -1) const Q_REQUIRED_RESULT; void appendPath(const QString &s);
FileName right(int n) const Q_REQUIRED_RESULT;
using QString::size; using QString::size;
using QString::count; using QString::count;
using QString::length; using QString::length;
using QString::isEmpty; using QString::isEmpty;
using QString::isNull;
using QString::clear;
using QString::append;
private: private:
static Qt::CaseSensitivity cs; static Qt::CaseSensitivity cs;
FileName(const QString &string); FileName(const QString &string);
@@ -176,4 +183,6 @@ QT_BEGIN_NAMESPACE
QTCREATOR_UTILS_EXPORT uint qHash(const Utils::FileName &a); QTCREATOR_UTILS_EXPORT uint qHash(const Utils::FileName &a);
QT_END_NAMESPACE QT_END_NAMESPACE
Q_DECLARE_METATYPE(Utils::FileName)
#endif // FILEUTILS_H #endif // FILEUTILS_H

View File

@@ -309,14 +309,13 @@ struct InternalNode
void create(const QString &projectDir, const QSet<Utils::FileName> &newFilePaths, ProjectExplorer::FileType type) void create(const QString &projectDir, const QSet<Utils::FileName> &newFilePaths, ProjectExplorer::FileType type)
{ {
static const QChar separator = QChar('/'); static const QChar separator = QChar('/');
const QString projectDirWithSeparator = projectDir + separator; const Utils::FileName projectDirFileName = Utils::FileName::fromString(projectDir);
int projectDirWithSeparatorLength = projectDirWithSeparator.length();
foreach (const Utils::FileName &file, newFilePaths) { foreach (const Utils::FileName &file, newFilePaths) {
Utils::FileName fileWithoutPrefix; Utils::FileName fileWithoutPrefix;
bool isRelative; bool isRelative;
if (file.startsWith(projectDirWithSeparator)) { if (file.isChildOf(projectDirFileName)) {
isRelative = true; isRelative = true;
fileWithoutPrefix = file.mid(projectDirWithSeparatorLength); fileWithoutPrefix = file.relativeChildPath(projectDirFileName);
} else { } else {
isRelative = false; isRelative = false;
fileWithoutPrefix = file; fileWithoutPrefix = file;
@@ -328,7 +327,7 @@ struct InternalNode
#endif #endif
QStringListIterator it(parts); QStringListIterator it(parts);
InternalNode *currentNode = this; InternalNode *currentNode = this;
QString path = (isRelative ? projectDirWithSeparator : ""); QString path = (isRelative ? (projectDirFileName.toString() + '/') : QString(""));
while (it.hasNext()) { while (it.hasNext()) {
const QString &key = it.next(); const QString &key = it.next();
if (it.hasNext()) { // key is directory if (it.hasNext()) { // key is directory
@@ -683,7 +682,7 @@ void Qt4PriFileNode::folderChanged(const QString &folder)
newFiles += recursiveEnumerate(changedFolder); newFiles += recursiveEnumerate(changedFolder);
foreach (const Utils::FileName &file, m_recursiveEnumerateFiles) { foreach (const Utils::FileName &file, m_recursiveEnumerateFiles) {
if (!file.startsWith(changedFolder)) if (!file.isChildOf(Utils::FileName::fromString(changedFolder)))
newFiles.insert(file); newFiles.insert(file);
} }