Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void MkdirCommand::execute(FileSystem &fs) {
- if((verbose==2) | (verbose==3)){
- cout<<this->toString()+' '+this->getArgs()<<endl;
- }
- Directory* currentDirToReSet = &fs.getWorkingDirectory();
- Directory* currentDir = &fs.getWorkingDirectory();
- CdCommand cd(getArgs());
- if(cd.executeBool(fs)){
- cout<<"The directory already exists"<<endl;
- fs.setWorkingDirectory(currentDirToReSet);
- return;
- }
- fs.setWorkingDirectory(currentDir);
- string myArg = getArgs();
- if(myArg.size()!=0)
- myArg=myArg.substr(myArg.find_first_not_of(' '));
- if (myArg[0]=='/'){
- fs.setWorkingDirectory(&fs.getRootDirectory());
- myArg=myArg.substr(1);
- currentDir=&fs.getWorkingDirectory();
- }
- string currentName="";
- while (myArg.size()!=0){
- if((myArg[0]!='/') & (myArg[0]!='.')) {
- currentName += myArg[0];
- myArg=myArg.substr(1);
- }
- if(myArg[0]=='/'){
- bool found(false);
- for(size_t i(0);(i<currentDir->getChildren().size()) & (!found) ;i++){
- if(currentDir->getChildren()[i]->getName()==currentName ){
- if (currentDir->getChildren()[i]->isFile()){
- cout<<"The directory already exists"<<endl;
- return;
- }
- found=true;
- currentDir= dynamic_cast<Directory *>(currentDir->getChildren()[i]);
- fs.setWorkingDirectory(currentDir);
- }
- }
- if(!found){
- Directory* toAdd = new Directory(currentName,currentDir);
- currentDir->addFile(toAdd);
- fs.setWorkingDirectory(toAdd);
- currentDir=toAdd;
- }
- myArg=myArg.substr(1);
- currentName="";
- }
- if((myArg.size()>=3) && (myArg.substr(0,3)=="../")){
- if(&fs.getRootDirectory()==currentDir)
- return;
- myArg=myArg.substr(3);
- currentDir=currentDir->getParent();
- fs.setWorkingDirectory(currentDir);
- }
- }//while
- if(currentName.size()>0){//the myArg string is empty and we hold the name of the final directory
- Directory* toAdd = new Directory(currentName,currentDir);
- currentDir->addFile(toAdd);
- fs.setWorkingDirectory(toAdd);
- }
- fs.setWorkingDirectory(currentDirToReSet);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement