/*
Copyright (C) 2014-2019 de4dot@gmail.com
This file is part of dnSpy
dnSpy is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
dnSpy is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with dnSpy. If not, see .
*/
using System;
using System.Collections.Generic;
namespace dnSpy.Contracts.Debugger {
///
/// Target environment
///
public sealed class DbgEnvironment {
///
/// Gets the environment keys and values
///
public KeyValuePair[] Environment => environment.ToArray();
readonly List> environment;
///
/// Constructor
///
public DbgEnvironment() {
environment = new List>();
var e = System.Environment.GetEnvironmentVariables().GetEnumerator();
while (e.MoveNext()) {
if (e.Key is string k && e.Value is string v)
environment.Add(new KeyValuePair(k, v));
}
}
///
/// Copy constructor
///
/// Other instance
public DbgEnvironment(DbgEnvironment other) =>
environment = new List>(other.environment);
///
/// Clears the environment
///
public void Clear() => environment.Clear();
///
/// Removes a key from the environment
///
/// Key
public void Remove(string key) {
if (key is null)
throw new ArgumentNullException(nameof(key));
var env = environment;
for (int i = env.Count - 1; i >= 0; i--) {
if (env[i].Key == key)
env.RemoveAt(i);
}
}
///
/// Adds a key and value to the environment
///
/// Key
/// Value
public void Add(string key, string value) {
if (key is null)
throw new ArgumentNullException(nameof(key));
if (value is null)
throw new ArgumentNullException(nameof(value));
Remove(key);
environment.Add(new KeyValuePair(key, value));
}
///
/// Adds values to the environment
///
/// Environment
public void AddRange(IEnumerable> environment) {
if (environment is null)
throw new ArgumentNullException(nameof(environment));
this.environment.AddRange(environment);
}
}
}