using System; using System.Collections.Generic; using System.Text; namespace DatabaseExport { /// /// a simple setting in a test /// can have a default value, a value, and an id /// default value is used by TestSettingsDictionary for when /// the setting doesn't currently exist or have a value /// public class TestSetting { public string Id { get; } public string Value { get; set; } public string DefaultValue { get; } private const string SEPARATOR = "_x_"; public TestSetting(TestSetting copy, string value) { Id = copy.Id; DefaultValue = copy.DefaultValue; Value = value; } public TestSetting(TestSetting copy) { Id = copy.Id; DefaultValue = copy.DefaultValue; Value = copy.Value; } public TestSetting(string id, string value, string defaultValue) { Id = id; Value = value; DefaultValue = defaultValue; } public string ToSerializeString() { System.Diagnostics.Trace.Assert(Id.IndexOf(SEPARATOR) < 0); return string.Format("{0}={1}", Id.Replace("=", SEPARATOR), Value.Replace("=", SEPARATOR)); } public static bool TryParse(string s, out TestSetting ts) { ts = null; var tokens = s.Split(new[] { "=" }, StringSplitOptions.None); if (tokens.Length < 2) { return false; } var id = tokens[0].Replace(SEPARATOR, "="); var val = tokens[1].Replace(SEPARATOR, "="); ts = new TestSetting(id, val, val); return true; } } /// /// holds all possible settings for a test /// public class TestSettingDictionary { public TestSettingDictionary() { } public TestSettingDictionary(TestSettingDictionary copy) { var e = copy._lookup.GetEnumerator(); while (e.MoveNext()) { _lookup[e.Current.Key] = new TestSetting(e.Current.Value); } } private const string _SEPARATOR = "_X_"; private Dictionary _lookup = new Dictionary(); public string GetValue(string ID, string defaultValue) { if (!_lookup.ContainsKey(ID)) { return defaultValue; } else { if (null == _lookup[ID].Value) { return _lookup[ID].DefaultValue; } else { return _lookup[ID].Value; } } } public void UnLoad() { _lookup.Clear(); } /// /// used to change the value in the dictionary (just the value, leave everything else the same) /// /// /// public void SetValue(TestSetting setting, string value) { //we do it this way to avoid Add() and also to avoid accidentally reusing the input setting inappropriately _lookup[setting.Id] = new TestSetting(setting, value); } /// /// used to initialize a value in the dictionary /// /// public void SetValue(TestSetting setting) { _lookup[setting.Id] = setting; } public void SetValue(string id, string value) { if (!_lookup.ContainsKey(id)) { _lookup[id] = new TestSetting(id, value, value); } else { _lookup[id].Value = value; } } public string ToSerializeString() { var sb = new StringBuilder(); foreach (var s in _lookup.Values) { var sVal = s.ToSerializeString(); System.Diagnostics.Trace.Assert(sVal.IndexOf(_SEPARATOR) < 0); sVal = sVal.Replace(System.Globalization.CultureInfo.InvariantCulture.TextInfo.ListSeparator, _SEPARATOR); if (sb.Length > 0) { sb.Append(System.Globalization.CultureInfo.InvariantCulture.TextInfo.ListSeparator); } sb.Append(sVal); } return sb.ToString(); } public void LoadSettings(string s) { var tokens = s.Split(new[] { System.Globalization.CultureInfo.InvariantCulture.TextInfo.ListSeparator }, StringSplitOptions.None); foreach (var token in tokens) { var tok = token.Replace(_SEPARATOR, System.Globalization.CultureInfo.InvariantCulture.TextInfo.ListSeparator); TestSetting ts; //we prefer the default settings from the application //if for some reason this key is no longer used, we can still stick it in the storage and use it as is if (TestSetting.TryParse(tok, out ts)) { if (!_lookup.ContainsKey(ts.Id))//no longer has a default setting, just use as is { _lookup[ts.Id] = ts; } else { _lookup[ts.Id].Value = ts.Value; }//default setting exists, just set the value } } } } }