QtcProcess: Introduce a QtcProcess::Arguments class

This is used to get a platform-agnostic handle on "command line
arguments". It essentially wraps a single QString on Windows,
and a QStringList everywhere else.

As a consequence, several occurrences of #ifdef Q_OS_*
can be removed from the codebase.

Change-Id: Ic93118c1bd0bce0ebb58f416d395dbaebb861772
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@digia.com>
This commit is contained in:
hjk
2014-02-05 10:43:21 +01:00
committed by Eike Ziller
parent a0d29eeb03
commit c68ebeed2e
14 changed files with 1257 additions and 1196 deletions

View File

@@ -56,6 +56,22 @@ public:
void terminate();
void interrupt();
class QTCREATOR_UTILS_EXPORT Arguments
{
public:
static Arguments createWindowsArgs(const QString &args);
static Arguments createUnixArgs(const QStringList &args);
QString toWindowsArgs() const;
QStringList toUnixArgs() const;
QString toString() const;
private:
QString m_windowsArgs;
QStringList m_unixArgs;
bool m_isWindows;
};
enum SplitError {
SplitOk = 0, //! All went just fine
BadQuoting, //! Command contains quoting errors
@@ -64,45 +80,33 @@ public:
//! Quote a single argument for usage in a unix shell command
static QString quoteArgUnix(const QString &arg);
//! Quote a single argument and append it to a unix shell command
static void addArgUnix(QString *args, const QString &arg);
//! Join an argument list into a unix shell command
static QString joinArgsUnix(const QStringList &args);
#ifdef Q_OS_WIN
//! Quote a single argument for usage in a shell command
static QString quoteArg(const QString &arg);
static QString quoteArg(const QString &arg, OsType osType = HostOsInfo::hostOs());
//! Quote a single argument and append it to a shell command
static void addArg(QString *args, const QString &arg);
static void addArg(QString *args, const QString &arg, OsType osType = HostOsInfo::hostOs());
//! Join an argument list into a shell command
static QString joinArgs(const QStringList &args);
static QString joinArgs(const QStringList &args, OsType osType = HostOsInfo::hostOs());
//! Prepare argument of a shell command for feeding into QProcess
static QString prepareArgs(const QString &cmd, SplitError *err,
const Environment *env = 0, const QString *pwd = 0);
static Arguments prepareArgs(const QString &cmd, SplitError *err,
OsType osType = HostOsInfo::hostOs(),
const Environment *env = 0, const QString *pwd = 0);
//! Prepare a shell command for feeding into QProcess
static void prepareCommand(const QString &command, const QString &arguments,
QString *outCmd, QString *outArgs,
const Environment *env = 0, const QString *pwd = 0);
#else
static QString quoteArg(const QString &arg) { return quoteArgUnix(arg); }
static void addArg(QString *args, const QString &arg) { addArgUnix(args, arg); }
static QString joinArgs(const QStringList &args) { return joinArgsUnix(args); }
static QStringList prepareArgs(const QString &cmd, SplitError *err,
const Environment *env = 0, const QString *pwd = 0)
{ return splitArgs(cmd, true, err, env, pwd); }
static bool prepareCommand(const QString &command, const QString &arguments,
QString *outCmd, QStringList *outArgs,
QString *outCmd, Arguments *outArgs, OsType osType = HostOsInfo::hostOs(),
const Environment *env = 0, const QString *pwd = 0);
#endif
//! Quote and append each argument to a shell command
static void addArgs(QString *args, const QStringList &inArgs);
//! Append already quoted arguments to a shell command
static void addArgs(QString *args, const QString &inArgs);
//! Split a shell command into separate arguments. ArgIterator is usually a better choice.
static QStringList splitArgs(const QString &cmd, bool abortOnMeta = false, SplitError *err = 0,
static QStringList splitArgs(const QString &cmd, OsType osType = HostOsInfo::hostOs(),
bool abortOnMeta = false, SplitError *err = 0,
const Environment *env = 0, const QString *pwd = 0);
//! Safely replace the expandos in a shell command
static bool expandMacros(QString *cmd, AbstractMacroExpander *mx);
static QString expandMacros(const QString &str, AbstractMacroExpander *mx);
static bool expandMacros(QString *cmd, AbstractMacroExpander *mx,
OsType osType = HostOsInfo::hostOs());
static QString expandMacros(const QString &str, AbstractMacroExpander *mx,
OsType osType = HostOsInfo::hostOs());
/*! Iterate over arguments from a command line.
* Assumes that the name of the actual command is *not* part of the line.
@@ -110,7 +114,9 @@ public:
*/
class QTCREATOR_UTILS_EXPORT ArgIterator {
public:
ArgIterator(QString *str) : m_str(str), m_pos(0), m_prev(-1) {}
ArgIterator(QString *str, OsType osType = HostOsInfo::hostOs())
: m_str(str), m_pos(0), m_prev(-1), m_osType(osType)
{}
//! Get the next argument. Returns false on encountering end of first command.
bool next();
//! True iff the argument is a plain string, possibly after unquoting.
@@ -126,11 +132,14 @@ public:
QString *m_str, m_value;
int m_pos, m_prev;
bool m_simple;
OsType m_osType;
};
class QTCREATOR_UTILS_EXPORT ConstArgIterator {
public:
ConstArgIterator(const QString &str) : m_str(str), m_ait(&m_str) {}
ConstArgIterator(const QString &str, OsType osType = HostOsInfo::hostOs())
: m_str(str), m_ait(&m_str, osType)
{}
bool next() { return m_ait.next(); }
bool isSimple() const { return m_ait.isSimple(); }
QString value() const { return m_ait.value(); }