From 4bf75c50b0342bc6f4f061a44c15ee44811bde28 Mon Sep 17 00:00:00 2001 From: Milian Wolff Date: Tue, 13 May 2014 17:11:53 +0200 Subject: [PATCH] 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/12424 --- src/env.cpp | 30 +++++------------------------- src/env.h | 1 - src/main.cpp | 5 +---- 3 files changed, 6 insertions(+), 30 deletions(-) diff --git a/src/env.cpp b/src/env.cpp index 9a5bb821..83299e15 100644 --- a/src/env.cpp +++ b/src/env.cpp @@ -32,6 +32,7 @@ #include #include #include +#include static Env *env_instance = NULL; @@ -46,35 +47,14 @@ Env *Env::instance() Env::Env() : QObject(QCoreApplication::instance()) { + const QProcessEnvironment &env = QProcessEnvironment::systemEnvironment(); + foreach(const QString &key, env.keys()) { + m_map[key] = env.value(key); + } } // public: -void Env::parse(const char **envp) -{ - const char **env = (const char **)NULL; - QString envvar, name, value; - int indexOfEquals; - // Loop for each of the = 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 { return m_map; diff --git a/src/env.h b/src/env.h index 6d72b39b..3b9a522e 100644 --- a/src/env.h +++ b/src/env.h @@ -40,7 +40,6 @@ class Env : public QObject public: static Env *instance(); - void parse(const char ** envp); QVariantMap asVariantMap() const; private: diff --git a/src/main.cpp b/src/main.cpp index bb044657..aada2cc6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -54,7 +54,7 @@ static google_breakpad::ExceptionHandler* eh; #error Something is wrong with the setup. Please report to the mailing list! #endif -int main(int argc, char** argv, const char** envp) +int main(int argc, char** argv) { // Setup Google Breakpad exception handler #ifdef Q_OS_LINUX @@ -91,9 +91,6 @@ int main(int argc, char** argv, const char** envp) app.setOrganizationDomain("www.ofilabs.com"); app.setApplicationVersion(PHANTOMJS_VERSION_STRING); - // Prepare the "env" singleton using the environment variables - Env::instance()->parse(envp); - // Registering an alternative Message Handler qInstallMessageHandler(Utils::messageHandler);