1// Copyright (C) 2018 The Qt Company Ltd.
2// Copyright (C) 2018 Intel Corporation.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
4
5// Note: A copy of this file is used in Qt Designer (qttools/src/designer/src/lib/shared/rcc_p.h)
6
7#ifndef RCC_H
8#define RCC_H
9
10#include <qstringlist.h>
11#include <qhash.h>
12#include <qstring.h>
13
14typedef struct ZSTD_CCtx_s ZSTD_CCtx;
15
16QT_BEGIN_NAMESPACE
17
18class RCCFileInfo;
19class QIODevice;
20class QTextStream;
21
22
23class RCCResourceLibrary
24{
25 RCCResourceLibrary(const RCCResourceLibrary &);
26 RCCResourceLibrary &operator=(const RCCResourceLibrary &);
27
28public:
29 RCCResourceLibrary(quint8 formatVersion);
30 ~RCCResourceLibrary();
31
32 bool output(QIODevice &outDevice, QIODevice &tempDevice, QIODevice &errorDevice);
33
34 bool readFiles(bool listMode, QIODevice &errorDevice);
35
36 enum Format { Binary, C_Code, Pass1, Pass2, Python_Code };
37 void setFormat(Format f) { m_format = f; }
38 Format format() const { return m_format; }
39
40 void setInputFiles(const QStringList &files) { m_fileNames = files; }
41 QStringList inputFiles() const { return m_fileNames; }
42
43 QStringList dataFiles() const;
44
45 // Return a map of resource identifier (':/newPrefix/images/p1.png') to file.
46 typedef QHash<QString, QString> ResourceDataFileMap;
47 ResourceDataFileMap resourceDataFileMap() const;
48
49 void setVerbose(bool b) { m_verbose = b; }
50 bool verbose() const { return m_verbose; }
51
52 void setInitName(const QString &name) { m_initName = name; }
53 QString initName() const { return m_initName; }
54
55 void setOutputName(const QString &name) { m_outputName = name; }
56 QString outputName() const { return m_outputName; }
57
58 enum class CompressionAlgorithm {
59 Zlib,
60 Zstd,
61
62 Best = 99,
63 None = -1
64 };
65
66 static CompressionAlgorithm parseCompressionAlgorithm(QStringView algo, QString *errorMsg);
67 void setCompressionAlgorithm(CompressionAlgorithm algo) { m_compressionAlgo = algo; }
68 CompressionAlgorithm compressionAlgorithm() const { return m_compressionAlgo; }
69
70 static int parseCompressionLevel(CompressionAlgorithm algo, const QString &level, QString *errorMsg);
71 void setCompressLevel(int c) { m_compressLevel = c; }
72 int compressLevel() const { return m_compressLevel; }
73
74 void setCompressThreshold(int t) { m_compressThreshold = t; }
75 int compressThreshold() const { return m_compressThreshold; }
76
77 void setResourceRoot(const QString &root) { m_resourceRoot = root; }
78 QString resourceRoot() const { return m_resourceRoot; }
79
80 void setUseNameSpace(bool v) { m_useNameSpace = v; }
81 bool useNameSpace() const { return m_useNameSpace; }
82
83 QStringList failedResources() const { return m_failedResources; }
84
85 int formatVersion() const { return m_formatVersion; }
86
87 void setNoZstd(bool v) { m_noZstd = v; }
88 bool noZstd() const { return m_noZstd; }
89
90private:
91 struct Strings {
92 Strings();
93 const QString TAG_RCC;
94 const QString TAG_RESOURCE;
95 const QString TAG_FILE;
96 const QString ATTRIBUTE_LANG;
97 const QString ATTRIBUTE_PREFIX;
98 const QString ATTRIBUTE_ALIAS;
99 const QString ATTRIBUTE_EMPTY;
100 const QString ATTRIBUTE_THRESHOLD;
101 const QString ATTRIBUTE_COMPRESS;
102 const QString ATTRIBUTE_COMPRESSALGO;
103 };
104 friend class RCCFileInfo;
105 void reset();
106 bool addFile(const QString &alias, RCCFileInfo file);
107 bool interpretResourceFile(QIODevice *inputDevice, const QString &file,
108 QString currentPath = QString(), bool listMode = false);
109 bool writeHeader();
110 bool writeDataBlobs();
111 bool writeDataNames();
112 bool writeDataStructure();
113 bool writeInitializer();
114 void writeMangleNamespaceFunction(const QByteArray &name);
115 void writeAddNamespaceFunction(const QByteArray &name);
116 void writeDecimal(int value);
117 void writeHex(quint8 number);
118 void write2HexDigits(quint8 number);
119 void writeNumber2(quint16 number);
120 void writeNumber4(quint32 number);
121 void writeNumber8(quint64 number);
122 void writeChar(char c) { m_out.append(c); }
123 void writeByteArray(const QByteArray &);
124 void write(const char *, int len);
125 void writeString(const char *s) { write(s, len: static_cast<int>(strlen(s: s))); }
126
127#if QT_CONFIG(zstd)
128 ZSTD_CCtx *m_zstdCCtx;
129#endif
130
131 const Strings m_strings;
132 RCCFileInfo *m_root;
133 QStringList m_fileNames;
134 QString m_resourceRoot;
135 QString m_initName;
136 QString m_outputName;
137 Format m_format;
138 bool m_verbose;
139 CompressionAlgorithm m_compressionAlgo;
140 int m_compressLevel;
141 int m_compressThreshold;
142 int m_treeOffset;
143 int m_namesOffset;
144 int m_dataOffset;
145 quint32 m_overallFlags;
146 bool m_useNameSpace;
147 QStringList m_failedResources;
148 QIODevice *m_errorDevice;
149 QIODevice *m_outDevice;
150 QByteArray m_out;
151 quint8 m_formatVersion;
152 bool m_noZstd;
153};
154
155QT_END_NAMESPACE
156
157#endif // RCC_H
158

source code of qtbase/src/tools/rcc/rcc.h