diff options
author | Jake Petroules <jake.petroules@qt.io> | 2017-06-28 20:05:45 -0700 |
---|---|---|
committer | Joerg Bornemann <joerg.bornemann@qt.io> | 2017-09-18 10:03:35 +0000 |
commit | b32d41b8788ea0c8d41c50effb6e15d8e6133a63 (patch) | |
tree | d7b692539316232d9c8763e65210f13d383e4491 /src/plugins | |
parent | c2dc4989fce13df6e39dd6d4b02f3961741a688a (diff) | |
download | qbs-b32d41b8788ea0c8d41c50effb6e15d8e6133a63.tar.gz |
Convert FileSaver to use a standard C++ interface
Change-Id: I31686a9e0e54d7f7c3f06161540bf9f229573a9c
Reviewed-by: Joerg Bornemann <joerg.bornemann@qt.io>
Diffstat (limited to 'src/plugins')
7 files changed, 114 insertions, 76 deletions
diff --git a/src/plugins/generator/visualstudio/io/msbuildprojectwriter.cpp b/src/plugins/generator/visualstudio/io/msbuildprojectwriter.cpp index f912f3a19..e18618fcd 100644 --- a/src/plugins/generator/visualstudio/io/msbuildprojectwriter.cpp +++ b/src/plugins/generator/visualstudio/io/msbuildprojectwriter.cpp @@ -51,6 +51,8 @@ static const QString kMSBuildSchemaURI = class MSBuildProjectWriterPrivate : public IMSBuildNodeVisitor { public: + std::ostream *device; + QByteArray buffer; QScopedPointer<QXmlStreamWriter> writer; void visitStart(const MSBuildImport *import) override; @@ -81,10 +83,11 @@ public: void visitEnd(const MSBuildPropertyGroup *propertyGroup) override; }; -MSBuildProjectWriter::MSBuildProjectWriter(QIODevice *device) +MSBuildProjectWriter::MSBuildProjectWriter(std::ostream *device) : d(new MSBuildProjectWriterPrivate) { - d->writer.reset(new QXmlStreamWriter(device)); + d->device = device; + d->writer.reset(new QXmlStreamWriter(&d->buffer)); d->writer->setAutoFormatting(true); } @@ -95,10 +98,14 @@ MSBuildProjectWriter::~MSBuildProjectWriter() bool MSBuildProjectWriter::write(const MSBuildProject *project) { + d->buffer.clear(); d->writer->writeStartDocument(); project->accept(d); d->writer->writeEndDocument(); - return !d->writer->hasError(); + if (d->writer->hasError()) + return false; + d->device->write(&*std::begin(d->buffer), d->buffer.size()); + return d->device->good(); } void MSBuildProjectWriterPrivate::visitStart(const MSBuildImport *import) diff --git a/src/plugins/generator/visualstudio/io/msbuildprojectwriter.h b/src/plugins/generator/visualstudio/io/msbuildprojectwriter.h index c70889971..cfe6beb7b 100644 --- a/src/plugins/generator/visualstudio/io/msbuildprojectwriter.h +++ b/src/plugins/generator/visualstudio/io/msbuildprojectwriter.h @@ -31,7 +31,9 @@ #ifndef MSBUILDPROJECTWRITER_H #define MSBUILDPROJECTWRITER_H -#include <QtCore/qiodevice.h> +#include <ostream> + +#include <QtCore/qglobal.h> namespace qbs { @@ -42,7 +44,7 @@ class MSBuildProjectWriter { Q_DISABLE_COPY(MSBuildProjectWriter) public: - explicit MSBuildProjectWriter(QIODevice *device); + explicit MSBuildProjectWriter(std::ostream *device); ~MSBuildProjectWriter(); bool write(const MSBuildProject *project); diff --git a/src/plugins/generator/visualstudio/io/visualstudiosolutionwriter.cpp b/src/plugins/generator/visualstudio/io/visualstudiosolutionwriter.cpp index 99180e2e2..d55a8cbb9 100644 --- a/src/plugins/generator/visualstudio/io/visualstudiosolutionwriter.cpp +++ b/src/plugins/generator/visualstudio/io/visualstudiosolutionwriter.cpp @@ -37,26 +37,28 @@ #include <tools/hostosinfo.h> #include <tools/pathutils.h> +#include <tools/stlutils.h> #include <tools/visualstudioversioninfo.h> #include <QtCore/qdir.h> #include <QtCore/qfile.h> #include <QtCore/qfileinfo.h> -#include <QtCore/qtextstream.h> #include <QtCore/quuid.h> #include <vector> namespace qbs { +using namespace Internal; + class VisualStudioSolutionWriterPrivate { public: - QIODevice *device; - QString baseDir; + std::ostream *device; + std::string baseDir; }; -VisualStudioSolutionWriter::VisualStudioSolutionWriter(QIODevice *device) +VisualStudioSolutionWriter::VisualStudioSolutionWriter(std::ostream *device) : d(new VisualStudioSolutionWriterPrivate) { d->device = device; @@ -66,81 +68,99 @@ VisualStudioSolutionWriter::~VisualStudioSolutionWriter() { } -QString VisualStudioSolutionWriter::projectBaseDirectory() const +std::string VisualStudioSolutionWriter::projectBaseDirectory() const { return d->baseDir; } -void VisualStudioSolutionWriter::setProjectBaseDirectory(const QString &dir) +void VisualStudioSolutionWriter::setProjectBaseDirectory(const std::string &dir) { d->baseDir = dir; } bool VisualStudioSolutionWriter::write(const VisualStudioSolution *solution) { - QTextStream out(d->device); - out << QString(QLatin1String("Microsoft Visual Studio Solution File, " - "Format Version %1\n" - "# Visual Studio %2\n")) - .arg(solution->versionInfo().solutionVersion()) - .arg(solution->versionInfo().version().majorVersion()); + auto &out = *d->device; + out << u8"Microsoft Visual Studio Solution File, Format Version " + << solution->versionInfo().solutionVersion().toStdString() + << u8"\n# Visual Studio " + << solution->versionInfo().version().majorVersion() + << u8"\n"; for (const auto &project : solution->fileProjects()) { - auto projectFilePath = project->filePath(); + auto projectFilePath = project->filePath().toStdString(); // Try to make the project file path relative to the // solution file path if we're writing to a file device - if (!d->baseDir.isEmpty()) { - const QDir solutionDir(d->baseDir); + if (!d->baseDir.empty()) { + const QDir solutionDir(QString::fromStdString(d->baseDir)); projectFilePath = Internal::PathUtils::toNativeSeparators( - solutionDir.relativeFilePath(projectFilePath), - Internal::HostOsInfo::HostOsWindows); + solutionDir.relativeFilePath(QString::fromStdString(projectFilePath)), + Internal::HostOsInfo::HostOsWindows).toStdString(); } - out << QStringLiteral("Project(\"%1\") = \"%2\", \"%3\", \"%4\"\n") - .arg(project->projectTypeGuid().toString()) - .arg(QFileInfo(projectFilePath).baseName()) - .arg(projectFilePath) - .arg(project->guid().toString()); + out << u8"Project(\"" + << project->projectTypeGuid().toString().toStdString() + << u8"\") = \"" + << QFileInfo(QString::fromStdString(projectFilePath)).baseName().toStdString() + << u8"\", \"" + << projectFilePath + << u8"\", \"" + << project->guid().toString().toStdString() + << u8"\"\n"; const auto dependencies = solution->dependencies(project); if (!dependencies.isEmpty()) { - out << "\tProjectSection(ProjectDependencies) = postProject\n"; + out << u8"\tProjectSection(ProjectDependencies) = postProject\n"; for (const auto &dependency : dependencies) - out << QStringLiteral("\t\t%1 = %1\n").arg(dependency->guid().toString()); + out << u8"\t\t" + << dependency->guid().toString().toStdString() + << u8" = " + << dependency->guid().toString().toStdString() + << u8"\n"; - out << "\tEndProjectSection\n"; + out << u8"\tEndProjectSection\n"; } - out << "EndProject\n"; + out << u8"EndProject\n"; } for (const auto &project : solution->folderProjects()) { - out << QStringLiteral("Project(\"%1\") = \"%2\", \"%3\", \"%4\"\n") - .arg(project->projectTypeGuid().toString()) - .arg(project->name()) - .arg(project->name()) - .arg(project->guid().toString()); - out << QStringLiteral("EndProject\n"); + out << u8"Project(\"" + << project->projectTypeGuid().toString().toStdString() + << u8"\") = \"" + << project->name().toStdString() + << u8"\", \"" + << project->name().toStdString() + << u8"\", \"" + << project->guid().toString().toStdString() + << u8"\"\n"; + + out << u8"EndProject\n"; } - out << "Global\n"; + out << u8"Global\n"; for (const auto &globalSection : solution->globalSections()) { - out << QStringLiteral("\tGlobalSection(%1) = %2\n") - .arg(globalSection->name()) - .arg(globalSection->isPost() - ? QStringLiteral("postSolution") - : QStringLiteral("preSolution")); + out << u8"\tGlobalSection(" + << globalSection->name().toStdString() + << u8") = " + << (globalSection->isPost() ? u8"postSolution" : u8"preSolution") + << u8"\n"; for (const auto &property : globalSection->properties()) - out << QStringLiteral("\t\t%1 = %2\n").arg(property.first).arg(property.second); - out << "\tEndGlobalSection\n"; + out << u8"\t\t" + << property.first.toStdString() + << u8" = " + << property.second.toStdString() + << u8"\n"; + + out << u8"\tEndGlobalSection\n"; } - out << "EndGlobal\n"; + out << u8"EndGlobal\n"; - return out.status() == QTextStream::Ok; + return out.good(); } } // namespace qbs diff --git a/src/plugins/generator/visualstudio/io/visualstudiosolutionwriter.h b/src/plugins/generator/visualstudio/io/visualstudiosolutionwriter.h index d5b0107f0..62ab45ead 100644 --- a/src/plugins/generator/visualstudio/io/visualstudiosolutionwriter.h +++ b/src/plugins/generator/visualstudio/io/visualstudiosolutionwriter.h @@ -31,8 +31,11 @@ #ifndef VISUALSTUDIOSOLUTIONWRITER_H #define VISUALSTUDIOSOLUTIONWRITER_H -#include <QtCore/qiodevice.h> -#include <QtCore/qscopedpointer.h> +#include <memory> +#include <ostream> +#include <string> + +#include <QtCore/qglobal.h> namespace qbs { @@ -45,18 +48,18 @@ class VisualStudioSolutionWriter { Q_DISABLE_COPY(VisualStudioSolutionWriter) public: - explicit VisualStudioSolutionWriter(QIODevice *device); + explicit VisualStudioSolutionWriter(std::ostream *device); ~VisualStudioSolutionWriter(); - QString projectBaseDirectory() const; - void setProjectBaseDirectory(const QString &dir); + std::string projectBaseDirectory() const; + void setProjectBaseDirectory(const std::string &dir); bool write(const VisualStudioSolution *solution); private: void addDefaultGlobalSections(); - QScopedPointer<VisualStudioSolutionWriterPrivate> d; + std::unique_ptr<VisualStudioSolutionWriterPrivate> d; }; } // namespace qbs diff --git a/src/plugins/generator/visualstudio/visualstudiogenerator.cpp b/src/plugins/generator/visualstudio/visualstudiogenerator.cpp index 5b69ade31..e5cf62003 100644 --- a/src/plugins/generator/visualstudio/visualstudiogenerator.cpp +++ b/src/plugins/generator/visualstudio/visualstudiogenerator.cpp @@ -249,7 +249,7 @@ static void writeProjectFiles(const QMap<QString, std::shared_ptr<MSBuildProject while (it.hasNext()) { it.next(); const auto projectFilePath = it.key(); - Internal::FileSaver file(projectFilePath); + Internal::FileSaver file(projectFilePath.toStdString()); if (!file.open()) throw ErrorInfo(Tr::tr("Cannot open %s for writing").arg(projectFilePath)); @@ -264,12 +264,12 @@ static void writeSolution(const std::shared_ptr<VisualStudioSolution> &solution, const QString &solutionFilePath, const Internal::Logger &logger) { - Internal::FileSaver file(solutionFilePath); + Internal::FileSaver file(solutionFilePath.toStdString()); if (!file.open()) throw ErrorInfo(Tr::tr("Cannot open %s for writing").arg(solutionFilePath)); VisualStudioSolutionWriter writer(file.device()); - writer.setProjectBaseDirectory(QFileInfo(solutionFilePath).path()); + writer.setProjectBaseDirectory(QFileInfo(solutionFilePath).path().toStdString()); if (!(writer.write(solution.get()) && file.commit())) throw ErrorInfo(Tr::tr("Failed to generate %1").arg(solutionFilePath)); @@ -300,7 +300,8 @@ void VisualStudioGenerator::visitProject(const GeneratableProject &project) const auto buildDir = project.baseBuildDirectory(); d->guidPool = std::make_shared<VisualStudioGuidPool>( - buildDir.absoluteFilePath(project.name() + QStringLiteral(".guid.txt"))); + buildDir.absoluteFilePath(project.name() + + QStringLiteral(".guid.txt")).toStdString()); d->solutionFilePath = buildDir.absoluteFilePath(project.name() + QStringLiteral(".sln")); d->solution = std::make_shared<VisualStudioSolution>(d->versionInfo); @@ -311,7 +312,7 @@ void VisualStudioGenerator::visitProject(const GeneratableProject &project) const auto relativeProjectFilePath = QFileInfo(d->solutionFilePath).dir() .relativeFilePath(projectFilePath); auto targetProject = std::make_shared<MSBuildQbsGenerateProject>(project, d->versionInfo); - targetProject->setGuid(d->guidPool->drawProductGuid(relativeProjectFilePath)); + targetProject->setGuid(d->guidPool->drawProductGuid(relativeProjectFilePath.toStdString())); d->msbuildProjects.insert(projectFilePath, targetProject); addPropertySheets(targetProject); @@ -346,7 +347,7 @@ void VisualStudioGenerator::visitProduct(const GeneratableProject &project, .dir().relativeFilePath(projectFilePath); auto targetProject = std::make_shared<MSBuildQbsProductProject>(project, productData, d->versionInfo); - targetProject->setGuid(d->guidPool->drawProductGuid(relativeProjectFilePath)); + targetProject->setGuid(d->guidPool->drawProductGuid(relativeProjectFilePath.toStdString())); addPropertySheets(targetProject); diff --git a/src/plugins/generator/visualstudio/visualstudioguidpool.cpp b/src/plugins/generator/visualstudio/visualstudioguidpool.cpp index 492244b80..780735fcb 100644 --- a/src/plugins/generator/visualstudio/visualstudioguidpool.cpp +++ b/src/plugins/generator/visualstudio/visualstudioguidpool.cpp @@ -30,9 +30,11 @@ #include "visualstudioguidpool.h" #include <tools/filesaver.h> -#include <QtCore/qfile.h> +#include <QtCore/quuid.h> +#include <fstream> #include <map> +#include <memory> #include <json.h> @@ -43,17 +45,20 @@ namespace qbs { class VisualStudioGuidPoolPrivate { public: - QString storeFilePath; + std::string storeFilePath; std::map<std::string, QUuid> productGuids; }; -VisualStudioGuidPool::VisualStudioGuidPool(const QString &storeFilePath) - : d(new VisualStudioGuidPoolPrivate) +VisualStudioGuidPool::VisualStudioGuidPool(const std::string &storeFilePath) + : d(std::make_shared<VisualStudioGuidPoolPrivate>()) { // Read any existing GUIDs from the on-disk store - QFile file(d->storeFilePath = storeFilePath); - if (file.exists() && file.open(QIODevice::ReadOnly)) { - const auto data = JsonDocument::fromJson(file.readAll().toStdString()).object(); + std::ifstream file(d->storeFilePath = storeFilePath); + if (file.is_open()) { + const auto data = JsonDocument::fromJson(std::string { + std::istreambuf_iterator<std::ifstream::char_type>(file), + std::istreambuf_iterator<std::ifstream::char_type>() + }).object(); for (auto it = data.constBegin(), end = data.constEnd(); it != end; ++it) { d->productGuids.insert({ it.key(), @@ -72,16 +77,16 @@ VisualStudioGuidPool::~VisualStudioGuidPool() productData.insert(it.first, it.second.toString().toStdString()); const auto data = JsonDocument(productData).toJson(); - file.write(QByteArray::fromRawData(data.data(), int(data.size()))); + file.write(std::vector<char> { data.cbegin(), data.cend() }); file.commit(); } } -QUuid VisualStudioGuidPool::drawProductGuid(const QString &productName) +QUuid VisualStudioGuidPool::drawProductGuid(const std::string &productName) { - if (d->productGuids.find(productName.toStdString()) == d->productGuids.cend()) - d->productGuids.insert({ productName.toStdString(), QUuid::createUuid() }); - return d->productGuids.at(productName.toStdString()); + if (d->productGuids.find(productName) == d->productGuids.cend()) + d->productGuids.insert({ productName, QUuid::createUuid() }); + return d->productGuids.at(productName); } } // namespace qbs diff --git a/src/plugins/generator/visualstudio/visualstudioguidpool.h b/src/plugins/generator/visualstudio/visualstudioguidpool.h index a21bb1f94..dd6dc9eba 100644 --- a/src/plugins/generator/visualstudio/visualstudioguidpool.h +++ b/src/plugins/generator/visualstudio/visualstudioguidpool.h @@ -31,8 +31,8 @@ #ifndef VISUALSTUDIOGUIDPOOL_H #define VISUALSTUDIOGUIDPOOL_H -#include <QtCore/qobject.h> -#include <QtCore/qscopedpointer.h> +#include <memory> + #include <QtCore/quuid.h> namespace qbs { @@ -48,13 +48,13 @@ class VisualStudioGuidPoolPrivate; class VisualStudioGuidPool { public: - explicit VisualStudioGuidPool(const QString &storeFilePath); + explicit VisualStudioGuidPool(const std::string &storeFilePath); ~VisualStudioGuidPool(); - QUuid drawProductGuid(const QString &productName); + QUuid drawProductGuid(const std::string &productName); private: - QScopedPointer<VisualStudioGuidPoolPrivate> d; + std::shared_ptr<VisualStudioGuidPoolPrivate> d; }; } // namespace qbs |