using System; using System.Collections.Concurrent; using System.IO; using System.Linq; namespace BinaryObjectScanner.Utilities { /// /// Dictionary manipulation methods /// public static class Dictionary { /// /// Append one result to a results dictionary /// /// Dictionary to append to /// Key to add information to /// String value to add public static void AppendToDictionary(ConcurrentDictionary> original, string key, string value) { // If the value is empty, don't add it if (string.IsNullOrWhiteSpace(value)) return; var values = new ConcurrentQueue(); values.Enqueue(value); AppendToDictionary(original, key, values); } /// /// Append one result to a results dictionary /// /// Dictionary to append to /// Key to add information to /// String value to add public static void AppendToDictionary(ConcurrentDictionary> original, string key, ConcurrentQueue values) { // If the dictionary is null, just return if (original == null) return; // Use a placeholder value if the key is null key = key ?? "NO FILENAME"; // Add the key if needed and then append the lists original.TryAdd(key, new ConcurrentQueue()); original[key].AddRange(values); } /// /// Append one results dictionary to another /// /// Dictionary to append to /// Dictionary to pull from public static void AppendToDictionary(ConcurrentDictionary> original, ConcurrentDictionary> addition) { // If either dictionary is missing, just return if (original == null || addition == null) return; // Loop through each of the addition keys and add accordingly foreach (string key in addition.Keys) { original.TryAdd(key, new ConcurrentQueue()); original[key].AddRange(addition[key]); } } /// /// Remove empty or null keys from a results dictionary /// /// Dictionary to clean public static void ClearEmptyKeys(ConcurrentDictionary> original) { // If the dictionary is missing, we can't do anything if (original == null) return; // Get a list of all of the keys var keys = original.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 (original[key] == null || !original[key].Any()) original.TryRemove(key, out _); } } /// /// Prepend a parent path from dictionary keys, if possible /// /// Dictionary to strip values from /// Path to strip from the keys public static void PrependToKeys(ConcurrentDictionary> original, string pathToPrepend) { // If the dictionary is missing, we can't do anything if (original == null) return; // 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 = original.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)}"; original[newKey] = original[currentKey]; original.TryRemove(currentKey, out _); } } /// /// Strip a parent path from dictionary keys, if possible /// /// Dictionary to strip values from /// Path to strip from the keys public static void StripFromKeys(ConcurrentDictionary> original, string pathToStrip) { // If either is missing, we can't do anything if (original == null || string.IsNullOrEmpty(pathToStrip)) return; // Get a list of all of the keys var keys = original.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); original[newKey] = original[currentKey]; original.TryRemove(currentKey, out _); } } } }