Skip to content
Permalink
Browse files

Port QProcess::splitCommand from qt 5.15

This is a useful method, and desirable in Qt < 5.15. It's also not
exposed currently to Python, so this doubles as a way for plugins
to use this method.
  • Loading branch information
nyalldawson committed Jan 6, 2021
1 parent 8e5b036 commit d1761bdb15ae8e45d6e20f5e81aa1476cd75540c
Showing with 80 additions and 0 deletions.
  1. +11 −0 python/core/auto_generated/qgsrunprocess.sip.in
  2. +58 −0 src/core/qgsrunprocess.cpp
  3. +11 −0 src/core/qgsrunprocess.h
@@ -29,6 +29,17 @@ https://lists.qt-project.org/pipermail/development/2015-July/022205.html

static QgsRunProcess *create( const QString &action, bool capture ) /Factory/;

static QStringList splitCommand( const QString &command );
%Docstring
Splits the string ``command`` into a list of tokens, and returns
the list.

Tokens with spaces can be surrounded by double quotes; three
consecutive double quotes represent the quote character itself.

.. versionadded:: 3.18
%End

private:
QgsRunProcess( const QString &action, bool capture );
~QgsRunProcess();
@@ -178,6 +178,59 @@ void QgsRunProcess::processError( QProcess::ProcessError err )
QgsDebugMsg( "Got error: " + QString( "%d" ).arg( err ) );
}
}

QStringList QgsRunProcess::splitCommand( const QString &command )
{
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
return QProcess::splitCommand( command );
#else
// taken from Qt 5.15's implementation
QStringList args;
QString tmp;
int quoteCount = 0;
bool inQuote = false;

// handle quoting. tokens can be surrounded by double quotes
// "hello world". three consecutive double quotes represent
// the quote character itself.
for ( int i = 0; i < command.size(); ++i )
{
if ( command.at( i ) == QLatin1Char( '"' ) )
{
++quoteCount;
if ( quoteCount == 3 )
{
// third consecutive quote
quoteCount = 0;
tmp += command.at( i );
}
continue;
}
if ( quoteCount )
{
if ( quoteCount == 1 )
inQuote = !inQuote;
quoteCount = 0;
}
if ( !inQuote && command.at( i ).isSpace() )
{
if ( !tmp.isEmpty() )
{
args += tmp;
tmp.clear();
}
}
else
{
tmp += command.at( i );
}
}
if ( !tmp.isEmpty() )
args += tmp;

return args;
#endif
}
#else
QgsRunProcess::QgsRunProcess( const QString &action, bool )
{
@@ -188,4 +241,9 @@ QgsRunProcess::QgsRunProcess( const QString &action, bool )
QgsRunProcess::~QgsRunProcess()
{
}

QStringList QgsRunProcess::splitCommand( const QString & )
{
return QStringList();
}
#endif
@@ -53,6 +53,17 @@ class CORE_EXPORT QgsRunProcess: public QObject SIP_NODEFAULTCTORS
static QgsRunProcess *create( const QString &action, bool capture ) SIP_FACTORY
{ return new QgsRunProcess( action, capture ); }

/**
* Splits the string \a command into a list of tokens, and returns
* the list.
*
* Tokens with spaces can be surrounded by double quotes; three
* consecutive double quotes represent the quote character itself.
*
* \since QGIS 3.18
*/
static QStringList splitCommand( const QString &command );

private:
QgsRunProcess( const QString &action, bool capture ) SIP_FORCE;
~QgsRunProcess() override SIP_FORCE;

0 comments on commit d1761bd

Please sign in to comment.
You can’t perform that action at this time.