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 <hjk@qt.io>
This commit is contained in:
Eike Ziller
2020-09-04 12:00:24 +02:00
parent 9a7535d9e9
commit 13b72eb621

View File

@@ -23,10 +23,12 @@
** **
****************************************************************************/ ****************************************************************************/
#include "algorithm.h"
#include "filesearch.h" #include "filesearch.h"
#include "algorithm.h"
#include "fileutils.h" #include "fileutils.h"
#include "mapreduce.h" #include "mapreduce.h"
#include "qtcassert.h"
#include <QCoreApplication> #include <QCoreApplication>
#include <QMutex> #include <QMutex>
@@ -63,19 +65,20 @@ QString clippedText(const QString &text, int maxLength)
} }
// returns success // returns success
bool openStream(const QString &filePath, QTextCodec *encoding, QTextStream *stream, QFile *file, bool getFileContent(const QString &filePath,
QTextCodec *encoding,
QString *tempString, QString *tempString,
const QMap<QString, QString> &fileToContentsMap) const QMap<QString, QString> &fileToContentsMap)
{ {
if (fileToContentsMap.contains(filePath)) { if (fileToContentsMap.contains(filePath)) {
*tempString = fileToContentsMap.value(filePath); *tempString = fileToContentsMap.value(filePath);
stream->setString(tempString);
} else { } else {
file->setFileName(filePath); QFile file(filePath);
if (!file->open(QIODevice::ReadOnly)) if (!file.open(QIODevice::ReadOnly))
return false; return false;
stream->setDevice(file); const QByteArray content = file.readAll();
stream->setCodec(encoding); *tempString = QTC_GUARD(encoding) ? encoding->toUnicode(content)
: QTextCodec::codecForLocale()->toUnicode(content);
} }
return true; return true;
} }
@@ -139,13 +142,12 @@ void FileSearch::operator()(QFutureInterface<FileSearchResultList> &futureInterf
futureInterface.setProgressRange(0, 1); futureInterface.setProgressRange(0, 1);
futureInterface.setProgressValue(0); futureInterface.setProgressValue(0);
FileSearchResultList results; FileSearchResultList results;
QFile file;
QTextStream stream;
QString tempString; QString tempString;
if (!openStream(item.filePath, item.encoding, &stream, &file, &tempString, fileToContentsMap)) { if (!getFileContent(item.filePath, item.encoding, &tempString, fileToContentsMap)) {
futureInterface.cancel(); // failure futureInterface.cancel(); // failure
return; return;
} }
QTextStream stream(&tempString);
int lineNr = 0; int lineNr = 0;
while (!stream.atEnd()) { while (!stream.atEnd()) {
@@ -215,8 +217,6 @@ void FileSearch::operator()(QFutureInterface<FileSearchResultList> &futureInterf
if (futureInterface.isCanceled()) if (futureInterface.isCanceled())
break; break;
} }
if (file.isOpen())
file.close();
if (!futureInterface.isCanceled()) { if (!futureInterface.isCanceled()) {
futureInterface.reportResult(results); futureInterface.reportResult(results);
futureInterface.setProgressValue(1); futureInterface.setProgressValue(1);
@@ -259,13 +259,12 @@ void FileSearchRegExp::operator()(QFutureInterface<FileSearchResultList> &future
futureInterface.setProgressRange(0, 1); futureInterface.setProgressRange(0, 1);
futureInterface.setProgressValue(0); futureInterface.setProgressValue(0);
FileSearchResultList results; FileSearchResultList results;
QFile file;
QTextStream stream;
QString tempString; QString tempString;
if (!openStream(item.filePath, item.encoding, &stream, &file, &tempString, fileToContentsMap)) { if (!getFileContent(item.filePath, item.encoding, &tempString, fileToContentsMap)) {
futureInterface.cancel(); // failure futureInterface.cancel(); // failure
return; return;
} }
QTextStream stream(&tempString);
int lineNr = 0; int lineNr = 0;
QString line; QString line;
@@ -292,8 +291,6 @@ void FileSearchRegExp::operator()(QFutureInterface<FileSearchResultList> &future
if (futureInterface.isCanceled()) if (futureInterface.isCanceled())
break; break;
} }
if (file.isOpen())
file.close();
if (!futureInterface.isCanceled()) { if (!futureInterface.isCanceled()) {
futureInterface.reportResult(results); futureInterface.reportResult(results);
futureInterface.setProgressValue(1); futureInterface.setProgressValue(1);