Commit e0d779a3 authored by Tobias Hunger's avatar Tobias Hunger

Env: Add method to get the difference between two Envs

Add a method returning the difference between two Environments.

Reviewed-by: dt
parent be84387f
......@@ -321,6 +321,38 @@ void Environment::modify(const QList<EnvironmentItem> & list)
*this = resultEnvironment;
}
QList<EnvironmentItem> Environment::diff(const Environment &other) const
{
QMap<QString, QString>::const_iterator thisIt = constBegin();
QMap<QString, QString>::const_iterator otherIt = other.constBegin();
QList<EnvironmentItem> 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);
......
......@@ -76,6 +76,8 @@ public:
void set(const QString &key, const QString &value);
void unset(const QString &key);
void modify(const QList<EnvironmentItem> & list);
/// Return the Environment changes necessary to modify this into the other environment.
QList<EnvironmentItem> diff(const Environment &other) const;
bool hasKey(const QString &key);
void appendOrSet(const QString &key, const QString &value, const QString &sep = QString());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment