m_text(NULL),
m_inverseSearch(false),
m_caseSensitive(false),
- m_cache(cache)
+ m_cache(cache),
+ m_ignoreBinaries(false)
{
setSearchMode(smFiles);
}
}
+/**
+ * Checks whether a file is a binary file.
+ *
+ * @param filename filename with path
+ *
+ * @return <i>false</i>: the first 16kByte of the file does not contain
+ * a NUL char ('\0')<br>
+ * <i>true</i>: 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.
*
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);
*/
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 <i>true</i>: binary files will not be found
+ */
+void FileFinder::setIgnoreBinaries(bool ignoreBinaries)
+{
+ m_ignoreBinaries = ignoreBinaries;
+}
+
/**
* Searches a regular expression in the file content.
*
* @return <i>true</i>: 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;
}
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.
*/
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();
}
}
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(),
} 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