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
11bool isSupportedExtension(const QString &ext)
12{
13 return ext == QLatin1String("qml")
14 || ext == QLatin1String("js") || ext == QLatin1String("qs")
15 || ext == QLatin1String("ui") || ext == QLatin1String("jui");
16}
17
18ReadQrcResult readQrcFile(const QString &resourceFile, const QString &content)
19{
20 ReadQrcResult result;
21 QString dirPath = QFileInfo(resourceFile).path();
22 QXmlStreamReader reader(content);
23 bool isFileTag = false;
24 QStringList tagStack;
25 tagStack << QLatin1String("RCC") << QLatin1String("qresource") << QLatin1String("file");
26 int curDepth = 0;
27 while (!reader.atEnd()) {
28 QXmlStreamReader::TokenType t = reader.readNext();
29 switch (t) {
30 case QXmlStreamReader::StartElement:
31 if (curDepth >= tagStack.size() || reader.name() != tagStack.at(i: curDepth)) {
32 result.errorString = FMT::tr(sourceText: "unexpected <%1> tag\n")
33 .arg(a: reader.name().toString());
34 result.line = reader.lineNumber();
35 return result;
36 }
37 if (++curDepth == tagStack.size())
38 isFileTag = true;
39 break;
40
41 case QXmlStreamReader::EndElement:
42 isFileTag = false;
43 if (curDepth == 0 || reader.name() != tagStack.at(i: curDepth - 1)) {
44 result.errorString = FMT::tr(sourceText: "unexpected closing <%1> tag\n")
45 .arg(a: reader.name().toString());
46 result.line = reader.lineNumber();
47 return result;
48 }
49 --curDepth;
50 break;
51
52 case QXmlStreamReader::Characters:
53 if (isFileTag) {
54 QString fn = reader.text().toString();
55 if (!QFileInfo(fn).isAbsolute())
56 fn = dirPath + QLatin1Char('/') + fn;
57 QFileInfo cfi(fn);
58 if (isSupportedExtension(ext: cfi.suffix()))
59 result.files << cfi.filePath();
60 }
61 break;
62
63 default:
64 break;
65 }
66 }
67 if (reader.error() != QXmlStreamReader::NoError) {
68 result.errorString = reader.errorString();
69 result.line = reader.lineNumber();
70 }
71 return result;
72}
73

source code of qttools/src/linguist/shared/qrcreader.cpp