Advertisement
Guest User

Untitled

a guest
Nov 25th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.86 KB | None | 0 0
  1. void CpCommand:: execute(FileSystem & fs) {
  2.     if ((verbose == 2) | (verbose == 3)) {
  3.         cout << this->toString() + ' ' + this->getArgs() << endl;
  4.     }
  5.  
  6.     string myArg = getArgs();
  7.     string source = myArg.substr(0,myArg.find_first_of(' '));
  8.     string destination = myArg.substr(myArg.find_first_of(' ')+1);
  9.  
  10.     if (source =="/")
  11.         return;
  12.  
  13.     string currentName="";
  14.     Directory& toReturn = fs.getWorkingDirectory();
  15.     Directory* currDir=&fs.getWorkingDirectory();
  16.     if(source[0]=='/') {
  17.         *currDir = fs.getRootDirectory();
  18.         source=source.substr(1);
  19.     }
  20.  
  21.     while(source.size()!=0){
  22.         if((source[0]!='.')&(source[0]!='/')){
  23.             currentName+=source[0];
  24.             source=source.substr(1);
  25.         }
  26.         if((source.size()==1)&&(source[0]=='/')&(currentName.size()>0)){
  27.             source="";
  28.         }
  29.  
  30.         if((source.size()>1)&&(source[0]=='/')&(currentName.size()>0)){
  31.             bool found(false);
  32.             for(size_t i(0);(i<currDir->getChildren().size())&(!found);i++){
  33.                 if(currDir->getChildren()[i]->getName()==currentName){
  34.                     currDir= dynamic_cast<Directory*>(currDir->getChildren()[i]);
  35.                     currentName="";
  36.                     source=source.substr(1);
  37.                     found=true;
  38.                 }
  39.             }
  40.             if(!found){
  41.                 cout<<"No such file or directory"<<endl;
  42.                 return;
  43.             }
  44.         }
  45.         if((source.size()>2)&& (source[0]=='.')&(source[1]=='.')&(source[2]=='/')){
  46.             if((currDir)==(&fs.getRootDirectory())){
  47.                 return;
  48.             }
  49.             else{
  50.                 currDir=(currDir->getParent());
  51.                 source=source.substr(3);
  52.             }
  53.         }
  54.         if((source.size()>=2)&& (source[0]=='.')&(source[1]=='.')){
  55.             if((currDir)==(&fs.getRootDirectory())){
  56.                 return;
  57.             }
  58.             else{
  59.                 currDir=(currDir->getParent());
  60.                 source=source.substr(2);
  61.             }
  62.         }
  63.  
  64.     }
  65.     bool found(false);
  66.     for(size_t i(0);(i<currDir->getChildren().size())&(!found);i++){
  67.         if(currDir->getChildren()[i]->getName()==currentName){
  68.             found=true;
  69.             if(currDir->getChildren()[i]->isFile()){
  70.                 CdCommand cd(destination);
  71.                 if(cd.executeBool(fs)){
  72.                     bool found=false;
  73.                     for (size_t i(0);i<fs.getWorkingDirectory().getChildren().size();i++)
  74.                     {
  75.                         if(fs.getWorkingDirectory().getChildren()[i]->getName()==currDir->getChildren()[i]->getName())
  76.                             found=true;
  77.                     }
  78.                     if(!found)
  79.                     fs.getWorkingDirectory().addFile(new File(currentName,currDir->getChildren()[i]->getSize()));
  80.                 }
  81.                 else{
  82.                     cout<<"No such file or directory"<<endl;
  83.                     return;
  84.                 }
  85.             }
  86.             else{//its a directory
  87.  
  88.                 CdCommand cd(destination);
  89.                 if(cd.executeBool(fs)){
  90.                     bool found=false;
  91.                     for (size_t i(0);i<fs.getWorkingDirectory().getChildren().size();i++)
  92.                     {
  93.                         if(fs.getWorkingDirectory().getChildren()[i]->getName()==currDir->getChildren()[i]->getName())
  94.                             found=true;
  95.                     }
  96.                     if(!found)
  97.                     fs.getWorkingDirectory().addFile(new Directory(dynamic_cast<Directory&>(*currDir->getChildren()[i])));
  98.                 }
  99.                 else{
  100.                     cout<<"No such file or directory"<<endl;
  101.                     return;
  102.                 }
  103.             }
  104.         }
  105.     }
  106.  
  107.     fs.setWorkingDirectory(&toReturn);
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement