KItinerary

uic9183utils.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 "uic9183utils.h"
8#include "uic9183block.h"
9#include "logging.h"
10
11#include <string.h>
12
13using namespace KItinerary;
14
15int Uic9183Utils::readAsciiEncodedNumber(const char* data, int size, int offset, int length)
16{
17 if (!data || offset < 0 || length < 1 || size < 1 || offset + length > size) {
18 qCWarning(Log) << "Invalid UIC 918.3 read" << offset << length << size;
19 return {};
20 }
21
22 int v = 0;
23 for (int i = 0; i < length; ++i) {
24 v *= 10;
25 v += (*(data + offset + i)) - '0';
26 }
27 return v;
28}
29
30int Uic9183Utils::readAsciiEncodedNumber(const QByteArray &data, int offset, int length)
31{
32 return readAsciiEncodedNumber(data.constData(), data.size(), offset, length);
33}
34
35int Uic9183Utils::readAsciiEncodedNumber(const Uic9183Block &block, int offset, int length)
36{
37 return readAsciiEncodedNumber(block.content(), block.contentSize(), offset, length);
38}
39
40QString Uic9183Utils::readUtf8String(const char* data, int size, int offset, int length)
41{
42 if (length == 0) { // common in ÖBB RCT2 blocks...
43 return {};
44 }
45
46 if (!data || offset < 0 || length < 1 || size < 1 || offset + length > size) {
47 qCWarning(Log) << "Invalid UIC 918.3 read" << offset << length << size;
48 return {};
49 }
50
51 return QString::fromUtf8(data + offset, strnlen(data + offset, length));
52}
53
54QString Uic9183Utils::readUtf8String(const QByteArray& data, int offset, int length)
55{
56 return readUtf8String(data.constData(), data.size(), offset, length);
57}
58
59QString Uic9183Utils::readUtf8String(const Uic9183Block &block, int offset, int length)
60{
61 return readUtf8String(block.content(), block.contentSize(), offset, length);
62}
A data block from a UIC 918.3 ticket.
int contentSize() const
Returns the size of the content data.
const char * content() const
Returns the payload data (not including the block header).
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
const char * constData() const const
qsizetype size() const const
QString fromUtf8(QByteArrayView str)
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.