1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2018 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the test suite of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
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 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | #include <qfile.h> |
30 | #include <qjsonarray.h> |
31 | #include <qjsondocument.h> |
32 | #include <qjsonobject.h> |
33 | #include <qhashfunctions.h> |
34 | #include <qstringlist.h> |
35 | #include <cstdlib> |
36 | |
37 | static bool readFromDevice(QIODevice *device, QJsonArray *allMetaObjects) |
38 | { |
39 | const QByteArray contents = device->readAll(); |
40 | if (contents.isEmpty()) |
41 | return true; |
42 | |
43 | QJsonParseError error {}; |
44 | QJsonDocument metaObjects = QJsonDocument::fromJson(json: contents, error: &error); |
45 | if (error.error != QJsonParseError::NoError) { |
46 | fprintf(stderr, format: "%s at %d\n" , error.errorString().toUtf8().constData(), error.offset); |
47 | return false; |
48 | } |
49 | |
50 | allMetaObjects->append(value: metaObjects.object()); |
51 | return true; |
52 | } |
53 | |
54 | int collectJson(const QStringList &jsonFiles, const QString &outputFile) |
55 | { |
56 | qSetGlobalQHashSeed(newSeed: 0); |
57 | |
58 | QFile output; |
59 | if (outputFile.isEmpty()) { |
60 | if (!output.open(stdout, ioFlags: QIODevice::WriteOnly)) { |
61 | fprintf(stderr, format: "Error opening stdout for writing\n" ); |
62 | return EXIT_FAILURE; |
63 | } |
64 | } else { |
65 | output.setFileName(outputFile); |
66 | if (!output.open(flags: QIODevice::WriteOnly)) { |
67 | fprintf(stderr, format: "Error opening %s for writing\n" , qPrintable(outputFile)); |
68 | return EXIT_FAILURE; |
69 | } |
70 | } |
71 | |
72 | QJsonArray allMetaObjects; |
73 | if (jsonFiles.isEmpty()) { |
74 | QFile f; |
75 | if (!f.open(stdin, ioFlags: QIODevice::ReadOnly)) { |
76 | fprintf(stderr, format: "Error opening stdin for reading\n" ); |
77 | return EXIT_FAILURE; |
78 | } |
79 | |
80 | if (!readFromDevice(device: &f, allMetaObjects: &allMetaObjects)) { |
81 | fprintf(stderr, format: "Error parsing data from stdin\n" ); |
82 | return EXIT_FAILURE; |
83 | } |
84 | } |
85 | |
86 | for (const QString &jsonFile: jsonFiles) { |
87 | QFile f(jsonFile); |
88 | if (!f.open(flags: QIODevice::ReadOnly)) { |
89 | fprintf(stderr, format: "Error opening %s for reading\n" , qPrintable(jsonFile)); |
90 | return EXIT_FAILURE; |
91 | } |
92 | |
93 | if (!readFromDevice(device: &f, allMetaObjects: &allMetaObjects)) { |
94 | fprintf(stderr, format: "Error parsing %s\n" , qPrintable(jsonFile)); |
95 | return EXIT_FAILURE; |
96 | } |
97 | } |
98 | |
99 | QJsonDocument doc(allMetaObjects); |
100 | output.write(data: doc.toJson()); |
101 | |
102 | return EXIT_SUCCESS; |
103 | } |
104 | |