Implemented the init_process() function.

This commit is contained in:
LoRd_MuldeR 2014-11-21 19:46:45 +01:00
parent c10ede3ada
commit 92e36b7e6b
2 changed files with 60 additions and 4 deletions

View File

@ -23,18 +23,24 @@
#include <QString>
//Forward Declarations
class QProcess;
///////////////////////////////////////////////////////////////////////////////
namespace MUtils
{
//Random
//Temp Folder
const QString &temp_folder(void);
//Process Utils
void init_process(QProcess &process, const QString &wokringDir, const bool bReplaceTempDir = true);
//Random
void seed_rand(void);
QString rand_str(const bool &bLong = false);
quint32 next_rand32(void);
quint64 next_rand64(void);
//Temp Folder
const QString &temp_folder(void);
}
///////////////////////////////////////////////////////////////////////////////

View File

@ -29,6 +29,7 @@
//Qt
#include <QDir>
#include <QReadWriteLock>
#include <QProcess>
//CRT
#include <cstdlib>
@ -229,3 +230,52 @@ const QString &MUtils::temp_folder(void)
return (*g_temp_folder_path);
}
///////////////////////////////////////////////////////////////////////////////
// PROCESS UTILS
///////////////////////////////////////////////////////////////////////////////
void MUtils::init_process(QProcess &process, const QString &wokringDir, const bool bReplaceTempDir)
{
//Environment variable names
static const char *const s_envvar_names_temp[] =
{
"TEMP", "TMP", "TMPDIR", "HOME", "USERPROFILE", "HOMEPATH", NULL
};
static const char *const s_envvar_names_remove[] =
{
"WGETRC", "SYSTEM_WGETRC", "HTTP_PROXY", "FTP_PROXY", "NO_PROXY", "GNUPGHOME", "LC_ALL", "LC_COLLATE", "LC_CTYPE", "LC_MESSAGES", "LC_MONETARY", "LC_NUMERIC", "LC_TIME", "LANG", NULL
};
//Initialize environment
QProcessEnvironment env = process.processEnvironment();
if(env.isEmpty()) env = QProcessEnvironment::systemEnvironment();
//Clean a number of enviroment variables that might affect our tools
for(size_t i = 0; s_envvar_names_remove[i]; i++)
{
env.remove(QString::fromLatin1(s_envvar_names_remove[i]));
env.remove(QString::fromLatin1(s_envvar_names_remove[i]).toLower());
}
const QString tempDir = QDir::toNativeSeparators(temp_folder());
//Replace TEMP directory in environment
if(bReplaceTempDir)
{
for(size_t i = 0; s_envvar_names_temp[i]; i++)
{
env.insert(s_envvar_names_temp[i], tempDir);
}
}
//Setup PATH variable
const QString path = env.value("PATH", QString()).trimmed();
env.insert("PATH", path.isEmpty() ? tempDir : QString("%1;%2").arg(tempDir, path));
//Setup QPorcess object
process.setWorkingDirectory(wokringDir);
process.setProcessChannelMode(QProcess::MergedChannels);
process.setReadChannel(QProcess::StandardOutput);
process.setProcessEnvironment(env);
}