• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

marble

  • sources
  • kde-4.12
  • kdeedu
  • marble
  • tools
  • routing-instructions
tools/routing-instructions/main.cpp
Go to the documentation of this file.
1 //
2 // This file is part of the Marble Virtual Globe.
3 //
4 // This program is free software licensed under the GNU LGPL. You can
5 // find a copy of this license in LICENSE.txt in the top directory of
6 // the source code.
7 //
8 // Copyright 2010 Dennis Nienhüser <earthwings@gentoo.org>
9 //
10 
11 #include "routing/instructions/WaypointParser.h"
12 #include "routing/instructions/InstructionTransformation.h"
13 #include "routing/instructions/RoutingInstruction.h"
14 
15 #include <QCoreApplication>
16 #include <QLocale>
17 #include <QFile>
18 #include <QTextStream>
19 #include <QTranslator>
20 #include <QStringList>
21 #include <QFileInfo>
22 #include <QDir>
23 
24 using namespace Marble;
25 
26 QString adjustGosmoreVersion( QTextStream &stream, WaypointParser &parser )
27 {
28  QString content = stream.readAll();
29  if ( !QCoreApplication::instance()->arguments().contains( "--routino" ) ) {
30  QStringList lines = content.split( '\r' );
31  if ( lines.size() > 2 ) {
32  QStringList fields = lines.at( lines.size()-2 ).split(',');
33  parser.setFieldIndex( WaypointParser::RoadType, fields.size()-3 );
34  parser.setFieldIndex( WaypointParser::TotalSecondsRemaining, fields.size()-2 );
35  parser.setFieldIndex( WaypointParser::RoadName, fields.size()-1 );
36  }
37  }
38  return content;
39 }
40 
41 void loadTranslations( QCoreApplication &app, QTranslator &translator )
42 {
43  const QString lang = QLocale::system().name();
44  QString code;
45 
46  int index = lang.indexOf ( '_' );
47  if ( lang == "C" ) {
48  code = "en";
49  }
50  else if ( index != -1 ) {
51  code = lang.left ( index );
52  }
53  else {
54  index = lang.indexOf ( '@' );
55  if ( index != -1 )
56  code = lang.left ( index );
57  else
58  code = lang;
59  }
60 
61  QString const i18nDir = "/usr/share/marble/translations";
62  QString const relativeDir = app.applicationDirPath() + "/translations";
63  foreach( const QString &path, QStringList() << i18nDir << relativeDir << QDir::currentPath() ) {
64  foreach( const QString &lang, QStringList() << lang << code ) {
65  QFileInfo translations = QFileInfo( path + "/routing-instructions_" + lang + ".qm" );
66  if ( translations.exists() && translator.load( translations.absoluteFilePath() ) ) {
67  app.installTranslator( &translator );
68  return;
69  }
70  }
71  }
72 }
73 
74 void usage()
75 {
76  QTextStream console( stderr );
77  console << "Usage: routing-instructions [options] [file]\n";
78  console << '\n' << "file should be a text file with gosmore or routino output.";
79  console << " If file is not given, stdin is read.";
80  console << "\nOptions:\n";
81  console << "\t--routino\t\tParse routino output. When not given, gosmore format is assumed\n";
82  console << "\t--dense\t\t\tReplicate the gosmore output format and only exchange road names with driving instructions\n";
83  console << "\t--csv\t\t\tUse csv output format\n";
84  console << "\t--remaining-duration\tInclude the remaining duration in the output\n";
85  console << "\nTranslations:\n";
86  console << "The system locale is examined to load translation files.";
87  console << " Translation files must be named routing-instructions_$lang.qm,";
88  console << " where $lang is a two-letter ISO 639 language code, optionally suffixed by an underscore";
89  console << " and an uppercase two-letter ISO 3166 country code, e.g. nl or de_DE. Such files are searched for";
90  console << " in /usr/share/marble/translations, the translations subdir of the applications installation";
91  console << " directory and the current working directory.\n";
92  console << "\nExamples:\n";
93  console << "export QUERY_STRING=\"flat=49.0&flon=8.3&tlat=49.0&tlon=8.35&fastest=1&v=motorcar\"\n";
94  console << "gosmore gosmore.pak | routing-instructions\n";
95  console << "LC_ALL=\"zh_TW\" gosmore gosmore.pak | routing-instructions --dense\n";
96  console << "LC_ALL=\"nl.UTF-8\" routing-instructions gosmore.txt\n";
97 }
98 
99 int main( int argc, char* argv[] )
100 {
101  QCoreApplication app( argc, argv );
102  QTranslator translator;
103  loadTranslations( app, translator );
104 
105  QStringList const arguments = QCoreApplication::instance()->arguments();
106  if ( arguments.contains( "--help" ) || arguments.contains( "-h" ) ) {
107  usage();
108  return 0;
109  }
110 
111  RoutingInstructions directions;
112  WaypointParser parser;
113  if ( arguments.contains( "--routino" ) )
114  {
115  parser.setLineSeparator( "\n" );
116  parser.setFieldSeparator( '\t' );
117  parser.setFieldIndex( WaypointParser::RoadName, 10 );
118  }
119  else
120  {
121  parser.addJunctionTypeMapping( "Jr", RoutingWaypoint::Roundabout );
122  }
123 
124  if ( argc > 1 && !( QString( argv[argc-1] ).startsWith( "--" ) ) )
125  {
126  QString filename( argv[argc-1] );
127  QFile input( filename );
128  input.open( QIODevice::ReadOnly );
129  QTextStream fileStream( &input );
130  QString content = adjustGosmoreVersion( fileStream, parser );
131  QTextStream stream( &content );
132  directions = InstructionTransformation::process( parser.parse( stream ) );
133  }
134  else
135  {
136  QTextStream console( stdin );
137  console.setCodec( "UTF-8" );
138  console.setAutoDetectUnicode( true );
139  QString content = adjustGosmoreVersion( console, parser );
140  QTextStream stream( &content );
141  directions = InstructionTransformation::process( parser.parse( stream ) );
142  }
143 
144  QTextStream console( stdout );
145  console.setCodec( "UTF-8" );
146  if ( arguments.contains( "--dense" ) )
147  {
148  console << "Content-Type: text/plain\n\n";
149  }
150 
151  for ( int i = 0; i < directions.size(); ++i )
152  {
153  console << directions[i] << '\n';
154  }
155 
156  return 0;
157 }
Marble::WaypointParser::RoadName
Definition: WaypointParser.h:33
Marble::WaypointParser::TotalSecondsRemaining
Definition: WaypointParser.h:34
Marble::WaypointParser::RoadType
Definition: WaypointParser.h:35
RoutingInstruction.h
Marble::WaypointParser::setLineSeparator
void setLineSeparator(const QString &separator)
The line separator used in the stream passed to parse.
Definition: WaypointParser.cpp:44
usage
void usage()
Definition: tools/routing-instructions/main.cpp:74
Marble::RoutingInstructions
QVector< RoutingInstruction > RoutingInstructions
Definition: RoutingInstruction.h:157
InstructionTransformation.h
Marble::WaypointParser::parse
RoutingWaypoints parse(QTextStream &stream) const
Parses the given stream and returns the extracted waypoint list.
Definition: WaypointParser.cpp:59
Marble::WaypointParser::addJunctionTypeMapping
void addJunctionTypeMapping(const QString &key, RoutingWaypoint::JunctionType value)
Associate the given string key with the given junction type.
Definition: WaypointParser.cpp:54
main
int main(int argc, char *argv[])
Definition: tools/routing-instructions/main.cpp:99
Marble::InstructionTransformation::process
static RoutingInstructions process(const RoutingWaypoints &waypoints)
Transforms waypoints and metadata into driving directions.
Definition: InstructionTransformation.cpp:18
Marble::WaypointParser::setFieldIndex
void setFieldIndex(Field field, int index)
Associate the zero-based field no index with the given semantic type.
Definition: WaypointParser.cpp:39
adjustGosmoreVersion
QString adjustGosmoreVersion(QTextStream &stream, WaypointParser &parser)
Definition: tools/routing-instructions/main.cpp:26
loadTranslations
void loadTranslations(QCoreApplication &app, QTranslator &translator)
Definition: tools/routing-instructions/main.cpp:41
WaypointParser.h
Marble::WaypointParser::setFieldSeparator
void setFieldSeparator(const QChar &separator)
The field separator.
Definition: WaypointParser.cpp:49
QCoreApplication
Marble::RoutingWaypoint::Roundabout
Definition: RoutingWaypoint.h:31
Marble::WaypointParser
Definition: WaypointParser.h:25
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:38:51 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

marble

Skip menu "marble"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal