qt6windows7/examples/corelib/serialization/convert/datastreamconverter.cpp

148 lines
4.1 KiB
C++
Raw Normal View History

2023-10-30 06:33:08 +08:00
// Copyright (C) 2018 Intel Corporation.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "datastreamconverter.h"
2023-11-02 01:02:52 +08:00
#include "debugtextdumper.h"
2023-10-30 06:33:08 +08:00
#include <QDataStream>
2023-11-02 01:02:52 +08:00
using namespace Qt::StringLiterals;
2023-10-30 06:33:08 +08:00
static const char dataStreamOptionHelp[] =
"byteorder=host|big|little Byte order to use.\n"
2023-11-02 01:02:52 +08:00
"version=<n> QDataStream version (default: Qt 6.0).\n"
2023-10-30 06:33:08 +08:00
;
static const char signature[] = "qds";
2023-11-02 01:02:52 +08:00
static DataStreamConverter dataStreamConverter;
static DebugTextDumper debugTextDumper;
2023-10-30 06:33:08 +08:00
QDataStream &operator<<(QDataStream &ds, const VariantOrderedMap &map)
{
ds << qint64(map.size());
for (const auto &pair : map)
ds << pair.first << pair.second;
return ds;
}
QDataStream &operator>>(QDataStream &ds, VariantOrderedMap &map)
{
map.clear();
qint64 size;
ds >> size;
map.reserve(size);
while (size-- > 0) {
VariantOrderedMap::value_type pair;
ds >> pair.first >> pair.second;
map.append(pair);
}
return ds;
}
DataStreamConverter::DataStreamConverter()
{
qRegisterMetaType<VariantOrderedMap>();
}
2023-11-02 01:02:52 +08:00
QString DataStreamConverter::name() const
2023-10-30 06:33:08 +08:00
{
2023-11-02 01:02:52 +08:00
return "datastream"_L1;
2023-10-30 06:33:08 +08:00
}
2023-11-02 01:02:52 +08:00
Converter::Directions DataStreamConverter::directions() const
2023-10-30 06:33:08 +08:00
{
2023-11-02 01:02:52 +08:00
return Direction::InOut;
2023-10-30 06:33:08 +08:00
}
2023-11-02 01:02:52 +08:00
Converter::Options DataStreamConverter::outputOptions() const
2023-10-30 06:33:08 +08:00
{
return SupportsArbitraryMapKeys;
}
2023-11-02 01:02:52 +08:00
const char *DataStreamConverter::optionsHelp() const
2023-10-30 06:33:08 +08:00
{
return dataStreamOptionHelp;
}
2023-11-02 01:02:52 +08:00
bool DataStreamConverter::probeFile(QIODevice *f) const
2023-10-30 06:33:08 +08:00
{
return f->isReadable() && f->peek(sizeof(signature) - 1) == signature;
}
2023-11-02 01:02:52 +08:00
QVariant DataStreamConverter::loadFile(QIODevice *f, const Converter *&outputConverter) const
2023-10-30 06:33:08 +08:00
{
if (!outputConverter)
2023-11-02 01:02:52 +08:00
outputConverter = &debugTextDumper;
2023-10-30 06:33:08 +08:00
char c;
2023-11-02 01:02:52 +08:00
if (f->read(sizeof(signature) - 1) != signature || !f->getChar(&c) || (c != 'l' && c != 'B')) {
2023-10-30 06:33:08 +08:00
fprintf(stderr, "Could not load QDataStream file: invalid signature.\n");
exit(EXIT_FAILURE);
}
QDataStream ds(f);
ds.setByteOrder(c == 'l' ? QDataStream::LittleEndian : QDataStream::BigEndian);
std::underlying_type<QDataStream::Version>::type version;
ds >> version;
ds.setVersion(QDataStream::Version(version));
QVariant result;
ds >> result;
return result;
}
2023-11-02 01:02:52 +08:00
void DataStreamConverter::saveFile(QIODevice *f, const QVariant &contents,
const QStringList &options) const
2023-10-30 06:33:08 +08:00
{
2023-11-02 01:02:52 +08:00
QDataStream::Version version = QDataStream::Qt_6_0;
2023-10-30 06:33:08 +08:00
auto order = QDataStream::ByteOrder(QSysInfo::ByteOrder);
for (const QString &option : options) {
const QStringList pair = option.split('=');
if (pair.size() == 2) {
if (pair.first() == "byteorder") {
if (pair.last() == "little") {
order = QDataStream::LittleEndian;
continue;
} else if (pair.last() == "big") {
order = QDataStream::BigEndian;
continue;
} else if (pair.last() == "host") {
order = QDataStream::ByteOrder(QSysInfo::ByteOrder);
continue;
}
}
if (pair.first() == "version") {
bool ok;
int n = pair.last().toInt(&ok);
if (ok) {
version = QDataStream::Version(n);
continue;
}
fprintf(stderr, "Invalid version number '%s': must be a number from 1 to %d.\n",
qPrintable(pair.last()), QDataStream::Qt_DefaultCompiledVersion);
exit(EXIT_FAILURE);
}
}
fprintf(stderr, "Unknown QDataStream formatting option '%s'. Available options are:\n%s",
qPrintable(option), dataStreamOptionHelp);
exit(EXIT_FAILURE);
}
2023-11-02 01:02:52 +08:00
char c = order == QDataStream::LittleEndian ? 'l' : 'B';
2023-10-30 06:33:08 +08:00
f->write(signature);
f->write(&c, 1);
QDataStream ds(f);
ds.setVersion(version);
ds.setByteOrder(order);
ds << std::underlying_type<decltype(version)>::type(version);
ds << contents;
}