File indexing completed on 2024-03-24 15:23:37

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2010 Dennis Nienhüser <nienhueser@kde.org>
0004 //
0005 
0006 #include "routing/instructions/WaypointParser.h"
0007 #include "routing/instructions/InstructionTransformation.h"
0008 #include "routing/instructions/RoutingInstruction.h"
0009 
0010 #include <QCoreApplication>
0011 #include <QLocale>
0012 #include <QFile>
0013 #include <QTextStream>
0014 #include <QTranslator>
0015 #include <QStringList>
0016 #include <QFileInfo>
0017 #include <QDir>
0018 
0019 using namespace Marble;
0020 
0021 QString adjustGosmoreVersion( QTextStream &stream, WaypointParser &parser )
0022 {
0023     QString content = stream.readAll();
0024     if ( !QCoreApplication::instance()->arguments().contains( "--routino" ) ) {
0025         QStringList lines = content.split(QLatin1Char('\r'));
0026         if ( lines.size() > 2 ) {
0027             QStringList fields = lines.at( lines.size()-2 ).split(QLatin1Char(','));
0028             parser.setFieldIndex( WaypointParser::RoadType, fields.size()-3 );
0029             parser.setFieldIndex( WaypointParser::TotalSecondsRemaining, fields.size()-2 );
0030             parser.setFieldIndex( WaypointParser::RoadName, fields.size()-1 );
0031         }
0032     }
0033     return content;
0034 }
0035 
0036 void loadTranslations( QCoreApplication &app, QTranslator &translator )
0037 {
0038     const QString lang = QLocale::system().name();
0039     QString code;
0040 
0041     int index = lang.indexOf(QLatin1Char('_'));
0042     if (lang == QLatin1String("C")) {
0043         code = "en";
0044     }
0045     else if ( index != -1 ) {
0046         code = lang.left ( index );
0047     }
0048     else {
0049         index = lang.indexOf(QLatin1Char('@'));
0050         if ( index != -1 )
0051             code = lang.left ( index );
0052         else
0053             code = lang;
0054     }
0055 
0056     QString const i18nDir = "/usr/share/marble/translations";
0057     QString const relativeDir = app.applicationDirPath() + QLatin1String("/translations");
0058     auto const paths = QStringList() << i18nDir << relativeDir << QDir::currentPath();
0059     for( const QString &path: paths ) {
0060         for( const QString &lang: QStringList() << lang << code ) {
0061             QFileInfo translations = QFileInfo( path + QLatin1String("/routing-instructions_") + lang + QLatin1String(".qm"));
0062             if ( translations.exists() && translator.load( translations.absoluteFilePath() ) ) {
0063                 app.installTranslator( &translator );
0064                 return;
0065             }
0066         }
0067     }
0068 }
0069 
0070 void usage()
0071 {
0072     QTextStream console( stderr );
0073     console << "Usage: routing-instructions [options] [file]\n";
0074     console << '\n' << "file should be a text file with gosmore or routino output.";
0075     console << " If file is not given, stdin is read.";
0076     console << "\nOptions:\n";
0077     console << "\t--routino\t\tParse routino output. When not given, gosmore format is assumed\n";
0078     console << "\t--dense\t\t\tReplicate the gosmore output format and only exchange road names with driving instructions\n";
0079     console << "\t--csv\t\t\tUse csv output format\n";
0080     console << "\t--remaining-duration\tInclude the remaining duration in the output\n";
0081     console << "\nTranslations:\n";
0082     console << "The system locale is examined to load translation files.";
0083     console << " Translation files must be named routing-instructions_$lang.qm,";
0084     console << " where $lang is a two-letter ISO 639 language code, optionally suffixed by an underscore";
0085     console << " and an uppercase two-letter ISO 3166 country code, e.g. nl or de_DE. Such files are searched for";
0086     console << " in /usr/share/marble/translations, the translations subdir of the applications installation";
0087     console << " directory and the current working directory.\n";
0088     console << "\nExamples:\n";
0089     console << "export QUERY_STRING=\"flat=49.0&flon=8.3&tlat=49.0&tlon=8.35&fastest=1&v=motorcar\"\n";
0090     console << "gosmore gosmore.pak | routing-instructions\n";
0091     console << "LC_ALL=\"zh_TW\" gosmore gosmore.pak | routing-instructions --dense\n";
0092     console << "LC_ALL=\"nl.UTF-8\" routing-instructions gosmore.txt\n";
0093 }
0094 
0095 int main( int argc, char* argv[] )
0096 {
0097     QCoreApplication app( argc, argv );
0098     QTranslator translator;
0099     loadTranslations( app, translator );
0100 
0101     QStringList const arguments = QCoreApplication::instance()->arguments();
0102     if ( arguments.contains( "--help" ) || arguments.contains( "-h" ) ) {
0103         usage();
0104         return 0;
0105     }
0106 
0107     RoutingInstructions directions;
0108     WaypointParser parser;
0109     if ( arguments.contains( "--routino" ) )
0110     {
0111         parser.setLineSeparator( "\n" );
0112         parser.setFieldSeparator(QLatin1Char('\t'));
0113         parser.setFieldIndex( WaypointParser::RoadName, 10 );
0114     }
0115     else
0116     {
0117         parser.addJunctionTypeMapping( "Jr", RoutingWaypoint::Roundabout );
0118     }
0119 
0120     if ( argc > 1 && !( QString( argv[argc-1] ).startsWith( "--" ) ) )
0121     {
0122         QString filename( argv[argc-1] );
0123         QFile input( filename );
0124         input.open( QIODevice::ReadOnly );
0125         QTextStream fileStream( &input );
0126         QString content = adjustGosmoreVersion( fileStream, parser );
0127         QTextStream stream( &content );
0128         directions = InstructionTransformation::process( parser.parse( stream ) );
0129     }
0130     else
0131     {
0132         QTextStream console( stdin );
0133         console.setCodec( "UTF-8" );
0134         console.setAutoDetectUnicode( true );
0135         QString content = adjustGosmoreVersion( console, parser );
0136         QTextStream stream( &content );
0137         directions = InstructionTransformation::process( parser.parse( stream ) );
0138     }
0139 
0140     QTextStream console( stdout );
0141     console.setCodec( "UTF-8" );
0142     if ( arguments.contains( "--dense" ) )
0143     {
0144         console << "Content-Type: text/plain\n\n";
0145     }
0146 
0147     for ( int i = 0; i < directions.size(); ++i )
0148     {
0149         console << directions[i] << '\n';
0150     }
0151 
0152     return 0;
0153 }