]> gitweb.hamatoma.de Git - reqt/commitdiff
ReSearch: next / previous page, HitPosition
authorHamatoma <hamatoma@gmx.de>
Thu, 24 Nov 2016 23:58:51 +0000 (00:58 +0100)
committerHamatoma <hamatoma@gmx.de>
Thu, 24 Nov 2016 23:58:51 +0000 (00:58 +0100)
appl/research/filecache.cpp
appl/research/filefilter.cpp
appl/research/filefilter.hpp
appl/research/filefinder.cpp
appl/research/mainwindow.cpp
appl/research/mainwindow.hpp
appl/research/mainwindow.ui
base/ReFileSearch.cpp
base/ReStringUtils.cpp
cunit/cuReFileSearch.cpp
cunit/cuReQStringUtils.cpp

index 0f9e343f86a0c7c0dd86b8e99e96b2cc4f2ef6ec..f65cee97c90d92962b08f650857335445d1a6a3b 100644 (file)
@@ -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);
 }
 
index 0a2f3c17c162d7c2d6ff77718e03c0d4306f0bcd..205775777930e5b6512966696ce9ee7c0b9bf596 100644 (file)
@@ -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]);
index 2cf113c68ac856c9751b825f78fbb617bab0c94c..827d625c37a6005192ff458084b3ddc1ec3858e2 100644 (file)
@@ -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
index 028caa993880c2ba582970162cb686fddcb41d64..34ff2d6685b92fedb135d98e4d02457dad7a45f9 100644 (file)
@@ -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)
index 9639c1df1dcde7fee1d3b6acd2261cb7e1df48a1..d211ddbca0abfebf94d5631174a97cfd1383f43a 100644 (file)
@@ -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(),
index a192cad29b482699f1f838ad76efdb8f82d8dabd..240f7a44c1323cc9f95f7d1c26a39d6428733d37 100644 (file)
@@ -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
index b5e5a0178e766c6ea9bdde4dba4ad3faf5379b72..605bc0b4da167832292fab9bbfde5a4d6d84b9a2 100644 (file)
@@ -18,7 +18,7 @@
     <item>
      <widget class="QTabWidget" name="tabWidget">
       <property name="currentIndex">
-       <number>0</number>
+       <number>1</number>
       </property>
       <widget class="QWidget" name="tabFiles">
        <attribute name="title">
@@ -348,7 +348,7 @@ If the checkbox &quot;inverse search&quot; is checked a file is found only if th
                 <string notr="true"/>
                </property>
               </widget>
-              <widget class="QWidget" name="">
+              <widget class="QWidget" name="layoutWidget">
                <property name="geometry">
                 <rect>
                  <x>19</x>
@@ -578,6 +578,9 @@ If the checkbox &quot;inverse search&quot; is checked a file is found only if th
               <property name="autoDefault">
                <bool>true</bool>
               </property>
+              <property name="default">
+               <bool>true</bool>
+              </property>
              </widget>
             </item>
             <item>
@@ -601,6 +604,9 @@ If the checkbox &quot;inverse search&quot; is checked a file is found only if th
                 <height>16777215</height>
                </size>
               </property>
+              <property name="toolTip">
+               <string>Show the previous page of hits</string>
+              </property>
               <property name="text">
                <string notr="true">&lt;</string>
               </property>
@@ -614,8 +620,11 @@ If the checkbox &quot;inverse search&quot; is checked a file is found only if th
                 <height>16777215</height>
                </size>
               </property>
+              <property name="toolTip">
+               <string>Show the next page of hits</string>
+              </property>
               <property name="text">
-               <string>&gt;</string>
+               <string notr="true">&gt;</string>
               </property>
              </widget>
             </item>
@@ -638,7 +647,7 @@ If the checkbox &quot;inverse search&quot; is checked a file is found only if th
                <bool>true</bool>
               </property>
               <property name="currentText">
-               <string>0</string>
+               <string notr="true">1</string>
               </property>
              </widget>
             </item>
@@ -661,7 +670,7 @@ If the checkbox &quot;inverse search&quot; is checked a file is found only if th
                <bool>true</bool>
               </property>
               <property name="currentText">
-               <string>100</string>
+               <string notr="true">10</string>
               </property>
              </widget>
             </item>
index aad76a7da5e80e45b330a5ff32ee10e86414656f..84d659fddb0a9dabe06edea07010cda5001a020b 100644 (file)
@@ -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;
 }
 
index 4f729c81f94c6d293956fd0cb5a35c47684f19b4..5192262ed7732357d834612ec4078b51e6a474c8 100644 (file)
@@ -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;
index cad10ef665acc58bb1f115be1bac9e1bf3538384..9c56f5b3b72d303444f367ad156bdbee47d17387 100644 (file)
@@ -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();
        }
index fe743cbe1674afc3aea61343875e85b6a5ef2164..087a2c262844183c3c61fcd78352158db30d6567 100644 (file)
@@ -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)));