From 9a3fde051887ee67709f400da67992d952dafefa Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Thu, 31 Oct 2024 20:54:36 -0400 Subject: [PATCH] Add ProtectionDictionary type (unused) --- BinaryObjectScanner/ProtectionDictionary.cs | 193 ++++++++++++++++++++ 1 file changed, 193 insertions(+) create mode 100644 BinaryObjectScanner/ProtectionDictionary.cs diff --git a/BinaryObjectScanner/ProtectionDictionary.cs b/BinaryObjectScanner/ProtectionDictionary.cs new file mode 100644 index 00000000..e9f11fbf --- /dev/null +++ b/BinaryObjectScanner/ProtectionDictionary.cs @@ -0,0 +1,193 @@ +using System; +#if NET20 || NET35 +using System.Collections.Generic; +#else +using System.Collections.Concurrent; +#endif +using System.IO; +using System.Linq; +using BinaryObjectScanner.Utilities; + +namespace BinaryObjectScanner +{ +#if NET20 || NET35 + public class ProtectionDictionary : Dictionary> +#else + public class ProtectionDictionary : ConcurrentDictionary> +#endif + { + /// + /// Append one result to a results dictionary + /// + /// Key to add information to + /// String value to add + public void Append(string key, string value) + { + // If the value is empty, don't add it + if (string.IsNullOrEmpty(value)) + return; + +#if NET20 || NET35 + var values = new Queue(); +#else + var values = new ConcurrentQueue(); +#endif + values.Enqueue(value); + Append(key, values); + } + + /// + /// Append one set of results to a results dictionary + /// + /// Key to add information to + /// String value array to add + public void Append(string key, string[] values) + { + // Use a placeholder value if the key is null + key ??= "NO FILENAME"; + + // Add the key if needed and then append the lists +#if NET20 || NET35 + if (!original.ContainsKey(key)) + original[key] = new Queue(); +#else + TryAdd(key, new ConcurrentQueue()); +#endif + this[key].AddRange(values); + } + + /// + /// Append one set of results to a results dictionary + /// + /// Key to add information to + /// String value to add +#if NET20 || NET35 + public void Append(string key, Queue values) +#else + public void Append(string key, ConcurrentQueue values) +#endif + { + // Use a placeholder value if the key is null + key ??= "NO FILENAME"; + + // Add the key if needed and then append the lists +#if NET20 || NET35 + if (!original.ContainsKey(key)) + original[key] = new Queue(); +#else + TryAdd(key, new ConcurrentQueue()); +#endif + this[key].AddRange(values); + } + + /// + /// Append one results dictionary to another + /// + /// Dictionary to pull from + public void Append(ProtectionDictionary? addition) + { + // If the dictionary is missing, just return + if (addition == null) + return; + + // Loop through each of the addition keys and add accordingly + foreach (string key in addition.Keys) + { +#if NET20 || NET35 + if (!original.ContainsKey(key)) + original[key] = new Queue(); +#else + TryAdd(key, new ConcurrentQueue()); +#endif + this[key].AddRange(addition[key]); + } + } + + /// + /// Remove empty or null keys from a results dictionary + /// + public void ClearEmptyKeys() + { + // Get a list of all of the keys + var keys = Keys.ToList(); + + // Iterate and reset keys + for (int i = 0; i < keys.Count; i++) + { + // Get the current key + string key = keys[i]; + + // If the key is empty, remove it + if (this[key] == null || !this[key].Any()) +#if NET20 || NET35 + Remove(key); +#else + TryRemove(key, out _); +#endif + } + } + + /// + /// Prepend a parent path from dictionary keys, if possible + /// + /// Path to strip from the keys + public void PrependToKeys(string pathToPrepend) + { + // Use a placeholder value if the path is null + pathToPrepend = (pathToPrepend ?? "ARCHIVE").TrimEnd(Path.DirectorySeparatorChar); + + // Get a list of all of the keys + var keys = Keys.ToList(); + + // Iterate and reset keys + for (int i = 0; i < keys.Count; i++) + { + // Get the current key + string currentKey = keys[i]; + + // Otherwise, get the new key name and transfer over + string newKey = $"{pathToPrepend}{Path.DirectorySeparatorChar}{currentKey.Trim(Path.DirectorySeparatorChar)}"; + this[newKey] = this[currentKey]; +#if NET20 || NET35 + Remove(currentKey); +#else + TryRemove(currentKey, out _); +#endif + } + } + + /// + /// Strip a parent path from dictionary keys, if possible + /// + /// Path to strip from the keys + public void StripFromKeys(string? pathToStrip) + { + // If the path is missing, we can't do anything + if (string.IsNullOrEmpty(pathToStrip)) + return; + + // Get a list of all of the keys + var keys = Keys.ToList(); + + // Iterate and reset keys + for (int i = 0; i < keys.Count; i++) + { + // Get the current key + string currentKey = keys[i]; + + // If the key doesn't start with the path, don't touch it + if (!currentKey.StartsWith(pathToStrip, StringComparison.OrdinalIgnoreCase)) + continue; + + // Otherwise, get the new key name and transfer over + string newKey = currentKey.Substring(pathToStrip!.Length); + this[newKey] = this[currentKey]; +#if NET20 || NET35 + Remove(currentKey); +#else + TryRemove(currentKey, out _); +#endif + } + } + } +} \ No newline at end of file