Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C++ and design Pattern (Composite design Pattern )

Reply
Thread Tools

C++ and design Pattern (Composite design Pattern )

 
 
Pallav singh
Guest
Posts: n/a
 
      01-22-2012
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ and design Pattern (Composite design Pattern ) Pallav singh C++ 1 01-22-2012 10:48 PM
C++ and design Pattern (Composite design Pattern ) Pallav singh C++ 0 01-22-2012 10:26 PM
Feedback on my design and how to use DCI Design Pattern? josh Ruby 5 12-27-2011 10:31 AM
May I have a example of design pattern of "composite", I still feel fuzzy after reading book of Addison-Wesley's"design pattern " jones9413@yahoo.com C++ 1 08-31-2007 04:09 AM
documents related to factory design pattern and Abstract foctory pattern. sunny C++ 1 12-07-2006 04:26 AM



Advertisments