1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2018 Intel Corporation. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the examples of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:BSD$ |
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 The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** BSD License Usage |
18 | ** Alternatively, you may use this file under the terms of the BSD license |
19 | ** as follows: |
20 | ** |
21 | ** "Redistribution and use in source and binary forms, with or without |
22 | ** modification, are permitted provided that the following conditions are |
23 | ** met: |
24 | ** * Redistributions of source code must retain the above copyright |
25 | ** notice, this list of conditions and the following disclaimer. |
26 | ** * Redistributions in binary form must reproduce the above copyright |
27 | ** notice, this list of conditions and the following disclaimer in |
28 | ** the documentation and/or other materials provided with the |
29 | ** distribution. |
30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
31 | ** contributors may be used to endorse or promote products derived |
32 | ** from this software without specific prior written permission. |
33 | ** |
34 | ** |
35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
46 | ** |
47 | ** $QT_END_LICENSE$ |
48 | ** |
49 | ****************************************************************************/ |
50 | |
51 | #include "converter.h" |
52 | |
53 | #include <QCommandLineParser> |
54 | #include <QCommandLineOption> |
55 | #include <QCoreApplication> |
56 | #include <QFile> |
57 | #include <QFileInfo> |
58 | |
59 | #include <stdio.h> |
60 | |
61 | static QList<Converter *> *availableConverters; |
62 | |
63 | Converter::Converter() |
64 | { |
65 | if (!availableConverters) |
66 | availableConverters = new QList<Converter *>; |
67 | availableConverters->append(this); |
68 | } |
69 | |
70 | Converter::~Converter() |
71 | { |
72 | availableConverters->removeAll(this); |
73 | } |
74 | |
75 | int main(int argc, char *argv[]) |
76 | { |
77 | QCoreApplication app(argc, argv); |
78 | |
79 | QStringList inputFormats; |
80 | QStringList outputFormats; |
81 | for (Converter *conv : qAsConst(*availableConverters)) { |
82 | auto direction = conv->directions(); |
83 | QString name = conv->name(); |
84 | if (direction & Converter::In) |
85 | inputFormats << name; |
86 | if (direction & Converter::Out) |
87 | outputFormats << name; |
88 | } |
89 | inputFormats.sort(); |
90 | outputFormats.sort(); |
91 | inputFormats.prepend("auto" ); |
92 | outputFormats.prepend("auto" ); |
93 | |
94 | QCommandLineParser parser; |
95 | parser.setApplicationDescription(QStringLiteral("Qt file format conversion tool" )); |
96 | parser.addHelpOption(); |
97 | |
98 | QCommandLineOption inputFormatOption(QStringList{"I" , "input-format" }); |
99 | inputFormatOption.setDescription(QLatin1String("Select the input format for the input file. Available formats: " ) + |
100 | inputFormats.join(", " )); |
101 | inputFormatOption.setValueName("format" ); |
102 | inputFormatOption.setDefaultValue(inputFormats.constFirst()); |
103 | parser.addOption(inputFormatOption); |
104 | |
105 | QCommandLineOption outputFormatOption(QStringList{"O" , "output-format" }); |
106 | outputFormatOption.setDescription(QLatin1String("Select the output format for the output file. Available formats: " ) + |
107 | outputFormats.join(", " )); |
108 | outputFormatOption.setValueName("format" ); |
109 | outputFormatOption.setDefaultValue(outputFormats.constFirst()); |
110 | parser.addOption(outputFormatOption); |
111 | |
112 | QCommandLineOption optionOption(QStringList{"o" , "option" }); |
113 | optionOption.setDescription(QStringLiteral("Format-specific options. Use --format-options to find out what options are available." )); |
114 | optionOption.setValueName("options..." ); |
115 | optionOption.setDefaultValues({}); |
116 | parser.addOption(optionOption); |
117 | |
118 | QCommandLineOption formatOptionsOption("format-options" ); |
119 | formatOptionsOption.setDescription(QStringLiteral("Prints the list of valid options for --option for the converter format <format>." )); |
120 | formatOptionsOption.setValueName("format" ); |
121 | parser.addOption(formatOptionsOption); |
122 | |
123 | parser.addPositionalArgument(QStringLiteral("[source]" ), |
124 | QStringLiteral("File to read from (stdin if none)" )); |
125 | parser.addPositionalArgument(QStringLiteral("[destination]" ), |
126 | QStringLiteral("File to write to (stdout if none)" )); |
127 | |
128 | parser.process(app); |
129 | |
130 | if (parser.isSet(formatOptionsOption)) { |
131 | QString format = parser.value(formatOptionsOption); |
132 | for (Converter *conv : qAsConst(*availableConverters)) { |
133 | if (conv->name() == format) { |
134 | const char *help = conv->optionsHelp(); |
135 | if (help) |
136 | printf("The following options are available for format '%s':\n\n%s" , qPrintable(format), help); |
137 | else |
138 | printf("Format '%s' supports no options.\n" , qPrintable(format)); |
139 | return EXIT_SUCCESS; |
140 | } |
141 | } |
142 | |
143 | fprintf(stderr, "Unknown file format '%s'\n" , qPrintable(format)); |
144 | return EXIT_FAILURE; |
145 | } |
146 | |
147 | Converter *inconv = nullptr; |
148 | QString format = parser.value(inputFormatOption); |
149 | if (format != "auto" ) { |
150 | for (Converter *conv : qAsConst(*availableConverters)) { |
151 | if (conv->name() == format) { |
152 | inconv = conv; |
153 | break; |
154 | } |
155 | } |
156 | |
157 | if (!inconv) { |
158 | fprintf(stderr, "Unknown file format \"%s\"\n" , qPrintable(format)); |
159 | return EXIT_FAILURE; |
160 | } |
161 | } |
162 | |
163 | Converter *outconv = nullptr; |
164 | format = parser.value(outputFormatOption); |
165 | if (format != "auto" ) { |
166 | for (Converter *conv : qAsConst(*availableConverters)) { |
167 | if (conv->name() == format) { |
168 | outconv = conv; |
169 | break; |
170 | } |
171 | } |
172 | |
173 | if (!outconv) { |
174 | fprintf(stderr, "Unknown file format \"%s\"\n" , qPrintable(format)); |
175 | return EXIT_FAILURE; |
176 | } |
177 | } |
178 | |
179 | QStringList files = parser.positionalArguments(); |
180 | QFile input(files.value(0)); |
181 | QFile output(files.value(1)); |
182 | |
183 | if (input.fileName().isEmpty()) |
184 | input.open(stdin, QIODevice::ReadOnly); |
185 | else |
186 | input.open(QIODevice::ReadOnly); |
187 | if (!input.isOpen()) { |
188 | fprintf(stderr, "Could not open \"%s\" for reading: %s\n" , |
189 | qPrintable(input.fileName()), qPrintable(input.errorString())); |
190 | return EXIT_FAILURE; |
191 | } |
192 | |
193 | if (output.fileName().isEmpty()) |
194 | output.open(stdout, QIODevice::WriteOnly | QIODevice::Truncate); |
195 | else |
196 | output.open(QIODevice::WriteOnly | QIODevice::Truncate); |
197 | if (!output.isOpen()) { |
198 | fprintf(stderr, "Could not open \"%s\" for writing: %s\n" , |
199 | qPrintable(output.fileName()), qPrintable(output.errorString())); |
200 | return EXIT_FAILURE; |
201 | } |
202 | |
203 | if (!inconv) { |
204 | // probe the input to find a file format |
205 | for (Converter *conv : qAsConst(*availableConverters)) { |
206 | if (conv->directions() & Converter::In && conv->probeFile(&input)) { |
207 | inconv = conv; |
208 | break; |
209 | } |
210 | } |
211 | |
212 | if (!inconv) { |
213 | fprintf(stderr, "Could not determine input format. pass -I option.\n" ); |
214 | return EXIT_FAILURE; |
215 | } |
216 | } |
217 | |
218 | if (!outconv) { |
219 | // probe the output to find a file format |
220 | for (Converter *conv : qAsConst(*availableConverters)) { |
221 | if (conv->directions() & Converter::Out && conv->probeFile(&output)) { |
222 | outconv = conv; |
223 | break; |
224 | } |
225 | } |
226 | } |
227 | |
228 | // now finally perform the conversion |
229 | QVariant data = inconv->loadFile(&input, outconv); |
230 | Q_ASSERT_X(outconv, "Converter Tool" , |
231 | "Internal error: converter format did not provide default" ); |
232 | outconv->saveFile(&output, data, parser.values(optionOption)); |
233 | return EXIT_SUCCESS; |
234 | } |
235 | |