KItinerary

iatabcbpsections.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 "iatabcbpsections.h"
8#include "iatabcbpconstants_p.h"
9#include "logging.h"
10
11using namespace Qt::Literals::StringLiterals;
12using namespace KItinerary;
13using namespace KItinerary::IataBcbpConstants;
14
15QString IataBcbpSectionBase::readString(int offset, int length) const
16{
17 if (m_data.size() >= offset + length) {
18 return m_data.mid(offset, length).trimmed().toString();
19 }
20 return {};
21}
22
23int IataBcbpSectionBase::readNumericValue(int offset, int length, int base) const
24{
25 if (m_data.size() >= offset + length) {
26 return m_data.mid(offset, length).toInt(nullptr, base);
27 }
28 return 0;
29}
30
31
32IataBcbpUniqueMandatorySection::IataBcbpUniqueMandatorySection(QStringView data)
33{
34 m_data = data;
35}
36
37bool IataBcbpUniqueMandatorySection::isValid() const
38{
39 const auto legCount = numberOfLegs();
40 return legCount >= 1 && legCount <= 4;
41}
42
43IataBcbpUniqueConditionalSection::IataBcbpUniqueConditionalSection(QStringView data)
44{
45 if (data.size() < MinimumUniqueConditionalSize) {
46 return;
47 }
48 m_data = data;
49 m_data = data.left(MinimumUniqueConditionalSize + fieldSize());
50}
51
52bool IataBcbpUniqueConditionalSection::isValid() const
53{
54 if (m_data.size() >= 11) {
55 // issue date
56 if (std::any_of(m_data.begin() + 8, m_data.begin() + 11, [](auto c) { return !c.isDigit() && c != ' '_L1; }) || dayOfIssue() > 366) {
57 return false;
58 }
59 }
60 return true;
61}
62
63QDate IataBcbpUniqueConditionalSection::dateOfIssue(const QDateTime &contextDate) const
64{
65 const auto day = dayOfIssue() - 1;
66 if (m_data.size() < 11 || day < 0) {
67 return {};
68 }
69
70 const auto year = contextDate.date().year() - contextDate.date().year() % 10 + yearOfIssue();
71 const auto d = QDate(year, 1, 1).addDays(day);
72 // TODO shouldn't this rather be d > contextDate?
73 if (year > contextDate.date().year()) {
74 return QDate(year - 10, 1, 1).addDays(day);
75 }
76 return d;
77}
78
79IataBcbpRepeatedMandatorySection::IataBcbpRepeatedMandatorySection(QStringView data)
80{
81 m_data = data;
82}
83
84static bool isValidAirportCode(QStringView s)
85{
86 return std::all_of(s.begin(), s.end(), [](const QChar c) { return c.isLetter() && c.isUpper(); });
87}
88
89bool IataBcbpRepeatedMandatorySection::isValid() const
90{
91 if (m_data.size() < RepeatedMandatoryMinimalViableSize) {
92 return false;
93 }
94
95 return isValidAirportCode(m_data.mid(7, 3))
96 && isValidAirportCode(m_data.mid(10, 3))
97 && std::all_of(m_data.begin() + 21, m_data.begin() + 24, [](auto c) { return c.isDigit() || c == ' '_L1; })
98 && dayOfFlight() <= 366;
99}
100
102{
103 const auto day = dayOfFlight() - 1;
104 if (day < 0) {
105 return {}; // no set
106 }
107 const auto d = QDate(contextDate.date().year(), 1, 1).addDays(day);
108 if (d < contextDate.date()) {
109 return QDate(d.year() + 1, 1, 1).addDays(day);
110 }
111 return d;
112}
113
114IataBcbpRepeatedConditionalSection::IataBcbpRepeatedConditionalSection(QStringView data)
115{
116 if (data.size() < 2) {
117 return;
118 }
119 m_data = data;
120 m_data = data.left(conditionalFieldSize() + 2);
121}
122
123IataBcbpSecuritySection::IataBcbpSecuritySection(QStringView data)
124{
125 if (data.size() < MinimumSecuritySectionSize) {
126 return;
127 }
128 m_data = data;
129 m_data = data.left(size() + MinimumSecuritySectionSize);
130}
131
132#include "moc_iatabcbpsections.cpp"
Q_INVOKABLE QDate dateOfFlight(const QDateTime &contextDate=QDateTime::currentDateTime()) const
Date of the flight.
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
QStringView left(qsizetype length) const const
QStringView mid(qsizetype start, qsizetype length) const const
const_iterator begin() const const
const_iterator end() const const
qsizetype size() const const
int toInt(bool *ok, int base) const const
QString toString() const const
QStringView trimmed() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:09:58 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.