KItinerary

plist-dump.cpp
1/*
2 SPDX-FileCopyrightText: 2023 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include <kitinerary_version.h>
7
8#include <../lib/plist/plistreader.cpp>
9#include "../lib/plist/plistdata_p.h"
10
11#include <QCommandLineParser>
12#include <QCoreApplication>
13#include <QFile>
14#include <QJsonDocument>
15
16#include <iostream>
17
18using namespace KItinerary;
19
20void dumpValue(const QVariant &v)
21{
22 if (v.userType() == qMetaTypeId<PListArray>()) {
23 const auto a = v.value<PListArray>();
24 std::cout << "[";
25 for (uint64_t i = 0; i < a.size(); ++i) {
26 std::cout << a.value(i);
27 if (i != a.size() - 1) {
28 std::cout << ",";
29 }
30 }
31 std::cout << "]";
32 return;
33 }
34
35 if (v.userType() == qMetaTypeId<PListDict>()) {
36 const auto a = v.value<PListDict>();
37 std::cout << "{";
38 for (uint64_t i = 0; i < a.size(); ++i) {
39 std::cout << a.key(i) << ":" << a.value(i);
40 if (i != a.size() - 1) {
41 std::cout << ", ";
42 }
43 }
44 std::cout << "}";
45 return;
46 }
47
48 if (v.userType() == qMetaTypeId<PListUid>()) {
49 std::cout << "UID:" << v.value<PListUid>().value;
50 return;
51 }
52
53 std::cout << qPrintable(v.toString());
54}
55
56static void writeIndent(int indent)
57{
58 while (indent) {
59 std::cout << " ";
60 --indent;
61 }
62}
63
64static void dumpRecursive(const PListReader &reader, uint64_t idx, int indent = 0)
65{
66 const auto type = reader.objectType(idx);
67 switch (type) {
68 case PListObjectType::Array:
69 {
70 const auto a = reader.object(idx).value<PListArray>();
71 std::cout << "[" << std::endl;
72 for (uint64_t i = 0; i < a.size(); ++i) {
73 writeIndent(indent + 1);
74 dumpRecursive(reader, a.value(i), indent + 1);
75 }
76 writeIndent(indent);
77 std::cout << "]" << std::endl;
78 break;
79 }
80 case PListObjectType::Dict:
81 {
82 const auto d = reader.object(idx).value<PListDict>();
83 std::cout << "{" << std::endl;
84 for (uint64_t i = 0; i < d.size(); ++i) {
85 writeIndent(indent + 1);
86 dumpValue(reader.object(d.key(i)));
87 std::cout << ": ";
88 dumpRecursive(reader, d.value(i), indent + 1);
89 }
90 writeIndent(indent);
91 std::cout << "}" << std::endl;
92 break;
93 }
94 default:
95 dumpValue(reader.object(idx));
96 std::cout << std::endl;
97 }
98}
99
100int main(int argc, char **argv)
101{
102 QCoreApplication::setApplicationName(QStringLiteral("plist-dump"));
103 QCoreApplication::setApplicationVersion(QStringLiteral(KITINERARY_VERSION_STRING));
104 QCoreApplication::setOrganizationDomain(QStringLiteral("kde.org"));
105 QCoreApplication::setOrganizationName(QStringLiteral("KDE"));
106 QCoreApplication app(argc, argv);
107
108 QCommandLineParser parser;
109 parser.setApplicationDescription(QStringLiteral("Dump binary plist content."));
110 parser.addHelpOption();
111 parser.addVersionOption();
112 parser.addPositionalArgument(QStringLiteral("input"), QStringLiteral("File to read data from, omit for using stdin."));
113 parser.process(app);
114
115 QFile file;
116 if (parser.positionalArguments().isEmpty()) {
117 file.open(stdin, QFile::ReadOnly);
118 } else {
119 file.setFileName(parser.positionalArguments().at(0));
120 if (!file.open(QFile::ReadOnly)) {
121 std::cerr << qPrintable(file.errorString()) << std::endl;
122 return 1;
123 }
124 }
125
126 const auto buffer = file.readAll();
127 PListReader reader(buffer);
128
129 // object list
130 for (uint64_t i = 0; i < reader.objectCount(); ++i) {
131 std::cout << i << ": [" << (int)reader.objectType(i) << "] ";
132 dumpValue(reader.object(i));
133 std::cout << std::endl;
134 }
135 std::cout << std::endl << std::endl;
136
137 // object tree
138 dumpRecursive(reader, reader.rootObjectIndex());
139 std::cout << std::endl << std::endl;
140
141 // NSKeyedArchiver
142 std::cout << QJsonDocument(reader.unpackKeyedArchive().toObject()).toJson().constData() << std::endl;
143}
144
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
const char * constData() const const
QCommandLineOption addHelpOption()
void addPositionalArgument(const QString &name, const QString &description, const QString &syntax)
QCommandLineOption addVersionOption()
QStringList positionalArguments() const const
void process(const QCoreApplication &app)
void setApplicationDescription(const QString &description)
void setApplicationName(const QString &application)
void setApplicationVersion(const QString &version)
void setOrganizationDomain(const QString &orgDomain)
void setOrganizationName(const QString &orgName)
bool open(FILE *fh, OpenMode mode, FileHandleFlags handleFlags)
void setFileName(const QString &name)
QString errorString() const const
QByteArray readAll()
QByteArray toJson(JsonFormat format) const const
const_reference at(qsizetype i) const const
bool isEmpty() const const
QString toString() const const
int userType() const const
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 4 2024 12:00:26 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.