diff options
Diffstat (limited to 'sources/shiboken6/ApiExtractor/fileout.cpp')
-rw-r--r-- | sources/shiboken6/ApiExtractor/fileout.cpp | 168 |
1 files changed, 59 insertions, 109 deletions
diff --git a/sources/shiboken6/ApiExtractor/fileout.cpp b/sources/shiboken6/ApiExtractor/fileout.cpp index b35693a38..6f9ec4d8a 100644 --- a/sources/shiboken6/ApiExtractor/fileout.cpp +++ b/sources/shiboken6/ApiExtractor/fileout.cpp @@ -1,34 +1,10 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of Qt for Python. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** 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 https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 #include "fileout.h" #include "messages.h" #include "reporthandler.h" +#include "exception.h" #include <QtCore/QFileInfo> #include <QtCore/QDir> @@ -36,8 +12,8 @@ #include <cstdio> -bool FileOut::dummy = false; -bool FileOut::diff = false; +bool FileOut::m_dryRun = false; +bool FileOut::m_diff = false; #ifdef Q_OS_LINUX static const char colorDelete[] = "\033[31m"; @@ -52,32 +28,34 @@ static const char colorReset[] = ""; #endif FileOut::FileOut(QString n) : - name(std::move(n)), - stream(&tmp), - isDone(false) + stream(&m_buffer), + m_name(std::move(n)), + m_isDone(false) { } FileOut::~FileOut() { - if (!isDone) - done(); + if (!m_isDone) { + qCWarning(lcShiboken).noquote().nospace() << __FUNCTION__ + << " file " << m_name << " not written."; + } } -static QList<int> lcsLength(const QByteArrayList &a, const QByteArrayList &b) +static QList<qsizetype> lcsLength(const QByteArrayList &a, const QByteArrayList &b) { - const int height = a.size() + 1; - const int width = b.size() + 1; + const auto height = a.size() + 1; + const auto width = b.size() + 1; - QList<int> res(width * height, 0); + QList<qsizetype> res(width * height, 0); - for (int row = 1; row < height; row++) { - for (int col = 1; col < width; col++) { - if (a[row-1] == b[col-1]) - res[width * row + col] = res[width * (row-1) + col-1] + 1; + for (qsizetype row = 1; row < height; row++) { + for (qsizetype col = 1; col < width; col++) { + if (a.at(row - 1) == b.at(col - 1)) + res[width * row + col] = res[width * (row - 1) + col - 1] + 1; else - res[width * row + col] = qMax(res[width * row + col-1], - res[width * (row-1) + col]); + res[width * row + col] = qMax(res[width * row + col - 1], + res[width * (row - 1) + col]); } } return res; @@ -92,8 +70,8 @@ enum Type { struct Unit { Type type; - int start; - int end; + qsizetype start; + qsizetype end; void print(const QByteArrayList &a, const QByteArrayList &b) const; }; @@ -103,33 +81,33 @@ void Unit::print(const QByteArrayList &a, const QByteArrayList &b) const switch (type) { case Unchanged: if ((end - start) > 9) { - for (int i = start; i <= start + 2; i++) + for (auto i = start; i <= start + 2; ++i) std::printf(" %s\n", a.at(i).constData()); std::printf("%s=\n= %d more lines\n=%s\n", - colorInfo, end - start - 6, colorReset); - for (int i = end - 2; i <= end; i++) + colorInfo, int(end - start - 6), colorReset); + for (auto i = end - 2; i <= end; ++i) std::printf(" %s\n", a.at(i).constData()); } else { - for (int i = start; i <= end; i++) + for (auto i = start; i <= end; ++i) std::printf(" %s\n", a.at(i).constData()); } break; case Add: std::fputs(colorAdd, stdout); - for (int i = start; i <= end; i++) + for (auto i = start; i <= end; ++i) std::printf("+ %s\n", b.at(i).constData()); std::fputs(colorReset, stdout); break; case Delete: std::fputs(colorDelete, stdout); - for (int i = start; i <= end; i++) + for (auto i = start; i <= end; ++i) std::printf("- %s\n", a.at(i).constData()); std::fputs(colorReset, stdout); break; } } -static void unitAppend(Type type, int pos, QList<Unit> *units) +static void unitAppend(Type type, qsizetype pos, QList<Unit> *units) { if (!units->isEmpty() && units->last().type == type) units->last().end = pos; @@ -137,9 +115,9 @@ static void unitAppend(Type type, int pos, QList<Unit> *units) units->append(Unit{type, pos, pos}); } -static QList<Unit> diffHelper(const QList<int> &lcs, - const QByteArrayList &a, const QByteArrayList &b, - int row, int col) +static QList<Unit> diffHelper(const QList<qsizetype> &lcs, + const QByteArrayList &a, const QByteArrayList &b, + qsizetype row, qsizetype col) { if (row > 0 && col > 0 && a.at(row - 1) == b.at(col - 1)) { QList<Unit> result = diffHelper(lcs, a, b, row - 1, col - 1); @@ -147,7 +125,7 @@ static QList<Unit> diffHelper(const QList<int> &lcs, return result; } - const int width = b.size() + 1; + const auto width = b.size() + 1; if (col > 0 && (row == 0 || lcs.at(width * row + col -1 ) >= lcs.at(width * (row - 1) + col))) { QList<Unit> result = diffHelper(lcs, a, b, row, col - 1); @@ -172,77 +150,49 @@ static void diff(const QByteArrayList &a, const QByteArrayList &b) FileOut::State FileOut::done() { - QString errorMessage; - const State result = done(&errorMessage); - if (result == Failure) - qCWarning(lcShiboken, "%s", qPrintable(errorMessage)); - return result; -} - -FileOut::State FileOut::done(QString *errorMessage) -{ - Q_ASSERT(!isDone); - if (name.isEmpty()) - return Failure; + if (m_isDone) + return Success; - isDone = true; bool fileEqual = false; - QFile fileRead(name); + QFile fileRead(m_name); QFileInfo info(fileRead); stream.flush(); QByteArray original; - if (info.exists() && (diff || (info.size() == tmp.size()))) { - if (!fileRead.open(QIODevice::ReadOnly)) { - *errorMessage = msgCannotOpenForReading(fileRead); - return Failure; - } + if (info.exists() && (m_diff || (info.size() == m_buffer.size()))) { + if (!fileRead.open(QIODevice::ReadOnly)) + throw Exception(msgCannotOpenForReading(fileRead)); original = fileRead.readAll(); fileRead.close(); - fileEqual = (original == tmp); + fileEqual = (original == m_buffer); } - if (fileEqual) + if (fileEqual) { + m_isDone = true; return Unchanged; + } - if (!FileOut::dummy) { + if (!FileOut::m_dryRun) { QDir dir(info.absolutePath()); if (!dir.mkpath(dir.absolutePath())) { - *errorMessage = QStringLiteral("unable to create directory '%1'") - .arg(QDir::toNativeSeparators(dir.absolutePath())); - return Failure; + const QString message = QString::fromLatin1("Unable to create directory '%1'") + .arg(QDir::toNativeSeparators(dir.absolutePath())); + throw Exception(message); } - QFile fileWrite(name); - if (!fileWrite.open(QIODevice::WriteOnly)) { - *errorMessage = msgCannotOpenForWriting(fileWrite); - return Failure; - } - if (fileWrite.write(tmp) == -1 || !fileWrite.flush()) { - *errorMessage = msgWriteFailed(fileWrite, tmp.size()); - return Failure; - } + QFile fileWrite(m_name); + if (!fileWrite.open(QIODevice::WriteOnly)) + throw Exception(msgCannotOpenForWriting(fileWrite)); + if (fileWrite.write(m_buffer) == -1 || !fileWrite.flush()) + throw Exception(msgWriteFailed(fileWrite, m_buffer.size())); } - if (diff) { - std::printf("%sFile: %s%s\n", colorInfo, qPrintable(name), colorReset); - ::diff(original.split('\n'), tmp.split('\n')); + if (m_diff) { + std::printf("%sFile: %s%s\n", colorInfo, qPrintable(m_name), colorReset); + ::diff(original.split('\n'), m_buffer.split('\n')); std::printf("\n"); } - return Success; -} + m_isDone = true; -void FileOut::touchFile(const QString &filePath) -{ - QFile toucher(filePath); - qint64 size = toucher.size(); - if (!toucher.open(QIODevice::ReadWrite)) { - qCWarning(lcShiboken).noquote().nospace() - << QStringLiteral("Failed to touch file '%1'") - .arg(QDir::toNativeSeparators(filePath)); - return; - } - toucher.resize(size+1); - toucher.resize(size); - toucher.close(); + return Success; } |