diff options
-rw-r--r-- | src/lib/corelib/corelib.qbs | 2 | ||||
-rw-r--r-- | src/lib/corelib/tools/filesaver.cpp | 83 | ||||
-rw-r--r-- | src/lib/corelib/tools/filesaver.h | 65 | ||||
-rw-r--r-- | src/lib/corelib/tools/tools.pri | 2 |
4 files changed, 152 insertions, 0 deletions
diff --git a/src/lib/corelib/corelib.qbs b/src/lib/corelib/corelib.qbs index 784afee5e..2c025b1b1 100644 --- a/src/lib/corelib/corelib.qbs +++ b/src/lib/corelib/corelib.qbs @@ -354,6 +354,8 @@ QbsLibrary { "executablefinder.h", "fileinfo.cpp", "fileinfo.h", + "filesaver.cpp", + "filesaver.h", "filetime.h", "generateoptions.cpp", "hostosinfo.h", diff --git a/src/lib/corelib/tools/filesaver.cpp b/src/lib/corelib/tools/filesaver.cpp new file mode 100644 index 000000000..b7ba69143 --- /dev/null +++ b/src/lib/corelib/tools/filesaver.cpp @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing +** +** This file is part of Qbs. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms and +** conditions see http://www.qt.io/terms-conditions. For further information +** use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +****************************************************************************/ + +#include "filesaver.h" + +#include <QFile> +#include <QSaveFile> + +namespace qbs { +namespace Internal { + +FileSaver::FileSaver(const QString &filePath, bool overwriteIfUnchanged) + : m_filePath(filePath), m_overwriteIfUnchanged(overwriteIfUnchanged) +{ +} + +QIODevice *FileSaver::device() +{ + return m_memoryDevice.data(); +} + +bool FileSaver::open() +{ + if (!m_overwriteIfUnchanged) { + QFile file(m_filePath); + if (file.open(QIODevice::ReadOnly)) + m_oldFileContents = file.readAll(); + else + m_oldFileContents.clear(); + } + + m_newFileContents.clear(); + m_memoryDevice.reset(new QBuffer(&m_newFileContents)); + return m_memoryDevice->open(QIODevice::WriteOnly); +} + +bool FileSaver::commit() +{ + if (!m_overwriteIfUnchanged && m_oldFileContents == m_newFileContents) + return true; // no need to write unchanged data + + QSaveFile saveFile(m_filePath); + if (!saveFile.open(QIODevice::WriteOnly)) + return false; + + saveFile.write(m_newFileContents); + return saveFile.commit(); +} + +qint64 FileSaver::write(const QByteArray &data) +{ + return device()->write(data); +} + +} // namespace Internal +} // namespace qbs diff --git a/src/lib/corelib/tools/filesaver.h b/src/lib/corelib/tools/filesaver.h new file mode 100644 index 000000000..ae69a83d6 --- /dev/null +++ b/src/lib/corelib/tools/filesaver.h @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing +** +** This file is part of Qbs. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms and +** conditions see http://www.qt.io/terms-conditions. For further information +** use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 or version 3 as published by the Free +** Software Foundation and appearing in the file LICENSE.LGPLv21 and +** LICENSE.LGPLv3 included in the packaging of this file. Please review the +** following information to ensure the GNU Lesser General Public License +** requirements will be met: https://www.gnu.org/licenses/lgpl.html and +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +****************************************************************************/ + +#ifndef FILESAVER_H +#define FILESAVER_H + +#include <QBuffer> +#include <QByteArray> +#include <QScopedPointer> +#include <QString> + +namespace qbs { +namespace Internal { + +/*! + * QSaveFile wrapper which doesn't update the target file if the contents are unchanged. + */ +class FileSaver { +public: + FileSaver(const QString &filePath, bool overwriteIfUnchanged = false); + + QIODevice *device(); + bool open(); + bool commit(); + qint64 write(const QByteArray &data); + +private: + QByteArray m_newFileContents; + QByteArray m_oldFileContents; + QScopedPointer<QBuffer> m_memoryDevice; + const QString m_filePath; + const bool m_overwriteIfUnchanged; +}; + +} // namespace Internal +} // namespace qbs + +#endif // FILESAVER_H diff --git a/src/lib/corelib/tools/tools.pri b/src/lib/corelib/tools/tools.pri index 585985406..7bcf60516 100644 --- a/src/lib/corelib/tools/tools.pri +++ b/src/lib/corelib/tools/tools.pri @@ -10,6 +10,7 @@ HEADERS += \ $$PWD/error.h \ $$PWD/executablefinder.h \ $$PWD/fileinfo.h \ + $$PWD/filesaver.h \ $$PWD/filetime.h \ $$PWD/generateoptions.h \ $$PWD/id.h \ @@ -54,6 +55,7 @@ SOURCES += \ $$PWD/error.cpp \ $$PWD/executablefinder.cpp \ $$PWD/fileinfo.cpp \ + $$PWD/filesaver.cpp \ $$PWD/generateoptions.cpp \ $$PWD/id.cpp \ $$PWD/jsliterals.cpp \ |