Skip to content

Commit 9d4956a

Browse files
author
Hugo Mercier
committedJul 18, 2013
refs #8045 Fix the way main() parse path names with special characters
1 parent c8e818a commit 9d4956a

File tree

1 file changed

+84
-207
lines changed

1 file changed

+84
-207
lines changed
 

‎src/app/main.cpp

Lines changed: 84 additions & 207 deletions
Original file line numberDiff line numberDiff line change
@@ -371,208 +371,105 @@ int main( int argc, char *argv[] )
371371
//put all QGIS settings in the same place
372372
configpath = QgsApplication::qgisSettingsPath();
373373
QgsDebugMsg( QString( "Android: configpath set to %1" ).arg( configpath ) );
374-
#elif defined(Q_WS_WIN)
375-
for ( int i = 1; i < argc; i++ )
376-
{
377-
QString arg = argv[i];
374+
#endif
378375

379-
if ( arg == "--help" || arg == "-?" )
380-
{
381-
usage( argv[0] );
382-
return 2;
383-
}
384-
else if ( arg == "-nologo" || arg == "-n" )
385-
{
386-
myHideSplash = true;
387-
}
388-
else if ( arg == "--noplugins" || arg == "-P" )
389-
{
390-
myRestorePlugins = false;
391-
}
392-
else if ( arg == "--nocustomization" || arg == "-C" )
393-
{
394-
myCustomization = false;
395-
}
396-
else if ( i + 1 < argc && ( arg == "--snapshot" || arg == "-s" ) )
397-
{
398-
mySnapshotFileName = QDir::convertSeparators( QFileInfo( QFile::decodeName( argv[++i] ) ).absoluteFilePath() );
399-
}
400-
else if ( i + 1 < argc && ( arg == "--width" || arg == "-w" ) )
401-
{
402-
mySnapshotWidth = QString( argv[++i] ).toInt();
403-
}
404-
else if ( i + 1 < argc && ( arg == "--height" || arg == "-h" ) )
405-
{
406-
mySnapshotHeight = QString( argv[++i] ).toInt();
407-
}
408-
else if ( i + 1 < argc && ( arg == "--lang" || arg == "-l" ) )
409-
{
410-
myTranslationCode = argv[++i];
411-
}
412-
else if ( i + 1 < argc && ( arg == "--project" || arg == "-p" ) )
413-
{
414-
myProjectFileName = QDir::convertSeparators( QFileInfo( QFile::decodeName( argv[++i] ) ).absoluteFilePath() );
415-
}
416-
else if ( i + 1 < argc && ( arg == "--extent" || arg == "-e" ) )
417-
{
418-
myInitialExtent = argv[++i];
419-
}
420-
else if ( i + 1 < argc && ( arg == "--optionspath" || arg == "-o" ) )
421-
{
422-
optionpath = argv[++i];
423-
}
424-
else if ( i + 1 < argc && ( arg == "--configpath" || arg == "-c" ) )
425-
{
426-
configpath = argv[++i];
427-
}
428-
else if ( i + 1 < argc && ( arg == "--code" || arg == "-f" ) )
429-
{
430-
pythonfile = argv[++i];
431-
}
432-
else if ( i + 1 < argc && ( arg == "--customizationfile" || arg == "-z" ) )
433-
{
434-
customizationfile = argv[++i];
435-
}
436-
else
437-
{
438-
myFileList.append( QDir::convertSeparators( QFileInfo( QFile::decodeName( argv[i] ) ).absoluteFilePath() ) );
439-
}
376+
QStringList args;
377+
{
378+
// Build a local QCoreApplication from arguments. This way, arguments are correctly parsed from their native locale
379+
// It will use QString::fromLocal8Bit( argv ) under Unix and GetCommandLine() under Windows.
380+
QCoreApplication coreApp( argc, argv );
381+
args = QCoreApplication::arguments();
440382
}
441-
#else
383+
442384
if ( !bundleclicked( argc, argv ) )
443385
{
444-
445-
////////////////////////////////////////////////////////////////
446-
// Use the GNU Getopts utility to parse cli arguments
447-
// Invokes ctor `GetOpt (int argc, char **argv, char *optstring);'
448-
///////////////////////////////////////////////////////////////
449-
int optionChar;
450-
while ( 1 )
386+
for ( int i = 1; i < args.size(); ++i )
451387
{
452-
static struct option long_options[] =
388+
QString arg = args[i];
389+
390+
if ( arg == "--help" || arg == "-?" )
453391
{
454-
/* These options set a flag. */
455-
{"help", no_argument, 0, '?'},
456-
{"nologo", no_argument, 0, 'n'},
457-
{"noplugins", no_argument, 0, 'P'},
458-
{"nocustomization", no_argument, 0, 'C'},
459-
/* These options don't set a flag.
460-
* We distinguish them by their indices. */
461-
{"snapshot", required_argument, 0, 's'},
462-
{"width", required_argument, 0, 'w'},
463-
{"height", required_argument, 0, 'h'},
464-
{"lang", required_argument, 0, 'l'},
465-
{"project", required_argument, 0, 'p'},
466-
{"extent", required_argument, 0, 'e'},
467-
{"optionspath", required_argument, 0, 'o'},
468-
{"configpath", required_argument, 0, 'c'},
469-
{"customizationfile", required_argument, 0, 'z'},
470-
{"code", required_argument, 0, 'f'},
471-
{"android", required_argument, 0, 'a'},
472-
{0, 0, 0, 0}
473-
};
474-
475-
/* getopt_long stores the option index here. */
476-
int option_index = 0;
477-
478-
optionChar = getopt_long( argc, argv, "swhlpeoc",
479-
long_options, &option_index );
480-
QgsDebugMsg( QString( "Qgis main Debug" ) + optionChar );
481-
/* Detect the end of the options. */
482-
if ( optionChar == -1 )
483-
break;
484-
485-
switch ( optionChar )
392+
usage( args[0].toStdString() );
393+
return 2;
394+
}
395+
else if ( arg == "--nologo" || arg == "-n" )
396+
{
397+
myHideSplash = true;
398+
}
399+
else if ( arg == "--noplugins" || arg == "-P" )
400+
{
401+
myRestorePlugins = false;
402+
}
403+
else if ( arg == "--nocustomization" || arg == "-C" )
404+
{
405+
myCustomization = false;
406+
}
407+
else if ( i + 1 < argc && ( arg == "--snapshot" || arg == "-s" ) )
408+
{
409+
mySnapshotFileName = QDir::convertSeparators( QFileInfo( args[++i] ).absoluteFilePath() );
410+
}
411+
else if ( i + 1 < argc && ( arg == "--width" || arg == "-w" ) )
412+
{
413+
mySnapshotWidth = QString( args[++i] ).toInt();
414+
}
415+
else if ( i + 1 < argc && ( arg == "--height" || arg == "-h" ) )
416+
{
417+
mySnapshotHeight = QString( args[++i] ).toInt();
418+
}
419+
else if ( i + 1 < argc && ( arg == "--lang" || arg == "-l" ) )
420+
{
421+
myTranslationCode = args[++i];
422+
}
423+
else if ( i + 1 < argc && ( arg == "--project" || arg == "-p" ) )
424+
{
425+
myProjectFileName = QDir::convertSeparators( QFileInfo( args[++i] ).absoluteFilePath() );
426+
}
427+
else if ( i + 1 < argc && ( arg == "--extent" || arg == "-e" ) )
428+
{
429+
myInitialExtent = args[++i];
430+
}
431+
else if ( i + 1 < argc && ( arg == "--optionspath" || arg == "-o" ) )
432+
{
433+
optionpath = QDir::convertSeparators( QDir( args[++i] ).absolutePath() );
434+
}
435+
else if ( i + 1 < argc && ( arg == "--configpath" || arg == "-c" ) )
436+
{
437+
configpath = QDir::convertSeparators( QDir( args[++i] ).absolutePath() );
438+
}
439+
else if ( i + 1 < argc && ( arg == "--code" || arg == "-f" ) )
440+
{
441+
pythonfile = QDir::convertSeparators( QFileInfo( args[++i] ).absoluteFilePath() );
442+
}
443+
else if ( i + 1 < argc && ( arg == "--customizationfile" || arg == "-z" ) )
486444
{
487-
case 0:
488-
/* If this option set a flag, do nothing else now. */
489-
if ( long_options[option_index].flag != 0 )
490-
break;
491-
printf( "option %s", long_options[option_index].name );
492-
if ( optarg )
493-
printf( " with arg %s", optarg );
494-
printf( "\n" );
495-
break;
496-
497-
case 's':
498-
mySnapshotFileName = QDir::convertSeparators( QFileInfo( QFile::decodeName( optarg ) ).absoluteFilePath() );
499-
break;
500-
501-
case 'w':
502-
mySnapshotWidth = QString( optarg ).toInt();
503-
break;
504-
505-
case 'h':
506-
mySnapshotHeight = QString( optarg ).toInt();
507-
break;
508-
509-
case 'n':
510-
myHideSplash = true;
511-
break;
512-
513-
case 'l':
514-
myTranslationCode = optarg;
515-
break;
516-
517-
case 'p':
518-
myProjectFileName = QDir::convertSeparators( QFileInfo( QFile::decodeName( optarg ) ).absoluteFilePath() );
519-
break;
520-
521-
case 'P':
522-
myRestorePlugins = false;
523-
break;
524-
525-
case 'C':
526-
myCustomization = false;
527-
break;
528-
529-
case 'e':
530-
myInitialExtent = optarg;
531-
break;
532-
533-
case 'o':
534-
optionpath = optarg;
535-
break;
536-
537-
case 'c':
538-
configpath = optarg;
539-
break;
540-
541-
case 'f':
542-
pythonfile = optarg;
543-
break;
544-
545-
case 'z':
546-
customizationfile = optarg;
547-
break;
548-
549-
case '?':
550-
usage( argv[0] );
551-
return 2; // XXX need standard exit codes
552-
break;
553-
554-
default:
555-
QgsDebugMsg( QString( "%1: getopt returned character code %2" ).arg( argv[0] ).arg( optionChar ) );
556-
return 1; // XXX need standard exit codes
445+
customizationfile = QDir::convertSeparators( QFileInfo( args[++i] ).absoluteFilePath() );
446+
}
447+
else
448+
{
449+
myFileList.append( QDir::convertSeparators( QFileInfo( args[i] ).absoluteFilePath() ) );
557450
}
558451
}
452+
}
559453

