Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void RenameCommand::execute(FileSystem &fs) {
- if((verbose==2) | (verbose==3)){
- cout<<this->toString()+' '+this->getArgs()<<endl;
- }
- string source = "";
- string newName = "";
- string myArg = getArgs();
- myArg = myArg.substr(myArg.find_first_not_of(' '));
- while (myArg[0] != ' ') {
- source += myArg[0];
- myArg = myArg.substr(1);
- }
- while (myArg.size() > 0) {
- if (myArg[0] == ' ')
- myArg = myArg.substr(1);
- else {
- newName += myArg[0];
- myArg = myArg.substr(1);
- }
- }
- string workingDirName = fs.getWorkingDirectory().getName();
- Directory* currDir = &fs.getWorkingDirectory();
- if ((source[0] == '/') & (source.size() > 1)) {
- *currDir = fs.getRootDirectory();
- source = source.substr(1);
- }
- string currentName = "";
- bool found(false);
- while (!found) {
- if ((source[0] != '.') & (source[0] != '/')) {
- currentName += source[0];
- source = source.substr(1);
- }
- if ((source.size() == 0) & (currentName.size() > 0)) {
- if (currentName == workingDirName) {
- cout << "Can’t rename the working directory" << endl;
- return;
- }
- size_t counter = 0;
- size_t pos = -1;
- for (size_t i(0); i < currDir->getChildren().size(); i++) {
- if (currDir->getChildren()[i]->getName() == currentName) {
- counter++;
- pos = i;
- }
- }
- for (size_t i(0); i < currDir->getChildren().size(); i++)
- if (currDir->getChildren()[i]->getName() == newName)
- return;
- if (counter == 1) {
- currDir->getChildren()[pos]->setName(newName);
- found = true;
- return;
- } else if (counter > 1)
- return;
- else {
- cout << "No such file or directory" << endl;
- return;
- }
- }
- if ((source[0] == '/') & (currentName.size() > 0)) {
- bool stop(false);
- for (size_t i(0); (i<currDir->getChildren().size()) & (!stop); i++) {
- if (currDir->getChildren()[i]->getName() == currentName) {
- stop=true;
- currDir=(dynamic_cast<Directory*>(currDir->getChildren()[i]));
- source=source.substr(1);
- currentName="";
- }
- }
- if(!stop){
- cout << "No such file or directory" << endl;
- return;
- }
- }
- if((source.size()>=3) & (source[0]=='.') & (source[1]=='.') & (source[2]=='/')){
- currDir=currDir->getParent();
- source=source.substr(3);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement