/// <summary> /// Retrieves the node at the directory path for the given <see cref="AegisVirtualDirectoryPath"/>. /// </summary> /// <param name="directoryPath">The directory path to follow.</param> /// <param name="createIfNotExists">Flag indicating we should create the node (including all intermediate nodes) if it doesn't exist.</param> /// <returns>The node at the directory path specified, or null if it doesn't exist and <paramref name="createIfNotExists"/> is not specified.</returns> /// <remarks>The node may not actually contain the file.</remarks> private VirtualFileTreeNode GetNodeForDirectoryPath(AegisVirtualDirectoryPath directoryPath, bool createIfNotExists = false) { var curNode = this.Root; int dirDepth; for (dirDepth = 0; dirDepth < directoryPath.Components.Length; dirDepth++) { var dir = new AegisVirtualDirectoryPath(directoryPath.Components.Take(dirDepth + 1)); if (!curNode.Children.ContainsKey(dir)) { if (createIfNotExists) { var newChild = new VirtualFileTreeNode(dir, curNode); curNode.Children.Add(dir, newChild); } else { break; } } curNode = curNode.Children[dir]; } return(dirDepth == directoryPath.Components.Length ? curNode : null); }
/// <summary> /// Prunes the node from its tree if it's an empty leaf (i.e. contains no files, /// no children, and isn't the root). This method then recursively moves up the tree /// to prune the entire subtree in case pruning <paramref name="node"/> made its parent /// and empty leaf (and so on). /// </summary> /// <param name="node">The node to prune, if it's an empty leaf.</param> private static void PruneNodeIfNeeded(VirtualFileTreeNode node) { while (node.Parent != null && node.IsEmpty) { node.Parent.Children.Remove(node.Directory); // Move up one level and repeat the check. node = node.Parent; } }
/// <summary> /// Initializes a new instance of the <see cref="VirtualFileTreeNode"/> class. /// </summary> /// <param name="directory">The virtual directory at this node.</param> /// <param name="parent">The parent of this node.</param> public VirtualFileTreeNode(AegisVirtualDirectoryPath directory, VirtualFileTreeNode parent) { this.Directory = directory; this.Parent = parent; }