| Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/qtdeclarative/src/qtdeclarative/tools/qmlcachegen/resourcefilter.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||||||||
| 2 | ** | - | ||||||||||||
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||||||||
| 4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||
| 5 | ** | - | ||||||||||||
| 6 | ** This file is part of the QtQml module of the Qt Toolkit. | - | ||||||||||||
| 7 | ** | - | ||||||||||||
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ | - | ||||||||||||
| 9 | ** Commercial License Usage | - | ||||||||||||
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
| 11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
| 12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||||||||
| 16 | ** | - | ||||||||||||
| 17 | ** GNU General Public License Usage | - | ||||||||||||
| 18 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||
| 19 | ** General Public License version 3 as published by the Free Software | - | ||||||||||||
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT | - | ||||||||||||
| 21 | ** included in the packaging of this file. Please review the following | - | ||||||||||||
| 22 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||
| 24 | ** | - | ||||||||||||
| 25 | ** $QT_END_LICENSE$ | - | ||||||||||||
| 26 | ** | - | ||||||||||||
| 27 | ****************************************************************************/ | - | ||||||||||||
| 28 | #include <QString> | - | ||||||||||||
| 29 | #include <QXmlStreamReader> | - | ||||||||||||
| 30 | #include <QFile> | - | ||||||||||||
| 31 | #include <QDir> | - | ||||||||||||
| 32 | - | |||||||||||||
| 33 | int filterResourceFile(const QString &input, const QString &output) | - | ||||||||||||
| 34 | { | - | ||||||||||||
| 35 | enum State { | - | ||||||||||||
| 36 | InitialState, | - | ||||||||||||
| 37 | InRCC, | - | ||||||||||||
| 38 | InResource, | - | ||||||||||||
| 39 | InFile | - | ||||||||||||
| 40 | }; | - | ||||||||||||
| 41 | State state = InitialState; | - | ||||||||||||
| 42 | - | |||||||||||||
| 43 | QString prefix; | - | ||||||||||||
| 44 | QString currentFileName; | - | ||||||||||||
| 45 | QXmlStreamAttributes fileAttributes; | - | ||||||||||||
| 46 | - | |||||||||||||
| 47 | QFile file(input); | - | ||||||||||||
| 48 | if (!file.open(QIODevice::ReadOnly)) {
| 0 | ||||||||||||
| 49 | fprintf(stderr, "Cannot open %s for reading.\n", qPrintable(input)); | - | ||||||||||||
| 50 | return EXIT_FAILURE; never executed: return 1 ; | 0 | ||||||||||||
| 51 | } | - | ||||||||||||
| 52 | - | |||||||||||||
| 53 | QDir inputDirectory = QFileInfo(file).absoluteDir(); | - | ||||||||||||
| 54 | QDir outputDirectory = QFileInfo(output).absoluteDir(); | - | ||||||||||||
| 55 | - | |||||||||||||
| 56 | QString outputString; | - | ||||||||||||
| 57 | QXmlStreamWriter writer(&outputString); | - | ||||||||||||
| 58 | writer.setAutoFormatting(true); | - | ||||||||||||
| 59 | - | |||||||||||||
| 60 | QStringList remainingFiles; | - | ||||||||||||
| 61 | - | |||||||||||||
| 62 | QXmlStreamReader reader(&file); | - | ||||||||||||
| 63 | while (!reader.atEnd()) {
| 0 | ||||||||||||
| 64 | switch (reader.readNext()) { | - | ||||||||||||
| 65 | case QXmlStreamReader::StartDocument: { never executed: case QXmlStreamReader::StartDocument: | 0 | ||||||||||||
| 66 | QStringRef version = reader.documentVersion(); | - | ||||||||||||
| 67 | if (!version.isEmpty())
| 0 | ||||||||||||
| 68 | writer.writeStartDocument(version.toString()); never executed: writer.writeStartDocument(version.toString()); | 0 | ||||||||||||
| 69 | else | - | ||||||||||||
| 70 | writer.writeStartDocument(); never executed: writer.writeStartDocument(); | 0 | ||||||||||||
| 71 | break; never executed: break; | 0 | ||||||||||||
| 72 | } | - | ||||||||||||
| 73 | case QXmlStreamReader::EndDocument: never executed: case QXmlStreamReader::EndDocument: | 0 | ||||||||||||
| 74 | writer.writeEndDocument(); | - | ||||||||||||
| 75 | break; never executed: break; | 0 | ||||||||||||
| 76 | case QXmlStreamReader::StartElement: never executed: case QXmlStreamReader::StartElement: | 0 | ||||||||||||
| 77 | if (reader.name() == QStringLiteral("RCC")) { | - | ||||||||||||
| 78 | if (state != InitialState) {
| 0 | ||||||||||||
| 79 | fprintf(stderr, "Unexpected RCC tag in line %d\n", int(reader.lineNumber())); | - | ||||||||||||
| 80 | return EXIT_FAILURE; never executed: return 1 ; | 0 | ||||||||||||
| 81 | } | - | ||||||||||||
| 82 | state = InRCC; | - | ||||||||||||
| 83 | } else if (reader.name() == QStringLiteral("qresource")) { | - | ||||||||||||
| 84 | if (state != InRCC) {
| 0 | ||||||||||||
| 85 | fprintf(stderr, "Unexpected qresource tag in line %d\n", int(reader.lineNumber())); | - | ||||||||||||
| 86 | return EXIT_FAILURE; never executed: return 1 ; | 0 | ||||||||||||
| 87 | } | - | ||||||||||||
| 88 | state = InResource; | - | ||||||||||||
| 89 | QXmlStreamAttributes attributes = reader.attributes(); | - | ||||||||||||
| 90 | if (attributes.hasAttribute(QStringLiteral("prefix"))) never executed: return qstring_literal_temp;
| 0 | ||||||||||||
| 91 | prefix = attributes.value(QStringLiteral("prefix")).toString(); never executed: prefix = attributes.value(([]() noexcept -> QString { enum { Size = sizeof(u"" "prefix")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" "prefix" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return qstring_literal_temp; }())).toString();never executed: return qstring_literal_temp; | 0 | ||||||||||||
| 92 | if (!prefix.startsWith(QLatin1Char('/')))
| 0 | ||||||||||||
| 93 | prefix.prepend(QLatin1Char('/')); never executed: prefix.prepend(QLatin1Char('/')); | 0 | ||||||||||||
| 94 | if (!prefix.endsWith(QLatin1Char('/')))
| 0 | ||||||||||||
| 95 | prefix.append(QLatin1Char('/')); never executed: prefix.append(QLatin1Char('/')); | 0 | ||||||||||||
| 96 | } else if (reader.name() == QStringLiteral("file")) { | - | ||||||||||||
| 97 | if (state != InResource) {
| 0 | ||||||||||||
| 98 | fprintf(stderr, "Unexpected file tag in line %d\n", int(reader.lineNumber())); | - | ||||||||||||
| 99 | return EXIT_FAILURE; never executed: return 1 ; | 0 | ||||||||||||
| 100 | } | - | ||||||||||||
| 101 | state = InFile; | - | ||||||||||||
| 102 | fileAttributes = reader.attributes(); | - | ||||||||||||
| 103 | continue; never executed: continue; | 0 | ||||||||||||
| 104 | } | - | ||||||||||||
| 105 | writer.writeStartElement(reader.name().toString()); dead code: writer.writeStartElement(reader.name().toString()); | - | ||||||||||||
| 106 | writer.writeAttributes(reader.attributes()); dead code: writer.writeAttributes(reader.attributes()); | - | ||||||||||||
| 107 | continue; dead code: continue; | - | ||||||||||||
| 108 | - | |||||||||||||
| 109 | case QXmlStreamReader::EndElement: never executed: case QXmlStreamReader::EndElement: | 0 | ||||||||||||
| 110 | if (reader.name() == QStringLiteral("file")) { | - | ||||||||||||
| 111 | if (state != InFile) {
| 0 | ||||||||||||
| 112 | fprintf(stderr, "Unexpected end of file tag in line %d\n", int(reader.lineNumber())); | - | ||||||||||||
| 113 | return EXIT_FAILURE; never executed: return 1 ; | 0 | ||||||||||||
| 114 | } | - | ||||||||||||
| 115 | state = InResource; | - | ||||||||||||
| 116 | continue; never executed: continue; | 0 | ||||||||||||
| 117 | } else if (reader.name() == QStringLiteral("qresource")) { dead code: elsedead code: if (reader.name() == ([]() noexcept | - | ||||||||||||
| 118 | if (state != InResource) {
| 0 | ||||||||||||
| 119 | fprintf(stderr, "Unexpected end of qresource tag in line %d\n", int(reader.lineNumber())); | - | ||||||||||||
| 120 | return EXIT_FAILURE; never executed: return 1 ; | 0 | ||||||||||||
| 121 | } | - | ||||||||||||
| 122 | state = InRCC; | - | ||||||||||||
| 123 | } else if (reader.name() == QStringLiteral("RCC")) { | - | ||||||||||||
| 124 | if (state != InRCC) {
| 0 | ||||||||||||
| 125 | fprintf(stderr, "Unexpected end of RCC tag in line %d\n", int(reader.lineNumber())); | - | ||||||||||||
| 126 | return EXIT_FAILURE; never executed: return 1 ; | 0 | ||||||||||||
| 127 | } | - | ||||||||||||
| 128 | state = InitialState; | - | ||||||||||||
| 129 | } | - | ||||||||||||
| 130 | writer.writeEndElement(); | - | ||||||||||||
| 131 | continue; never executed: continue; | 0 | ||||||||||||
| 132 | - | |||||||||||||
| 133 | case QXmlStreamReader::Characters: never executed: case QXmlStreamReader::Characters: | 0 | ||||||||||||
| 134 | if (reader.isWhitespace())
| 0 | ||||||||||||
| 135 | break; never executed: break; | 0 | ||||||||||||
| 136 | if (state != InFile)
| 0 | ||||||||||||
| 137 | return EXIT_FAILURE; never executed: return 1 ; | 0 | ||||||||||||
| 138 | currentFileName = reader.text().toString(); | - | ||||||||||||
| 139 | if (currentFileName.isEmpty())
| 0 | ||||||||||||
| 140 | continue; never executed: continue; | 0 | ||||||||||||
| 141 | - | |||||||||||||
| 142 | if (!currentFileName.endsWith(QStringLiteral(".qml")) && !currentFileName.endsWith(QStringLiteral(".js"))) { never executed: return qstring_literal_temp;never executed: return qstring_literal_temp;
| 0 | ||||||||||||
| 143 | writer.writeStartElement(QStringLiteral("file")); never executed: return qstring_literal_temp; | 0 | ||||||||||||
| 144 | - | |||||||||||||
| 145 | if (!fileAttributes.hasAttribute(QStringLiteral("alias"))) never executed: return qstring_literal_temp;
| 0 | ||||||||||||
| 146 | fileAttributes.append(QStringLiteral("alias"), currentFileName); never executed: fileAttributes.append(([]() noexcept -> QString { enum { Size = sizeof(u"" "alias")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" "alias" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return qstring_literal_temp; }()), currentFileName);never executed: return qstring_literal_temp; | 0 | ||||||||||||
| 147 | - | |||||||||||||
| 148 | currentFileName = inputDirectory.absoluteFilePath(currentFileName); | - | ||||||||||||
| 149 | currentFileName = outputDirectory.relativeFilePath(currentFileName); | - | ||||||||||||
| 150 | - | |||||||||||||
| 151 | remainingFiles << currentFileName; | - | ||||||||||||
| 152 | - | |||||||||||||
| 153 | writer.writeAttributes(fileAttributes); | - | ||||||||||||
| 154 | writer.writeCharacters(currentFileName); | - | ||||||||||||
| 155 | writer.writeEndElement(); | - | ||||||||||||
| 156 | } never executed: end of block | 0 | ||||||||||||
| 157 | continue; never executed: continue; | 0 | ||||||||||||
| 158 | - | |||||||||||||
| 159 | default: break; never executed: break;never executed: default: | 0 | ||||||||||||
| 160 | } | - | ||||||||||||
| 161 | } | - | ||||||||||||
| 162 | - | |||||||||||||
| 163 | if (!remainingFiles.isEmpty()) {
| 0 | ||||||||||||
| 164 | QFile outputFile(output); | - | ||||||||||||
| 165 | if (!outputFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
| 0 | ||||||||||||
| 166 | fprintf(stderr, "Cannot open %s for writing.\n", qPrintable(output)); | - | ||||||||||||
| 167 | return EXIT_FAILURE; never executed: return 1 ; | 0 | ||||||||||||
| 168 | } | - | ||||||||||||
| 169 | const QByteArray outputStringUtf8 = outputString.toUtf8(); | - | ||||||||||||
| 170 | if (outputFile.write(outputStringUtf8) != outputStringUtf8.size())
| 0 | ||||||||||||
| 171 | return EXIT_FAILURE; never executed: return 1 ; | 0 | ||||||||||||
| 172 | - | |||||||||||||
| 173 | outputFile.close(); | - | ||||||||||||
| 174 | if (outputFile.error() != QFileDevice::NoError)
| 0 | ||||||||||||
| 175 | return EXIT_FAILURE; never executed: return 1 ; | 0 | ||||||||||||
| 176 | - | |||||||||||||
| 177 | // The build system expects this output if we wrote a qrc file and no output | - | ||||||||||||
| 178 | // if no files remain. | - | ||||||||||||
| 179 | fprintf(stdout, "New resource file written with %d files.\n", remainingFiles.count()); | - | ||||||||||||
| 180 | } never executed: end of block | 0 | ||||||||||||
| 181 | - | |||||||||||||
| 182 | return EXIT_SUCCESS; never executed: return 0 ; | 0 | ||||||||||||
| 183 | } | - | ||||||||||||
| Source code | Switch to Preprocessed file |