Utils: Simplify FileReader interface

It was either used with QIODevice::Text, or not. Moving the
\r\n -> \n replacement to the result accessor make passing
the flags unnecessary.

Change-Id: I686f992a07734f1805617c245774b357c6d42025
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
hjk
2024-12-03 12:44:57 +01:00
parent 2b2ef52d80
commit fe4838b46c
16 changed files with 44 additions and 51 deletions

View File

@@ -1091,8 +1091,8 @@ JsonSchema *JsonSchemaManager::schemaByName(const QString &baseName) const
JsonSchema *JsonSchemaManager::parseSchema(const QString &schemaFileName) const
{
FileReader reader;
if (reader.fetch(FilePath::fromString(schemaFileName), QIODevice::Text)) {
const QString &contents = QString::fromUtf8(reader.data());
if (reader.fetch(FilePath::fromString(schemaFileName))) {
const QString &contents = QString::fromUtf8(reader.text());
JsonValue *json = JsonValue::create(contents, &m_pool);
if (json && json->kind() == JsonValue::Object)
return new JsonSchema(json->toObject(), this);

View File

@@ -332,7 +332,7 @@ QFuture<PluginDumper::QmlTypeDescription> PluginDumper::loadQmlTypeDescription(c
for (const FilePath &p: paths) {
Utils::FileReader reader;
if (!reader.fetch(p, QFile::Text)) {
if (!reader.fetch(p)) {
result.errors += reader.errorString();
continue;
}
@@ -341,7 +341,7 @@ QFuture<PluginDumper::QmlTypeDescription> PluginDumper::loadQmlTypeDescription(c
CppQmlTypesLoader::BuiltinObjects objs;
QList<ModuleApiInfo> apis;
QStringList deps;
CppQmlTypesLoader::parseQmlTypeDescriptions(reader.data(), &objs, &apis, &deps,
CppQmlTypesLoader::parseQmlTypeDescriptions(reader.text(), &objs, &apis, &deps,
&error, &warning, p.toString());
if (!error.isEmpty()) {
result.errors += Tr::tr("Failed to parse \"%1\".\nError: %2").arg(p.toUserOutput(), error);

View File

@@ -56,26 +56,27 @@ QByteArray FileReader::fetchQrc(const QString &fileName)
return file.readAll();
}
bool FileReader::fetch(const FilePath &filePath, QIODevice::OpenMode mode)
bool FileReader::fetch(const FilePath &filePath)
{
QTC_ASSERT(!(mode & ~(QIODevice::ReadOnly | QIODevice::Text)), return false);
const expected_str<QByteArray> contents = filePath.fileContents();
if (!contents) {
m_errorString = contents.error();
return false;
}
m_data = *contents;
if (mode & QIODevice::Text)
m_data = m_data.replace("\r\n", "\n");
return true;
}
bool FileReader::fetch(const FilePath &filePath, QIODevice::OpenMode mode, QString *errorString)
QByteArray FileReader::text() const
{
if (fetch(filePath, mode))
QByteArray res = m_data;
res = res.replace("\r\n", "\n");
return res;
}
bool FileReader::fetch(const FilePath &filePath, QString *errorString)
{
if (fetch(filePath))
return true;
if (errorString)
*errorString = m_errorString;
@@ -83,9 +84,9 @@ bool FileReader::fetch(const FilePath &filePath, QIODevice::OpenMode mode, QStri
}
#ifdef QT_GUI_LIB
bool FileReader::fetch(const FilePath &filePath, QIODevice::OpenMode mode, QWidget *parent)
bool FileReader::fetch(const FilePath &filePath, QWidget *parent)
{
if (fetch(filePath, mode))
if (fetch(filePath))
return true;
if (parent)
QMessageBox::critical(parent, Tr::tr("File Error"), m_errorString);

View File

@@ -149,16 +149,13 @@ class QTCREATOR_UTILS_EXPORT FileReader
{
public:
static QByteArray fetchQrc(const QString &fileName); // Only for internal resources
bool fetch(const FilePath &filePath, QIODevice::OpenMode mode = QIODevice::NotOpen); // QIODevice::ReadOnly is implicit
bool fetch(const FilePath &filePath, QIODevice::OpenMode mode, QString *errorString);
bool fetch(const FilePath &filePath, QString *errorString)
{ return fetch(filePath, QIODevice::NotOpen, errorString); }
bool fetch(const FilePath &filePath);
bool fetch(const FilePath &filePath, QString *errorString);
#ifdef QT_GUI_LIB
bool fetch(const FilePath &filePath, QIODevice::OpenMode mode, QWidget *parent);
bool fetch(const FilePath &filePath, QWidget *parent)
{ return fetch(filePath, QIODevice::NotOpen, parent); }
bool fetch(const FilePath &filePath, QWidget *parent);
#endif // QT_GUI_LIB
const QByteArray &data() const { return m_data; }
QByteArray text() const; // data with replaced \r\n -> \n
const QString &errorString() const { return m_errorString; }
private:
QByteArray m_data;

View File

@@ -750,11 +750,11 @@ static void modifyManufacturerTag(const FilePath &avdPath, TagModification modif
const FilePath configFilePath = avdPath / "config.ini";
FileReader reader;
if (!reader.fetch(configFilePath, QIODevice::ReadOnly | QIODevice::Text))
if (!reader.fetch(configFilePath))
return;
FileSaver saver(configFilePath);
QTextStream textStream(reader.data());
QTextStream textStream(reader.text());
while (!textStream.atEnd()) {
QString line = textStream.readLine();
if (line.contains("hw.device.manufacturer")) {

View File

@@ -100,8 +100,8 @@ private:
return {};
Utils::FileReader reader;
// Do not use QIODevice::Text as we have to deal with byte offsets.
if (reader.fetch(Utils::FilePath::fromString(filePath), QIODevice::ReadOnly))
// Do not use FileReader::text as we have to deal with byte offsets.
if (reader.fetch(Utils::FilePath::fromString(filePath)))
return reader.data();
return {};

View File

@@ -264,8 +264,8 @@ FilePath ReadExportedDiagnosticsTest::createFile(const Utils::FilePath &yamlFile
const FilePath newFileName = m_baseDir->filePath().resolvePath(yamlFilePath);
FileReader reader;
if (QTC_GUARD(reader.fetch(yamlFilePath, QIODevice::ReadOnly | QIODevice::Text))) {
QByteArray contents = reader.data();
if (QTC_GUARD(reader.fetch(yamlFilePath))) {
QByteArray contents = reader.text();
contents.replace("FILE_PATH", filePathToInject.toString().toLocal8Bit());
FileSaver fileSaver(newFileName, QIODevice::WriteOnly | QIODevice::Text);

View File

@@ -2812,9 +2812,9 @@ bool GitClient::getCommitData(const FilePath &workingDirectory,
if (!templateFile.isEmpty()) {
templateFile = repoDirectory.resolvePath(templateFile);
FileReader reader;
if (!reader.fetch(templateFile, QIODevice::Text, errorMessage))
if (!reader.fetch(templateFile, errorMessage))
return false;
*commitTemplate = QString::fromLocal8Bit(reader.data());
*commitTemplate = QString::fromLocal8Bit(reader.text());
}
break;
}

View File

@@ -1416,7 +1416,7 @@ bool PerforcePluginPrivate::activateCommit()
// Pipe file into p4 submit -i
FileReader reader;
if (!reader.fetch(Utils::FilePath::fromString(m_commitMessageFileName), QIODevice::Text)) {
if (!reader.fetch(Utils::FilePath::fromString(m_commitMessageFileName))) {
VcsOutputWindow::appendError(reader.errorString());
return false;
}
@@ -1425,7 +1425,7 @@ bool PerforcePluginPrivate::activateCommit()
submitArgs << QLatin1String("submit") << QLatin1String("-i");
const PerforceResponse submitResponse = runP4Cmd(settings().topLevelSymLinkTarget(), submitArgs,
LongTimeOut|RunFullySynchronous|CommandToWindow|StdErrToWindow|ErrorToWindow|ShowBusyCursor,
{}, reader.data());
{}, reader.text());
if (submitResponse.error)
return false;

View File

@@ -176,10 +176,8 @@ static bool createFile(CustomWizardFile cwFile,
qDebug() << "generating " << targetPath << sourcePath << fm;
// Read contents of source file
const QFile::OpenMode openMode
= cwFile.binary ? QIODevice::ReadOnly : (QIODevice::ReadOnly|QIODevice::Text);
FileReader reader;
if (!reader.fetch(FilePath::fromString(sourcePath), openMode, errorMessage))
if (!reader.fetch(FilePath::fromString(sourcePath), errorMessage))
return false;
GeneratedFile generatedFile;
@@ -190,7 +188,7 @@ static bool createFile(CustomWizardFile cwFile,
generatedFile.setBinaryContents(reader.data());
} else {
// Template file: Preprocess.
const QString contentsIn = QString::fromLocal8Bit(reader.data());
const QString contentsIn = QString::fromLocal8Bit(reader.text());
generatedFile.setContents(CustomWizardContext::processFile(fm, contentsIn));
}

View File

@@ -118,11 +118,8 @@ Core::GeneratedFile JsonWizardFileGenerator::generateFile(const File &file,
MacroExpander *expander, QString *errorMessage)
{
// Read contents of source file
const QFile::OpenMode openMode = file.isBinary.toBool() ?
QIODevice::ReadOnly : (QIODevice::ReadOnly|QIODevice::Text);
FileReader reader;
if (!reader.fetch(file.source, openMode, errorMessage))
if (!reader.fetch(file.source, errorMessage))
return Core::GeneratedFile();
// Generate file information:
@@ -155,7 +152,7 @@ Core::GeneratedFile JsonWizardFileGenerator::generateFile(const File &file,
return expander->resolveMacro(n, ret);
});
gf.setContents(TemplateEngine::processText(&nested, QString::fromUtf8(reader.data()),
gf.setContents(TemplateEngine::processText(&nested, QString::fromUtf8(reader.text()),
errorMessage));
if (!errorMessage->isEmpty()) {
*errorMessage = Tr::tr("When processing \"%1\":<br>%2")

View File

@@ -185,10 +185,10 @@ Core::IDocument::OpenResult ObjectsMapDocument::openImpl(QString *error,
QByteArray text;
if (realFileName.fileName() == "objects.map") {
Utils::FileReader reader;
if (!reader.fetch(realFileName, QIODevice::Text, error))
if (!reader.fetch(realFileName, error))
return OpenResult::ReadError;
text = reader.data();
text = reader.text();
} else {
const Utils::FilePath base = settings().squishPath();
if (base.isEmpty()) {

View File

@@ -88,11 +88,11 @@ static FormatOutput format(const FormatInput &input)
// Read text back
Utils::FileReader reader;
if (!reader.fetch(sourceFile.filePath(), QIODevice::Text)) {
if (!reader.fetch(sourceFile.filePath())) {
return Utils::make_unexpected(Tr::tr("Cannot read file \"%1\": %2.")
.arg(sourceFile.filePath().toUserOutput(), reader.errorString()));
}
return QString::fromUtf8(reader.data());
return QString::fromUtf8(reader.text());
}
case Command::PipeProcessing: {

View File

@@ -235,11 +235,11 @@ bool NickNameDialog::populateModelFromMailCapFile(const FilePath &fileName,
if (fileName.isEmpty())
return true;
FileReader reader;
if (!reader.fetch(fileName, QIODevice::Text, errorMessage))
if (!reader.fetch(fileName, errorMessage))
return false;
// Split into lines and read
NickNameEntry entry;
const QStringList lines = QString::fromUtf8(reader.data()).trimmed().split(QLatin1Char('\n'));
const QStringList lines = QString::fromUtf8(reader.text()).trimmed().split(QLatin1Char('\n'));
const int count = lines.size();
for (int i = 0; i < count; i++) {
if (entry.parse(lines.at(i))) {

View File

@@ -37,10 +37,10 @@ IDocument::OpenResult SubmitEditorFile::open(QString *errorString, const FilePat
return OpenResult::ReadError;
FileReader reader;
if (!reader.fetch(realFilePath, QIODevice::Text, errorString))
if (!reader.fetch(realFilePath, errorString))
return OpenResult::ReadError;
const QString text = QString::fromLocal8Bit(reader.data());
const QString text = QString::fromLocal8Bit(reader.text());
if (!m_editor->setFileContents(text.toUtf8()))
return OpenResult::CannotHandle;

View File

@@ -242,11 +242,11 @@ static inline QStringList fieldTexts(const QString &fileContents)
void VcsBaseSubmitEditor::createUserFields(const FilePath &fieldConfigFile)
{
FileReader reader;
if (!reader.fetch(fieldConfigFile, QIODevice::Text, ICore::dialogParent()))
if (!reader.fetch(fieldConfigFile, ICore::dialogParent()))
return;
// Parse into fields
const QStringList fields = fieldTexts(QString::fromUtf8(reader.data()));
const QStringList fields = fieldTexts(QString::fromUtf8(reader.text()));
if (fields.empty())
return;
// Create a completer on user names