2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
|
|
|
|
** Contact: http://www.qt.io/licensing
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2015-01-14 18:07:15 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms and
|
|
|
|
|
** conditions see http://www.qt.io/terms-conditions. For further information
|
2014-10-01 13:21:18 +02:00
|
|
|
** use the contact form at http://www.qt.io/contact-us.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2014-10-01 13:21:18 +02:00
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2012-10-02 09:12:39 +02:00
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** In addition, as a special exception, The Qt Company gives you certain additional
|
|
|
|
|
** rights. These rights are described in The Qt Company LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2008-12-02 14:09:21 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#ifndef FILESEARCH_H
|
|
|
|
|
#define FILESEARCH_H
|
|
|
|
|
|
|
|
|
|
#include "utils_global.h"
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QFuture>
|
|
|
|
|
#include <QMap>
|
|
|
|
|
#include <QStack>
|
|
|
|
|
#include <QDir>
|
|
|
|
|
#include <QTextDocument>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2011-08-17 12:54:58 +02:00
|
|
|
QT_FORWARD_DECLARE_CLASS(QTextCodec)
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
namespace Utils {
|
|
|
|
|
|
2010-06-24 15:44:46 +02:00
|
|
|
class QTCREATOR_UTILS_EXPORT FileIterator
|
|
|
|
|
{
|
|
|
|
|
public:
|
2015-03-23 15:19:42 +01:00
|
|
|
class Item
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
Item(const QString &path, QTextCodec *codec)
|
|
|
|
|
: filePath(path), encoding(codec)
|
|
|
|
|
{}
|
|
|
|
|
QString filePath;
|
|
|
|
|
QTextCodec *encoding;
|
|
|
|
|
};
|
2010-06-24 15:44:46 +02:00
|
|
|
|
2015-12-16 14:58:04 +01:00
|
|
|
typedef Item value_type;
|
|
|
|
|
|
2015-03-23 15:19:42 +01:00
|
|
|
class const_iterator
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
typedef std::forward_iterator_tag iterator_category;
|
|
|
|
|
typedef Item value_type;
|
|
|
|
|
typedef std::ptrdiff_t difference_type;
|
|
|
|
|
typedef const value_type *pointer;
|
|
|
|
|
typedef const value_type &reference;
|
|
|
|
|
|
2015-12-16 14:58:04 +01:00
|
|
|
const_iterator(const FileIterator *parent, Item item, int id)
|
2015-03-23 15:19:42 +01:00
|
|
|
: m_parent(parent), m_item(item), m_index(id)
|
|
|
|
|
{}
|
|
|
|
|
const Item operator*() const { return m_item; }
|
|
|
|
|
const Item *operator->() const { return &m_item; }
|
2015-12-16 14:58:04 +01:00
|
|
|
void operator++() { m_parent->advance(this); }
|
2015-03-23 15:19:42 +01:00
|
|
|
bool operator==(const const_iterator &other) const
|
|
|
|
|
{
|
|
|
|
|
return m_parent == other.m_parent && m_index == other.m_index;
|
|
|
|
|
}
|
|
|
|
|
bool operator!=(const const_iterator &other) const { return !operator==(other); }
|
|
|
|
|
|
2015-12-16 14:58:04 +01:00
|
|
|
const FileIterator *m_parent;
|
2015-03-23 15:19:42 +01:00
|
|
|
Item m_item;
|
|
|
|
|
int m_index; // -1 == end
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
virtual ~FileIterator() {}
|
2015-12-16 14:58:04 +01:00
|
|
|
void advance(const_iterator *it) const;
|
|
|
|
|
const_iterator begin() const;
|
|
|
|
|
const_iterator end() const;
|
2015-03-23 15:19:42 +01:00
|
|
|
|
|
|
|
|
virtual int maxProgress() const = 0;
|
|
|
|
|
virtual int currentProgress() const = 0;
|
2010-06-24 15:44:46 +02:00
|
|
|
|
2013-10-14 16:33:18 +02:00
|
|
|
protected:
|
2015-03-23 15:19:42 +01:00
|
|
|
virtual void update(int requestedIndex) = 0;
|
|
|
|
|
virtual int currentFileCount() const = 0;
|
|
|
|
|
virtual QString fileAt(int index) const = 0;
|
|
|
|
|
virtual QTextCodec *codecAt(int index) const = 0;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class QTCREATOR_UTILS_EXPORT FileListIterator : public FileIterator
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
explicit FileListIterator(const QStringList &fileList,
|
|
|
|
|
const QList<QTextCodec *> encodings);
|
|
|
|
|
|
|
|
|
|
int maxProgress() const override;
|
|
|
|
|
int currentProgress() const override;
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
void update(int requestedIndex) override;
|
|
|
|
|
int currentFileCount() const override;
|
|
|
|
|
QString fileAt(int index) const override;
|
|
|
|
|
QTextCodec *codecAt(int index) const override;
|
2013-10-14 16:33:18 +02:00
|
|
|
|
2010-06-24 15:44:46 +02:00
|
|
|
private:
|
2015-03-23 15:19:42 +01:00
|
|
|
QTextCodec *encodingAt(int index) const;
|
|
|
|
|
QStringList m_files;
|
2010-10-11 11:34:17 +02:00
|
|
|
QList<QTextCodec *> m_encodings;
|
2015-03-23 15:19:42 +01:00
|
|
|
int m_maxIndex;
|
2010-06-24 15:44:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class QTCREATOR_UTILS_EXPORT SubDirFileIterator : public FileIterator
|
|
|
|
|
{
|
|
|
|
|
public:
|
2010-10-11 11:34:17 +02:00
|
|
|
SubDirFileIterator(const QStringList &directories, const QStringList &filters,
|
|
|
|
|
QTextCodec *encoding = 0);
|
2010-06-24 15:44:46 +02:00
|
|
|
|
2015-03-23 15:19:42 +01:00
|
|
|
int maxProgress() const override;
|
|
|
|
|
int currentProgress() const override;
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
void update(int requestedIndex) override;
|
|
|
|
|
int currentFileCount() const override;
|
|
|
|
|
QString fileAt(int index) const override;
|
|
|
|
|
QTextCodec *codecAt(int index) const override;
|
2010-06-24 15:44:46 +02:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
QStringList m_filters;
|
2010-10-11 11:34:17 +02:00
|
|
|
QTextCodec *m_encoding;
|
2015-03-23 15:19:42 +01:00
|
|
|
QStack<QDir> m_dirs;
|
|
|
|
|
QStack<qreal> m_progressValues;
|
|
|
|
|
QStack<bool> m_processedValues;
|
|
|
|
|
qreal m_progress;
|
|
|
|
|
QStringList m_files;
|
2010-06-24 15:44:46 +02:00
|
|
|
};
|
|
|
|
|
|
2009-05-08 12:09:21 +02:00
|
|
|
class QTCREATOR_UTILS_EXPORT FileSearchResult
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
FileSearchResult() {}
|
2010-06-11 08:57:38 +02:00
|
|
|
FileSearchResult(QString fileName, int lineNumber, QString matchingLine,
|
|
|
|
|
int matchStart, int matchLength,
|
|
|
|
|
QStringList regexpCapturedTexts)
|
|
|
|
|
: fileName(fileName),
|
|
|
|
|
lineNumber(lineNumber),
|
|
|
|
|
matchingLine(matchingLine),
|
|
|
|
|
matchStart(matchStart),
|
|
|
|
|
matchLength(matchLength),
|
|
|
|
|
regexpCapturedTexts(regexpCapturedTexts)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
QString fileName;
|
|
|
|
|
int lineNumber;
|
|
|
|
|
QString matchingLine;
|
|
|
|
|
int matchStart;
|
|
|
|
|
int matchLength;
|
2010-06-11 08:57:38 +02:00
|
|
|
QStringList regexpCapturedTexts;
|
2008-12-02 12:01:29 +01:00
|
|
|
};
|
|
|
|
|
|
2010-06-25 09:16:30 +02:00
|
|
|
typedef QList<FileSearchResult> FileSearchResultList;
|
|
|
|
|
|
|
|
|
|
QTCREATOR_UTILS_EXPORT QFuture<FileSearchResultList> findInFiles(const QString &searchTerm, FileIterator *files,
|
2009-08-05 12:43:47 +02:00
|
|
|
QTextDocument::FindFlags flags, QMap<QString, QString> fileToContentsMap = QMap<QString, QString>());
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-06-25 09:16:30 +02:00
|
|
|
QTCREATOR_UTILS_EXPORT QFuture<FileSearchResultList> findInFilesRegExp(const QString &searchTerm, FileIterator *files,
|
2009-08-05 12:43:47 +02:00
|
|
|
QTextDocument::FindFlags flags, QMap<QString, QString> fileToContentsMap = QMap<QString, QString>());
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-06-11 08:57:38 +02:00
|
|
|
QTCREATOR_UTILS_EXPORT QString expandRegExpReplacement(const QString &replaceText, const QStringList &capturedTexts);
|
2012-11-30 16:15:07 +01:00
|
|
|
QTCREATOR_UTILS_EXPORT QString matchCaseReplacement(const QString &originalText, const QString &replaceText);
|
2010-06-11 08:57:38 +02:00
|
|
|
|
2008-12-02 14:09:21 +01:00
|
|
|
} // namespace Utils
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2008-12-02 14:09:21 +01:00
|
|
|
#endif // FILESEARCH_H
|