From ce24bd2d05d312bde5825d5580d61383bf80c86f Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Mon, 15 Oct 2018 23:21:56 +0200 Subject: [PATCH] Added an optional parameter to init_process() function, which allows for passing custom environment variables. --- include/MUtils/Global.h | 3 ++- src/Global.cpp | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/include/MUtils/Global.h b/include/MUtils/Global.h index 0e9aff3..27b709d 100644 --- a/include/MUtils/Global.h +++ b/include/MUtils/Global.h @@ -31,6 +31,7 @@ //Forward Declarations class QProcess; class QDir; +template class QHash; /////////////////////////////////////////////////////////////////////////////// @@ -113,7 +114,7 @@ namespace MUtils * * \param extraPaths A read-only pointer to a QStringList object containing additional paths that will be added (prepended) to the sub-process' `PATH` environment variable. This parameter can be `NULL`, in which case *no* additional paths are added. */ - MUTILS_API void init_process(QProcess &process, const QString &wokringDir, const bool bReplaceTempDir = true, const QStringList *const extraPaths = NULL); + MUTILS_API void init_process(QProcess &process, const QString &wokringDir, const bool bReplaceTempDir = true, const QStringList *const extraPaths = NULL, const QHash *const extraEnv = NULL); /** * \brief Generates a *random* unsigned 32-Bit value. diff --git a/src/Global.cpp b/src/Global.cpp index 141229d..9177c35 100644 --- a/src/Global.cpp +++ b/src/Global.cpp @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -514,12 +515,12 @@ bool MUtils::remove_directory(const QString &folderPath, const bool &recursive) static void prependToPath(QProcessEnvironment &env, const QString &value) { - const QLatin1String PATH = QLatin1String("PATH"); + static const QLatin1String PATH("PATH"); const QString path = env.value(PATH, QString()).trimmed(); env.insert(PATH, path.isEmpty() ? value : QString("%1;%2").arg(value, path)); } -void MUtils::init_process(QProcess &process, const QString &wokringDir, const bool bReplaceTempDir, const QStringList *const extraPaths) +void MUtils::init_process(QProcess &process, const QString &wokringDir, const bool bReplaceTempDir, const QStringList *const extraPaths, const QHash *const extraEnv) { //Environment variable names static const char *const s_envvar_names_temp[] = @@ -565,6 +566,15 @@ void MUtils::init_process(QProcess &process, const QString &wokringDir, const bo } } + //Setup environment + if (extraEnv && (!extraEnv->isEmpty())) + { + for (QHash::ConstIterator iter = extraEnv->constBegin(); iter != extraEnv->constEnd(); iter++) + { + env.insert(iter.key(), iter.value()); + } + } + //Setup QPorcess object process.setWorkingDirectory(wokringDir); process.setProcessChannelMode(QProcess::MergedChannels); -- 2.11.0