KItinerary

vendor1154block.cpp
1/*
2 SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "vendor1154block.h"
7#include "uic9183utils.h"
8#include "logging.h"
9
10#include <QVariant>
11
12using namespace KItinerary;
13
14// 1154UT vendor block sub-block format
15// 2x field type
16// 3x field size as ASCII number
17// nx field content
18enum {
19 SubBlockTypeOffset = 0,
20 SubBlockTypeSize = 2,
21 SubBlockLengthOffset = SubBlockTypeOffset + SubBlockTypeSize,
22 SubBlockLengthSize = 3,
23 SubBlockHeaderSize = SubBlockTypeSize + SubBlockLengthSize,
24 SubBlockContentOffset = SubBlockLengthOffset + SubBlockLengthSize,
25};
26
27// known field types:
28// KJ: passenger name
29// OD: begin of validity
30// DO: end of validity
31// EM: ?
32// KD: type of card (0 == none?)
33// KC: card number
34// KK: transaction code
35// KS: routing, as a | separated list of UIC station codes?
36// KM: distance in kilometers
37// RT: seat reservations as '#' separated "<train num>|<coach num>|<seat num>" triples
38
39Vendor1154UTSubBlock::Vendor1154UTSubBlock() = default;
40
41Vendor1154UTSubBlock::Vendor1154UTSubBlock(const Uic9183Block &block, int offset)
42 : m_offset(offset)
43{
44 if (block.isNull()) {
45 return;
46 }
47
48 if (block.contentSize() < offset + SubBlockHeaderSize) {
49 qCWarning(Log) << "1154UT sub-block too small";
50 return;
51 }
52
53 m_block = block;
54 if (block.contentSize() < offset + size()) {
55 qCWarning(Log) << "1154UT sub-block size exceeds 1154UT block size";
56 m_block = {};
57 }
58}
59
60bool Vendor1154UTSubBlock::isNull() const
61{
62 return m_block.isNull();
63}
64
66{
67 return contentSize() + SubBlockHeaderSize;
68}
69
71{
72 if (m_offset + size() >= m_block.contentSize()) { // we are the last block
73 return {};
74 }
75 return Vendor1154UTSubBlock(m_block, m_offset + size());
76}
77
79{
80 if (isNull()) {
81 return 0;
82 }
83 return Uic9183Utils::readAsciiEncodedNumber(m_block.content(), m_block.size(), m_offset + SubBlockLengthOffset, SubBlockLengthSize);
84}
85
86const char* Vendor1154UTSubBlock::id() const
87{
88 if (isNull()) {
89 return nullptr;
90 }
91 return m_block.content() + m_offset + SubBlockTypeOffset;
92}
93
94const char* Vendor1154UTSubBlock::content() const
95{
96 if (isNull()) {
97 return nullptr;
98 }
99 return m_block.content() + m_offset + SubBlockHeaderSize;
100}
101
103{
104 if (isNull()) {
105 return {};
106 }
107 return QString::fromUtf8(content(), contentSize());
108}
109
110Vendor1154UTBlock::Vendor1154UTBlock(const Uic9183Block &block)
111 : m_block(block)
112{
113}
114
115bool Vendor1154UTBlock::isValid() const
116{
117 return !m_block.isNull();
118}
119
124
125Vendor1154UTSubBlock Vendor1154UTBlock::findSubBlock(const char id[SubBlockTypeSize]) const
126{
127 auto sblock = firstBlock();
128 while (!sblock.isNull()) {
129 if (strncmp(sblock.id(), id, SubBlockTypeSize) == 0) {
130 return sblock;
131 }
132 sblock = sblock.nextBlock();
133 }
134 return {};
135}
136
138{
139 if (str.size() != 2 || !isValid()) {
140 return {};
141 }
142 const auto b = findSubBlock(str.toUtf8().constData());
143 return b.isNull() ? QVariant() : QVariant::fromValue(b);
144}
145
146#include "moc_vendor1154block.cpp"
A data block from a UIC 918.3 ticket.
int contentSize() const
Returns the size of the content data.
bool isNull() const
Checks if the block is valid or empty/default constructed.
int size() const
Returns the size of the entire block data.
const char * content() const
Returns the payload data (not including the block header).
Vendor1154UTSubBlock findSubBlock(const char id[2]) const
Finds a S-block by type.
Vendor1154UTSubBlock firstBlock() const
First S-block, for iterating.
UIC 918.3 1154UT vendor data block sub-block.
const char * id() const
Type of the block.
int size() const
Size of the entire S-block.
QString toString() const
Content as Unicode string.
Vendor1154UTSubBlock nextBlock() const
Next sub-block in the 1154UT block.
int contentSize() const
Size of the content the sub-block.
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
const char * constData() const const
QString fromUtf8(QByteArrayView str)
qsizetype size() const const
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:28:48 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.