Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qrcreader.cpp
Go to the documentation of this file.
1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#include "qrcreader.h"
5#include "fmt.h"
6
7#include <QtCore/qcoreapplication.h>
8#include <QtCore/qfileinfo.h>
9#include <QtCore/qxmlstream.h>
10
11using namespace Qt::Literals::StringLiterals;
12
14
15bool isSupportedExtension(const QString &ext)
16{
17 return ext == "qml"_L1 || ext == "js"_L1 || ext == "qs"_L1 || ext == "ui"_L1 || ext == "jui"_L1;
18}
19
20ReadQrcResult readQrcFile(const QString &resourceFile, const QString &content)
21{
22 ReadQrcResult result;
23 QString dirPath = QFileInfo(resourceFile).path();
24 QXmlStreamReader reader(content);
25 bool isFileTag = false;
26 QStringList tagStack{ "RCC"_L1, "qresource"_L1, "file"_L1 };
27 int curDepth = 0;
28 while (!reader.atEnd()) {
29 QXmlStreamReader::TokenType t = reader.readNext();
30 switch (t) {
31 case QXmlStreamReader::StartElement:
32 if (curDepth >= tagStack.size() || reader.name() != tagStack.at(curDepth)) {
33 result.errorString = FMT::tr("unexpected <%1> tag\n")
34 .arg(reader.name().toString());
35 result.line = reader.lineNumber();
36 return result;
37 }
38 if (++curDepth == tagStack.size())
39 isFileTag = true;
40 break;
41
42 case QXmlStreamReader::EndElement:
43 isFileTag = false;
44 if (curDepth == 0 || reader.name() != tagStack.at(curDepth - 1)) {
45 result.errorString = FMT::tr("unexpected closing <%1> tag\n")
46 .arg(reader.name().toString());
47 result.line = reader.lineNumber();
48 return result;
49 }
50 --curDepth;
51 break;
52
53 case QXmlStreamReader::Characters:
54 if (isFileTag) {
55 QString fn = reader.text().toString();
56 if (!QFileInfo(fn).isAbsolute())
57 fn = dirPath + u'/' + fn;
58 QFileInfo cfi(fn);
59 if (isSupportedExtension(cfi.suffix()))
60 result.files << cfi.filePath();
61 }
62 break;
63
64 default:
65 break;
66 }
67 }
68 if (reader.error() != QXmlStreamReader::NoError) {
69 result.errorString = reader.errorString();
70 result.line = reader.lineNumber();
71 }
72 return result;
73}
74
75QT_END_NAMESPACE
ReadQrcResult readQrcFile(const QString &resourceFile, const QString &content)
Definition qrcreader.cpp:20
QT_BEGIN_NAMESPACE bool isSupportedExtension(const QString &ext)
Definition qrcreader.cpp:15