Env: Add method to get the difference between two Envs

Add a method returning the difference between two Environments.

Reviewed-by: dt
This commit is contained in:
Tobias Hunger
2011-02-15 15:32:20 +01:00
parent be84387f80
commit e0d779a310
2 changed files with 34 additions and 0 deletions

View File

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