Advertisement
Guest User

mkdir

a guest
Nov 25th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. void MkdirCommand::execute(FileSystem &fs) {
  2.     if((verbose==2) | (verbose==3)){
  3.         cout<<this->toString()+' '+this->getArgs()<<endl;
  4.     }
  5.  
  6.     Directory* currentDirToReSet = &fs.getWorkingDirectory();
  7.     Directory* currentDir =  &fs.getWorkingDirectory();
  8.     CdCommand cd(getArgs());
  9.     if(cd.executeBool(fs)){
  10.         cout<<"The directory already exists"<<endl;
  11.         fs.setWorkingDirectory(currentDirToReSet);
  12.         return;
  13.     }
  14.     fs.setWorkingDirectory(currentDir);
  15.  
  16.     string myArg = getArgs();
  17.  
  18.     if(myArg.size()!=0)
  19.         myArg=myArg.substr(myArg.find_first_not_of(' '));
  20.  
  21.     if (myArg[0]=='/'){
  22.         fs.setWorkingDirectory(&fs.getRootDirectory());
  23.         myArg=myArg.substr(1);
  24.         currentDir=&fs.getWorkingDirectory();
  25.     }
  26.  
  27.     string currentName="";
  28.  
  29.     while (myArg.size()!=0){
  30.         if((myArg[0]!='/') & (myArg[0]!='.')) {
  31.             currentName += myArg[0];
  32.             myArg=myArg.substr(1);
  33.         }
  34.         if(myArg[0]=='/'){
  35.             bool found(false);
  36.             for(size_t i(0);(i<currentDir->getChildren().size()) & (!found) ;i++){
  37.                 if(currentDir->getChildren()[i]->getName()==currentName ){
  38.                     if (currentDir->getChildren()[i]->isFile()){
  39.                         cout<<"The directory already exists"<<endl;
  40.                         return;
  41.                     }
  42.                     found=true;
  43.                     currentDir= dynamic_cast<Directory *>(currentDir->getChildren()[i]);
  44.                     fs.setWorkingDirectory(currentDir);
  45.                 }
  46.             }
  47.             if(!found){
  48.                 Directory* toAdd = new Directory(currentName,currentDir);
  49.                 currentDir->addFile(toAdd);
  50.                 fs.setWorkingDirectory(toAdd);
  51.                 currentDir=toAdd;
  52.             }
  53.             myArg=myArg.substr(1);
  54.             currentName="";
  55.         }
  56.         if((myArg.size()>=3) && (myArg.substr(0,3)=="../")){
  57.             if(&fs.getRootDirectory()==currentDir)
  58.                 return;
  59.             myArg=myArg.substr(3);
  60.             currentDir=currentDir->getParent();
  61.             fs.setWorkingDirectory(currentDir);
  62.         }
  63.     }//while
  64.     if(currentName.size()>0){//the myArg string is empty and we hold the name of the final directory
  65.         Directory* toAdd = new Directory(currentName,currentDir);
  66.         currentDir->addFile(toAdd);
  67.         fs.setWorkingDirectory(toAdd);
  68.     }
  69.     fs.setWorkingDirectory(currentDirToReSet);
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement