KItinerary

extractorutil.cpp
1/*
2 SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "extractorutil.h"
8#include "text/terminalfinder_p.h"
9
10#include <KItinerary/Flight>
11#include <KItinerary/Place>
12
13#include <QDebug>
14#include <QRegularExpression>
15
16using namespace KItinerary;
17
18static QString trimAirportName(QStringView in)
19{
20 QString out = in.toString();
21 while (!out.isEmpty()) {
22 const auto c = out.at(out.size() - 1);
23 if (c.isSpace() || c == QLatin1Char('-') || c == QLatin1Char(',')) {
24 out.chop(1);
25 } else {
26 break;
27 }
28 }
29
30 return out;
31}
32
33static std::tuple<QString, QString> splitAirportName(const QString &name)
34{
35 static TerminalFinder finder(u"^.*", u"$");
36 const auto result = finder.find(name);
37 if (result.hasResult()) {
38 const auto airportName = trimAirportName(QStringView(name).left(result.start));
39
40 // try to recurse, sometimes this is indeed repeated...
41 QString recName;
42 QString recTerminal;
43 std::tie(recName, recTerminal) = splitAirportName(airportName);
44 if (recName == name || recTerminal.isEmpty()) {
45 return std::make_tuple(airportName, result.name);
46 } else {
47 return std::make_tuple(recName, recTerminal);
48 }
49 }
50
51 return std::make_tuple(name, QString());
52}
53
55{
56 if (flight.departureTerminal().isEmpty()) {
57 auto a = flight.departureAirport();
58 QString name;
59 QString terminal;
60 std::tie(name, terminal) = splitAirportName(a.name());
61 a.setName(name);
62 flight.setDepartureAirport(a);
63 flight.setDepartureTerminal(terminal);
64 }
65
66 if (flight.arrivalTerminal().isEmpty()) {
67 auto a = flight.arrivalAirport();
68 QString name;
69 QString terminal;
70 std::tie(name, terminal) = splitAirportName(a.name());
71 a.setName(name);
72 flight.setArrivalAirport(a);
73 flight.setArrivalTerminal(terminal);
74 }
75
76 return flight;
77}
A flight.
Definition flight.h:25
Flight extractTerminals(Flight flight)
Move terminal indications from airport names to the correct fields.
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
const QChar at(qsizetype position) const const
void chop(qsizetype n)
bool isEmpty() const const
qsizetype size() const const
QString toString() const const
QTextStream & left(QTextStream &stream)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:28:48 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.