From 13b72eb62140c25ca6f293d490d8668fd83ec66a Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Fri, 4 Sep 2020 12:00:24 +0200 Subject: [PATCH] FileSearch: Remove usage of QTextStream::setCodec Gone in Qt6. Read the whole file contents and decode instead. This is not ideal since we possibly load big files into memory. But otherwise we'd have to implement a buffering, file-based IODevice that reads into a buffer, part by part converting to unicode with QTextCodec::toUnicode(const char *input, int size, QTextCodec::ConverterState *state), similar to how it is done in QTextStream from Qt5 (QTextStreamPrivate::fillReadBuffer). Task-number: QTCREATORBUG-24098 Change-Id: I22e251c8217d49880df59980cf32a8febf93364b Reviewed-by: hjk --- src/libs/utils/filesearch.cpp | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/src/libs/utils/filesearch.cpp b/src/libs/utils/filesearch.cpp index efc46b77de2..5601b763a39 100644 --- a/src/libs/utils/filesearch.cpp +++ b/src/libs/utils/filesearch.cpp @@ -23,10 +23,12 @@ ** ****************************************************************************/ -#include "algorithm.h" #include "filesearch.h" + +#include "algorithm.h" #include "fileutils.h" #include "mapreduce.h" +#include "qtcassert.h" #include #include @@ -63,19 +65,20 @@ QString clippedText(const QString &text, int maxLength) } // returns success -bool openStream(const QString &filePath, QTextCodec *encoding, QTextStream *stream, QFile *file, - QString *tempString, - const QMap &fileToContentsMap) +bool getFileContent(const QString &filePath, + QTextCodec *encoding, + QString *tempString, + const QMap &fileToContentsMap) { if (fileToContentsMap.contains(filePath)) { *tempString = fileToContentsMap.value(filePath); - stream->setString(tempString); } else { - file->setFileName(filePath); - if (!file->open(QIODevice::ReadOnly)) + QFile file(filePath); + if (!file.open(QIODevice::ReadOnly)) return false; - stream->setDevice(file); - stream->setCodec(encoding); + const QByteArray content = file.readAll(); + *tempString = QTC_GUARD(encoding) ? encoding->toUnicode(content) + : QTextCodec::codecForLocale()->toUnicode(content); } return true; } @@ -139,13 +142,12 @@ void FileSearch::operator()(QFutureInterface &futureInterf futureInterface.setProgressRange(0, 1); futureInterface.setProgressValue(0); FileSearchResultList results; - QFile file; - QTextStream stream; QString tempString; - if (!openStream(item.filePath, item.encoding, &stream, &file, &tempString, fileToContentsMap)) { + if (!getFileContent(item.filePath, item.encoding, &tempString, fileToContentsMap)) { futureInterface.cancel(); // failure return; } + QTextStream stream(&tempString); int lineNr = 0; while (!stream.atEnd()) { @@ -215,8 +217,6 @@ void FileSearch::operator()(QFutureInterface &futureInterf if (futureInterface.isCanceled()) break; } - if (file.isOpen()) - file.close(); if (!futureInterface.isCanceled()) { futureInterface.reportResult(results); futureInterface.setProgressValue(1); @@ -259,13 +259,12 @@ void FileSearchRegExp::operator()(QFutureInterface &future futureInterface.setProgressRange(0, 1); futureInterface.setProgressValue(0); FileSearchResultList results; - QFile file; - QTextStream stream; QString tempString; - if (!openStream(item.filePath, item.encoding, &stream, &file, &tempString, fileToContentsMap)) { + if (!getFileContent(item.filePath, item.encoding, &tempString, fileToContentsMap)) { futureInterface.cancel(); // failure return; } + QTextStream stream(&tempString); int lineNr = 0; QString line; @@ -292,8 +291,6 @@ void FileSearchRegExp::operator()(QFutureInterface &future if (futureInterface.isCanceled()) break; } - if (file.isOpen()) - file.close(); if (!futureInterface.isCanceled()) { futureInterface.reportResult(results); futureInterface.setProgressValue(1);