/// <summary> /// Sets the Value and the Discription for the given Key. /// </summary> /// <param name="key">Full Key (incl. Type and Key)</param> /// <param name="value"> Value</param> /// <param name="description">Optional Description for this key</param> public void Set(string key, string value, string description = null) { // TODO: Check right syntax (full.type.name:key) ValueDescriptionEntry VDE; if (!Storage.TryGetValue(key, out VDE)) { VDE = new ValueDescriptionEntry() { Value = value, Description = description } } ; else { if (value != null) { VDE.Value = value; } if (description != null) { VDE.Description = description; } } Storage[key] = VDE; }
/// <summary> /// Saves all Settings to a File. /// </summary> /// <param name="stream">Output Stream</param> /// <param name="alignedValues">Optional easy to read output</param> public void Save(Stream stream, bool alignedValues = false) { using (StreamWriter sw = new StreamWriter(stream)) { if (Common.Count > 0) { sw.WriteLine("[Common]"); foreach (var item in Common) { sw.WriteLine("{0}={1}", item.Key, item.Value); } } string[] keytemp = Keys.ToArray(); var typeKeys = keytemp.Select(k => k.Substring(0, k.IndexOf(':'))).Distinct(); foreach (var typekey in typeKeys.OrderBy(k => k)) { sw.WriteLine("[{0}]", typekey); int keyLength = 1; int valueLength = 1; if (alignedValues) { foreach (var key in Storage.Where(k => k.Key.StartsWith(typekey)).Select(k => k.Key.Substring(k.Key.IndexOf(":") + 1)).ToArray()) { string fullkey = string.Format("{0}:{1}", typekey, key); ValueDescriptionEntry VDE = GetValueDescriptionEntry(fullkey); keyLength = Math.Max(keyLength, key.Length); valueLength = Math.Max(valueLength, VDE.Value.Length); } } foreach (var key in Storage.Where(k => k.Key.StartsWith(typekey)).Select(k => k.Key.Substring(k.Key.IndexOf(":") + 1)).ToArray()) { string fullkey = string.Format("{0}:{1}", typekey, key); ValueDescriptionEntry VDE = GetValueDescriptionEntry(fullkey); string description = " // "; if (string.IsNullOrWhiteSpace(VDE.Description)) { description = string.Empty; } else { description += VDE.Description; } sw.WriteLine("{0}={1}{2}", key.ToString().PadRight(keyLength), (VDE.Value != null ? VDE.Value : "").PadRight(valueLength), description); } sw.WriteLine(); } sw.Close(); } //// TODO: Implement Settings Save //throw new NotImplementedException(); }
private static KeyValueStore Load(Stream stream, string source) { KeyValueStore locaKeyValueStore = new KeyValueStore(); List <string> locaErrors = new List <string>(); using (StreamReader sr = new StreamReader(stream)) { int currentLine = 0; string currentTypeKey = ""; while (sr.Peek() >= 0) { currentLine++; bool waitforTypeKey = false; string rawData = sr.ReadLine(); string editedData = rawData.TrimStart(' ').TrimEnd(' '); if (editedData.StartsWith("[")) { if (editedData.IndexOf(']') == -1) { waitforTypeKey = true; locaErrors.Add("ERROR Line " + currentLine.ToString() + ": missing ']' (end of TypeKey deklaration)"); continue; } currentTypeKey = editedData.Substring(1, editedData.IndexOf(']') - 1).TrimStart(' ').TrimEnd(' '); continue; } else if (waitforTypeKey) { continue; } else if (editedData.Contains("=")) { string key = editedData.Substring(0, editedData.IndexOf('=')).TrimEnd(' '); editedData = editedData.Remove(0, editedData.IndexOf('=') + 1); if (currentTypeKey == "Common") { locaKeyValueStore.Common.Add(key, editedData.TrimStart(' ').TrimEnd(' ')); continue; } ValueDescriptionEntry VDE = new ValueDescriptionEntry(); if (editedData.IndexOf("//") >= 0) { VDE.Value = editedData.Substring(0, editedData.IndexOf("//")).TrimStart(' ').TrimEnd(' '); VDE.Description = editedData.Remove(0, editedData.IndexOf("//") + 2).TrimStart(' ').TrimEnd(' '); } else { VDE.Value = editedData.TrimStart(' ').TrimEnd(' '); } if (string.IsNullOrWhiteSpace(VDE.Value)) { locaErrors.Add("WARNING Line " + currentLine.ToString() + ": missing Value"); } if (string.IsNullOrWhiteSpace(VDE.Description)) { locaErrors.Add("WARNING Line " + currentLine.ToString() + ": missing Description"); } locaKeyValueStore.Set(FullKey(currentTypeKey, key), VDE); } else if (string.IsNullOrWhiteSpace(editedData)) { continue; } else { locaErrors.Add("ERROR Line " + currentLine.ToString() + ": could not be deserialized, make sure it matches the Format([TYPEKEY] or KEY=VALUE//DESCRIPTION)"); } } } if (locaErrors.Count > 0) { locaKeyValueStore.LoadingErrors.Add(source, locaErrors); } return(locaKeyValueStore); }
/// <summary> /// Merge information with the given KeyValueStore-Instance. /// </summary> /// <param name="keyValueStore">Source KeyValueStore</param> public void Merge(KeyValueStore keyValueStore) { //merge storage dictonary foreach (var key in keyValueStore.Storage.Keys) { ValueDescriptionEntry newVDE; keyValueStore.Storage.TryGetValue(key, out newVDE); if (Storage.Keys.Contains(key)) { ValueDescriptionEntry orgVDE; Storage.TryGetValue(key, out orgVDE); if (orgVDE == null) { orgVDE = new ValueDescriptionEntry(); } if (string.IsNullOrEmpty(newVDE.Value)) { newVDE.Value = orgVDE.Value; } if (string.IsNullOrEmpty(newVDE.Description)) { newVDE.Value = orgVDE.Description; } Storage[key] = newVDE; } else { Storage.Add(key, newVDE != null ? newVDE : new ValueDescriptionEntry()); } } //merge common dictonary foreach (var key in keyValueStore.Common.Keys) { string newValue; keyValueStore.Common.TryGetValue(key, out newValue); if (Common.Keys.Contains(key)) { string orgValue; Common.TryGetValue(key, out orgValue); if (string.IsNullOrEmpty(newValue)) { newValue = orgValue; } Common[key] = newValue; } else { Common.Add(key, newValue); } } //merge loadingError Diconary foreach (var key in keyValueStore.LoadingErrors.Keys) { List <string> newErrorList; keyValueStore.LoadingErrors.TryGetValue(key, out newErrorList); if (LoadingErrors.Keys.Contains(key)) { List <string> orgErrorList; LoadingErrors.TryGetValue(key, out orgErrorList); foreach (var item in orgErrorList) { if (!newErrorList.Contains(item)) { newErrorList.Add(item); } } LoadingErrors[key] = newErrorList; } else { LoadingErrors.Add(key, newErrorList); } } }
/// <summary> /// Sets the Value and the Discription for the given Key. /// </summary> /// <param name="key">Full Key (incl. Type and Key)</param> /// <param name="valueDescriptionEntry">ValueDescriptionEntry</param> private void Set(string key, ValueDescriptionEntry valueDescriptionEntry) { Set(key, valueDescriptionEntry.Value, valueDescriptionEntry.Description); }