You are on page 1of 4

#include <iostream> #include <cstring> #include <cstdlib> #include <sys/stat.h> #include <dirent.h> #include <unistd.

h> #include <sstream> #include <fstream> #include <map> #define MAX_PATH_LEN 2048 #define FPATH "/satya/ExampCode/file_list.txt" using namespace std; class DirWalk { private: struct dirStats { string dirName; int fc; }ds; //static int n; int file_count; map<string,string> fileMap; map<string,string>::iterator map_it; map<string,int> fcMap; map<string,int>::iterator fcMap_it; public : DirWalk() { //this->n=0; file_count=0; }; ~DirWalk(){ fileMap.clear();fcMap.clear();}; //static void setcounters(int); void parceDir(string); void writeToFile(void); void showFiles(void); void showDirStats(void); inline void setFileCount(int i){ this->file_count=i;}; inline int getFileCount(){return this->file_count;}; }; //void DirWalk::setcounters(int i){n=i;}; void DirWalk::showFiles() { for(map_it=fileMap.begin();map_it!=fileMap.end();map_it++) { cout<<"file :"<<(*map_it).first<<" path :"<<(*map_it).second <<endl; } }; void DirWalk::writeToFile() { ofstream fd(FPATH,ios_base::out|ios::trunc); //std::ostringbuf osb; stringbuf buff; for(map_it=fileMap.begin();map_it!=fileMap.end();map_it++) { //buff.sputn(std::string((*map_it).first).c_str(),std::s tring((*map_it).first).length());

//buff.sputn(std::string("\t").c_str(),1); //buff.sputn(std::string((*map_it).second).c_str(),std:: string((*map_it).second).length()); //buff.sputn(std::string("\n").c_str(),1); fd.write(std::string((*map_it).first).c_str(),std::strin g((*map_it).first).length()); fd.write(std::string("\t").c_str(),std::string("\t").len gth()); fd.write(std::string((*map_it).second).c_str(),std::stri ng((*map_it).second).length()); fd.write(std::string("\n").c_str(),std::string("\n").len gth()); } }; void DirWalk::showDirStats() { for(fcMap_it=fcMap.begin();fcMap_it!=fcMap.end();fcMap_it++) { cout<<" DIR :"<<(*fcMap_it).first<<" number Of files :"<<(*fcM ap_it).second<<endl; } cout<<"Total number of files :"<<fileMap.size()<<endl; }; void DirWalk::parceDir(string path) { char cwd[MAX_PATH_LEN]; int c; int flag=1; struct dirent *entry; struct stat st_buf; //struct dirStats ds; if(chdir(path.c_str())==-1) cout<<"error :it is not a directory :"<<endl; else { if (!getcwd(cwd, MAX_PATH_LEN+1)) { cout<<"getcwd:"; return; } cout<<"working directory :"<<cwd<<endl; DIR *dir_=opendir("."); string dname=path.substr(path.find_last_of("/")+1); cout<<"directory name :"<<dname<<endl; if(flag!=0&&getFileCount()!=0) { this->ds.fc=getFileCount(); this->ds.dirName=dname; //fcMap.insert(pair<string,int>(ds.dirName,ds.fc )); } if(flag!=0&&ds.fc!=0){ this->ds.fc=getFileCount(); fcMap.insert(pair<string,int>(th is->ds.dirName,ds.fc));} if(!dir_)

{ cout<<"error in openning a directory"<<endl; exit(1); } string strEntry; while((entry=readdir(dir_))!=NULL) { if(strcmp(entry->d_name,".")==0){ c=0; continue;} if(strcmp(entry->d_name,"..")==0) continue; strEntry=path+"/"+entry->d_name; flag=0; int status=stat(strEntry.c_str(),&st_buf); if(status==0) { if(st_buf.st_mode & S_IFDIR) { dname=entry->d_name; //flag=1; this->ds.dirName=entry-> d_name;; cout<<"it is a directory :"<<this->ds.dirName<<endl; } //if(c==1) {this->ds.fc=getFileC ount(),fcMap.insert(pair<string,int>(this->ds.dirName,ds.fc));} if(st_buf.st_mode & S_IFREG) { flag=1; c+=1; cout<<"name :"<<entry-> d_name<<" :number of files :"<<c<<endl; cout<<"dir :"<<this->ds .dirName<<" files :"<<this->ds.fc<<endl; this->setFileCount(c); this->fileMap.insert(pai r<string,string>(string(entry->d_name),strEntry)); //if(c==1) {this->ds.fc= getFileCount(),fcMap.insert(pair<string,int>(this->ds.dirName,ds.fc));} continue; } } if(st_buf.st_mode & S_IFDIR) parceDir(strEntry); //restoring the directory if(chdir("..")==-1) { cout<<"error : can't change dir ectory back from :"<<endl; } }

closedir(dir_); } }; int main() { string dir = string("/home/csis-rad/satya/netshare"); DirWalk *dw=new DirWalk(); //DirWalk::setcounters(0); dw->parceDir(dir); dw->showFiles(); dw->showDirStats(); dw->writeToFile(); dw->~DirWalk(); }

You might also like