1 /****************************************************************************
2 **
3 ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the tools applications of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
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 Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef RCC_H
43 #define RCC_H
44 
45 #include <stdio.h>
46 
47 #include <QFileInfo>
48 #include <QHash>
49 #include <QIODevice>
50 #include <QLocale>
51 #include <QString>
52 #include <QStringList>
53 
54 #define TAG_RCC "RCC"
55 #define TAG_RESOURCE "qresource"
56 #define TAG_FILE "file"
57 
58 #define ATTRIBUTE_LANG "lang"
59 #define ATTRIBUTE_PREFIX "prefix"
60 #define ATTRIBUTE_ALIAS "alias"
61 #define ATTRIBUTE_THRESHOLD "threshold"
62 #define ATTRIBUTE_COMPRESS "compress"
63 
64 
65 #define CONSTANT_HEADER_SIZE 8
66 #define CONSTANT_COMPRESSLEVEL_DEFAULT -1
67 #define CONSTANT_COMPRESSTHRESHOLD_DEFAULT 70
68 
69 struct RCCFileInfo;
70 
71 class RCCResourceLibrary
72 {
73 public:
74     inline RCCResourceLibrary();
75     ~RCCResourceLibrary();
76 
77     bool output(const QString &out_name);
78 
79     bool readFiles();
80 
setInputFiles(QStringList files)81     inline void setInputFiles(QStringList files) { mFileNames = files; }
inputFiles()82     inline QStringList inputFiles() const { return mFileNames; }
83 
84     QStringList dataFiles() const;
85 
setVerbose(bool b)86     inline void setVerbose(bool b) { mVerbose = b; }
verbose()87     inline bool verbose() const { return mVerbose; }
88 
setCompressLevel(int c)89     inline void setCompressLevel(int c) { mCompressLevel = c; }
compressLevel()90     inline int compressLevel() const { return mCompressLevel; }
91 
setCompressThreshold(int t)92     inline void setCompressThreshold(int t) { mCompressThreshold = t; }
compressThreshold()93     inline int compressThreshold() const { return mCompressThreshold; }
94 
setResourceRoot(QString str)95     inline void setResourceRoot(QString str) { mResourceRoot = str; }
resourceRoot()96     inline QString resourceRoot() const { return mResourceRoot; }
97 
98 private:
99     RCCFileInfo *root;
100     bool addFile(const QString &alias, const RCCFileInfo &file);
101     bool interpretResourceFile(QIODevice *inputDevice, QString file, QString currentPath = QString());
102 
103     bool writeHeader(FILE *out);
104     bool writeDataBlobs(FILE *out);
105     bool writeDataNames(FILE *out);
106     bool writeDataStructure(FILE *out, int version);
107     bool writeInitializer(FILE *out);
108 
109     QStringList mFileNames;
110     QString mResourceRoot;
111     bool mVerbose;
112     int mCompressLevel;
113     int mCompressThreshold;
114     int mTreeOffset, mNamesOffset, mDataOffset;
115 };
116 
RCCResourceLibrary()117 inline RCCResourceLibrary::RCCResourceLibrary()
118 {
119     root = 0;
120     mVerbose = false;
121     mCompressLevel = -1;
122     mCompressThreshold = 70;
123     mTreeOffset = mNamesOffset = mDataOffset = 0;
124 }
125 
126 struct RCCFileInfo
127 {
128     enum Flags
129     {
130         NoFlags = 0x00,
131         Compressed = 0x01,
132         Directory = 0x02
133     };
134 
135     inline RCCFileInfo(QString name = QString(), QFileInfo fileInfo = QFileInfo(),
136                        QLocale locale = QLocale(), uint flags = NoFlags,
137                        int compressLevel = CONSTANT_COMPRESSLEVEL_DEFAULT, int compressThreshold = CONSTANT_COMPRESSTHRESHOLD_DEFAULT);
~RCCFileInfoRCCFileInfo138     ~RCCFileInfo() { qDeleteAll(children); }
resourceNameRCCFileInfo139     inline QString resourceName() {
140         QString resource = name;
141         for(RCCFileInfo *p = parent; p; p = p->parent)
142             resource = resource.prepend(p->name + "/");
143         return ":" + resource;
144     }
145 
146     int flags;
147     QString name;
148     QLocale locale;
149     QFileInfo fileInfo;
150     RCCFileInfo *parent;
151     QHash<QString, RCCFileInfo*> children;
152     int mCompressLevel;
153     int mCompressThreshold;
154 
155     qint64 nameOffset, dataOffset, childOffset;
156     qint64 writeDataBlob(FILE *out, qint64 offset);
157     qint64 writeDataName(FILE *out, qint64 offset);
158     void writeDataInfo(FILE *out, int version);
159 };
160 
RCCFileInfo(QString name,QFileInfo fileInfo,QLocale locale,uint flags,int compressLevel,int compressThreshold)161 inline RCCFileInfo::RCCFileInfo(QString name, QFileInfo fileInfo, QLocale locale, uint flags,
162                                 int compressLevel, int compressThreshold)
163 {
164     this->name = name;
165     this->fileInfo = fileInfo;
166     this->locale = locale;
167     this->flags = flags;
168     this->parent = 0;
169     this->nameOffset = this->dataOffset = this->childOffset = 0;
170     this->mCompressLevel = compressLevel;
171     this->mCompressThreshold = compressThreshold;
172 }
173 
174 #endif
175