Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void CpCommand:: execute(FileSystem & fs) {
- if ((verbose == 2) | (verbose == 3)) {
- cout << this->toString() + ' ' + this->getArgs() << endl;
- }
- string myArg = getArgs();
- string source = myArg.substr(0,myArg.find_first_of(' '));
- string destination = myArg.substr(myArg.find_first_of(' ')+1);
- if (source =="/")
- return;
- string currentName="";
- Directory& toReturn = fs.getWorkingDirectory();
- Directory* currDir=&fs.getWorkingDirectory();
- if(source[0]=='/') {
- *currDir = fs.getRootDirectory();
- source=source.substr(1);
- }
- while(source.size()!=0){
- if((source[0]!='.')&(source[0]!='/')){
- currentName+=source[0];
- source=source.substr(1);
- }
- if((source.size()==1)&&(source[0]=='/')&(currentName.size()>0)){
- source="";
- }
- if((source.size()>1)&&(source[0]=='/')&(currentName.size()>0)){
- bool found(false);
- for(size_t i(0);(i<currDir->getChildren().size())&(!found);i++){
- if(currDir->getChildren()[i]->getName()==currentName){
- currDir= dynamic_cast<Directory*>(currDir->getChildren()[i]);
- currentName="";
- source=source.substr(1);
- found=true;
- }
- }
- if(!found){
- cout<<"No such file or directory"<<endl;
- return;
- }
- }
- if((source.size()>2)&& (source[0]=='.')&(source[1]=='.')&(source[2]=='/')){
- if((currDir)==(&fs.getRootDirectory())){
- return;
- }
- else{
- currDir=(currDir->getParent());
- source=source.substr(3);
- }
- }
- if((source.size()>=2)&& (source[0]=='.')&(source[1]=='.')){
- if((currDir)==(&fs.getRootDirectory())){
- return;
- }
- else{
- currDir=(currDir->getParent());
- source=source.substr(2);
- }
- }
- }
- bool found(false);
- for(size_t i(0);(i<currDir->getChildren().size())&(!found);i++){
- if(currDir->getChildren()[i]->getName()==currentName){
- found=true;
- if(currDir->getChildren()[i]->isFile()){
- CdCommand cd(destination);
- if(cd.executeBool(fs)){
- bool found=false;
- for (size_t i(0);i<fs.getWorkingDirectory().getChildren().size();i++)
- {
- if(fs.getWorkingDirectory().getChildren()[i]->getName()==currDir->getChildren()[i]->getName())
- found=true;
- }
- if(!found)
- fs.getWorkingDirectory().addFile(new File(currentName,currDir->getChildren()[i]->getSize()));
- }
- else{
- cout<<"No such file or directory"<<endl;
- return;
- }
- }
- else{//its a directory
- CdCommand cd(destination);
- if(cd.executeBool(fs)){
- bool found=false;
- for (size_t i(0);i<fs.getWorkingDirectory().getChildren().size();i++)
- {
- if(fs.getWorkingDirectory().getChildren()[i]->getName()==currDir->getChildren()[i]->getName())
- found=true;
- }
- if(!found)
- fs.getWorkingDirectory().addFile(new Directory(dynamic_cast<Directory&>(*currDir->getChildren()[i])));
- }
- else{
- cout<<"No such file or directory"<<endl;
- return;
- }
- }
- }
- }
- fs.setWorkingDirectory(&toReturn);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement