diff --git a/src/libs/qmljs/jsoncheck.cpp b/src/libs/qmljs/jsoncheck.cpp index 3cb44111f9d..c76152a4d34 100644 --- a/src/libs/qmljs/jsoncheck.cpp +++ b/src/libs/qmljs/jsoncheck.cpp @@ -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); diff --git a/src/libs/qmljs/qmljsplugindumper.cpp b/src/libs/qmljs/qmljsplugindumper.cpp index fa6358e4a2a..ad327cb5f85 100644 --- a/src/libs/qmljs/qmljsplugindumper.cpp +++ b/src/libs/qmljs/qmljsplugindumper.cpp @@ -332,7 +332,7 @@ QFuture 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::loadQmlTypeDescription(c CppQmlTypesLoader::BuiltinObjects objs; QList 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); diff --git a/src/libs/utils/fileutils.cpp b/src/libs/utils/fileutils.cpp index e20ebb853d6..885375fb172 100644 --- a/src/libs/utils/fileutils.cpp +++ b/src/libs/utils/fileutils.cpp @@ -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 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); diff --git a/src/libs/utils/fileutils.h b/src/libs/utils/fileutils.h index a894d33a403..2f23277fef2 100644 --- a/src/libs/utils/fileutils.h +++ b/src/libs/utils/fileutils.h @@ -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; diff --git a/src/plugins/android/androiddevice.cpp b/src/plugins/android/androiddevice.cpp index 663cfe57470..94a9ee9ddee 100644 --- a/src/plugins/android/androiddevice.cpp +++ b/src/plugins/android/androiddevice.cpp @@ -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")) { diff --git a/src/plugins/clangtools/clangtoolslogfilereader.cpp b/src/plugins/clangtools/clangtoolslogfilereader.cpp index 21f34f35c10..185dab40363 100644 --- a/src/plugins/clangtools/clangtoolslogfilereader.cpp +++ b/src/plugins/clangtools/clangtoolslogfilereader.cpp @@ -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 {}; diff --git a/src/plugins/clangtools/readexporteddiagnosticstest.cpp b/src/plugins/clangtools/readexporteddiagnosticstest.cpp index f6bf5e1118e..38bbee73908 100644 --- a/src/plugins/clangtools/readexporteddiagnosticstest.cpp +++ b/src/plugins/clangtools/readexporteddiagnosticstest.cpp @@ -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); diff --git a/src/plugins/git/gitclient.cpp b/src/plugins/git/gitclient.cpp index 1e8b5c05bbf..85f8dbfd7a4 100644 --- a/src/plugins/git/gitclient.cpp +++ b/src/plugins/git/gitclient.cpp @@ -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; } diff --git a/src/plugins/perforce/perforceplugin.cpp b/src/plugins/perforce/perforceplugin.cpp index 389879afe96..1cabd56984d 100644 --- a/src/plugins/perforce/perforceplugin.cpp +++ b/src/plugins/perforce/perforceplugin.cpp @@ -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; diff --git a/src/plugins/projectexplorer/customwizard/customwizard.cpp b/src/plugins/projectexplorer/customwizard/customwizard.cpp index fe81b3ba7e7..e79a454b06e 100644 --- a/src/plugins/projectexplorer/customwizard/customwizard.cpp +++ b/src/plugins/projectexplorer/customwizard/customwizard.cpp @@ -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)); } diff --git a/src/plugins/projectexplorer/jsonwizard/jsonwizardfilegenerator.cpp b/src/plugins/projectexplorer/jsonwizard/jsonwizardfilegenerator.cpp index 1644257a971..340b91709ce 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonwizardfilegenerator.cpp +++ b/src/plugins/projectexplorer/jsonwizard/jsonwizardfilegenerator.cpp @@ -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\":
%2") diff --git a/src/plugins/squish/objectsmapdocument.cpp b/src/plugins/squish/objectsmapdocument.cpp index 3cb7b5fd7c5..83816a1b64b 100644 --- a/src/plugins/squish/objectsmapdocument.cpp +++ b/src/plugins/squish/objectsmapdocument.cpp @@ -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()) { diff --git a/src/plugins/texteditor/formattexteditor.cpp b/src/plugins/texteditor/formattexteditor.cpp index 20a7d9c8bc8..e28abd73663 100644 --- a/src/plugins/texteditor/formattexteditor.cpp +++ b/src/plugins/texteditor/formattexteditor.cpp @@ -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: { diff --git a/src/plugins/vcsbase/nicknamedialog.cpp b/src/plugins/vcsbase/nicknamedialog.cpp index 39791dc7472..1efa23f584f 100644 --- a/src/plugins/vcsbase/nicknamedialog.cpp +++ b/src/plugins/vcsbase/nicknamedialog.cpp @@ -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))) { diff --git a/src/plugins/vcsbase/submiteditorfile.cpp b/src/plugins/vcsbase/submiteditorfile.cpp index ebdd14bf0f9..e00f6080818 100644 --- a/src/plugins/vcsbase/submiteditorfile.cpp +++ b/src/plugins/vcsbase/submiteditorfile.cpp @@ -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; diff --git a/src/plugins/vcsbase/vcsbasesubmiteditor.cpp b/src/plugins/vcsbase/vcsbasesubmiteditor.cpp index 995fb5c848c..2c9b917240a 100644 --- a/src/plugins/vcsbase/vcsbasesubmiteditor.cpp +++ b/src/plugins/vcsbase/vcsbasesubmiteditor.cpp @@ -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