From 0a5827fd204739b4b37c85fb4f1c1d5751aec173 Mon Sep 17 00:00:00 2001 From: shenleban tongying Date: Tue, 3 Sep 2024 12:54:50 -0400 Subject: [PATCH] opt: remove `parsecmdline.cc` by using `QProcess::splitCommand` --- src/dict/programs.cc | 3 +-- src/externalviewer.cc | 13 ++++++------- src/parsecmdline.cc | 42 ------------------------------------------ src/parsecmdline.hh | 11 ----------- 4 files changed, 7 insertions(+), 62 deletions(-) delete mode 100644 src/parsecmdline.cc delete mode 100644 src/parsecmdline.hh diff --git a/src/dict/programs.cc b/src/dict/programs.cc index 1591135a..eebd903a 100644 --- a/src/dict/programs.cc +++ b/src/dict/programs.cc @@ -6,7 +6,6 @@ #include "htmlescape.hh" #include "utf8.hh" #include "wstring_qt.hh" -#include "parsecmdline.hh" #include "iconv.hh" #include "utils.hh" #include "globalbroadcaster.hh" @@ -142,7 +141,7 @@ RunInstance::RunInstance(): bool RunInstance::start( Config::Program const & prg, QString const & word, QString & error ) { - QStringList args = parseCommandLine( prg.commandLine ); + QStringList args = QProcess::splitCommand( prg.commandLine ); if ( !args.empty() ) { QString programName = args.first(); diff --git a/src/externalviewer.cc b/src/externalviewer.cc index e623ac4b..eb85934e 100644 --- a/src/externalviewer.cc +++ b/src/externalviewer.cc @@ -4,7 +4,6 @@ #include #include #include "externalviewer.hh" -#include "parsecmdline.hh" #include "gddebug.hh" ExternalViewer::ExternalViewer( @@ -34,18 +33,18 @@ void ExternalViewer::start() connect( &viewer, &QProcess::finished, this, &QObject::deleteLater ); connect( &viewer, &QProcess::errorOccurred, this, &QObject::deleteLater ); - QStringList args = parseCommandLine( viewerCmdLine ); - + QStringList args = QProcess::splitCommand( viewerCmdLine ); if ( !args.isEmpty() ) { - QString program = args.first(); - args.pop_front(); + const QString program = args.takeFirst(); args.push_back( tempFileName ); viewer.start( program, args, QIODevice::NotOpen ); - if ( !viewer.waitForStarted() ) + if ( !viewer.waitForStarted() ) { throw exCantRunViewer( viewerCmdLine.toUtf8().data() ); + } } - else + else { throw exCantRunViewer( tr( "the viewer program name is empty" ).toUtf8().data() ); + } } bool ExternalViewer::stop() diff --git a/src/parsecmdline.cc b/src/parsecmdline.cc deleted file mode 100644 index b2d7b064..00000000 --- a/src/parsecmdline.cc +++ /dev/null @@ -1,42 +0,0 @@ -#include "parsecmdline.hh" - -QStringList parseCommandLine( QString const & commandLine ) -{ - // Parse arguments. Handle quotes correctly. - QStringList args; - bool openQuote = false; - bool possibleDoubleQuote = false; - bool startNew = true; - for ( QString::const_iterator c = commandLine.begin(), e = commandLine.end(); c != e; ) { - if ( *c == '"' && !possibleDoubleQuote ) { - ++c; - if ( !openQuote ) { - openQuote = true; - if ( startNew ) { - args.push_back( QString() ); - startNew = false; - } - } - else - possibleDoubleQuote = true; - } - else if ( possibleDoubleQuote && *c != '"' ) { - openQuote = false; - possibleDoubleQuote = false; - } - else if ( *c == ' ' && !openQuote ) { - ++c; - startNew = true; - } - else { - if ( startNew ) { - args.push_back( QString() ); - startNew = false; - } - args.last().push_back( *c++ ); - possibleDoubleQuote = false; - } - } - - return args; -} diff --git a/src/parsecmdline.hh b/src/parsecmdline.hh deleted file mode 100644 index eda250fb..00000000 --- a/src/parsecmdline.hh +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef PARSECMDLINE_HH -#define PARSECMDLINE_HH - -#include - -/// Given a command line (name of the executable with optional arguments), -/// separates-out the name and all the arguments into a list. Supports quotes -/// and double-quotes. -QStringList parseCommandLine( QString const & ); - -#endif // PARSECMDLINE_HH