Added an optional parameter to init_process() function, which allows for passing custom environment variables.

This commit is contained in:
LoRd_MuldeR 2018-10-15 23:21:56 +02:00
parent 41d6398c45
commit ce24bd2d05
2 changed files with 14 additions and 3 deletions

View File

@ -31,6 +31,7 @@
//Forward Declarations //Forward Declarations
class QProcess; class QProcess;
class QDir; class QDir;
template<typename K, typename V> 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. * \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<QString, QString> *const extraEnv = NULL);
/** /**
* \brief Generates a *random* unsigned 32-Bit value. * \brief Generates a *random* unsigned 32-Bit value.

View File

@ -38,6 +38,7 @@
#include <QProcess> #include <QProcess>
#include <QTextCodec> #include <QTextCodec>
#include <QPair> #include <QPair>
#include <QHash>
#include <QListIterator> #include <QListIterator>
#include <QMutex> #include <QMutex>
#include <QThreadStorage> #include <QThreadStorage>
@ -514,12 +515,12 @@ bool MUtils::remove_directory(const QString &folderPath, const bool &recursive)
static void prependToPath(QProcessEnvironment &env, const QString &value) 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(); const QString path = env.value(PATH, QString()).trimmed();
env.insert(PATH, path.isEmpty() ? value : QString("%1;%2").arg(value, path)); 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<QString, QString> *const extraEnv)
{ {
//Environment variable names //Environment variable names
static const char *const s_envvar_names_temp[] = 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<QString, QString>::ConstIterator iter = extraEnv->constBegin(); iter != extraEnv->constEnd(); iter++)
{
env.insert(iter.key(), iter.value());
}
}
//Setup QPorcess object //Setup QPorcess object
process.setWorkingDirectory(wokringDir); process.setWorkingDirectory(wokringDir);
process.setProcessChannelMode(QProcess::MergedChannels); process.setProcessChannelMode(QProcess::MergedChannels);