OSDN Git Service

Added an optional parameter to init_process() function, which allows for passing...
authorLoRd_MuldeR <mulder2@gmx.de>
Mon, 15 Oct 2018 21:21:56 +0000 (23:21 +0200)
committerLoRd_MuldeR <mulder2@gmx.de>
Mon, 15 Oct 2018 21:21:56 +0000 (23:21 +0200)
include/MUtils/Global.h
src/Global.cpp

index 0e9aff3..27b709d 100644 (file)
@@ -31,6 +31,7 @@
 //Forward Declarations
 class QProcess;
 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.
        */
-       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.
index 141229d..9177c35 100644 (file)
@@ -38,6 +38,7 @@
 #include <QProcess>
 #include <QTextCodec>
 #include <QPair>
+#include <QHash>
 #include <QListIterator>
 #include <QMutex>
 #include <QThreadStorage>
@@ -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<QString, QString> *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<QString, QString>::ConstIterator iter = extraEnv->constBegin(); iter != extraEnv->constEnd(); iter++)
+               {
+                       env.insert(iter.key(), iter.value());
+               }
+       }
+
        //Setup QPorcess object
        process.setWorkingDirectory(wokringDir);
        process.setProcessChannelMode(QProcess::MergedChannels);