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 int index = m_fieldIndices[field];
17 if ( index >= 0 && index < fields.size() ) {
18 return fields[index];
19 }
20
21 return defaultValue;
22}
23
24// The default values are suitable for older versions of gosmore (the one shipped with Ubuntu Lucid Lynx)
25WaypointParser::WaypointParser() : m_lineSeparator(QStringLiteral("\n")), m_fieldSeparator(QLatin1Char(','))
26{
27 setFieldIndex( Latitude, 0 );
28 setFieldIndex( Longitude, 1 );
29 setFieldIndex( JunctionType, 2 );
30 setFieldIndex( RoadName, 4 );
31}
32
33void WaypointParser::setFieldIndex( Field field, int index )
34{
35 m_fieldIndices[field] = index;
36}
37
38void WaypointParser::setLineSeparator( const QString &separator )
39{
40 m_lineSeparator = separator;
41}
42
43void WaypointParser::setFieldSeparator( const QChar &separator )
44{
45 m_fieldSeparator = separator;
46}
47
48void WaypointParser::addJunctionTypeMapping( const QString &key, RoutingWaypoint::JunctionType value )
49{
50 m_junctionTypeMapping[key] = value;
51}
52
53RoutingWaypoints WaypointParser::parse( QTextStream &stream ) const
54{
55 RoutingWaypoints result;
56 QString input = stream.readAll();
57 QStringList lines = input.split( m_lineSeparator );
58 for( const QString &line: lines ) {
59 if ( !line.trimmed().isEmpty() &&
60 !line.trimmed().startsWith(QLatin1Char('#')) &&
61 !line.startsWith( QLatin1String( "Content-Type: text/plain" ) ) ) {
62 QStringList entries = line.split( m_fieldSeparator );
63 if ( entries.size() >= 1 + m_fieldIndices[RoadName] ) {
64 qreal lon = readField<qreal>( Longitude, entries );
65 qreal lat = readField<qreal>( Latitude, entries );
66 RoutingPoint point( lon, lat );
67 QString junctionTypeRaw = readField<QString>( JunctionType, entries, QString() );
68 RoutingWaypoint::JunctionType junctionType = RoutingWaypoint::Other;
69 if ( m_junctionTypeMapping.contains( junctionTypeRaw ) ) {
70 junctionType = m_junctionTypeMapping[junctionTypeRaw];
71 }
72 QString roadType = readField<QString>( RoadType, entries, QString() );
73 int secondsRemaining = readField<int>( TotalSecondsRemaining, entries, -1 );
74 QString roadName = readField<QString>( RoadName, entries, QString() );
75
76 // Road names may contain the field separator
77 for (int i = 2 + m_fieldIndices[RoadName]; i<entries.size(); ++i)
78 {
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 Jun 21 2024 12:00:07 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.