• entries
53
56
• views
33381

Dutch Elmgorithm Disease

189 views

I feel like an utter noob at this rate. My main mission for a period of time I won't embarass myself by revealing has been to get the files in each Component to list themselves out in a directory tree. As you can see, its only been going from bad to worse.

The problem is that there is some absolutely maddening self referencing going on, and while I can see the problem and where it exists, I cannot for the life of me figure out whats been going wrong. I want this over, so I'm going to turn to the people of GDNet to see if they can offer assistance on a way around it.

private void RefreshImageModel()        {            treeFiles.BeginUpdate();            treeFiles.Nodes.Clear();            TreeNode component, file, folder, current, next = null;            Queue<String> folders = new Queue<String>();            foreach (Component c in image.Components)            {                component = new TreeNode();                component.Tag = "Component";                component.Text = c.Name;                component.ImageIndex = 1;                component.SelectedImageIndex = 1;                treeFiles.Nodes.Add(component);                foreach (InstallFile ifile in c.Files)                {                    current = component;                    foreach(string s in ifile.RelativePath.Split('\\'))                        folders.Enqueue(s);                    foreach (string s in folders)                    {                        if (current.Nodes.Count > 0)                        {                            foreach (TreeNode tn in current.Nodes)                            {                                if (tn.Text.Equals(s))                                    current = tn;                            }                            if (next == null || next == current)                            {                                folder = CreateDirectory(s);                                current.Nodes.Add(folder);                                current = folder;                            }                        }                        else                        {                            folder = CreateDirectory(s);                            current.Nodes.Add(folder);                            current = folder;                        }                    }                    file = new TreeNode();                    file.Tag = "File";                    file.Text = ifile.Name;                    file.ImageIndex = 2;                    file.SelectedImageIndex = 2;                    current.Nodes.Add(file);                }            }            treeFiles.EndUpdate();            treeFiles.ExpandAll();        }

BotH: If anyone wants to give me the thrashing I so justly deserve for that pun, drop me a PM and we can arrange a time and place.

My brain turned into scrambled eggs trying to figure out your code (not your fault - I'm a bit sleep deprived atm) so I have no idea why it's broken.

However, here's how I'd do it:
- Assemble a list of files and their paths
- Clear out all tree nodes and add a single root node
- Iterate over the list
- Call AddTreeNode(rootnode, file) for each file

- Find the first path separator (i.e. / or \) in the string, starting from the left
- If there is no path separator, the remaining string is a file name - add a child node to the given node with the file's name as its text, and return
- Strip off everything to the left of the separator and call it PathFragment
- Search node's children to see if there is a child matching PathFragment
- If so, keep that node as NextChild
- If not, add a new child node to node and store the new node in NextChild
- Take everything to the right of the path separator in file and call it NextFile

That should do what you need, assuming that what I think you need is actually what you really do need.

Oh, and I'm up for giving you that thrashing - my schedule's pretty much wide open, so whatever works for you.

Thanks for responding ApochPiQ, that sounds a bit like what I have been doing.

The idea for the code above is this:

Clear out all Nodes
Parse through the list of Components
Add a new Component Node, and set as the current Node
Parse through the list of the Components files
For every file, split the path using the path seperator. The path does not contain the filename, it is just a directory reference
For every directory in the path (each string returned by the split), check all child Nodes in the current Node to see if any is already a reference to that path.
If one is, set it to the next Node.
If not, create a new child Node, and set that to the current Node.
When we have done iterating through the directory structure, we assume its time to create the file Node. This is done, and added to the current as a child.

The problem seems to occur when you add a new Node, which by default doesn't have any children. Hence, it always jumps to create a new Node, and sets that to the current Node. Again, no children...

It had occured to me that I could write a check to make sure that a child was not the same name as its parent, but that could mess up fringe cases where two directories would be legitimately the same name.

To do it recursively didn't occur to me, I might take a look into that.

Create an account

Register a new account