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 "algorithm.h"
#include "fileutils.h"
#include "mapreduce.h"
#include "qtcassert.h"
#include <QCoreApplication>
#include <QMutex>
@@ -63,19 +65,20 @@ QString clippedText(const QString &text, int maxLength)
}
// returns success
bool openStream(const QString &filePath, QTextCodec *encoding, QTextStream *stream, QFile *file,
bool getFileContent(const QString &filePath,
QTextCodec *encoding,
QString *tempString,
const QMap<QString, QString> &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<FileSearchResultList> &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<FileSearchResultList> &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<FileSearchResultList> &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<FileSearchResultList> &future
if (futureInterface.isCanceled())
break;
}
if (file.isOpen())
file.close();
if (!futureInterface.isCanceled()) {
futureInterface.reportResult(results);
futureInterface.setProgressValue(1);