Merge pull request #1742 from shenlebantongying/opt/del-parsecmd

opt: remove `parsecmdline.cc` by using `QProcess::splitCommand`
This commit is contained in:
xiaoyifang 2024-09-04 08:30:37 +08:00 committed by GitHub
commit e5c25cd7b3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 7 additions and 62 deletions

View file

@ -6,7 +6,6 @@
#include "htmlescape.hh" #include "htmlescape.hh"
#include "utf8.hh" #include "utf8.hh"
#include "wstring_qt.hh" #include "wstring_qt.hh"
#include "parsecmdline.hh"
#include "iconv.hh" #include "iconv.hh"
#include "utils.hh" #include "utils.hh"
#include "globalbroadcaster.hh" #include "globalbroadcaster.hh"
@ -142,7 +141,7 @@ RunInstance::RunInstance():
bool RunInstance::start( Config::Program const & prg, QString const & word, QString & error ) 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() ) { if ( !args.empty() ) {
QString programName = args.first(); QString programName = args.first();

View file

@ -4,7 +4,6 @@
#include <QDir> #include <QDir>
#include <QTimer> #include <QTimer>
#include "externalviewer.hh" #include "externalviewer.hh"
#include "parsecmdline.hh"
#include "gddebug.hh" #include "gddebug.hh"
ExternalViewer::ExternalViewer( ExternalViewer::ExternalViewer(
@ -34,18 +33,18 @@ void ExternalViewer::start()
connect( &viewer, &QProcess::finished, this, &QObject::deleteLater ); connect( &viewer, &QProcess::finished, this, &QObject::deleteLater );
connect( &viewer, &QProcess::errorOccurred, this, &QObject::deleteLater ); connect( &viewer, &QProcess::errorOccurred, this, &QObject::deleteLater );
QStringList args = parseCommandLine( viewerCmdLine ); QStringList args = QProcess::splitCommand( viewerCmdLine );
if ( !args.isEmpty() ) { if ( !args.isEmpty() ) {
QString program = args.first(); const QString program = args.takeFirst();
args.pop_front();
args.push_back( tempFileName ); args.push_back( tempFileName );
viewer.start( program, args, QIODevice::NotOpen ); viewer.start( program, args, QIODevice::NotOpen );
if ( !viewer.waitForStarted() ) if ( !viewer.waitForStarted() ) {
throw exCantRunViewer( viewerCmdLine.toUtf8().data() ); throw exCantRunViewer( viewerCmdLine.toUtf8().data() );
} }
else }
else {
throw exCantRunViewer( tr( "the viewer program name is empty" ).toUtf8().data() ); throw exCantRunViewer( tr( "the viewer program name is empty" ).toUtf8().data() );
}
} }
bool ExternalViewer::stop() bool ExternalViewer::stop()

View file

@ -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;
}

View file

@ -1,11 +0,0 @@
#ifndef PARSECMDLINE_HH
#define PARSECMDLINE_HH
#include <QStringList>
/// 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