Marble

WaypointParser.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2010 Dennis Nienhüser <nienhueser@kde.org>
4//
5
6#include "WaypointParser.h"
7
8#include <QDebug>
9
10namespace Marble
11{
12
13// Template specialization to avoid shifting a QString through a QVariant and back
14template<>
15QString WaypointParser::readField<QString>(Field field, const QStringList &fields, const QString &defaultValue) const
16{
17 int index = m_fieldIndices[field];
18 if (index >= 0 && index < fields.size()) {
19 return fields[index];
20 }
21
22 return defaultValue;
23}
24
25// The default values are suitable for older versions of gosmore (the one shipped with Ubuntu Lucid Lynx)
26WaypointParser::WaypointParser()
27 : m_lineSeparator(QStringLiteral("\n"))
28 , m_fieldSeparator(QLatin1Char(','))
29{
30 setFieldIndex(Latitude, 0);
31 setFieldIndex(Longitude, 1);
32 setFieldIndex(JunctionType, 2);
33 setFieldIndex(RoadName, 4);
34}
35
36void WaypointParser::setFieldIndex(Field field, int index)
37{
38 m_fieldIndices[field] = index;
39}
40
41void WaypointParser::setLineSeparator(const QString &separator)
42{
43 m_lineSeparator = separator;
44}
45
46void WaypointParser::setFieldSeparator(const QChar &separator)
47{
48 m_fieldSeparator = separator;
49}
50
51void WaypointParser::addJunctionTypeMapping(const QString &key, RoutingWaypoint::JunctionType value)
52{
53 m_junctionTypeMapping[key] = value;
54}
55
56RoutingWaypoints WaypointParser::parse(QTextStream &stream) const
57{
58 RoutingWaypoints result;
59 QString input = stream.readAll();
60 const QStringList lines = input.split(m_lineSeparator);
61 for (const QString &line : lines) {
62 if (!line.trimmed().isEmpty() && !line.trimmed().startsWith(QLatin1Char('#')) && !line.startsWith(QLatin1StringView("Content-Type: text/plain"))) {
63 QStringList entries = line.split(m_fieldSeparator);
64 if (entries.size() >= 1 + m_fieldIndices[RoadName]) {
65 auto lon = readField<qreal>(Longitude, entries);
66 auto lat = readField<qreal>(Latitude, entries);
67 RoutingPoint point(lon, lat);
68 QString junctionTypeRaw = readField<QString>(JunctionType, entries, QString());
69 RoutingWaypoint::JunctionType junctionType = RoutingWaypoint::Other;
70 if (m_junctionTypeMapping.contains(junctionTypeRaw)) {
71 junctionType = m_junctionTypeMapping[junctionTypeRaw];
72 }
73 QString roadType = readField<QString>(RoadType, entries, QString());
74 int secondsRemaining = readField<int>(TotalSecondsRemaining, entries, -1);
75 QString roadName = readField<QString>(RoadName, entries, QString());
76
77 // Road names may contain the field separator
78 for (int i = 2 + m_fieldIndices[RoadName]; i < entries.size(); ++i) {
79 roadName += m_fieldSeparator + entries.at(i);
80 }
81
82 RoutingWaypoint item(point, junctionType, junctionTypeRaw, roadType, secondsRemaining, roadName);
83 result.push_back(item);
84 } else {
85 qDebug() << "Cannot parse " << line << "(detected " << entries.size() << " fields)";
86 }
87 }
88 }
89
90 return result;
91}
92
93} // namespace Marble
Binds a QML item to a specific geodetic location in screen coordinates.
const_reference at(qsizetype i) const const
qsizetype size() const const
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QString readAll()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 20 2024 11:52:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.