Advertisement
Guest User

Untitled

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