From e0d779a3106157a9bc83aa97e9d9fb1289dbdcbe Mon Sep 17 00:00:00 2001 From: Tobias Hunger Date: Tue, 15 Feb 2011 15:32:20 +0100 Subject: [PATCH] Env: Add method to get the difference between two Envs Add a method returning the difference between two Environments. Reviewed-by: dt --- src/libs/utils/environment.cpp | 32 ++++++++++++++++++++++++++++++++ src/libs/utils/environment.h | 2 ++ 2 files changed, 34 insertions(+) diff --git a/src/libs/utils/environment.cpp b/src/libs/utils/environment.cpp index 02827296a52..dffe886fcdd 100644 --- a/src/libs/utils/environment.cpp +++ b/src/libs/utils/environment.cpp @@ -321,6 +321,38 @@ void Environment::modify(const QList & list) *this = resultEnvironment; } +QList Environment::diff(const Environment &other) const +{ + QMap::const_iterator thisIt = constBegin(); + QMap::const_iterator otherIt = other.constBegin(); + + QList result; + while (thisIt != constEnd() || otherIt != other.constEnd()) { + if (thisIt == constEnd()) { + result.append(Utils::EnvironmentItem(otherIt.key(), otherIt.value())); + ++otherIt; + } else if (otherIt == constEnd()) { + Utils::EnvironmentItem item(thisIt.key(), QString()); + item.unset = true; + result.append(item); + ++thisIt; + } else if (thisIt.key() < otherIt.key()) { + Utils::EnvironmentItem item(thisIt.key(), QString()); + item.unset = true; + result.append(item); + ++thisIt; + } else if (thisIt.key() > otherIt.key()) { + result.append(Utils::EnvironmentItem(otherIt.key(), otherIt.value())); + ++otherIt; + } else { + result.append(Utils::EnvironmentItem(otherIt.key(), otherIt.value())); + ++otherIt; + ++thisIt; + } + } + return result; +} + bool Environment::hasKey(const QString &key) { return m_values.contains(key); diff --git a/src/libs/utils/environment.h b/src/libs/utils/environment.h index 0144d76993c..e946990c4ac 100644 --- a/src/libs/utils/environment.h +++ b/src/libs/utils/environment.h @@ -76,6 +76,8 @@ public: void set(const QString &key, const QString &value); void unset(const QString &key); void modify(const QList & list); + /// Return the Environment changes necessary to modify this into the other environment. + QList diff(const Environment &other) const; bool hasKey(const QString &key); void appendOrSet(const QString &key, const QString &value, const QString &sep = QString());