By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,324 Members | 1,699 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,324 IT Pros & Developers. It's quick & easy.

How to build a folder tree structure?

P: n/a
Dear All,

I'd like to build a structure similar to the folder tree which

1. Has a root.
2. Nodes can be folder or files.
3. Folder can contain folders and files.
4. Name of folders and files under a folder can not be same.

Anybody can give me some hint?

I appreciate your help.

Shuisheng

Nov 16 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
shuisheng wrote:
I'd like to build a structure similar to the folder tree which

1. Has a root.
2. Nodes can be folder or files.
3. Folder can contain folders and files.
4. Name of folders and files under a folder can not be same.

Anybody can give me some hint?
class FolderEntry
{
std::string name;
public:
FolderEntry(std::string const&);
};

class Folder : public FolderEntry
{
std::vector<FolderEntry*entries; // can be empty
...
void fillEntries(); // scanning the file system
};

class File : public FolderEntry
{
...
};

What other hints do you expect? Scanning folders and filling in
the 'entries' for any 'Folder' is platform-specific.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 16 '06 #2

P: n/a
It is tree structure with parent and sibling hierarchy:

struct node
{
node* first_sibling;
node* first_child;
// other data (like it is file or folder, etc)
};

For tree:

root
app
test1
test2
src
frm
usr1
usr2
usr3

root will be first allocated node struct.
root->first_sibling will point to first node at same level (so it will be
NULL), root->first_child will point to 'app'. app node (let say anode)
will be like: anode->first_sibling point to src, anode->first_child point
to test1, and so ...

Most operation will be recursive:

void dump(node* n)
{
dump(n->first_sibling);
dump(n->first_child);

}

Nov 16 '06 #3

P: n/a
Victor Bazarov wrote:
shuisheng wrote:
>I'd like to build a structure similar to the folder tree which

1. Has a root.
2. Nodes can be folder or files.
3. Folder can contain folders and files.
4. Name of folders and files under a folder can not be same.

Anybody can give me some hint?

class FolderEntry
{
std::string name;
public:
FolderEntry(std::string const&);
I'd add:

virtual ~FolderEntry() {}
};

class Folder : public FolderEntry
{
std::vector<FolderEntry*entries; // can be empty
...
void fillEntries(); // scanning the file system
};

class File : public FolderEntry
{
...
};

What other hints do you expect? Scanning folders and filling in
the 'entries' for any 'Folder' is platform-specific.
Nov 16 '06 #4

P: n/a
What you need here is a typical example of a Composite design patter.
As Victor explained, you'll need a base abstract class that handle
common operation (like a function that returns the size of the element)
and then derived classes for both files and folders (each class will
implement the size function by returning its actual size or the sum of
the sizes of each child).

Verifying that there are no duplicates in the same folder is trivial
here.

Rolf Magnus ha escrito:
Victor Bazarov wrote:
shuisheng wrote:
I'd like to build a structure similar to the folder tree which

1. Has a root.
2. Nodes can be folder or files.
3. Folder can contain folders and files.
4. Name of folders and files under a folder can not be same.

Anybody can give me some hint?
class FolderEntry
{
std::string name;
public:
FolderEntry(std::string const&);

I'd add:

virtual ~FolderEntry() {}
};

class Folder : public FolderEntry
{
std::vector<FolderEntry*entries; // can be empty
...
void fillEntries(); // scanning the file system
};

class File : public FolderEntry
{
...
};

What other hints do you expect? Scanning folders and filling in
the 'entries' for any 'Folder' is platform-specific.
Nov 17 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.