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 "datastreamconverter.h" |
52 | |
53 | #include <QDataStream> |
54 | #include <QDebug> |
55 | #include <QTextStream> |
56 | |
57 | static const char optionHelp[] = |
58 | "byteorder=host|big|little Byte order to use.\n" |
59 | "version=<n> QDataStream version (default: Qt 5.0).\n" |
60 | ; |
61 | |
62 | static const char signature[] = "qds" ; |
63 | |
64 | static DataStreamDumper dataStreamDumper; |
65 | static DataStreamConverter DataStreamConverter; |
66 | |
67 | QDataStream &operator<<(QDataStream &ds, const VariantOrderedMap &map) |
68 | { |
69 | ds << qint64(map.size()); |
70 | for (const auto &pair : map) |
71 | ds << pair.first << pair.second; |
72 | return ds; |
73 | } |
74 | |
75 | QDataStream &operator>>(QDataStream &ds, VariantOrderedMap &map) |
76 | { |
77 | map.clear(); |
78 | |
79 | qint64 size; |
80 | ds >> size; |
81 | map.reserve(size); |
82 | |
83 | while (size-- > 0) { |
84 | VariantOrderedMap::value_type pair; |
85 | ds >> pair.first >> pair.second; |
86 | map.append(pair); |
87 | } |
88 | |
89 | return ds; |
90 | } |
91 | |
92 | |
93 | static QString dumpVariant(const QVariant &v, const QString &indent = QLatin1String("\n" )) |
94 | { |
95 | QString result; |
96 | QString indented = indent + QLatin1String(" " ); |
97 | |
98 | int type = v.userType(); |
99 | if (type == qMetaTypeId<VariantOrderedMap>() || type == QMetaType::QVariantMap) { |
100 | const auto map = (type == QMetaType::QVariantMap) ? |
101 | VariantOrderedMap(v.toMap()) : qvariant_cast<VariantOrderedMap>(v); |
102 | |
103 | result = QLatin1String("Map {" ); |
104 | for (const auto &pair : map) { |
105 | result += indented + dumpVariant(pair.first, indented); |
106 | result.chop(1); // remove comma |
107 | result += QLatin1String(" => " ) + dumpVariant(pair.second, indented); |
108 | |
109 | } |
110 | result.chop(1); // remove comma |
111 | result += indent + QLatin1String("}," ); |
112 | } else if (type == QMetaType::QVariantList) { |
113 | const QVariantList list = v.toList(); |
114 | |
115 | result = QLatin1String("List [" ); |
116 | for (const auto &item : list) |
117 | result += indented + dumpVariant(item, indented); |
118 | result.chop(1); // remove comma |
119 | result += indent + QLatin1String("]," ); |
120 | } else { |
121 | QDebug debug(&result); |
122 | debug.nospace() << v << ','; |
123 | } |
124 | return result; |
125 | } |
126 | |
127 | QString DataStreamDumper::name() |
128 | { |
129 | return QStringLiteral("datastream-dump" ); |
130 | } |
131 | |
132 | Converter::Direction DataStreamDumper::directions() |
133 | { |
134 | return Out; |
135 | } |
136 | |
137 | Converter::Options DataStreamDumper::outputOptions() |
138 | { |
139 | return SupportsArbitraryMapKeys; |
140 | } |
141 | |
142 | const char *DataStreamDumper::optionsHelp() |
143 | { |
144 | return nullptr; |
145 | } |
146 | |
147 | bool DataStreamDumper::probeFile(QIODevice *f) |
148 | { |
149 | Q_UNUSED(f); |
150 | return false; |
151 | } |
152 | |
153 | QVariant DataStreamDumper::loadFile(QIODevice *f, Converter *&outputConverter) |
154 | { |
155 | Q_UNREACHABLE(); |
156 | Q_UNUSED(f); |
157 | Q_UNUSED(outputConverter); |
158 | return QVariant(); |
159 | } |
160 | |
161 | void DataStreamDumper::saveFile(QIODevice *f, const QVariant &contents, const QStringList &options) |
162 | { |
163 | Q_UNUSED(options); |
164 | QString s = dumpVariant(contents); |
165 | s[s.size() - 1] = QLatin1Char('\n'); // replace the comma with newline |
166 | |
167 | QTextStream out(f); |
168 | out << s; |
169 | } |
170 | |
171 | DataStreamConverter::DataStreamConverter() |
172 | { |
173 | qRegisterMetaType<VariantOrderedMap>(); |
174 | } |
175 | |
176 | QString DataStreamConverter::name() |
177 | { |
178 | return QStringLiteral("datastream" ); |
179 | } |
180 | |
181 | Converter::Direction DataStreamConverter::directions() |
182 | { |
183 | return InOut; |
184 | } |
185 | |
186 | Converter::Options DataStreamConverter::outputOptions() |
187 | { |
188 | return SupportsArbitraryMapKeys; |
189 | } |
190 | |
191 | const char *DataStreamConverter::optionsHelp() |
192 | { |
193 | return optionHelp; |
194 | } |
195 | |
196 | bool DataStreamConverter::probeFile(QIODevice *f) |
197 | { |
198 | return f->isReadable() && f->peek(sizeof(signature) - 1) == signature; |
199 | } |
200 | |
201 | QVariant DataStreamConverter::loadFile(QIODevice *f, Converter *&outputConverter) |
202 | { |
203 | if (!outputConverter) |
204 | outputConverter = &dataStreamDumper; |
205 | |
206 | char c; |
207 | if (f->read(sizeof(signature) -1) != signature || |
208 | !f->getChar(&c) || (c != 'l' && c != 'B')) { |
209 | fprintf(stderr, "Could not load QDataStream file: invalid signature.\n" ); |
210 | exit(EXIT_FAILURE); |
211 | } |
212 | |
213 | QDataStream ds(f); |
214 | ds.setByteOrder(c == 'l' ? QDataStream::LittleEndian : QDataStream::BigEndian); |
215 | |
216 | std::underlying_type<QDataStream::Version>::type version; |
217 | ds >> version; |
218 | ds.setVersion(QDataStream::Version(version)); |
219 | |
220 | QVariant result; |
221 | ds >> result; |
222 | return result; |
223 | } |
224 | |
225 | void DataStreamConverter::saveFile(QIODevice *f, const QVariant &contents, const QStringList &options) |
226 | { |
227 | QDataStream::Version version = QDataStream::Qt_5_0; |
228 | auto order = QDataStream::ByteOrder(QSysInfo::ByteOrder); |
229 | for (const QString &option : options) { |
230 | const QStringList pair = option.split('='); |
231 | if (pair.size() == 2) { |
232 | if (pair.first() == "byteorder" ) { |
233 | if (pair.last() == "little" ) { |
234 | order = QDataStream::LittleEndian; |
235 | continue; |
236 | } else if (pair.last() == "big" ) { |
237 | order = QDataStream::BigEndian; |
238 | continue; |
239 | } else if (pair.last() == "host" ) { |
240 | order = QDataStream::ByteOrder(QSysInfo::ByteOrder); |
241 | continue; |
242 | } |
243 | } |
244 | if (pair.first() == "version" ) { |
245 | bool ok; |
246 | int n = pair.last().toInt(&ok); |
247 | if (ok) { |
248 | version = QDataStream::Version(n); |
249 | continue; |
250 | } |
251 | |
252 | fprintf(stderr, "Invalid version number '%s': must be a number from 1 to %d.\n" , |
253 | qPrintable(pair.last()), QDataStream::Qt_DefaultCompiledVersion); |
254 | exit(EXIT_FAILURE); |
255 | } |
256 | } |
257 | |
258 | fprintf(stderr, "Unknown QDataStream formatting option '%s'. Available options are:\n%s" , |
259 | qPrintable(option), optionHelp); |
260 | exit(EXIT_FAILURE); |
261 | } |
262 | |
263 | char c = order == QDataStream::LittleEndian ? 'l' : 'B'; |
264 | f->write(signature); |
265 | f->write(&c, 1); |
266 | |
267 | QDataStream ds(f); |
268 | ds.setVersion(version); |
269 | ds.setByteOrder(order); |
270 | ds << std::underlying_type<decltype(version)>::type(version); |
271 | ds << contents; |
272 | } |
273 | |