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
2.0
Milian Wolff 2014-05-13 17:11:53 +02:00 committed by Ariya Hidayat
parent 3136898976
commit 4bf75c50b0
3 changed files with 6 additions and 30 deletions

View File

@ -32,6 +32,7 @@
#include <QCoreApplication>
#include <QString>
#include <QVariantMap>
#include <QProcessEnvironment>
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 <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
{
return m_map;

View File

@ -40,7 +40,6 @@ class Env : public QObject
public:
static Env *instance();
void parse(const char ** envp);
QVariantMap asVariantMap() const;
private:

View File

@ -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);