KItinerary

stationidentifier.cpp
1/*
2 SPDX-FileCopyrightText: 2018-2020 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "stationidentifier.h"
8
9#include <QString>
10
11#include <cstring>
12
13using namespace KItinerary::KnowledgeDb;
14
15UICIdentiferBase::UICIdentiferBase(const QString &id)
16{
17 const auto n = id.toUInt();
18 setValue(n > 9999999 ? n / 10 : n);
19}
20
21static bool containsOnlyLetters(const QString &s)
22{
23 for (const auto c : s) {
24 if (c < QLatin1Char('A') || c > QLatin1Char('Z')) {
25 return false;
26 }
27 }
28 return true;
29}
30
31FiveAlphaId::FiveAlphaId(const QString &id)
32{
33 if (id.size() != 5 || !containsOnlyLetters(id)) {
34 return;
35 }
36
37 setValue(fromChars(id.toUpper().toUtf8().constData()));
38}
39
40QString FiveAlphaId::toString() const
41{
42 auto id = value();
43 if (id == 0) {
44 return {};
45 }
46
47 QString s;
48 s.resize(5);
49 for (int i = 0; i < 5; ++i) {
50 s[4 - i] = QLatin1Char('@' + (id % 27));
51 id /= 27;
52 }
53
54 return s;
55}
56
57
58[[nodiscard]] static bool isValidVRCodeLetter(QChar c)
59{
60 return c.row() == 0 && ((c.cell() >= 'A' && c.cell() <= 'Z') || c.cell() == 0xC4 || c.cell() == 0xD6);
61}
62
63VRStationCode::VRStationCode(const QString &id)
64{
65 if (id.size() < 2 || id.size() > 4 || !std::all_of(id.begin(), id.end(), isValidVRCodeLetter)) {
66 return;
67 }
68
69 char buffer[4];
70 std::memset(buffer, 0, 4);
71 std::memcpy(buffer, id.toLatin1().constData(), id.size());
72 setValue(fromChars(buffer));
73}
74
75QString VRStationCode::toString() const
76{
77 auto id = value();
78 if (id == 0) {
79 return {};
80 }
81
82 QString s;
83 for (int i = 3; i >= 0; --i) {
84 const auto v = ((id >> (i*6)) % 32);
85 switch (v) {
86 case 0: return s;
87 case 27:
88 s += QChar(0xC4);
89 break;
90 case 28:
91 s += QChar(0xD6);
92 break;
93 default:
94 s += QLatin1Char('@' + v);
95 }
96 }
97
98 return s;
99}
100
101static_assert(sizeof(ViaRailStationCode) == 3);
Lookup functions, utilities and data types for the static knowledge database.
Definition airportdb.cpp:21
AlphaId< UnalignedNumber< 3 >, 4 > ViaRailStationCode
Via Rail station code.
const QList< QKeySequence > & begin()
const QList< QKeySequence > & end()
uchar cell() const const
uchar row() const const
void resize(qsizetype newSize, QChar fillChar)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 17 2024 11:50:52 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.