KItinerary

ssbticketbase.cpp
1/*
2 SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "ssbticketbase.h"
8
9#include <QDateTime>
10#include <QDebug>
11#include <QString>
12
13using namespace KItinerary;
14
15enum {
16 SSB_CHAR_WIDTH = 6,
17};
18
19SSBTicketBase::SSBTicketBase() = default;
20SSBTicketBase::~SSBTicketBase() = default;
21
22quint64 SSBTicketBase::readNumber(int start, int length) const
23{
24 if (start < 0 || length < 1 || start / 8 >= m_data.size() || (start + length) / 8 >= m_data.size() || length > 63) {
25 qWarning() << "invalid SSB read:" << start << length;
26 return {};
27 }
28
29 quint64 num = 0;
30 for (int i = 0; i < 8; ++i) {
31 num <<= 8;
32 num |= (uint8_t)*(m_data.constData() + (start / 8) + i);
33 }
34 num <<= start % 8;
35 num >>= 64 - length;
36
37 return num;
38}
39
40QString SSBTicketBase::readString(int start, int length) const
41{
42 QString res;
43 res.reserve(length);
44 for (int i = 0; i < length; ++i) {
45 auto n = readNumber(start + SSB_CHAR_WIDTH * i, SSB_CHAR_WIDTH);
46 if (n >= 36 && n != 63) {
47 continue;
48 }
49 if (n <= 9) {
50 res += QLatin1Char(n + '0');
51 } else if (n != 63) {
52 res += QLatin1Char(n - 10 + 'A');
53 } else {
54 // outside of the specification, seems to be in use in Finland though
55 res += u"Ä";
56 }
57 }
58 return res;
59}
60
62{
63 if (days <= 0 || days > 366) {
64 return {};
65 }
66
67 QDate dt(context.date().year(), 1, 1);
68 dt = dt.addDays(days - 1);
69 if (dt < context.date()) {
70 dt = QDate(context.date().year() + 1, 1, 1);
71 dt = dt.addDays(days - 1);
72 }
73 return dt;
74}
75
76#include "moc_ssbticketbase.cpp"
static QDate dayNumberToDate(int days, const QDateTime &context)
Convert a SSBv1 or v2 day number to a date based on context.
Q_SCRIPTABLE QString start(QString train="")
Q_SCRIPTABLE Q_NOREPLY void start()
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
QDate addDays(qint64 ndays) const const
int year() const const
QDate date() const const
void reserve(qsizetype size)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:52:35 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.