KItinerary

extractorutil.cpp
1 /*
2  SPDX-FileCopyrightText: 2019 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "extractorutil.h"
8 
9 #include <KItinerary/Flight>
10 #include <KItinerary/Place>
11 
12 #include <QDebug>
13 #include <QRegularExpression>
14 
15 using namespace KItinerary;
16 
17 static QString trimAirportName(QStringView in)
18 {
19  QString out = in.toString();
20  while (!out.isEmpty()) {
21  const auto c = out.at(out.size() - 1);
22  if (c.isSpace() || c == QLatin1Char('-') || c == QLatin1Char(',')) {
23  out.chop(1);
24  } else {
25  break;
26  }
27  }
28 
29  return out;
30 }
31 
32 static std::tuple<QString, QString> splitAirportName(const QString &name)
33 {
34  static QRegularExpression patterns[] = {
35  QRegularExpression(QStringLiteral("^(.*) \\((?:terminal|aerogare) (.*)\\)$"), QRegularExpression::CaseInsensitiveOption),
36  QRegularExpression(QStringLiteral("^(.*) \\((.*) (?:terminal|aerogare)\\)$"), QRegularExpression::CaseInsensitiveOption),
37  QRegularExpression(QStringLiteral("^(.*)[ -](?:terminal|aerogare) (.*)$"), QRegularExpression::CaseInsensitiveOption),
38  };
39 
40  for (const auto &re : patterns) {
41  const auto match = re.match(name);
42  if (match.hasMatch()) {
43  const auto name = trimAirportName(match.capturedView(1));
44 
45  // try to recurse, sometimes this is indeed repeated...
46  QString recName;
47  QString recTerminal;
48  std::tie(recName, recTerminal) = splitAirportName(name);
49  if (recName == name || recTerminal.isEmpty()) {
50  return std::make_tuple(trimAirportName(match.capturedView(1)), match.captured(2));
51  } else {
52  return std::make_tuple(recName, recTerminal);
53  }
54  break;
55  }
56  }
57 
58  return std::make_tuple(name, QString());
59 }
60 
62 {
63  if (flight.departureTerminal().isEmpty()) {
64  auto a = flight.departureAirport();
65  QString name;
66  QString terminal;
67  std::tie(name, terminal) = splitAirportName(a.name());
68  a.setName(name);
69  flight.setDepartureAirport(a);
70  flight.setDepartureTerminal(terminal);
71  }
72 
73  if (flight.arrivalTerminal().isEmpty()) {
74  auto a = flight.arrivalAirport();
75  QString name;
76  QString terminal;
77  std::tie(name, terminal) = splitAirportName(a.name());
78  a.setName(name);
79  flight.setArrivalAirport(a);
80  flight.setArrivalTerminal(terminal);
81  }
82 
83  return flight;
84 }
int size() const const
void chop(int n)
QString toString() const const
bool isEmpty() const const
A flight.
Definition: flight.h:24
Flight extractTerminals(Flight flight)
Move terminal indications from airport names to the correct fields.
KCOREADDONS_EXPORT Result match(QStringView pattern, QStringView str)
const char * name(StandardAction id)
const QChar at(int position) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Fri Aug 12 2022 03:54:15 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.