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// RZ: seat reservations for the return trip as '#' separated "<train num>|<coach num>|<seat num>" triples
39
40Vendor1154UTSubBlock::Vendor1154UTSubBlock() = default;
41
42Vendor1154UTSubBlock::Vendor1154UTSubBlock(const Uic9183Block &block, int offset)
43 : m_offset(offset)
44{
45 if (block.isNull()) {
46 return;
47 }
48
49 if (block.contentSize() < offset + SubBlockHeaderSize) {
50 qCWarning(Log) << "1154UT sub-block too small";
51 return;
52 }
53
54 m_block = block;
55 if (block.contentSize() < offset + size()) {
56 qCWarning(Log) << "1154UT sub-block size exceeds 1154UT block size";
57 m_block = {};
58 }
59}
60
61bool Vendor1154UTSubBlock::isNull() const
62{
63 return m_block.isNull();
64}
65
67{
68 return contentSize() + SubBlockHeaderSize;
69}
70
71Vendor1154UTSubBlock Vendor1154UTSubBlock::nextBlock() const
72{
73 if (m_offset + size() >= m_block.contentSize()) { // we are the last block
74 return {};
75 }
76 return Vendor1154UTSubBlock(m_block, m_offset + size());
77}
78
80{
81 if (isNull()) {
82 return 0;
83 }
84 return Uic9183Utils::readAsciiEncodedNumber(m_block.content(), m_block.size(), m_offset + SubBlockLengthOffset, SubBlockLengthSize);
85}
86
87const char* Vendor1154UTSubBlock::id() const
88{
89 if (isNull()) {
90 return nullptr;
91 }
92 return m_block.content() + m_offset + SubBlockTypeOffset;
93}
94
95const char* Vendor1154UTSubBlock::content() const
96{
97 if (isNull()) {
98 return nullptr;
99 }
100 return m_block.content() + m_offset + SubBlockHeaderSize;
101}
102
104{
105 if (isNull()) {
106 return {};
107 }
108 return QString::fromUtf8(content(), contentSize());
109}
110
111Vendor1154UTBlock::Vendor1154UTBlock(const Uic9183Block &block)
112 : m_block(block)
113{
114}
115
116bool Vendor1154UTBlock::isValid() const
117{
118 return !m_block.isNull();
119}
120
125
126Vendor1154UTSubBlock Vendor1154UTBlock::findSubBlock(const char id[SubBlockTypeSize]) const
127{
128 auto sblock = firstBlock();
129 while (!sblock.isNull()) {
130 if (strncmp(sblock.id(), id, SubBlockTypeSize) == 0) {
131 return sblock;
132 }
133 sblock = sblock.nextBlock();
134 }
135 return {};
136}
137
138QVariant Vendor1154UTBlock::findSubBlock(const QString &str) const
139{
140 if (str.size() != 2 || !isValid()) {
141 return {};
142 }
143 const auto b = findSubBlock(str.toUtf8().constData());
144 return b.isNull() ? QVariant() : QVariant::fromValue(b);
145}
146
147#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.
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-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:52:36 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.