From aa31f4ba80da61a6c63664253cba77b66491e486 Mon Sep 17 00:00:00 2001 From: Hamatoma Date: Fri, 25 Nov 2016 00:58:51 +0100 Subject: [PATCH] ReSearch: next / previous page, HitPosition --- appl/research/filecache.cpp | 2 ++ appl/research/filefilter.cpp | 50 +++++++++++++++++++++------- appl/research/filefilter.hpp | 20 +++++++++++- appl/research/filefinder.cpp | 45 ++++++++++++-------------- appl/research/mainwindow.cpp | 57 +++++++++++++++++++++++++++++--- appl/research/mainwindow.hpp | 5 +++ appl/research/mainwindow.ui | 19 ++++++++--- base/ReFileSearch.cpp | 8 ++++- base/ReStringUtils.cpp | 3 +- cunit/cuReFileSearch.cpp | 63 ++++++++++++++++++++++++++++++++++++ cunit/cuReQStringUtils.cpp | 3 +- 11 files changed, 226 insertions(+), 49 deletions(-) diff --git a/appl/research/filecache.cpp b/appl/research/filecache.cpp index 0f9e343..f65cee9 100644 --- a/appl/research/filecache.cpp +++ b/appl/research/filecache.cpp @@ -101,6 +101,8 @@ const QStringList &FileCache::lines(const QString &filename) const */ void FileCache::remove(const QString &filename) { + FileCacheItem* item = m_cache.value(filename); + delete item; m_cache.remove(filename); } diff --git a/appl/research/filefilter.cpp b/appl/research/filefilter.cpp index 0a2f3c1..2057757 100644 --- a/appl/research/filefilter.cpp +++ b/appl/research/filefilter.cpp @@ -6,7 +6,8 @@ * @param table table containing the files * @param list list widget to store the filtered lines */ -FileFilter::FileFilter(QTableWidget &table, QListWidget &list, FileCache& cache) : +FileFilter::FileFilter(QTableWidget &table, QListWidget &list, FileCache& cache, + HitPosition& lastHitPosition) : m_table(table), m_list(list), m_cache(cache), @@ -16,12 +17,22 @@ FileFilter::FileFilter(QTableWidget &table, QListWidget &list, FileCache& cache) m_fromHit(0), m_toHit(0), m_linesAbove(0), - m_linesBelow(0) - + m_linesBelow(0), + m_lastHitPosition(lastHitPosition) { } +/** + * Destructor. + */ +FileFilter::~FileFilter() +{ + delete m_excludeExpr; + delete m_includeExpr; + m_includeExpr = m_excludeExpr = NULL; +} + /** * Puts all filtered lines from the files in the table into the list. * @@ -63,13 +74,16 @@ void FileFilter::filter(const QString &includePattern, hitNo = filterOneFile(full, hitNo); } + if (hitNo <= toHit){ + m_lastHitPosition.m_hitCount = m_lastHitPosition.m_hitNo; + } } /** * Filters the lines of one file. * - * @param filename filename with path - * @param lastHit the last found hit in the previous files + * @param filename filename with path + * @param lastHit the last found hit in the previous files * @return the number of hits including the hits in the file */ int FileFilter::filterOneFile(const QString& filename, int lastHit) @@ -77,24 +91,38 @@ int FileFilter::filterOneFile(const QString& filename, int lastHit) m_cache.addOrUpdate(filename); const QStringList& lines = m_cache.lines(filename); - int lineNo = 0; + int lineNo = -1; int lastLine = -1; - int count = lines.size(); + int lastIx = lines.size() - 1; QString prefixHit = filename + "-"; QString prefixOther = prefixHit; if (m_linesAbove + m_linesBelow > 0){ prefixHit = ">" + prefixHit; prefixOther = " " + prefixOther; } - while(lastHit <= m_toHit && lineNo < count){ - QString line = lines[lineNo++]; + bool first = true; + if (m_lastHitPosition.m_filename == filename + && m_fromHit + 1 == m_lastHitPosition.m_hitNo){ + // we start from the stored position + first = false; + lastHit = m_lastHitPosition.m_hitNo; + lineNo = m_lastHitPosition.m_line; + } + while(lastHit <= m_toHit && lineNo < lastIx){ + QString line = lines[++lineNo]; if (m_includeExpr != NULL && ! m_includeExpr->match(line).hasMatch()) continue; if (m_excludeExpr != NULL && m_excludeExpr->match(line).hasMatch()) continue; - if (++lastHit >= m_fromHit){ + if (first){ + m_lastHitPosition.m_filename = filename; + first = false; + } + m_lastHitPosition.m_line = lineNo; + m_lastHitPosition.m_hitNo = ++lastHit; + if (lastHit >= m_fromHit){ for (int ix = max(lastLine + 1, max(0, lineNo - m_linesAbove)); - ix <= min(lineNo + m_linesBelow, count - 1); + ix <= min(lineNo + m_linesBelow, lastIx); ix++){ m_list.addItem((ix == lineNo ? prefixHit : prefixOther) + QString::number(ix + 1) + ": " + lines[ix]); diff --git a/appl/research/filefilter.hpp b/appl/research/filefilter.hpp index 2cf113c..827d625 100644 --- a/appl/research/filefilter.hpp +++ b/appl/research/filefilter.hpp @@ -2,10 +2,27 @@ #define FILEFILTER_H +class HitPosition { +public: + HitPosition() : + m_filename(), + m_line(0), + m_hitNo(0), + m_hitCount(-1) + {} +public: + QString m_filename; + int m_line; + int m_hitNo; + int m_hitCount; +}; + class FileFilter { public: - FileFilter(QTableWidget& table, QListWidget& list, FileCache& cache); + FileFilter(QTableWidget& table, QListWidget& list, FileCache& cache, + HitPosition& lastHitPosition); + virtual ~FileFilter(); public: void filter(const QString& includePattern, const QString& excludePattern, int fromHit, int toHit, int linesAbove, int linesBelow, @@ -23,6 +40,7 @@ private: int m_toHit; int m_linesAbove; int m_linesBelow; + HitPosition& m_lastHitPosition; }; #endif // FILEFILTER_H diff --git a/appl/research/filefinder.cpp b/appl/research/filefinder.cpp index 028caa9..34ff2d6 100644 --- a/appl/research/filefinder.cpp +++ b/appl/research/filefinder.cpp @@ -29,7 +29,10 @@ FileFinder::FileFinder(QTableWidget& table, FileCache& cache) : */ FileFinder::~FileFinder() { - + delete m_regularExpression; + delete m_text; + m_regularExpression = NULL; + m_text = NULL; } /** @@ -125,7 +128,7 @@ int FileFinder::ignoredFiles() const bool FileFinder::handleFile(const QString &full, const QString &path, const QString &node, const QFileInfo& info) { - bool found = false; + bool found = true; if (m_regularExpression != NULL){ found = searchWithRegExpr(full); } else if (m_text != NULL){ @@ -152,29 +155,23 @@ bool FileFinder::searchText(const QString &filename) m_cache.addOrUpdate(filename); const QStringList& lines = m_cache.lines(filename); bool rc = false; - if (m_caseSensitive){ - if (m_inverseSearch){ - rc = true; - for (int ix = 0; ix < lines.size(); ++ix){ - if (lines[ix].indexOf(m_text) < 0){ - rc = false; - break; - } - } - } else { - rc = false; - for (int ix = 0; ix < lines.size(); ++ix){ - if (lines[ix].indexOf(m_text) >= 0){ - rc = true; - break; - } - } - } + Qt::CaseSensitivity mode = m_caseSensitive + ? Qt::CaseSensitive : Qt::CaseInsensitive; + if (m_inverseSearch){ + rc = true; + for (int ix = 0; ix < lines.size(); ++ix){ + if (lines[ix].indexOf(m_text, mode) >= 0){ + rc = false; + break; + } + } } else { - if (m_inverseSearch){ - - } else { - + rc = false; + for (int ix = 0; ix < lines.size(); ++ix){ + if (lines[ix].indexOf(m_text, mode) >= 0){ + rc = true; + break; + } } } if (! rc) diff --git a/appl/research/mainwindow.cpp b/appl/research/mainwindow.cpp index 9639c1d..d211ddb 100644 --- a/appl/research/mainwindow.cpp +++ b/appl/research/mainwindow.cpp @@ -14,7 +14,8 @@ MainWindow::MainWindow(QApplication& application, const QString& homeDir, ui(new Ui::MainWindow), m_fileFinder(NULL), m_fileCache(new FileCache()), - m_test(true) + m_test(true), + m_lastHitPosition(new HitPosition()) { ReComboBox::setDefaultHistorySize(20); initializeGui(); @@ -28,6 +29,7 @@ MainWindow::~MainWindow() delete ui; delete m_fileCache; delete m_fileFinder; + delete m_lastHitPosition; } /** @@ -65,7 +67,17 @@ void MainWindow::initializeGui(){ connect(ui->pushButtonClear, SIGNAL(clicked()), this, SLOT(onClear())); connect(ui->pushButtonFilter, SIGNAL(clicked()), this, SLOT(onFilter())); connect(ui->pushButtonDetach, SIGNAL(clicked()), this, SLOT(onDetach())); + connect(ui->pushButtonPrevious, SIGNAL(clicked()), this, SLOT(onPrevious())); + connect(ui->pushButtonNext, SIGNAL(clicked()), this, SLOT(onNext())); + connect(ui->comboBoxExcludingPattern, SIGNAL(textEdited(QString,QString)), + this, SLOT(clearLastHitPosition(const QString&, const QString&))); + connect(ui->comboBoxIncludingPattern, SIGNAL(textEdited(QString,QString)), + this, SLOT(clearLastHitPosition(const QString&, const QString&))); + connect(ui->comboBoxLinesAbove, SIGNAL(textEdited(QString,QString)), + this, SLOT(clearLastHitPosition(const QString&, const QString&))); + connect(ui->comboBoxLinesBelow, SIGNAL(textEdited(QString,QString)), + this, SLOT(clearLastHitPosition(const QString&, const QString&))); delete m_fileFinder; m_fileFinder = new FileFinder(*ui->tableWidget, *m_fileCache); @@ -76,11 +88,18 @@ void MainWindow::initializeGui(){ if (m_test){ ui->comboBoxBaseDirectory->setCurrentText("/etc"); ui->comboBoxFilePatterns->setCurrentText("*asu*"); - ui->comboBoxIncludingPattern->setCurrentText("e"); + ui->comboBoxIncludingPattern->setCurrentText("#!"); onAdd(); } } +void MainWindow::clearLastHitPosition(const QString&, const QString&) +{ + m_lastHitPosition->m_filename.clear(); + m_lastHitPosition->m_hitCount = -1; + m_lastHitPosition->m_hitNo = -1; +} + /** * Shows the "about" window. */ @@ -128,7 +147,8 @@ void MainWindow::onClear() */ void MainWindow::onFilter() { - FileFilter filter(*ui->tableWidget, *ui->listWidgetHits, *m_fileCache); + FileFilter filter(*ui->tableWidget, *ui->listWidgetHits, *m_fileCache, + *m_lastHitPosition); int from = comboboxToInt(*ui->comboBoxFromHit, 0); int to = comboboxToInt(*ui->comboBoxToHit, from + 100); int above = comboboxToInt(*ui->comboBoxLinesAbove, 0); @@ -155,6 +175,33 @@ void MainWindow::onLanguageChange() initializeGui(); } +/** + * Shows the next page of filtered lines. + */ +void MainWindow::onNext() +{ + int first = comboInt(ui->comboBoxFromHit, 1); + int last = comboInt(ui->comboBoxToHit, first + 100); + int pageSize = last - first + 1; + first = first + pageSize; + ui->comboBoxFromHit->setCurrentText(QString::number(first)); + ui->comboBoxToHit->setCurrentText(QString::number(first + pageSize - 1)); + onFilter(); +} +/** + * Shows the previous page of filtered lines. + */ +void MainWindow::onPrevious() +{ + int first = comboInt(ui->comboBoxFromHit, 1); + int last = comboInt(ui->comboBoxToHit, first + 100); + int pageSize = last - first + 1; + first = max(1, first - pageSize); + ui->comboBoxFromHit->setCurrentText(QString::number(first)); + ui->comboBoxToHit->setCurrentText(QString::number(first + pageSize - 1)); + onFilter(); +} + /** * Handles the push of the button "select directory". */ @@ -191,9 +238,9 @@ void MainWindow::searchFiles(bool addNotDetach) } else { QDateTime olderThan = comboDate(ui->comboBoxOlder); QDateTime youngerThan = comboDate(ui->comboBoxYounger); - m_fileFinder->setOlderThan(olderThan.currentMSecsSinceEpoch() <= 0 + m_fileFinder->setOlderThan(olderThan.toMSecsSinceEpoch() <= 0 ? NULL : new QDateTime(olderThan)); - m_fileFinder->setYoungerThan(olderThan.currentMSecsSinceEpoch() <= 0 + m_fileFinder->setYoungerThan(olderThan.toMSecsSinceEpoch() <= 0 ? NULL : new QDateTime(youngerThan)); m_fileFinder->setTextToSearch(ui->comboBoxTextPattern->currentText(), ui->checkBoxCaseSensitiv->isChecked(), diff --git a/appl/research/mainwindow.hpp b/appl/research/mainwindow.hpp index a192cad..240f7a4 100644 --- a/appl/research/mainwindow.hpp +++ b/appl/research/mainwindow.hpp @@ -11,6 +11,7 @@ class FileFinder; class FileCache; +class HitPosition; namespace Ui { class MainWindow; } @@ -34,11 +35,14 @@ private: virtual void onLanguageChange(); void searchFiles(bool addNotDetach); private slots: + void clearLastHitPosition(const QString &, const QString &); void onAbout(); void onAdd(); void onClear(); void onDetach(); void onFilter(); + void onNext(); + void onPrevious(); void onSelectBaseDirectory(); void textEdited(const QString& oldString, const QString& newString); private: @@ -46,5 +50,6 @@ private: FileFinder* m_fileFinder; FileCache* m_fileCache; bool m_test; + HitPosition* m_lastHitPosition; }; #endif // MAINWINDOW_H diff --git a/appl/research/mainwindow.ui b/appl/research/mainwindow.ui index b5e5a01..605bc0b 100644 --- a/appl/research/mainwindow.ui +++ b/appl/research/mainwindow.ui @@ -18,7 +18,7 @@ - 0 + 1 @@ -348,7 +348,7 @@ If the checkbox "inverse search" is checked a file is found only if th - + 19 @@ -578,6 +578,9 @@ If the checkbox "inverse search" is checked a file is found only if th true + + true + @@ -601,6 +604,9 @@ If the checkbox "inverse search" is checked a file is found only if th 16777215 + + Show the previous page of hits + < @@ -614,8 +620,11 @@ If the checkbox "inverse search" is checked a file is found only if th 16777215 + + Show the next page of hits + - > + > @@ -638,7 +647,7 @@ If the checkbox "inverse search" is checked a file is found only if th true - 0 + 1 @@ -661,7 +670,7 @@ If the checkbox "inverse search" is checked a file is found only if th true - 100 + 10 diff --git a/base/ReFileSearch.cpp b/base/ReFileSearch.cpp index aad76a7..84d659f 100644 --- a/base/ReFileSearch.cpp +++ b/base/ReFileSearch.cpp @@ -13,7 +13,7 @@ */ ReFileSearch::ReFileSearch() : m_minDepth(0), - m_maxDepth(9999), + m_maxDepth(100), m_searchMode(smFilesAndDirs), m_matcher(), m_processedDirs(0), @@ -27,6 +27,9 @@ ReFileSearch::ReFileSearch() : */ ReFileSearch::~ReFileSearch() { + delete m_youngerThan; + delete m_olderThan; + m_youngerThan = m_olderThan = NULL; } /** @@ -87,6 +90,7 @@ void ReFileSearch::oneDirectory(const QString& directory, int depth) full += OS_SEPARATOR; full += node; QFileInfo info(full); + QDateTime current = info.lastModified(); if (m_olderThan != NULL && info.lastModified() > *m_olderThan) continue; if (m_youngerThan != NULL && info.lastModified() < *m_youngerThan) @@ -172,6 +176,7 @@ void ReFileSearch::setMinDepth(int minDepth) */ void ReFileSearch::setOlderThan(QDateTime *olderThan) { + delete m_olderThan; m_olderThan = olderThan; } @@ -194,6 +199,7 @@ void ReFileSearch::setPatterns(const QString& includeExcludePatterns) */ void ReFileSearch::setYoungerThan(QDateTime *youngerThan) { + delete m_youngerThan; m_youngerThan = youngerThan; } diff --git a/base/ReStringUtils.cpp b/base/ReStringUtils.cpp index 4f729c8..5192262 100644 --- a/base/ReStringUtils.cpp +++ b/base/ReStringUtils.cpp @@ -438,7 +438,8 @@ ReSuccess_t ReStringUtils::write(const char* file, const char* content, const char* mode) { FILE* fp = fopen(file, mode); if (fp != NULL) { - fputs(content, fp); + if (content != NULL) + fputs(content, fp); fclose(fp); } return fp != NULL; diff --git a/cunit/cuReFileSearch.cpp b/cunit/cuReFileSearch.cpp index cad10ef..9c56f5b 100644 --- a/cunit/cuReFileSearch.cpp +++ b/cunit/cuReFileSearch.cpp @@ -49,6 +49,8 @@ public: setPatterns("*.txt,-*1*,-*2*,-*4*,-file5.txt,dir*,-dir2"); setMinDepth(1); setMaxDepth(3); + setOlderThan(NULL); + setYoungerThan(NULL); setSearchMode(smFilesAndDirs); oneDirectory(m_baseDir, 0); checkEqu(4, m_found.size()); @@ -58,6 +60,65 @@ public: checkT(m_found.contains("dir1/dir2/dir3")); } + void testYoungerThan(){ + m_found.clear(); + setPatterns("file2.txt"); + setMinDepth(0); + setMaxDepth(2); + setSearchMode(smFiles); + setOlderThan(NULL); + + setYoungerThan(NULL); + oneDirectory(m_baseDir, 0); + checkEqu(3, m_found.size()); + checkT(m_found.contains("file2.txt")); + checkT(m_found.contains("dir1/file2.txt")); + checkT(m_found.contains("dir1/dir2/file2.txt")); + + QDateTime now = QDateTime::currentDateTime(); + + m_found.clear(); + setYoungerThan(new QDateTime(now.addDays(-1))); + oneDirectory(m_baseDir, 0); + checkEqu(3, m_found.size()); + checkT(m_found.contains("file2.txt")); + checkT(m_found.contains("dir1/file2.txt")); + checkT(m_found.contains("dir1/dir2/file2.txt")); + + m_found.clear(); + setYoungerThan(new QDateTime(now.addDays(1))); + oneDirectory(m_baseDir, 0); + checkEqu(0, m_found.size()); + } + + void testOlderThan(){ + m_found.clear(); + setPatterns("*1.txt"); + setMinDepth(0); + setMaxDepth(1); + setSearchMode(smFiles); + setYoungerThan(NULL); + setOlderThan(NULL); + oneDirectory(m_baseDir, 0); + checkEqu(2, m_found.size()); + checkT(m_found.contains("file1.txt")); + checkT(m_found.contains("dir1/file1.txt")); + + QDateTime now = QDateTime::currentDateTime(); + + m_found.clear(); + setOlderThan(new QDateTime(now.addDays(1))); + oneDirectory(m_baseDir, 0); + checkEqu(2, m_found.size()); + checkT(m_found.contains("file1.txt")); + checkT(m_found.contains("dir1/file1.txt")); + + m_found.clear(); + setOlderThan(new QDateTime(now.addDays(-1))); + oneDirectory(m_baseDir, 0); + checkEqu(0, m_found.size()); + } + virtual bool handleFile(const QString& full, const QString& path, const QString& node, const QFileInfo& info){ ReUseParameter(info); @@ -88,6 +149,8 @@ private: virtual void runTests() { makeTree(); + testOlderThan(); + testYoungerThan(); testOneDirectoryDepthExclude(); testBasic(); } diff --git a/cunit/cuReQStringUtils.cpp b/cunit/cuReQStringUtils.cpp index fe743cb..087a2c2 100644 --- a/cunit/cuReQStringUtils.cpp +++ b/cunit/cuReQStringUtils.cpp @@ -224,7 +224,8 @@ public: void testReadableDuration(){ #define clf(sec) clock_t(clock_t((sec) * CLOCKS_PER_SEC)) checkEqu("0.000 sec", ReQStringUtils::readableDuration(clf(0.0))); - checkEqu("1.234 sec", ReQStringUtils::readableDuration(clf(1.234))); + QString value = ReQStringUtils::readableDuration(clf(1.234)); + checkEqu("1.234 sec", value); checkEqu("59.250 sec", ReQStringUtils::readableDuration(clf(59.250))); checkEqu("1:01", ReQStringUtils::readableDuration(clf(61.251))); checkEqu("59:59", ReQStringUtils::readableDuration(clf(59*60+59.499))); -- 2.39.5