Marble

WaypointParser.cpp
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 //
3 // SPDX-FileCopyrightText: 2010 Dennis Nienhüser <[email protected]>
4 //
5 
6 #include "WaypointParser.h"
7 
8 #include <QDebug>
9 
10 namespace Marble
11 {
12 
13 // Template specialization to avoid shifting a QString through a QVariant and back
14 template<>
15 QString 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)
25 WaypointParser::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 
33 void WaypointParser::setFieldIndex( Field field, int index )
34 {
35  m_fieldIndices[field] = index;
36 }
37 
38 void WaypointParser::setLineSeparator( const QString &separator )
39 {
40  m_lineSeparator = separator;
41 }
42 
43 void WaypointParser::setFieldSeparator( const QChar &separator )
44 {
45  m_fieldSeparator = separator;
46 }
47 
48 void WaypointParser::addJunctionTypeMapping( const QString &key, RoutingWaypoint::JunctionType value )
49 {
50  m_junctionTypeMapping[key] = value;
51 }
52 
53 RoutingWaypoints 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
QString readAll()
QStringList split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
JunctionType
Junction types that affect instructions.
int size() const const
const T & at(int i) const const
Binds a QML item to a specific geodetic location in screen coordinates.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Oct 2 2023 03:52:10 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.