corelib/serialization examples: use string literals more
A couple more compilation units could use Qt::StringLiterals. Prefer QL1SV for the code constants, to keep code small. Convert fpToString() to take QL1SV instead of const char *, with suffix empty by default. Also rearranged some spacing, some if it suggested by clang-tidy. Pick-to: 6.6 6.5 Task-number: QTBUG-111228 Change-Id: I03d810d52afcd4a760d18f2553914b75af716b74 Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
This commit is contained in:
parent
e4d545b4c6
commit
319b2e0e86
@ -14,6 +14,8 @@
|
|||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
using namespace Qt::StringLiterals;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* To regenerate:
|
* To regenerate:
|
||||||
* curl -O https://www.iana.org/assignments/cbor-tags/cbor-tags.xml
|
* curl -O https://www.iana.org/assignments/cbor-tags/cbor-tags.xml
|
||||||
@ -252,31 +254,32 @@ template <typename T> static inline bool canConvertTo(double v)
|
|||||||
return v == floor(v);
|
return v == floor(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
static QString fpToString(double v, const char *suffix)
|
static QString fpToString(double v, QLatin1StringView suffix = ""_L1)
|
||||||
{
|
{
|
||||||
if (qIsInf(v))
|
if (qIsInf(v))
|
||||||
return v < 0 ? QStringLiteral("-inf") : QStringLiteral("inf");
|
return v < 0 ? "-inf"_L1 : "inf"_L1;
|
||||||
if (qIsNaN(v))
|
if (qIsNaN(v))
|
||||||
return QStringLiteral("nan");
|
return "nan"_L1;
|
||||||
if (canConvertTo<qint64>(v))
|
if (canConvertTo<qint64>(v))
|
||||||
return QString::number(qint64(v)) + ".0" + suffix;
|
return QString::number(qint64(v)) + ".0"_L1 + suffix;
|
||||||
if (canConvertTo<quint64>(v))
|
if (canConvertTo<quint64>(v))
|
||||||
return QString::number(quint64(v)) + ".0" + suffix;
|
return QString::number(quint64(v)) + ".0"_L1 + suffix;
|
||||||
|
|
||||||
QString s = QString::number(v, 'g', QLocale::FloatingPointShortest);
|
QString s = QString::number(v, 'g', QLocale::FloatingPointShortest);
|
||||||
if (!s.contains('.') && !s.contains('e'))
|
if (!s.contains(u'.') && !s.contains(u'e'))
|
||||||
s += '.';
|
s += u'.';
|
||||||
s += suffix;
|
if (suffix.size())
|
||||||
|
s += suffix;
|
||||||
return s;
|
return s;
|
||||||
};
|
};
|
||||||
|
|
||||||
void CborDumper::dumpOne(int nestingLevel)
|
void CborDumper::dumpOne(int nestingLevel)
|
||||||
{
|
{
|
||||||
QString indent(1, QLatin1Char(' '));
|
QString indent(1, u' ');
|
||||||
QString indented = indent;
|
QString indented = indent;
|
||||||
if (!opts.testFlag(ShowCompact)) {
|
if (!opts.testFlag(ShowCompact)) {
|
||||||
indent = QLatin1Char('\n') + QString(4 * nestingLevel, QLatin1Char(' '));
|
indent = u'\n' + QString(4 * nestingLevel, u' ');
|
||||||
indented = QLatin1Char('\n') + QString(4 + 4 * nestingLevel, QLatin1Char(' '));
|
indented = u'\n' + QString(4 + 4 * nestingLevel, u' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (reader.type()) {
|
switch (reader.type()) {
|
||||||
@ -316,7 +319,7 @@ void CborDumper::dumpOne(int nestingLevel)
|
|||||||
printStringWidthIndicator(r.data.size());
|
printStringWidthIndicator(r.data.size());
|
||||||
|
|
||||||
r = reader.readByteArray();
|
r = reader.readByteArray();
|
||||||
comma = QLatin1Char(',') + indented;
|
comma = u',' + indented;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
auto r = reader.readString();
|
auto r = reader.readString();
|
||||||
@ -325,7 +328,7 @@ void CborDumper::dumpOne(int nestingLevel)
|
|||||||
printStringWidthIndicator(r.data.toUtf8().size());
|
printStringWidthIndicator(r.data.toUtf8().size());
|
||||||
|
|
||||||
r = reader.readString();
|
r = reader.readString();
|
||||||
comma = QLatin1Char(',') + indented;
|
comma = u',' + indented;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -413,15 +416,15 @@ void CborDumper::dumpOne(int nestingLevel)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case QCborStreamReader::Float16:
|
case QCborStreamReader::Float16:
|
||||||
printf("%s", qPrintable(fpToString(reader.toFloat16(), "f16")));
|
printf("%s", qPrintable(fpToString(reader.toFloat16(), "f16"_L1)));
|
||||||
reader.next();
|
reader.next();
|
||||||
break;
|
break;
|
||||||
case QCborStreamReader::Float:
|
case QCborStreamReader::Float:
|
||||||
printf("%s", qPrintable(fpToString(reader.toFloat(), "f")));
|
printf("%s", qPrintable(fpToString(reader.toFloat(), "f"_L1)));
|
||||||
reader.next();
|
reader.next();
|
||||||
break;
|
break;
|
||||||
case QCborStreamReader::Double:
|
case QCborStreamReader::Double:
|
||||||
printf("%s", qPrintable(fpToString(reader.toDouble(), "")));
|
printf("%s", qPrintable(fpToString(reader.toDouble())));
|
||||||
reader.next();
|
reader.next();
|
||||||
break;
|
break;
|
||||||
case QCborStreamReader::Invalid:
|
case QCborStreamReader::Invalid:
|
||||||
@ -474,7 +477,7 @@ void CborDumper::dumpOneDetailed(int nestingLevel)
|
|||||||
};
|
};
|
||||||
|
|
||||||
auto printFp = [=](const char *descr, double d) {
|
auto printFp = [=](const char *descr, double d) {
|
||||||
QString s = fpToString(d, "");
|
QString s = fpToString(d);
|
||||||
if (s.size() <= 6)
|
if (s.size() <= 6)
|
||||||
return print(descr, "%s", qPrintable(s));
|
return print(descr, "%s", qPrintable(s));
|
||||||
return print(descr, "%a", d);
|
return print(descr, "%a", d);
|
||||||
@ -726,23 +729,20 @@ int main(int argc, char *argv[])
|
|||||||
setlocale(LC_ALL, "C");
|
setlocale(LC_ALL, "C");
|
||||||
|
|
||||||
QCommandLineParser parser;
|
QCommandLineParser parser;
|
||||||
parser.setApplicationDescription(QStringLiteral("CBOR Dumper tool"));
|
parser.setApplicationDescription("CBOR Dumper tool"_L1);
|
||||||
parser.addHelpOption();
|
parser.addHelpOption();
|
||||||
|
|
||||||
QCommandLineOption compact({QStringLiteral("c"), QStringLiteral("compact")},
|
QCommandLineOption compact({"c"_L1, "compact"_L1}, "Use compact form (no line breaks)"_L1);
|
||||||
QStringLiteral("Use compact form (no line breaks)"));
|
|
||||||
parser.addOption(compact);
|
parser.addOption(compact);
|
||||||
|
|
||||||
QCommandLineOption showIndicators({QStringLiteral("i"), QStringLiteral("indicators")},
|
QCommandLineOption showIndicators({ "i"_L1, "indicators"_L1 },
|
||||||
QStringLiteral("Show indicators for width of lengths and integrals"));
|
"Show indicators for width of lengths and integrals"_L1);
|
||||||
parser.addOption(showIndicators);
|
parser.addOption(showIndicators);
|
||||||
|
|
||||||
QCommandLineOption verbose({QStringLiteral("a"), QStringLiteral("annotated")},
|
QCommandLineOption verbose({"a"_L1, "annotated"_L1}, "Show bytes and annotated decoding"_L1);
|
||||||
QStringLiteral("Show bytes and annotated decoding"));
|
|
||||||
parser.addOption(verbose);
|
parser.addOption(verbose);
|
||||||
|
|
||||||
parser.addPositionalArgument(QStringLiteral("[source]"),
|
parser.addPositionalArgument("[source]"_L1, "CBOR file to read from"_L1);
|
||||||
QStringLiteral("CBOR file to read from"));
|
|
||||||
|
|
||||||
parser.process(app);
|
parser.process(app);
|
||||||
|
|
||||||
|
@ -16,6 +16,8 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
using namespace Qt::StringLiterals;
|
||||||
|
|
||||||
static CborConverter cborConverter;
|
static CborConverter cborConverter;
|
||||||
static CborDiagnosticDumper cborDiagnosticDumper;
|
static CborDiagnosticDumper cborDiagnosticDumper;
|
||||||
|
|
||||||
@ -120,7 +122,7 @@ static QCborValue convertFromVariant(const QVariant &v, TrimFloatingPoint fpTrim
|
|||||||
|
|
||||||
QString CborDiagnosticDumper::name()
|
QString CborDiagnosticDumper::name()
|
||||||
{
|
{
|
||||||
return QStringLiteral("cbor-dump");
|
return "cbor-dump"_L1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Converter::Direction CborDiagnosticDumper::directions()
|
Converter::Direction CborDiagnosticDumper::directions()
|
||||||
@ -213,7 +215,7 @@ const char *CborConverter::optionsHelp()
|
|||||||
bool CborConverter::probeFile(QIODevice *f)
|
bool CborConverter::probeFile(QIODevice *f)
|
||||||
{
|
{
|
||||||
if (QFile *file = qobject_cast<QFile *>(f)) {
|
if (QFile *file = qobject_cast<QFile *>(f)) {
|
||||||
if (file->fileName().endsWith(QLatin1String(".cbor")))
|
if (file->fileName().endsWith(".cbor"_L1))
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return f->isReadable() && f->peek(3) == QByteArray("\xd9\xd9\xf7", 3);
|
return f->isReadable() && f->peek(3) == QByteArray("\xd9\xd9\xf7", 3);
|
||||||
|
@ -7,6 +7,8 @@
|
|||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QTextStream>
|
#include <QTextStream>
|
||||||
|
|
||||||
|
using namespace Qt::StringLiterals;
|
||||||
|
|
||||||
static const char dataStreamOptionHelp[] =
|
static const char dataStreamOptionHelp[] =
|
||||||
"byteorder=host|big|little Byte order to use.\n"
|
"byteorder=host|big|little Byte order to use.\n"
|
||||||
"version=<n> QDataStream version (default: Qt 6.0).\n"
|
"version=<n> QDataStream version (default: Qt 6.0).\n"
|
||||||
@ -43,33 +45,33 @@ QDataStream &operator>>(QDataStream &ds, VariantOrderedMap &map)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static QString dumpVariant(const QVariant &v, const QString &indent = QLatin1String("\n"))
|
static QString dumpVariant(const QVariant &v, const QString &indent = "\n"_L1)
|
||||||
{
|
{
|
||||||
QString result;
|
QString result;
|
||||||
QString indented = indent + QLatin1String(" ");
|
QString indented = indent + " "_L1;
|
||||||
|
|
||||||
int type = v.userType();
|
int type = v.userType();
|
||||||
if (type == qMetaTypeId<VariantOrderedMap>() || type == QMetaType::QVariantMap) {
|
if (type == qMetaTypeId<VariantOrderedMap>() || type == QMetaType::QVariantMap) {
|
||||||
const auto map = (type == QMetaType::QVariantMap) ?
|
const auto map = (type == QMetaType::QVariantMap) ?
|
||||||
VariantOrderedMap(v.toMap()) : qvariant_cast<VariantOrderedMap>(v);
|
VariantOrderedMap(v.toMap()) : qvariant_cast<VariantOrderedMap>(v);
|
||||||
|
|
||||||
result = QLatin1String("Map {");
|
result = "Map {"_L1;
|
||||||
for (const auto &pair : map) {
|
for (const auto &pair : map) {
|
||||||
result += indented + dumpVariant(pair.first, indented);
|
result += indented + dumpVariant(pair.first, indented);
|
||||||
result.chop(1); // remove comma
|
result.chop(1); // remove comma
|
||||||
result += QLatin1String(" => ") + dumpVariant(pair.second, indented);
|
result += " => "_L1 + dumpVariant(pair.second, indented);
|
||||||
|
|
||||||
}
|
}
|
||||||
result.chop(1); // remove comma
|
result.chop(1); // remove comma
|
||||||
result += indent + QLatin1String("},");
|
result += indent + "},"_L1;
|
||||||
} else if (type == QMetaType::QVariantList) {
|
} else if (type == QMetaType::QVariantList) {
|
||||||
const QVariantList list = v.toList();
|
const QVariantList list = v.toList();
|
||||||
|
|
||||||
result = QLatin1String("List [");
|
result = "List ["_L1;
|
||||||
for (const auto &item : list)
|
for (const auto &item : list)
|
||||||
result += indented + dumpVariant(item, indented);
|
result += indented + dumpVariant(item, indented);
|
||||||
result.chop(1); // remove comma
|
result.chop(1); // remove comma
|
||||||
result += indent + QLatin1String("],");
|
result += indent + "],"_L1;
|
||||||
} else {
|
} else {
|
||||||
QDebug debug(&result);
|
QDebug debug(&result);
|
||||||
debug.nospace() << v << ',';
|
debug.nospace() << v << ',';
|
||||||
@ -79,7 +81,7 @@ static QString dumpVariant(const QVariant &v, const QString &indent = QLatin1Str
|
|||||||
|
|
||||||
QString DataStreamDumper::name()
|
QString DataStreamDumper::name()
|
||||||
{
|
{
|
||||||
return QStringLiteral("datastream-dump");
|
return "datastream-dump"_L1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Converter::Direction DataStreamDumper::directions()
|
Converter::Direction DataStreamDumper::directions()
|
||||||
@ -128,7 +130,7 @@ DataStreamConverter::DataStreamConverter()
|
|||||||
|
|
||||||
QString DataStreamConverter::name()
|
QString DataStreamConverter::name()
|
||||||
{
|
{
|
||||||
return QStringLiteral("datastream");
|
return "datastream"_L1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Converter::Direction DataStreamConverter::directions()
|
Converter::Direction DataStreamConverter::directions()
|
||||||
|
@ -9,6 +9,8 @@
|
|||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
#include <QJsonValue>
|
#include <QJsonValue>
|
||||||
|
|
||||||
|
using namespace Qt::StringLiterals;
|
||||||
|
|
||||||
static JsonConverter jsonConverter;
|
static JsonConverter jsonConverter;
|
||||||
|
|
||||||
static const char jsonOptionHelp[] =
|
static const char jsonOptionHelp[] =
|
||||||
@ -30,7 +32,7 @@ JsonConverter::JsonConverter()
|
|||||||
|
|
||||||
QString JsonConverter::name()
|
QString JsonConverter::name()
|
||||||
{
|
{
|
||||||
return "json";
|
return "json"_L1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Converter::Direction JsonConverter::directions()
|
Converter::Direction JsonConverter::directions()
|
||||||
@ -51,7 +53,7 @@ const char *JsonConverter::optionsHelp()
|
|||||||
bool JsonConverter::probeFile(QIODevice *f)
|
bool JsonConverter::probeFile(QIODevice *f)
|
||||||
{
|
{
|
||||||
if (QFile *file = qobject_cast<QFile *>(f)) {
|
if (QFile *file = qobject_cast<QFile *>(f)) {
|
||||||
if (file->fileName().endsWith(QLatin1String(".json")))
|
if (file->fileName().endsWith(".json"_L1))
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,9 +93,9 @@ void JsonConverter::saveFile(QIODevice *f, const QVariant &contents, const QStri
|
|||||||
{
|
{
|
||||||
QJsonDocument::JsonFormat format = QJsonDocument::Indented;
|
QJsonDocument::JsonFormat format = QJsonDocument::Indented;
|
||||||
for (const QString &s : options) {
|
for (const QString &s : options) {
|
||||||
if (s == QLatin1String("compact=no")) {
|
if (s == "compact=no"_L1) {
|
||||||
format = QJsonDocument::Indented;
|
format = QJsonDocument::Indented;
|
||||||
} else if (s == QLatin1String("compact=yes")) {
|
} else if (s == "compact=yes"_L1) {
|
||||||
format = QJsonDocument::Compact;
|
format = QJsonDocument::Compact;
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "Unknown option '%s' to JSON output. Valid options are:\n%s",
|
fprintf(stderr, "Unknown option '%s' to JSON output. Valid options are:\n%s",
|
||||||
|
@ -11,6 +11,8 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
using namespace Qt::StringLiterals;
|
||||||
|
|
||||||
static QList<Converter *> *availableConverters;
|
static QList<Converter *> *availableConverters;
|
||||||
|
|
||||||
Converter::Converter()
|
Converter::Converter()
|
||||||
@ -41,42 +43,44 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
inputFormats.sort();
|
inputFormats.sort();
|
||||||
outputFormats.sort();
|
outputFormats.sort();
|
||||||
inputFormats.prepend("auto");
|
inputFormats.prepend("auto"_L1);
|
||||||
outputFormats.prepend("auto");
|
outputFormats.prepend("auto"_L1);
|
||||||
|
|
||||||
QCommandLineParser parser;
|
QCommandLineParser parser;
|
||||||
parser.setApplicationDescription(QStringLiteral("Qt file format conversion tool"));
|
parser.setApplicationDescription("Qt file format conversion tool"_L1);
|
||||||
parser.addHelpOption();
|
parser.addHelpOption();
|
||||||
|
|
||||||
QCommandLineOption inputFormatOption(QStringList{"I", "input-format"});
|
QCommandLineOption inputFormatOption(QStringList{ "I"_L1, "input-format"_L1 });
|
||||||
inputFormatOption.setDescription(QLatin1String("Select the input format for the input file. Available formats: ") +
|
inputFormatOption.setDescription(
|
||||||
inputFormats.join(", "));
|
"Select the input format for the input file. Available formats: "_L1
|
||||||
inputFormatOption.setValueName("format");
|
+ inputFormats.join(", "_L1));
|
||||||
|
inputFormatOption.setValueName("format"_L1);
|
||||||
inputFormatOption.setDefaultValue(inputFormats.constFirst());
|
inputFormatOption.setDefaultValue(inputFormats.constFirst());
|
||||||
parser.addOption(inputFormatOption);
|
parser.addOption(inputFormatOption);
|
||||||
|
|
||||||
QCommandLineOption outputFormatOption(QStringList{"O", "output-format"});
|
QCommandLineOption outputFormatOption(QStringList{ "O"_L1, "output-format"_L1 });
|
||||||
outputFormatOption.setDescription(QLatin1String("Select the output format for the output file. Available formats: ") +
|
outputFormatOption.setDescription(
|
||||||
outputFormats.join(", "));
|
"Select the output format for the output file. Available formats: "_L1
|
||||||
outputFormatOption.setValueName("format");
|
+ outputFormats.join(", "_L1));
|
||||||
|
outputFormatOption.setValueName("format"_L1);
|
||||||
outputFormatOption.setDefaultValue(outputFormats.constFirst());
|
outputFormatOption.setDefaultValue(outputFormats.constFirst());
|
||||||
parser.addOption(outputFormatOption);
|
parser.addOption(outputFormatOption);
|
||||||
|
|
||||||
QCommandLineOption optionOption(QStringList{"o", "option"});
|
QCommandLineOption optionOption(QStringList{ "o"_L1, "option"_L1 });
|
||||||
optionOption.setDescription(QStringLiteral("Format-specific options. Use --format-options to find out what options are available."));
|
optionOption.setDescription(
|
||||||
optionOption.setValueName("options...");
|
"Format-specific options. Use --format-options to find out what options are available."_L1);
|
||||||
|
optionOption.setValueName("options..."_L1);
|
||||||
optionOption.setDefaultValues({});
|
optionOption.setDefaultValues({});
|
||||||
parser.addOption(optionOption);
|
parser.addOption(optionOption);
|
||||||
|
|
||||||
QCommandLineOption formatOptionsOption("format-options");
|
QCommandLineOption formatOptionsOption("format-options"_L1);
|
||||||
formatOptionsOption.setDescription(QStringLiteral("Prints the list of valid options for --option for the converter format <format>."));
|
formatOptionsOption.setDescription(
|
||||||
formatOptionsOption.setValueName("format");
|
"Prints the list of valid options for --option for the converter format <format>."_L1);
|
||||||
|
formatOptionsOption.setValueName("format"_L1);
|
||||||
parser.addOption(formatOptionsOption);
|
parser.addOption(formatOptionsOption);
|
||||||
|
|
||||||
parser.addPositionalArgument(QStringLiteral("[source]"),
|
parser.addPositionalArgument("[source]"_L1, "File to read from (stdin if none)"_L1);
|
||||||
QStringLiteral("File to read from (stdin if none)"));
|
parser.addPositionalArgument("[destination]"_L1, "File to write to (stdout if none)"_L1);
|
||||||
parser.addPositionalArgument(QStringLiteral("[destination]"),
|
|
||||||
QStringLiteral("File to write to (stdout if none)"));
|
|
||||||
|
|
||||||
parser.process(app);
|
parser.process(app);
|
||||||
|
|
||||||
@ -99,7 +103,7 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
Converter *inconv = nullptr;
|
Converter *inconv = nullptr;
|
||||||
QString format = parser.value(inputFormatOption);
|
QString format = parser.value(inputFormatOption);
|
||||||
if (format != "auto") {
|
if (format != "auto"_L1) {
|
||||||
for (Converter *conv : std::as_const(*availableConverters)) {
|
for (Converter *conv : std::as_const(*availableConverters)) {
|
||||||
if (conv->name() == format) {
|
if (conv->name() == format) {
|
||||||
inconv = conv;
|
inconv = conv;
|
||||||
@ -115,7 +119,7 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
Converter *outconv = nullptr;
|
Converter *outconv = nullptr;
|
||||||
format = parser.value(outputFormatOption);
|
format = parser.value(outputFormatOption);
|
||||||
if (format != "auto") {
|
if (format != "auto"_L1) {
|
||||||
for (Converter *conv : std::as_const(*availableConverters)) {
|
for (Converter *conv : std::as_const(*availableConverters)) {
|
||||||
if (conv->name() == format) {
|
if (conv->name() == format) {
|
||||||
outconv = conv;
|
outconv = conv;
|
||||||
|
@ -3,12 +3,14 @@
|
|||||||
|
|
||||||
#include "nullconverter.h"
|
#include "nullconverter.h"
|
||||||
|
|
||||||
|
using namespace Qt::StringLiterals;
|
||||||
|
|
||||||
static NullConverter nullConverter;
|
static NullConverter nullConverter;
|
||||||
Converter *Converter::null = &nullConverter;
|
Converter *Converter::null = &nullConverter;
|
||||||
|
|
||||||
QString NullConverter::name()
|
QString NullConverter::name()
|
||||||
{
|
{
|
||||||
return QLatin1String("null");
|
return "null"_L1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Converter::Direction NullConverter::directions()
|
Converter::Direction NullConverter::directions()
|
||||||
|
@ -6,6 +6,8 @@
|
|||||||
#include <QFile>
|
#include <QFile>
|
||||||
#include <QTextStream>
|
#include <QTextStream>
|
||||||
|
|
||||||
|
using namespace Qt::StringLiterals;
|
||||||
|
|
||||||
static void dumpVariant(QTextStream &out, const QVariant &v)
|
static void dumpVariant(QTextStream &out, const QVariant &v)
|
||||||
{
|
{
|
||||||
switch (v.userType()) {
|
switch (v.userType()) {
|
||||||
@ -44,7 +46,7 @@ static void dumpVariant(QTextStream &out, const QVariant &v)
|
|||||||
|
|
||||||
QString TextConverter::name()
|
QString TextConverter::name()
|
||||||
{
|
{
|
||||||
return QStringLiteral("text");
|
return "text"_L1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Converter::Direction TextConverter::directions()
|
Converter::Direction TextConverter::directions()
|
||||||
@ -65,7 +67,7 @@ const char *TextConverter::optionsHelp()
|
|||||||
bool TextConverter::probeFile(QIODevice *f)
|
bool TextConverter::probeFile(QIODevice *f)
|
||||||
{
|
{
|
||||||
if (QFile *file = qobject_cast<QFile *>(f))
|
if (QFile *file = qobject_cast<QFile *>(f))
|
||||||
return file->fileName().endsWith(QLatin1String(".txt"));
|
return file->fileName().endsWith(".txt"_L1);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,6 +13,8 @@
|
|||||||
#include <QXmlStreamReader>
|
#include <QXmlStreamReader>
|
||||||
#include <QXmlStreamWriter>
|
#include <QXmlStreamWriter>
|
||||||
|
|
||||||
|
using namespace Qt::StringLiterals;
|
||||||
|
|
||||||
static const char xmlOptionHelp[] =
|
static const char xmlOptionHelp[] =
|
||||||
"compact=no|yes Use compact XML form.\n";
|
"compact=no|yes Use compact XML form.\n";
|
||||||
|
|
||||||
@ -23,7 +25,7 @@ static QVariant variantFromXml(QXmlStreamReader &xml, Converter::Options options
|
|||||||
static QVariantList listFromXml(QXmlStreamReader &xml, Converter::Options options)
|
static QVariantList listFromXml(QXmlStreamReader &xml, Converter::Options options)
|
||||||
{
|
{
|
||||||
QVariantList list;
|
QVariantList list;
|
||||||
while (!xml.atEnd() && !(xml.isEndElement() && xml.name() == QLatin1String("list"))) {
|
while (!xml.atEnd() && !(xml.isEndElement() && xml.name() == "list"_L1)) {
|
||||||
xml.readNext();
|
xml.readNext();
|
||||||
switch (xml.tokenType()) {
|
switch (xml.tokenType()) {
|
||||||
case QXmlStreamReader::StartElement:
|
case QXmlStreamReader::StartElement:
|
||||||
@ -60,7 +62,7 @@ static QVariantList listFromXml(QXmlStreamReader &xml, Converter::Options option
|
|||||||
static VariantOrderedMap::value_type mapEntryFromXml(QXmlStreamReader &xml, Converter::Options options)
|
static VariantOrderedMap::value_type mapEntryFromXml(QXmlStreamReader &xml, Converter::Options options)
|
||||||
{
|
{
|
||||||
QVariant key, value;
|
QVariant key, value;
|
||||||
while (!xml.atEnd() && !(xml.isEndElement() && xml.name() == QLatin1String("entry"))) {
|
while (!xml.atEnd() && !(xml.isEndElement() && xml.name() == "entry"_L1)) {
|
||||||
xml.readNext();
|
xml.readNext();
|
||||||
switch (xml.tokenType()) {
|
switch (xml.tokenType()) {
|
||||||
case QXmlStreamReader::StartElement:
|
case QXmlStreamReader::StartElement:
|
||||||
@ -103,11 +105,11 @@ static QVariant mapFromXml(QXmlStreamReader &xml, Converter::Options options)
|
|||||||
QVariantMap map1;
|
QVariantMap map1;
|
||||||
VariantOrderedMap map2;
|
VariantOrderedMap map2;
|
||||||
|
|
||||||
while (!xml.atEnd() && !(xml.isEndElement() && xml.name() == QLatin1String("map"))) {
|
while (!xml.atEnd() && !(xml.isEndElement() && xml.name() == "map"_L1)) {
|
||||||
xml.readNext();
|
xml.readNext();
|
||||||
switch (xml.tokenType()) {
|
switch (xml.tokenType()) {
|
||||||
case QXmlStreamReader::StartElement:
|
case QXmlStreamReader::StartElement:
|
||||||
if (xml.name() == QLatin1String("entry")) {
|
if (xml.name() == "entry"_L1) {
|
||||||
auto pair = mapEntryFromXml(xml, options);
|
auto pair = mapEntryFromXml(xml, options);
|
||||||
if (options & Converter::SupportsArbitraryMapKeys)
|
if (options & Converter::SupportsArbitraryMapKeys)
|
||||||
map2.append(pair);
|
map2.append(pair);
|
||||||
@ -149,18 +151,18 @@ static QVariant mapFromXml(QXmlStreamReader &xml, Converter::Options options)
|
|||||||
static QVariant variantFromXml(QXmlStreamReader &xml, Converter::Options options)
|
static QVariant variantFromXml(QXmlStreamReader &xml, Converter::Options options)
|
||||||
{
|
{
|
||||||
QStringView name = xml.name();
|
QStringView name = xml.name();
|
||||||
if (name == QLatin1String("list"))
|
if (name == "list"_L1)
|
||||||
return listFromXml(xml, options);
|
return listFromXml(xml, options);
|
||||||
if (name == QLatin1String("map"))
|
if (name == "map"_L1)
|
||||||
return mapFromXml(xml, options);
|
return mapFromXml(xml, options);
|
||||||
if (name != QLatin1String("value")) {
|
if (name != "value"_L1) {
|
||||||
fprintf(stderr, "%lld:%lld: Invalid XML key '%s'.\n",
|
fprintf(stderr, "%lld:%lld: Invalid XML key '%s'.\n",
|
||||||
xml.lineNumber(), xml.columnNumber(), qPrintable(name.toString()));
|
xml.lineNumber(), xml.columnNumber(), qPrintable(name.toString()));
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
QXmlStreamAttributes attrs = xml.attributes();
|
QXmlStreamAttributes attrs = xml.attributes();
|
||||||
QStringView type = attrs.value(QLatin1String("type"));
|
QStringView type = attrs.value("type"_L1);
|
||||||
|
|
||||||
forever {
|
forever {
|
||||||
xml.readNext();
|
xml.readNext();
|
||||||
@ -182,7 +184,7 @@ static QVariant variantFromXml(QXmlStreamReader &xml, Converter::Options options
|
|||||||
QVariant result;
|
QVariant result;
|
||||||
if (type.isEmpty()) {
|
if (type.isEmpty()) {
|
||||||
// ok
|
// ok
|
||||||
} else if (type == QLatin1String("number")) {
|
} else if (type == "number"_L1) {
|
||||||
// try integer first
|
// try integer first
|
||||||
bool ok;
|
bool ok;
|
||||||
qint64 v = text.toLongLong(&ok);
|
qint64 v = text.toLongLong(&ok);
|
||||||
@ -198,27 +200,27 @@ static QVariant variantFromXml(QXmlStreamReader &xml, Converter::Options options
|
|||||||
}
|
}
|
||||||
result = d;
|
result = d;
|
||||||
}
|
}
|
||||||
} else if (type == QLatin1String("bytes")) {
|
} else if (type == "bytes"_L1) {
|
||||||
QByteArray data = text.toLatin1();
|
QByteArray data = text.toLatin1();
|
||||||
QStringView encoding = attrs.value("encoding");
|
QStringView encoding = attrs.value("encoding");
|
||||||
if (encoding == QLatin1String("base64url")) {
|
if (encoding == "base64url"_L1) {
|
||||||
result = QByteArray::fromBase64(data, QByteArray::Base64UrlEncoding);
|
result = QByteArray::fromBase64(data, QByteArray::Base64UrlEncoding);
|
||||||
} else if (encoding == QLatin1String("hex")) {
|
} else if (encoding == "hex"_L1) {
|
||||||
result = QByteArray::fromHex(data);
|
result = QByteArray::fromHex(data);
|
||||||
} else if (encoding.isEmpty() || encoding == QLatin1String("base64")) {
|
} else if (encoding.isEmpty() || encoding == "base64"_L1) {
|
||||||
result = QByteArray::fromBase64(data);
|
result = QByteArray::fromBase64(data);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "%lld:%lld: Invalid XML: unknown encoding '%s' for bytes.\n",
|
fprintf(stderr, "%lld:%lld: Invalid XML: unknown encoding '%s' for bytes.\n",
|
||||||
xml.lineNumber(), xml.columnNumber(), qPrintable(encoding.toString()));
|
xml.lineNumber(), xml.columnNumber(), qPrintable(encoding.toString()));
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
} else if (type == QLatin1String("string")) {
|
} else if (type == "string"_L1) {
|
||||||
result = text.toString();
|
result = text.toString();
|
||||||
} else if (type == QLatin1String("null")) {
|
} else if (type == "null"_L1) {
|
||||||
result = QVariant::fromValue(nullptr);
|
result = QVariant::fromValue(nullptr);
|
||||||
} else if (type == QLatin1String("CBOR simple type")) {
|
} else if (type == "CBOR simple type"_L1) {
|
||||||
result = QVariant::fromValue(QCborSimpleType(text.toShort()));
|
result = QVariant::fromValue(QCborSimpleType(text.toShort()));
|
||||||
} else if (type == QLatin1String("bits")) {
|
} else if (type == "bits"_L1) {
|
||||||
QBitArray ba;
|
QBitArray ba;
|
||||||
ba.resize(text.size());
|
ba.resize(text.size());
|
||||||
qsizetype n = 0;
|
qsizetype n = 0;
|
||||||
@ -238,13 +240,13 @@ static QVariant variantFromXml(QXmlStreamReader &xml, Converter::Options options
|
|||||||
result = ba;
|
result = ba;
|
||||||
} else {
|
} else {
|
||||||
int id = QMetaType::UnknownType;
|
int id = QMetaType::UnknownType;
|
||||||
if (type == QLatin1String("datetime"))
|
if (type == "datetime"_L1)
|
||||||
id = QMetaType::QDateTime;
|
id = QMetaType::QDateTime;
|
||||||
else if (type == QLatin1String("url"))
|
else if (type == "url"_L1)
|
||||||
id = QMetaType::QUrl;
|
id = QMetaType::QUrl;
|
||||||
else if (type == QLatin1String("uuid"))
|
else if (type == "uuid"_L1)
|
||||||
id = QMetaType::QUuid;
|
id = QMetaType::QUuid;
|
||||||
else if (type == QLatin1String("regex"))
|
else if (type == "regex"_L1)
|
||||||
id = QMetaType::QRegularExpression;
|
id = QMetaType::QRegularExpression;
|
||||||
else
|
else
|
||||||
id = QMetaType::fromName(type.toLatin1()).id();
|
id = QMetaType::fromName(type.toLatin1()).id();
|
||||||
@ -301,7 +303,7 @@ static void variantToXml(QXmlStreamWriter &xml, const QVariant &v)
|
|||||||
xml.writeEndElement();
|
xml.writeEndElement();
|
||||||
} else {
|
} else {
|
||||||
xml.writeStartElement("value");
|
xml.writeStartElement("value");
|
||||||
QString typeString = QStringLiteral("type");
|
QString typeString = "type"_L1;
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case QMetaType::Short:
|
case QMetaType::Short:
|
||||||
case QMetaType::UShort:
|
case QMetaType::UShort:
|
||||||
@ -401,7 +403,7 @@ static void variantToXml(QXmlStreamWriter &xml, const QVariant &v)
|
|||||||
|
|
||||||
QString XmlConverter::name()
|
QString XmlConverter::name()
|
||||||
{
|
{
|
||||||
return QStringLiteral("xml");
|
return "xml"_L1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Converter::Direction XmlConverter::directions()
|
Converter::Direction XmlConverter::directions()
|
||||||
@ -422,7 +424,7 @@ const char *XmlConverter::optionsHelp()
|
|||||||
bool XmlConverter::probeFile(QIODevice *f)
|
bool XmlConverter::probeFile(QIODevice *f)
|
||||||
{
|
{
|
||||||
if (QFile *file = qobject_cast<QFile *>(f)) {
|
if (QFile *file = qobject_cast<QFile *>(f)) {
|
||||||
if (file->fileName().endsWith(QLatin1String(".xml")))
|
if (file->fileName().endsWith(".xml"_L1))
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -449,9 +451,9 @@ void XmlConverter::saveFile(QIODevice *f, const QVariant &contents, const QStrin
|
|||||||
{
|
{
|
||||||
bool compact = false;
|
bool compact = false;
|
||||||
for (const QString &s : options) {
|
for (const QString &s : options) {
|
||||||
if (s == QLatin1String("compact=no")) {
|
if (s == "compact=no"_L1) {
|
||||||
compact = false;
|
compact = false;
|
||||||
} else if (s == QLatin1String("compact=yes")) {
|
} else if (s == "compact=yes"_L1) {
|
||||||
compact = true;
|
compact = true;
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "Unknown option '%s' to XML output. Valid options are:\n%s",
|
fprintf(stderr, "Unknown option '%s' to XML output. Valid options are:\n%s",
|
||||||
|
@ -11,6 +11,8 @@
|
|||||||
#include <QRandomGenerator>
|
#include <QRandomGenerator>
|
||||||
#include <QTextStream>
|
#include <QTextStream>
|
||||||
|
|
||||||
|
using namespace Qt::StringLiterals;
|
||||||
|
|
||||||
Character Game::player() const
|
Character Game::player() const
|
||||||
{
|
{
|
||||||
return mPlayer;
|
return mPlayer;
|
||||||
@ -25,35 +27,35 @@ QList<Level> Game::levels() const
|
|||||||
void Game::newGame()
|
void Game::newGame()
|
||||||
{
|
{
|
||||||
mPlayer = Character();
|
mPlayer = Character();
|
||||||
mPlayer.setName(QStringLiteral("Hero"));
|
mPlayer.setName("Hero"_L1);
|
||||||
mPlayer.setClassType(Character::Archer);
|
mPlayer.setClassType(Character::Archer);
|
||||||
mPlayer.setLevel(QRandomGenerator::global()->bounded(15, 21));
|
mPlayer.setLevel(QRandomGenerator::global()->bounded(15, 21));
|
||||||
|
|
||||||
mLevels.clear();
|
mLevels.clear();
|
||||||
mLevels.reserve(2);
|
mLevels.reserve(2);
|
||||||
|
|
||||||
Level village(QStringLiteral("Village"));
|
Level village("Village"_L1);
|
||||||
QList<Character> villageNpcs;
|
QList<Character> villageNpcs;
|
||||||
villageNpcs.reserve(2);
|
villageNpcs.reserve(2);
|
||||||
villageNpcs.append(Character(QStringLiteral("Barry the Blacksmith"),
|
villageNpcs.append(Character("Barry the Blacksmith"_L1,
|
||||||
QRandomGenerator::global()->bounded(8, 11),
|
QRandomGenerator::global()->bounded(8, 11),
|
||||||
Character::Warrior));
|
Character::Warrior));
|
||||||
villageNpcs.append(Character(QStringLiteral("Terry the Trader"),
|
villageNpcs.append(Character("Terry the Trader"_L1,
|
||||||
QRandomGenerator::global()->bounded(6, 8),
|
QRandomGenerator::global()->bounded(6, 8),
|
||||||
Character::Warrior));
|
Character::Warrior));
|
||||||
village.setNpcs(villageNpcs);
|
village.setNpcs(villageNpcs);
|
||||||
mLevels.append(village);
|
mLevels.append(village);
|
||||||
|
|
||||||
Level dungeon(QStringLiteral("Dungeon"));
|
Level dungeon("Dungeon"_L1);
|
||||||
QList<Character> dungeonNpcs;
|
QList<Character> dungeonNpcs;
|
||||||
dungeonNpcs.reserve(3);
|
dungeonNpcs.reserve(3);
|
||||||
dungeonNpcs.append(Character(QStringLiteral("Eric the Evil"),
|
dungeonNpcs.append(Character("Eric the Evil"_L1,
|
||||||
QRandomGenerator::global()->bounded(18, 26),
|
QRandomGenerator::global()->bounded(18, 26),
|
||||||
Character::Mage));
|
Character::Mage));
|
||||||
dungeonNpcs.append(Character(QStringLiteral("Eric's Left Minion"),
|
dungeonNpcs.append(Character("Eric's Left Minion"_L1,
|
||||||
QRandomGenerator::global()->bounded(5, 7),
|
QRandomGenerator::global()->bounded(5, 7),
|
||||||
Character::Warrior));
|
Character::Warrior));
|
||||||
dungeonNpcs.append(Character(QStringLiteral("Eric's Right Minion"),
|
dungeonNpcs.append(Character("Eric's Right Minion"_L1,
|
||||||
QRandomGenerator::global()->bounded(4, 9),
|
QRandomGenerator::global()->bounded(4, 9),
|
||||||
Character::Warrior));
|
Character::Warrior));
|
||||||
dungeon.setNpcs(dungeonNpcs);
|
dungeon.setNpcs(dungeonNpcs);
|
||||||
@ -64,9 +66,7 @@ void Game::newGame()
|
|||||||
//! [loadGame]
|
//! [loadGame]
|
||||||
bool Game::loadGame(Game::SaveFormat saveFormat)
|
bool Game::loadGame(Game::SaveFormat saveFormat)
|
||||||
{
|
{
|
||||||
QFile loadFile(saveFormat == Json
|
QFile loadFile(saveFormat == Json ? "save.json"_L1 : "save.dat"_L1);
|
||||||
? QStringLiteral("save.json")
|
|
||||||
: QStringLiteral("save.dat"));
|
|
||||||
|
|
||||||
if (!loadFile.open(QIODevice::ReadOnly)) {
|
if (!loadFile.open(QIODevice::ReadOnly)) {
|
||||||
qWarning("Couldn't open save file.");
|
qWarning("Couldn't open save file.");
|
||||||
@ -92,9 +92,7 @@ bool Game::loadGame(Game::SaveFormat saveFormat)
|
|||||||
//! [saveGame]
|
//! [saveGame]
|
||||||
bool Game::saveGame(Game::SaveFormat saveFormat) const
|
bool Game::saveGame(Game::SaveFormat saveFormat) const
|
||||||
{
|
{
|
||||||
QFile saveFile(saveFormat == Json
|
QFile saveFile(saveFormat == Json ? "save.json"_L1 : "save.dat"_L1);
|
||||||
? QStringLiteral("save.json")
|
|
||||||
: QStringLiteral("save.dat"));
|
|
||||||
|
|
||||||
if (!saveFile.open(QIODevice::WriteOnly)) {
|
if (!saveFile.open(QIODevice::WriteOnly)) {
|
||||||
qWarning("Couldn't open save file.");
|
qWarning("Couldn't open save file.");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user