mirror of https://github.com/vitalif/phantomjs
Simplify Env code by reusing QProcessEnvironment.
This gets rid of the custom parse code and slims down the API. https://github.com/ariya/phantomjs/issues/124242.0
parent
3136898976
commit
4bf75c50b0
30
src/env.cpp
30
src/env.cpp
|
@ -32,6 +32,7 @@
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QVariantMap>
|
#include <QVariantMap>
|
||||||
|
#include <QProcessEnvironment>
|
||||||
|
|
||||||
static Env *env_instance = NULL;
|
static Env *env_instance = NULL;
|
||||||
|
|
||||||
|
@ -46,35 +47,14 @@ Env *Env::instance()
|
||||||
Env::Env()
|
Env::Env()
|
||||||
: QObject(QCoreApplication::instance())
|
: QObject(QCoreApplication::instance())
|
||||||
{
|
{
|
||||||
|
const QProcessEnvironment &env = QProcessEnvironment::systemEnvironment();
|
||||||
|
foreach(const QString &key, env.keys()) {
|
||||||
|
m_map[key] = env.value(key);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// public:
|
// public:
|
||||||
|
|
||||||
void Env::parse(const char **envp)
|
|
||||||
{
|
|
||||||
const char **env = (const char **)NULL;
|
|
||||||
QString envvar, name, value;
|
|
||||||
int indexOfEquals;
|
|
||||||
// Loop for each of the <NAME>=<VALUE> pairs and split them into a map
|
|
||||||
for (env = envp; *env != (const char *)NULL; env++) {
|
|
||||||
envvar = QString(*env);
|
|
||||||
indexOfEquals = envvar.indexOf('=');
|
|
||||||
if (0 >= indexOfEquals) {
|
|
||||||
// Should never happen because names cannot contain "=" and cannot
|
|
||||||
// be empty. If it does happen, then just ignore this record.
|
|
||||||
// See: http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// Extract name and value (if it exists) from envvar
|
|
||||||
// NOTE:
|
|
||||||
// QString::mid() will gracefully return an empty QString when the
|
|
||||||
// specified position index is >= the length() of the string
|
|
||||||
name = envvar.left(indexOfEquals);
|
|
||||||
value = envvar.mid(indexOfEquals + 1);
|
|
||||||
m_map.insert(name, value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
QVariantMap Env::asVariantMap() const
|
QVariantMap Env::asVariantMap() const
|
||||||
{
|
{
|
||||||
return m_map;
|
return m_map;
|
||||||
|
|
|
@ -40,7 +40,6 @@ class Env : public QObject
|
||||||
public:
|
public:
|
||||||
static Env *instance();
|
static Env *instance();
|
||||||
|
|
||||||
void parse(const char ** envp);
|
|
||||||
QVariantMap asVariantMap() const;
|
QVariantMap asVariantMap() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -54,7 +54,7 @@ static google_breakpad::ExceptionHandler* eh;
|
||||||
#error Something is wrong with the setup. Please report to the mailing list!
|
#error Something is wrong with the setup. Please report to the mailing list!
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int main(int argc, char** argv, const char** envp)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
// Setup Google Breakpad exception handler
|
// Setup Google Breakpad exception handler
|
||||||
#ifdef Q_OS_LINUX
|
#ifdef Q_OS_LINUX
|
||||||
|
@ -91,9 +91,6 @@ int main(int argc, char** argv, const char** envp)
|
||||||
app.setOrganizationDomain("www.ofilabs.com");
|
app.setOrganizationDomain("www.ofilabs.com");
|
||||||
app.setApplicationVersion(PHANTOMJS_VERSION_STRING);
|
app.setApplicationVersion(PHANTOMJS_VERSION_STRING);
|
||||||
|
|
||||||
// Prepare the "env" singleton using the environment variables
|
|
||||||
Env::instance()->parse(envp);
|
|
||||||
|
|
||||||
// Registering an alternative Message Handler
|
// Registering an alternative Message Handler
|
||||||
qInstallMessageHandler(Utils::messageHandler);
|
qInstallMessageHandler(Utils::messageHandler);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue