How to build a folder tree structure?

Discussion in 'C++' started by shuisheng, Nov 16, 2006.

  1. shuisheng

    shuisheng Guest

    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
     
    shuisheng, Nov 16, 2006
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Nov 16, 2006
    #2
    1. Advertising

  3. 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);

    }
     
    PasalicZaharije, Nov 16, 2006
    #3
  4. shuisheng

    Rolf Magnus Guest

    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.
     
    Rolf Magnus, Nov 16, 2006
    #4
  5. shuisheng

    Guest

    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, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stub

    B tree, B+ tree and B* tree

    Stub, Nov 12, 2003, in forum: C Programming
    Replies:
    3
    Views:
    10,235
  2. sharan
    Replies:
    4
    Views:
    719
    CBFalconer
    Oct 30, 2007
  3. sharan
    Replies:
    2
    Views:
    864
    SM Ryan
    Oct 31, 2007
  4. sharan
    Replies:
    1
    Views:
    713
    CBFalconer
    Oct 30, 2007
  5. anne001
    Replies:
    1
    Views:
    253
Loading...

Share This Page