Mercurial > projects > qtd
annotate tools/drcc/main.cpp @ 410:bb0c0bbca312
Added wrappers for the QFontMetrics methods taking tabstop arrays.
Also removed some stray code template instantiations.
author | David Nadlinger <code@klickverbot.at> |
---|---|
date | Sun, 20 Mar 2011 20:43:06 +0100 |
parents | a2871e6b8b15 |
children |
rev | line source |
---|---|
57 | 1 /**************************************************************************** |
2 ** | |
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | |
4 ** Contact: Qt Software Information (qt-info@nokia.com) | |
5 ** | |
6 ** This file is part of the tools applications of the Qt Toolkit. | |
7 ** | |
8 ** $QT_BEGIN_LICENSE:LGPL$ | |
9 ** Commercial Usage | |
10 ** Licensees holding valid Qt Commercial licenses may use this file in | |
11 ** accordance with the Qt Commercial License Agreement provided with the | |
12 ** Software or, alternatively, in accordance with the terms contained in | |
13 ** a written agreement between you and Nokia. | |
14 ** | |
15 ** GNU Lesser General Public License Usage | |
16 ** Alternatively, this file may be used under the terms of the GNU Lesser | |
17 ** General Public License version 2.1 as published by the Free Software | |
18 ** Foundation and appearing in the file LICENSE.LGPL included in the | |
19 ** packaging of this file. Please review the following information to | |
20 ** ensure the GNU Lesser General Public License version 2.1 requirements | |
21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | |
22 ** | |
23 ** In addition, as a special exception, Nokia gives you certain | |
24 ** additional rights. These rights are described in the Nokia Qt LGPL | |
25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this | |
26 ** package. | |
27 ** | |
28 ** GNU General Public License Usage | |
29 ** Alternatively, this file may be used under the terms of the GNU | |
30 ** General Public License version 3.0 as published by the Free Software | |
31 ** Foundation and appearing in the file LICENSE.GPL included in the | |
32 ** packaging of this file. Please review the following information to | |
33 ** ensure the GNU General Public License version 3.0 requirements will be | |
34 ** met: http://www.gnu.org/copyleft/gpl.html. | |
35 ** | |
36 ** If you are unsure which license is appropriate for your use, please | |
37 ** contact the sales department at qt-sales@nokia.com. | |
38 ** $QT_END_LICENSE$ | |
39 ** | |
40 ****************************************************************************/ | |
41 | |
42 #include "rcc.h" | |
43 #include "qcorecmdlineargs_p.h" | |
44 | |
45 #include <QDebug> | |
46 #include <QDir> | |
47 #include <QFile> | |
48 #include <QFileInfo> | |
49 #include <QTextStream> | |
50 | |
51 QT_BEGIN_NAMESPACE | |
52 | |
53 void showHelp(const QString &argv0, const QString &error) | |
54 { | |
55 fprintf(stderr, "Qt resource compiler\n"); | |
56 if (!error.isEmpty()) | |
57 fprintf(stderr, "%s: %s\n", qPrintable(argv0), qPrintable(error)); | |
58 fprintf(stderr, "Usage: %s [options] <inputs>\n\n" | |
59 "Options:\n" | |
61
a2871e6b8b15
drcc: automatic initialization of resources with program start (disabled with option -no-static-initialize)
SokoL_SD
parents:
57
diff
changeset
|
60 " -o file write output to file rather than stdout\n" |
a2871e6b8b15
drcc: automatic initialization of resources with program start (disabled with option -no-static-initialize)
SokoL_SD
parents:
57
diff
changeset
|
61 " -name name create an external initialization function with name\n" |
a2871e6b8b15
drcc: automatic initialization of resources with program start (disabled with option -no-static-initialize)
SokoL_SD
parents:
57
diff
changeset
|
62 " -threshold level threshold to consider compressing files\n" |
a2871e6b8b15
drcc: automatic initialization of resources with program start (disabled with option -no-static-initialize)
SokoL_SD
parents:
57
diff
changeset
|
63 " -compress level compress input files by level\n" |
a2871e6b8b15
drcc: automatic initialization of resources with program start (disabled with option -no-static-initialize)
SokoL_SD
parents:
57
diff
changeset
|
64 " -root path prefix resource access path with root path\n" |
a2871e6b8b15
drcc: automatic initialization of resources with program start (disabled with option -no-static-initialize)
SokoL_SD
parents:
57
diff
changeset
|
65 " -no-compress disable all compression\n" |
a2871e6b8b15
drcc: automatic initialization of resources with program start (disabled with option -no-static-initialize)
SokoL_SD
parents:
57
diff
changeset
|
66 " -no-static-initialize disable automatic initialization of resources with program start\n" |
a2871e6b8b15
drcc: automatic initialization of resources with program start (disabled with option -no-static-initialize)
SokoL_SD
parents:
57
diff
changeset
|
67 " -binary output a binary file for use as a dynamic resource\n" |
a2871e6b8b15
drcc: automatic initialization of resources with program start (disabled with option -no-static-initialize)
SokoL_SD
parents:
57
diff
changeset
|
68 " -namespace turn off namespace macros\n" |
a2871e6b8b15
drcc: automatic initialization of resources with program start (disabled with option -no-static-initialize)
SokoL_SD
parents:
57
diff
changeset
|
69 " -project Output a resource file containing all\n" |
a2871e6b8b15
drcc: automatic initialization of resources with program start (disabled with option -no-static-initialize)
SokoL_SD
parents:
57
diff
changeset
|
70 " files from the current directory\n" |
a2871e6b8b15
drcc: automatic initialization of resources with program start (disabled with option -no-static-initialize)
SokoL_SD
parents:
57
diff
changeset
|
71 " -version display version\n" |
a2871e6b8b15
drcc: automatic initialization of resources with program start (disabled with option -no-static-initialize)
SokoL_SD
parents:
57
diff
changeset
|
72 " -help display this information\n", |
57 | 73 qPrintable(argv0)); |
74 } | |
75 | |
76 void dumpRecursive(const QDir &dir, QTextStream &out) | |
77 { | |
78 QFileInfoList entries = dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | |
79 | QDir::NoSymLinks); | |
80 foreach (QFileInfo entry, entries) { | |
81 if (entry.isDir()) { | |
82 dumpRecursive(entry.filePath(), out); | |
83 } else { | |
84 out << QLatin1String("<file>") | |
85 << entry.filePath() | |
86 << QLatin1String("</file>\n"); | |
87 } | |
88 } | |
89 } | |
90 | |
91 int createProject(const QString &outFileName) | |
92 { | |
93 QDir currentDir = QDir::current(); | |
94 QString currentDirName = currentDir.dirName(); | |
95 if (currentDirName.isEmpty()) | |
96 currentDirName = QLatin1String("root"); | |
97 | |
98 QFile file; | |
99 bool isOk = false; | |
100 if (outFileName.isEmpty()) { | |
101 isOk = file.open(stdout, QFile::WriteOnly | QFile::Text); | |
102 } else { | |
103 file.setFileName(outFileName); | |
104 isOk = file.open(QFile::WriteOnly | QFile::Text); | |
105 } | |
106 if (!isOk) { | |
107 fprintf(stderr, "Unable to open %s: %s\n", | |
108 outFileName.isEmpty() ? qPrintable(outFileName) : "standard output", | |
109 qPrintable(file.errorString())); | |
110 return 1; | |
111 } | |
112 | |
113 QTextStream out(&file); | |
114 out << QLatin1String("<!DOCTYPE RCC><RCC version=\"1.0\">\n" | |
115 "<qresource>\n"); | |
116 | |
117 // use "." as dir to get relative file pathes | |
118 dumpRecursive(QDir(QLatin1String(".")), out); | |
119 | |
120 out << QLatin1String("</qresource>\n" | |
121 "</RCC>\n"); | |
122 | |
123 return 0; | |
124 } | |
125 | |
126 int runRcc(int argc, char *argv[]) | |
127 { | |
128 QString outFilename; | |
129 bool helpRequested = false; | |
130 bool list = false; | |
131 bool projectRequested = false; | |
132 QStringList filenamesIn; | |
133 | |
134 QStringList args = qCmdLineArgs(argc, argv); | |
135 | |
136 RCCResourceLibrary library; | |
137 | |
138 //parse options | |
139 QString errorMsg; | |
140 for (int i = 1; i < args.count() && errorMsg.isEmpty(); i++) { | |
141 if (args[i].isEmpty()) | |
142 continue; | |
143 if (args[i][0] == QLatin1Char('-')) { // option | |
144 QString opt = args[i]; | |
145 if (opt == QLatin1String("-o")) { | |
146 if (!(i < argc-1)) { | |
147 errorMsg = QLatin1String("Missing output name"); | |
148 break; | |
149 } | |
150 outFilename = args[++i]; | |
151 } else if (opt == QLatin1String("-name")) { | |
152 if (!(i < argc-1)) { | |
153 errorMsg = QLatin1String("Missing target name"); | |
154 break; | |
155 } | |
156 library.setInitName(args[++i]); | |
157 } else if (opt == QLatin1String("-root")) { | |
158 if (!(i < argc-1)) { | |
159 errorMsg = QLatin1String("Missing root path"); | |
160 break; | |
161 } | |
162 library.setResourceRoot(QDir::cleanPath(args[++i])); | |
163 if (library.resourceRoot().isEmpty() | |
164 || library.resourceRoot().at(0) != QLatin1Char('/')) | |
165 errorMsg = QLatin1String("Root must start with a /"); | |
166 } else if (opt == QLatin1String("-compress")) { | |
167 if (!(i < argc-1)) { | |
168 errorMsg = QLatin1String("Missing compression level"); | |
169 break; | |
170 } | |
171 library.setCompressLevel(args[++i].toInt()); | |
172 } else if (opt == QLatin1String("-threshold")) { | |
173 if (!(i < argc-1)) { | |
174 errorMsg = QLatin1String("Missing compression threshold"); | |
175 break; | |
176 } | |
177 library.setCompressThreshold(args[++i].toInt()); | |
178 } else if (opt == QLatin1String("-binary")) { | |
179 library.setFormat(RCCResourceLibrary::Binary); | |
180 } else if (opt == QLatin1String("-namespace")) { | |
181 // library.setUseNameSpace(!library.useNameSpace()); | |
182 library.setUseNameSpace(false); | |
183 } else if (opt == QLatin1String("-verbose")) { | |
184 library.setVerbose(true); | |
185 } else if (opt == QLatin1String("-list")) { | |
186 list = true; | |
187 } else if (opt == QLatin1String("-version") || opt == QLatin1String("-v")) { | |
188 fprintf(stderr, "Qt Resource Compiler version %s\n", QT_VERSION_STR); | |
189 return 1; | |
190 } else if (opt == QLatin1String("-help") || opt == QLatin1String("-h")) { | |
191 helpRequested = true; | |
192 } else if (opt == QLatin1String("-no-compress")) { | |
193 library.setCompressLevel(-2); | |
61
a2871e6b8b15
drcc: automatic initialization of resources with program start (disabled with option -no-static-initialize)
SokoL_SD
parents:
57
diff
changeset
|
194 } else if (opt == QLatin1String("-no-static-initialize")) { |
a2871e6b8b15
drcc: automatic initialization of resources with program start (disabled with option -no-static-initialize)
SokoL_SD
parents:
57
diff
changeset
|
195 library.setStaticInitialize(false); |
57 | 196 } else if (opt == QLatin1String("-project")) { |
197 projectRequested = true; | |
198 } else { | |
199 errorMsg = QString::fromLatin1("Unknown option: '%1'").arg(args[i]); | |
200 } | |
201 } else { | |
202 if (!QFile::exists(args[i])) { | |
203 qWarning("%s: File does not exist '%s'", | |
204 qPrintable(args[0]), qPrintable(args[i])); | |
205 return 1; | |
206 } | |
207 filenamesIn.append(args[i]); | |
208 } | |
209 } | |
210 | |
211 if (projectRequested && !helpRequested) { | |
212 return createProject(outFilename); | |
213 } | |
214 | |
215 if (!filenamesIn.size() || !errorMsg.isEmpty() || helpRequested) { | |
216 showHelp(args[0], errorMsg); | |
217 return 1; | |
218 } | |
219 QFile errorDevice; | |
220 errorDevice.open(stderr, QIODevice::WriteOnly|QIODevice::Text); | |
221 | |
222 if (library.verbose()) | |
223 errorDevice.write("Qt resource compiler\n"); | |
224 | |
225 library.setInputFiles(filenamesIn); | |
226 | |
227 if (!library.readFiles(list, errorDevice)) | |
228 return 1; | |
229 | |
230 // open output | |
231 QFile out; | |
232 QIODevice::OpenMode mode = QIODevice::WriteOnly; | |
233 if (library.format() == RCCResourceLibrary::C_Code) | |
234 mode |= QIODevice::Text; | |
235 | |
236 if (outFilename.isEmpty() || outFilename == QLatin1String("-")) { | |
237 // using this overload close() only flushes. | |
238 out.open(stdout, mode); | |
239 } else { | |
240 out.setFileName(outFilename); | |
241 if (!out.open(mode)) { | |
242 const QString msg = QString::fromUtf8("Unable to open %1 for writing: %2\n").arg(outFilename).arg(out.errorString()); | |
243 errorDevice.write(msg.toUtf8()); | |
244 return 1; | |
245 } | |
246 } | |
247 | |
248 // do the task | |
249 if (list) { | |
250 const QStringList data = library.dataFiles(); | |
251 for (int i = 0; i < data.size(); ++i) { | |
252 out.write(qPrintable(QDir::cleanPath(data.at(i)))); | |
253 out.write("\n"); | |
254 } | |
255 return 0; | |
256 } | |
257 | |
258 return library.output(out, errorDevice) ? 0 : 1; | |
259 } | |
260 | |
261 QT_END_NAMESPACE | |
262 | |
263 int main(int argc, char *argv[]) | |
264 { | |
265 return QT_PREPEND_NAMESPACE(runRcc)(argc, argv); | |
266 } |