From: Hamatoma Date: Tue, 29 Nov 2016 23:00:34 +0000 (+0100) Subject: ReSearch: ignore binaries X-Git-Url: https://gitweb.hamatoma.de/?a=commitdiff_plain;h=3787af608194685ef31442666f36b51f7fc1391b;p=reqt ReSearch: ignore binaries --- diff --git a/appl/research/filefinder.cpp b/appl/research/filefinder.cpp index 34ff2d6..dd1c9cd 100644 --- a/appl/research/filefinder.cpp +++ b/appl/research/filefinder.cpp @@ -19,7 +19,8 @@ FileFinder::FileFinder(QTableWidget& table, FileCache& cache) : m_text(NULL), m_inverseSearch(false), m_caseSensitive(false), - m_cache(cache) + m_cache(cache), + m_ignoreBinaries(false) { setSearchMode(smFiles); } @@ -118,6 +119,33 @@ int FileFinder::ignoredFiles() const } +/** + * Checks whether a file is a binary file. + * + * @param filename filename with path + * + * @return false: the first 16kByte of the file does not contain + * a NUL char ('\0')
+ * true: not readable or the file contains a NUL char + */ +bool FileFinder::isBinaryFile(const QString& filename) +{ + bool rc = true; + FILE* fp = fopen(filename.toLocal8Bit().data(), "rb"); + if (fp != NULL){ + // We read the first 16 kByte: + char buffer[16*1024 + 1]; + int size = fread(buffer, 1, sizeof buffer - 1, fp); + if (size > 0){ + buffer[size] = '\0'; + const char* ptr = strchr(buffer, '\0'); + rc = ptr < buffer + size; + } + fclose(fp); + } + return rc; +} + /** * Handle a file found by the file search criteria. * @@ -133,7 +161,8 @@ bool FileFinder::handleFile(const QString &full, const QString &path, found = searchWithRegExpr(full); } else if (m_text != NULL){ found = searchText(full); - } + } else if (m_ignoreBinaries) + found = ! isBinaryFile(full); if (found){ if (m_addNotDetach) addToTable(full, path, node, info); @@ -152,33 +181,45 @@ bool FileFinder::handleFile(const QString &full, const QString &path, */ bool FileFinder::searchText(const QString &filename) { - m_cache.addOrUpdate(filename); - const QStringList& lines = m_cache.lines(filename); bool rc = false; - 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; + if (! m_ignoreBinaries || ! isBinaryFile(filename)){ + m_cache.addOrUpdate(filename); + const QStringList& lines = m_cache.lines(filename); + 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 { - rc = false; - for (int ix = 0; ix < lines.size(); ++ix){ - if (lines[ix].indexOf(m_text, mode) >= 0){ - rc = true; - break; + } else { + rc = false; + for (int ix = 0; ix < lines.size(); ++ix){ + if (lines[ix].indexOf(m_text, mode) >= 0){ + rc = true; + break; + } } } + if (! rc) + m_cache.remove(filename); } - if (! rc) - m_cache.remove(filename); return rc; } +/** + * Sets the flag "ignore binaries". + * + * @param ignoreBinaries true: binary files will not be found + */ +void FileFinder::setIgnoreBinaries(bool ignoreBinaries) +{ + m_ignoreBinaries = ignoreBinaries; +} + /** * Searches a regular expression in the file content. * @@ -186,29 +227,31 @@ bool FileFinder::searchText(const QString &filename) * @return true: the text has been found */ bool FileFinder::searchWithRegExpr(const QString& filename){ - m_cache.addOrUpdate(filename); - const QStringList& lines = m_cache.lines(filename); - bool rc = false; - if (m_inverseSearch){ - rc = true; - for (int ix = 0; ix < lines.size(); ++ix){ - if (m_regularExpression->match(lines[ix]).hasMatch()){ - rc = false; - break; - } - } - } else { - rc = false; - for (int ix = 0; ix < lines.size(); ++ix){ - if (m_regularExpression->match(lines[ix]).hasMatch()){ - rc = true; - break; + if (! m_ignoreBinaries || ! isBinaryFile(filename)){ + m_cache.addOrUpdate(filename); + const QStringList& lines = m_cache.lines(filename); + + if (m_inverseSearch){ + rc = true; + for (int ix = 0; ix < lines.size(); ++ix){ + if (m_regularExpression->match(lines[ix]).hasMatch()){ + rc = false; + break; + } + } + } else { + rc = false; + for (int ix = 0; ix < lines.size(); ++ix){ + if (m_regularExpression->match(lines[ix]).hasMatch()){ + rc = true; + break; + } } } + if (rc) + m_cache.remove(filename); } - if (rc) - m_cache.remove(filename); return rc; } diff --git a/appl/research/filefinder.hpp b/appl/research/filefinder.hpp index f30e40a..4a5714a 100644 --- a/appl/research/filefinder.hpp +++ b/appl/research/filefinder.hpp @@ -19,9 +19,12 @@ public: void search(const QString& baseDirectory, const QString& patterns, int minDepth, int maxDepth); void setAddNotDetach(bool addNotDetach); + void setIgnoreBinaries(bool ignoreBinaries); void setTextToSearch(const QString &text, bool caseSensitive, bool isRegularExpression, bool inverseSearch); + private: + bool isBinaryFile(const QString &filename); bool searchWithRegExpr(const QString &filename); bool searchText(const QString &filename); private: @@ -36,6 +39,7 @@ private: bool m_inverseSearch; bool m_caseSensitive; FileCache& m_cache; + bool m_ignoreBinaries; }; #endif // FILEFINDER_H diff --git a/appl/research/mainwindow.cpp b/appl/research/mainwindow.cpp index 252dec7..4b345dd 100644 --- a/appl/research/mainwindow.cpp +++ b/appl/research/mainwindow.cpp @@ -66,25 +66,6 @@ int MainWindow::addSingleFile(bool addNotDetach, const QString& filename, int& a return rc; } -/** - * Gets an integer from a combobox. - * - * If the text is not an integer the default value will be used. - * - * @param combobox the source of the integer - * @param defaultValue the value if the source value is invalid - */ -int MainWindow::comboboxToInt(QComboBox& combobox, int defaultValue){ - QString text = combobox.currentText(); - uint rc = 0; - if (ReQStringUtils::lengthOfUInt(text, 0, 10, &rc) != text.length()){ - say(LOG_ERROR, tr("not an integer: %1 using instead: %2").arg(text).arg(defaultValue)); - combobox.setCurrentText(QString::number(defaultValue)); - rc = (uint) defaultValue; - } - return (int) rc; -} - /** * Initializes the Graphical User Interface. */ @@ -125,7 +106,9 @@ void MainWindow::initializeGui(){ ui->tableWidget->setColumnWidth(colDate, 175); switchFilter(true); if (m_test){ - ui->comboBoxBaseDirectory->setCurrentText("/home/mhm"); + //ui->comboBoxBaseDirectory->setCurrentText("/home/mhm"); + //ui->comboBoxFilePatterns->setCurrentText("pflege.odt"); + onAdd(); } } @@ -234,10 +217,10 @@ void MainWindow::onFilter() qint64 start = QDateTime::currentMSecsSinceEpoch(); FileFilter filter(*ui->tableWidget, *ui->listWidgetHits, *m_fileCache, *m_lastHitPosition); - int from = comboboxToInt(*ui->comboBoxFromHit, 0); - int pageSize = comboboxToInt(*ui->comboBoxPageSize, s_defaultPageSize); - int above = comboboxToInt(*ui->comboBoxLinesAbove, 0); - int below = comboboxToInt(*ui->comboBoxLinesBelow, 0); + int from = comboInt(ui->comboBoxFromHit, 0); + int pageSize = comboInt(ui->comboBoxPageSize, s_defaultPageSize); + int above = comboInt(ui->comboBoxLinesAbove, 0); + int below = comboInt(ui->comboBoxLinesBelow, 0); ui->pushButtonFilter->setEnabled(false); filter.filter(ui->comboBoxIncludingPattern->currentText(), ui->comboBoxExcludingPattern->currentText(), @@ -370,6 +353,7 @@ void MainWindow::searchFiles(bool addNotDetach) } else { QDateTime olderThan = comboDate(ui->comboBoxOlder); QDateTime youngerThan = comboDate(ui->comboBoxYounger); + m_fileFinder->setIgnoreBinaries(ui->checkBoxIgnoreBinary->isChecked()); m_fileFinder->setOlderThan(olderThan.toMSecsSinceEpoch() <= 0 ? NULL : new QDateTime(olderThan)); m_fileFinder->setYoungerThan(olderThan.toMSecsSinceEpoch() <= 0 diff --git a/appl/research/mainwindow.hpp b/appl/research/mainwindow.hpp index ec9c81f..8050657 100644 --- a/appl/research/mainwindow.hpp +++ b/appl/research/mainwindow.hpp @@ -33,7 +33,6 @@ public: private: int addSingleFile(bool addNotDetach, const QString &filename, int &alreadyFound); - int comboboxToInt(QComboBox &combobox, int defaultValue); void initializeGui(); void logHits(); void onAddTimer(); diff --git a/appl/research/mainwindow.ui b/appl/research/mainwindow.ui index f13990e..8cae060 100644 --- a/appl/research/mainwindow.ui +++ b/appl/research/mainwindow.ui @@ -161,6 +161,16 @@ + + + + Ignore binaries + + + true + + +