Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. //######### REMOVE COMMAND ##########
  2. RmCommand::RmCommand(string args):BaseCommand(args){}
  3. void RmCommand::execute(FileSystem & fs){
  4.     string path = getArgs();
  5.     // can't delete root or current directory, TODO: check if relative path correct..
  6.     if(path == fs.getWorkingDirectory().getAbsolutePath() || path == fs.getRootDirectory().getAbsolutePath()){
  7.         cout << "Can’t remove directory" << endl;
  8.         return;
  9.     }
  10.     Directory* fatherDirectory = nullptr;
  11.     int lastSlashLoc = path.find_last_of("/");
  12.     string newPath = path.substr(0, lastSlashLoc);// New path of Father directory of 'gonna-be-deleted' file/dir
  13.     string baseName = path.substr(lastSlashLoc+1); // name of 'gonna-be-deleted' file/dir
  14.  
  15.     fatherDirectory = getDirByPath(fs, newPath);
  16.     if(fatherDirectory == nullptr)
  17.         cout << "invalid path" << endl;
  18.     BaseFile* child = fatherDirectory->getChildByName(baseName);
  19.     if(child==nullptr){ cout << "file/directory not found" << endl;}
  20.     else if(child->IsFile()){ // if file
  21.         delete ((File*)child);
  22.     } else if(!child->IsFile()){// if dir
  23.         delete ((Directory*)child);
  24.     }
  25.  
  26.     fatherDirectory = nullptr;
  27.     child = nullptr;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement