C++ and design Pattern (Composite design Pattern )

Discussion in 'C++' started by Pallav singh, Jan 22, 2012.

  1. Pallav singh

    Pallav singh Guest

    HI All

    i am new to C++ and design Pattern ... while implementing Composite
    design Pattern
    Facing Issue. Kindly help me Out.

    [root@arnav test]# cat Filesystem.cc
    # include <iostream>
    # include <vector>
    # include <string.h>
    # include <sstream>
    # include <ctime>
    using namespace std;

    #define PORTAGE_DIR "/home/test/"

    # include <stdio.h>
    #include <stdlib.h>
    # include <dirent.h>
    # include <sys/types.h>
    # include <sys/stat.h>

    ///////////////////////////////////////////////////////
    class Directory;

    class AbsFile {
    string name;
    class Directory* parent;
    protected:
    Directory* getParent() { return parent; }
    public:
    AbsFile( string n, Directory* p ) : name( n ) { parent = p; }
    virtual ~AbsFile() { }

    string getName() { return name; }
    virtual void fileSystemCrawler ( string = "" ) = 0;
    };

    ////////////////////////////////////////////////////////////////////

    class File : public AbsFile {
    stringstream content;

    public:
    File(string n, Directory * p) : AbsFile(n , p){ }
    ~File() { }

    /*virtual*/
    void fileSystemCrawler( string s = "") {
    struct stat crawl_stat;
    s = getName();

    if (s == "" || s == ".." || s ==".")
    return ;

    if (stat(s.c_str(), &crawl_stat) == 0 ) {
    cout<< " Filename " << s << " " << crawl_stat.st_size <<
    endl;
    }
    }

    };

    ////////////////////////////////////////////////////////////////////////////

    class Directory : public AbsFile {
    string name;
    public:
    Directory (string n, Directory * p = 0) : AbsFile(n, p) {
    }

    ~Directory() {
    }

    //////////////////////////////////////////////////////////////////////////
    int isdir(struct dirent **check_dirent) {
    struct stat check_stat = {0};

    if ( stat((*check_dirent)->d_name, &check_stat) == 0 ) {
    if( S_ISDIR(check_stat.st_mode) != 0 ) {
    if( strcmp( (*check_dirent)->d_name , "." ) != 0 ) {
    if( strcmp( (*check_dirent)->d_name , ".." ) != 0 ) {
    return 0;
    }
    }
    }
    }

    return 1;
    }

    //////////////////////////////////////////////////////////////////////////

    /*virtual*/
    void fileSystemCrawler ( string s = "" ) {
    /* Holds info for the main portage directory. */
    DIR *master;
    struct dirent *master_dirent;
    struct stat master_stat;

    /* Holds info for the crawler which goes in every category
    folder. */
    DIR *crawl;
    struct dirent *crawl_dirent;
    struct stat crawl_stat = {0};

    s = getName();

    if (chdir(s.c_str()) != 0)
    cout << "chdir() failed" << endl;

    system ("pwd");

    if ((master = opendir(s.c_str())) == (DIR *)0)
    cout << " opendir failed" << endl;

    if (s == "" || s == ".." || s ==".")
    return ;


    if (stat(s.c_str(), &crawl_stat) == 0 ) {
    cout<< " Directory " << s << " " << crawl_stat.st_size <<
    endl;
    }

    while((master_dirent = readdir(master)) != 0 ) {
    if (stat(master_dirent->d_name, &master_stat) == 0 ) {
    string str = master_dirent->d_name;

    if (isdir(&master_dirent) == 0)
    {
    Directory * ptr = new Directory (str,
    getParent());
    ptr->fileSystemCrawler(str);
    delete ptr;
    chdir("..");
    } else {
    File * ptr = new File(str, getParent());
    ptr->fileSystemCrawler(str);
    delete ptr;
    }
    }
    }

    closedir(master);
    }


    };


    Directory* initialize() {
    Directory* root = new Directory( "/home/test/" );
    return root;
    }

    //////////////////////////////////////////////////////////////

    int main( void ) {
    ////////// Composite, Proxy //////////
    Directory* root = initialize();
    root->fileSystemCrawler(); cout << '\n';

    delete root; cout << '\n';

    return 0;
    }

    ===================================================

    [root@arnav test]#
    [root@arnav test]#
    [root@arnav test]# ls
    a.out Backup.cc Backup_File.cc Filesystem.cc intermediate.cc
    test1 test2 test3
    [root@arnav test]# ./a.out
    /home/test
    Directory /home/test/ 4096
    Filename a.out 44683
    Filename Backup_File.cc 5015
    Filename Backup.cc 5027
    /home/test/test2
    opendir failed
    Segmentation fault (core dumped)
    [root@arnav test]#


    Thanks
    Pallav Singh
    Pallav singh, Jan 22, 2012
    #1
    1. Advertising

  2. Am 22.01.2012 23:28, schrieb Pallav singh:
    > /*virtual*/
    > void fileSystemCrawler ( string s = "" ) {
    > [...]
    > system ("pwd");
    >
    > if ((master = opendir(s.c_str())) == (DIR *)0)
    > cout << " opendir failed" << endl;


    There's no return here. Happily continuing with master==NULL ...
    And btw. why do you use "(DIR*)0" instead of just "NULL" or !(...)?

    > if (s == "" || s == ".." || s ==".")
    > return ;

    Maybe this should go before the opendir() call.

    >
    >
    > if (stat(s.c_str(), &crawl_stat) == 0 ) {
    > cout<< " Directory " << s << " " << crawl_stat.st_size <<
    > endl;
    > }
    >
    > while((master_dirent = readdir(master)) != 0 ) {

    I would guess your crash is here: calling readdir(NULL).

    > [...]
    > [root@arnav test]#
    > [root@arnav test]# ls
    > a.out Backup.cc Backup_File.cc Filesystem.cc intermediate.cc
    > test1 test2 test3
    > [root@arnav test]# ./a.out
    > /home/test
    > Directory /home/test/ 4096
    > Filename a.out 44683
    > Filename Backup_File.cc 5015
    > Filename Backup.cc 5027
    > /home/test/test2
    > opendir failed
    > Segmentation fault (core dumped)
    > [root@arnav test]#
    >
    >
    > Thanks
    > Pallav Singh



    Peter
    Peter Remmers, Jan 22, 2012
    #2
    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. sleigh
    Replies:
    1
    Views:
    2,666
    sleigh
    Feb 12, 2004
  2. Chad
    Replies:
    0
    Views:
    209
  3. Mike

    Composite vs non composite Controls

    Mike, Mar 10, 2005, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    249
    Sundararajan
    Mar 11, 2005
  4. Pallav singh
    Replies:
    0
    Views:
    341
    Pallav singh
    Jan 22, 2012
  5. Pallav singh
    Replies:
    0
    Views:
    387
    Pallav singh
    Jan 22, 2012
Loading...

Share This Page