Don't try to mmap past EOF
[qt-netbsd.git] / tools / linguist / lconvert / main.cpp
blob6f5f86ad7e329b3b421b9c0b9b5caec048029d61
1 /****************************************************************************
2 **
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the Qt Linguist of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
38 ** $QT_END_LICENSE$
40 ****************************************************************************/
42 #include "translator.h"
44 #include <QtCore/QCoreApplication>
45 #include <QtCore/QDebug>
46 #include <QtCore/QString>
47 #include <QtCore/QStringList>
49 static int usage(const QStringList &args)
51 Q_UNUSED(args);
53 QString loaders;
54 QString line(QLatin1String(" %1 - %2\n"));
55 foreach (Translator::FileFormat format, Translator::registeredFileFormats())
56 loaders += line.arg(format.extension, -5).arg(format.description);
58 qWarning("%s", qPrintable(QString(QLatin1String("\nUsage:\n"
59 " lconvert [options] <infile> [<infile>...]\n\n"
60 "lconvert is part of Qt's Linguist tool chain. It can be used as a\n"
61 "stand-alone tool to convert and filter translation data files.\n"
62 "The following file formats are supported:\n\n%1\n"
63 "If multiple input files are specified, they are merged with\n"
64 "translations from later files taking precedence.\n\n"
65 "Options:\n"
66 " -h\n"
67 " --help Display this information and exit.\n\n"
68 " -i <infile>\n"
69 " --input-file <infile>\n"
70 " Specify input file. Use if <infile> might start with a dash.\n"
71 " This option can be used several times to merge inputs.\n"
72 " May be '-' (standard input) for use in a pipe.\n\n"
73 " -o <outfile>\n"
74 " --output-file <outfile>\n"
75 " Specify output file. Default is '-' (standard output).\n\n"
76 " -if <informat>\n"
77 " --input-format <format>\n"
78 " Specify input format for subsequent <infile>s.\n"
79 " The format is auto-detected from the file name and defaults to 'ts'.\n\n"
80 " -of <outformat>\n"
81 " --output-format <outformat>\n"
82 " Specify output format. See -if.\n\n"
83 " --input-codec <codec>\n"
84 " Specify encoding for QM and PO input files. Default is 'Latin1'\n"
85 " for QM and 'UTF-8' for PO files. UTF-8 is always tried as well for\n"
86 " QM, corresponding to the possible use of the trUtf8() function.\n\n"
87 " --output-codec <codec>\n"
88 " Specify encoding for PO output files. Default is 'UTF-8'.\n\n"
89 " --drop-tags <regexp>\n"
90 " Drop named extra tags when writing TS or XLIFF files.\n"
91 " May be specified repeatedly.\n\n"
92 " --drop-translations\n"
93 " Drop existing translations and reset the status to 'unfinished'.\n"
94 " Note: this implies --no-obsolete.\n\n"
95 " --source-language <language>[_<region>]\n"
96 " Specify/override the language of the source strings. Defaults to\n"
97 " POSIX if not specified and the file does not name it yet.\n\n"
98 " --target-language <language>[_<region>]\n"
99 " Specify/override the language of the translation.\n"
100 " The target language is guessed from the file name if this option\n"
101 " is not specified and the file contents name no language yet.\n\n"
102 " --no-obsolete\n"
103 " Drop obsolete messages.\n\n"
104 " --no-finished\n"
105 " Drop finished messages.\n\n"
106 " --sort-contexts\n"
107 " Sort contexts in output TS file alphabetically.\n\n"
108 " --locations {absolute|relative|none}\n"
109 " Override how source code references are saved in TS files.\n"
110 " Default is absolute.\n\n"
111 " --no-ui-lines\n"
112 " Drop line numbers from references to UI files.\n\n"
113 " --verbose\n"
114 " be a bit more verbose\n\n"
115 "Long options can be specified with only one leading dash, too.\n\n"
116 "Return value:\n"
117 " 0 on success\n"
118 " 1 on command line parse failures\n"
119 " 2 on read failures\n"
120 " 3 on write failures\n")).arg(loaders)));
121 return 1;
124 struct File
126 QString name;
127 QString format;
130 int main(int argc, char *argv[])
132 QCoreApplication app(argc, argv);
133 QStringList args = app.arguments();
135 QList<File> inFiles;
136 QString inFormat(QLatin1String("auto"));
137 QString outFileName;
138 QString outFormat(QLatin1String("auto"));
139 QString targetLanguage;
140 QString sourceLanguage;
141 bool dropTranslations = false;
142 bool noObsolete = false;
143 bool noFinished = false;
144 bool verbose = false;
145 bool noUiLines = false;
146 Translator::LocationsType locations = Translator::DefaultLocations;
148 ConversionData cd;
149 Translator tr;
151 for (int i = 1; i < args.size(); ++i) {
152 if (args[i].startsWith(QLatin1String("--")))
153 args[i].remove(0, 1);
154 if (args[i] == QLatin1String("-o")
155 || args[i] == QLatin1String("-output-file")) {
156 if (++i >= args.size())
157 return usage(args);
158 outFileName = args[i];
159 } else if (args[i] == QLatin1String("-of")
160 || args[i] == QLatin1String("-output-format")) {
161 if (++i >= args.size())
162 return usage(args);
163 outFormat = args[i];
164 } else if (args[i] == QLatin1String("-i")
165 || args[i] == QLatin1String("-input-file")) {
166 if (++i >= args.size())
167 return usage(args);
168 File file;
169 file.name = args[i];
170 file.format = inFormat;
171 inFiles.append(file);
172 } else if (args[i] == QLatin1String("-if")
173 || args[i] == QLatin1String("-input-format")) {
174 if (++i >= args.size())
175 return usage(args);
176 inFormat = args[i];
177 } else if (args[i] == QLatin1String("-input-codec")) {
178 if (++i >= args.size())
179 return usage(args);
180 cd.m_codecForSource = args[i].toLatin1();
181 } else if (args[i] == QLatin1String("-output-codec")) {
182 if (++i >= args.size())
183 return usage(args);
184 cd.m_outputCodec = args[i].toLatin1();
185 } else if (args[i] == QLatin1String("-drop-tag")) {
186 if (++i >= args.size())
187 return usage(args);
188 cd.m_dropTags.append(args[i]);
189 } else if (args[i] == QLatin1String("-drop-translations")) {
190 dropTranslations = true;
191 } else if (args[i] == QLatin1String("-target-language")) {
192 if (++i >= args.size())
193 return usage(args);
194 targetLanguage = args[i];
195 } else if (args[i] == QLatin1String("-source-language")) {
196 if (++i >= args.size())
197 return usage(args);
198 sourceLanguage = args[i];
199 } else if (args[i].startsWith(QLatin1String("-h"))) {
200 usage(args);
201 return 0;
202 } else if (args[i] == QLatin1String("-no-obsolete")) {
203 noObsolete = true;
204 } else if (args[i] == QLatin1String("-no-finished")) {
205 noFinished = true;
206 } else if (args[i] == QLatin1String("-sort-contexts")) {
207 cd.m_sortContexts = true;
208 } else if (args[i] == QLatin1String("-locations")) {
209 if (++i >= args.size())
210 return usage(args);
211 if (args[i] == QLatin1String("none"))
212 locations = Translator::NoLocations;
213 else if (args[i] == QLatin1String("relative"))
214 locations = Translator::RelativeLocations;
215 else if (args[i] == QLatin1String("absolute"))
216 locations = Translator::AbsoluteLocations;
217 else
218 return usage(args);
219 } else if (args[i] == QLatin1String("-no-ui-lines")) {
220 noUiLines = true;
221 } else if (args[i] == QLatin1String("-verbose")) {
222 verbose = true;
223 } else if (args[i].startsWith(QLatin1Char('-'))) {
224 return usage(args);
225 } else {
226 File file;
227 file.name = args[i];
228 file.format = inFormat;
229 inFiles.append(file);
233 if (inFiles.isEmpty())
234 return usage(args);
236 tr.setLanguageCode(Translator::guessLanguageCodeFromFileName(inFiles[0].name));
238 if (!tr.load(inFiles[0].name, cd, inFiles[0].format)) {
239 qWarning() << qPrintable(cd.error());
240 return 2;
242 Translator::reportDuplicates(tr.resolveDuplicates(), inFiles[0].name, verbose);
244 for (int i = 1; i < inFiles.size(); ++i) {
245 Translator tr2;
246 if (!tr2.load(inFiles[i].name, cd, inFiles[i].format)) {
247 qWarning() << qPrintable(cd.error());
248 return 2;
250 Translator::reportDuplicates(tr2.resolveDuplicates(), inFiles[i].name, verbose);
251 for (int j = 0; j < tr2.messageCount(); ++j)
252 tr.replaceSorted(tr2.message(j));
255 if (!targetLanguage.isEmpty())
256 tr.setLanguageCode(targetLanguage);
257 if (!sourceLanguage.isEmpty())
258 tr.setSourceLanguageCode(sourceLanguage);
259 if (noObsolete)
260 tr.stripObsoleteMessages();
261 if (noFinished)
262 tr.stripFinishedMessages();
263 if (dropTranslations)
264 tr.dropTranslations();
265 if (noUiLines)
266 tr.dropUiLines();
267 if (locations != Translator::DefaultLocations)
268 tr.setLocationsType(locations);
270 tr.normalizeTranslations(cd);
271 if (!cd.errors().isEmpty()) {
272 qWarning("%s", qPrintable(cd.error()));
273 cd.clearErrors();
275 if (!tr.save(outFileName, cd, outFormat)) {
276 qWarning("%s", qPrintable(cd.error()));
277 return 3;
279 return 0;