Skip to content

Instantly share code, notes, and snippets.

@prashantvc
Created June 5, 2012 06:13
Show Gist options
  • Save prashantvc/2873055 to your computer and use it in GitHub Desktop.
Save prashantvc/2873055 to your computer and use it in GitHub Desktop.
Create hierarchical structure from Dropbox delta api
//Author casperOne
public class Node
{
private readonly IDictionary<string, Node> _nodes = new Dictionary<string, Node>();
public string Name { get; set; }
public string Path { get; set; }
public IDictionary<string, Node> Nodes
{
get
{
return _nodes;
}
}
public void Add(string path)
{
// Parse into a sequence of parts.
string[] parts = path.Split(new[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
// The current node. Start with this.
Node current = this;
// Iterate through the parts.
foreach (string part in parts)
{
// The child node.
Node child;
// Does the part exist in the current node? If
// not, then add.
if (!current._nodes.TryGetValue(part, out child))
{
// Add the child.
child = new Node
{
Name = part,
Path = path
};
// Add to the dictionary.
current._nodes[part] = child;
}
// Set the current to the child.
current = child;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment