opt: remove parsecmdline.cc by using QProcess::splitCommand

This commit is contained in:
shenleban tongying 2024-09-03 12:54:50 -04:00
parent ea4f45ad55
commit 0a5827fd20
4 changed files with 7 additions and 62 deletions

View file

@ -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();

View file

@ -4,7 +4,6 @@
#include <QDir>
#include <QTimer>
#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()

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