560-
// Add any remaining args to the file list - we will attempt to load them
561-
// as layers in the map view further down....
562-
QgsDebugMsg( QString( "Files specified on command line: %1" ).arg( optind ) );
563-
if ( optind < argc )
454+
/////////////////////////////////////////////////////////////////////
455+
// If no --project was specified, parse the args to look for a //
456+
// .qgs file and set myProjectFileName to it. This allows loading //
457+
// of a project file by clicking on it in various desktop managers //
458+
// where an appropriate mime-type has been set up. //
459+
/////////////////////////////////////////////////////////////////////
460+
if ( myProjectFileName.isEmpty() )
461+
{
462+
// check for a .qgs
463+
for ( int i = 0; i < args.size(); i++ )
564464
{
565-
while ( optind < argc )
465+
QString arg = QDir::convertSeparators( QFileInfo( args[i] ).absoluteFilePath() );
466+
if ( arg.contains( ".qgs" ) )
566467
{
567-
#ifdef QGISDEBUG
568-
int idx = optind;
569-
QgsDebugMsg( QString( "%1: %2" ).arg( idx ).arg( argv[idx] ) );
570-
#endif
571-
myFileList.append( QDir::convertSeparators( QFileInfo( QFile::decodeName( argv[optind++] ) ).absoluteFilePath() ) );
468+
myProjectFileName = arg;
469+
break;
572470
}
573471
}
574472
}
575-
#endif
576473

577474

578475
/////////////////////////////////////////////////////////////////////
@@ -875,26 +772,6 @@ int main( int argc, char *argv[] )
875772
QgsCustomization::instance(), SLOT( preNotify( QObject *, QEvent *, bool * ) )
876773
);
877774

878-
/////////////////////////////////////////////////////////////////////
879-
// If no --project was specified, parse the args to look for a //
880-
// .qgs file and set myProjectFileName to it. This allows loading //
881-
// of a project file by clicking on it in various desktop managers //
882-
// where an appropriate mime-type has been set up. //
883-
/////////////////////////////////////////////////////////////////////
884-
if ( myProjectFileName.isEmpty() )
885-
{
886-
// check for a .qgs
887-
for ( int i = 0; i < argc; i++ )
888-
{
889-
QString arg = QDir::convertSeparators( QFileInfo( QFile::decodeName( argv[i] ) ).absoluteFilePath() );
890-
if ( arg.contains( ".qgs" ) )
891-
{
892-
myProjectFileName = arg;
893-
break;
894-
}
895-
}
896-
}
897-
898775
/////////////////////////////////////////////////////////////////////
899776
// Load a project file if one was specified
900777
/////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)
Please sign in to comment.