Advertisement
Guest User

Untitled

a guest
Nov 25th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 37.60 KB | None | 0 0
  1. #include "../include/Commands.h"
  2. #include "../include/GlobalVariables.h"
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. BaseCommand::BaseCommand(string args):args(args) {}
  7.  
  8. string BaseCommand::getArgs() {
  9.     return args;
  10. }
  11.  
  12. BaseCommand::~BaseCommand() {}
  13.  
  14. PwdCommand::PwdCommand(string args):BaseCommand(args) {}
  15.  
  16. void PwdCommand::execute(FileSystem &fs) {
  17.     if((verbose==2) | (verbose==3)){
  18.         cout<<this->toString()+' '+this->getArgs()<<endl;
  19.     }
  20.     cout<<fs.getWorkingDirectory().getAbsolutePath()<<endl;
  21. }
  22.  
  23. string PwdCommand::toString() {
  24.     return "pwd";
  25. }
  26. PwdCommand::~PwdCommand() {}
  27.  
  28. CdCommand::CdCommand(string args):BaseCommand(args) {}
  29.  
  30.  
  31. void CdCommand::execute(FileSystem &fs) {
  32.     if((verbose==2) | (verbose==3)){
  33.         cout<<this->toString()+' '+this->getArgs()<<endl;
  34.     }
  35.  
  36.     string myArg = getArgs();
  37.     string currentName = "";
  38.  
  39.     if (myArg.size() == 0) {
  40.         cout << "The system cannot find the path specified" << endl;
  41.         return;
  42.     }
  43.  
  44.     if(myArg.size()!=0)
  45.         myArg=myArg.substr(myArg.find_first_not_of(' '));
  46.  
  47.     //the first char is '/' means we need to go to root directory
  48.     if (myArg[0] == '/') {
  49.         fs.setWorkingDirectory(&fs.getRootDirectory());
  50.         myArg = myArg.substr(1);
  51.     }
  52.  
  53.  
  54.     while (myArg.size() != 0) {
  55.         if (!(isLegalChar(myArg[0]))) {
  56.             cout << "The system cannot find the path specified" << endl;
  57.             return;
  58.         } else {
  59.             if (myArg[0] == '/') {
  60.                 Directory &currDir = fs.getWorkingDirectory();
  61.                 if ((currDir.getChildren().size() == 0) & (currentName.size() != 0)) {
  62.                     cout << "The system cannot find the path specified" << endl;
  63.                     return;
  64.                 } else {
  65.                     bool found(false);
  66.                     for (size_t i(0); (i < currDir.getChildren().size()) & (!found); i++) {
  67.                         if ((!currDir.getChildren()[i]->isFile()) && (currDir.getChildren()[i]->getName() == currentName)) {
  68.                             fs.setWorkingDirectory((Directory*)(currDir.getChildren()[i]));
  69.                             myArg = myArg.substr(1);
  70.                             currentName = "";
  71.                             found = true;
  72.                         }
  73.                     }
  74.                     if (!found) {
  75.                         cout << "The system cannot find the path specified" << endl;
  76.                         return;
  77.                     }
  78.                 }
  79.             }
  80.  
  81.             if (myArg[0] == '.') {
  82.                 if ((myArg.size()>2) && (myArg[1] == '.') & (myArg[2] == '/')) {
  83.                     if (&fs.getWorkingDirectory() == &fs.getRootDirectory()) {
  84.                         cout << "The system cannot find the path specified" << endl;
  85.                         return;
  86.                     } else {
  87.                         fs.setWorkingDirectory(fs.getWorkingDirectory().getParent());
  88.                         myArg = myArg.substr(3);
  89.                     }
  90.                 }
  91.                 else{
  92.                     if((myArg.size()>=2)& (myArg[1]=='.')){
  93.                         if (&fs.getWorkingDirectory() == &fs.getRootDirectory()) {
  94.                             cout << "The system cannot find the path specified" << endl;
  95.                             return;
  96.                         } else {
  97.                             fs.setWorkingDirectory(fs.getWorkingDirectory().getParent());
  98.                             myArg = myArg.substr(2);
  99.                         }
  100.                     }
  101.                 }
  102.             } else {
  103.                 bool toContinue(true);
  104.                 while (isLegalChar(myArg[0]) & toContinue) {
  105.                     if ((myArg[0] == '.') | (myArg[0] == '/'))
  106.                         toContinue = false;
  107.                     else {
  108.                         currentName = currentName + myArg[0];
  109.                         myArg = myArg.substr(1);
  110.                     }
  111.                 }
  112.             }
  113.         }
  114.     }//while
  115.  
  116.     //the myArg string is empty and there was no '/' at the end
  117.     if ((myArg.size() == 0) & (currentName.size() != 0)) {
  118.         Directory &currDir = fs.getWorkingDirectory();
  119.         if (currDir.getChildren().size() == 0) {
  120.             cout << "The system cannot find the path specified" << endl;
  121.             return;
  122.         } else {
  123.             bool found(false);
  124.             for (size_t i(0); (i < currDir.getChildren().size()) & (!found); i++) {
  125.                 if ((!currDir.getChildren()[i]->isFile()) && (currDir.getChildren()[i]->getName() == currentName)) {
  126.                     fs.setWorkingDirectory((Directory*)(currDir.getChildren()[i]));
  127.                     found = true;
  128.                 }
  129.             }
  130.             if (!found) {
  131.                 cout << "The system cannot find the path specified" << endl;
  132.                 return;
  133.             }
  134.         }
  135.     }
  136. }
  137.  
  138. bool isLegalChar(char a){
  139.     if(((a>='a') & (a<='z'))|((a>='A') & (a<='Z'))|((a>='0') & (a<='9'))|(a=='.')|(a=='/')|(a==' '))
  140.         return true;
  141.     else return false;
  142. }
  143.  
  144. string CdCommand::toString() {
  145.     return "cd";
  146. }
  147.  
  148. CdCommand::~CdCommand() {}
  149.  
  150. bool CdCommand::executeBool(FileSystem &fs) {
  151.     string myArg = getArgs();
  152.     string currentName = "";
  153.  
  154.     if (myArg.size() == 0) {
  155.         return false;
  156.     }
  157.  
  158.     if(myArg.size()!=0)
  159.         myArg=myArg.substr(myArg.find_first_not_of(' '));
  160.  
  161.     //the first char is '/' means we need to go to root directory
  162.     if (myArg[0] == '/') {
  163.         fs.setWorkingDirectory(&fs.getRootDirectory());
  164.         myArg = myArg.substr(1);
  165.     }
  166.  
  167.  
  168.     while (myArg.size() != 0) {
  169.         if (!(isLegalChar(myArg[0]))) {
  170.             return false;
  171.         } else {
  172.             if (myArg[0] == '/') {
  173.                 Directory &currDir = fs.getWorkingDirectory();
  174.                 if ((currDir.getChildren().size() == 0) & (currentName.size() != 0)) {
  175.                     return false;
  176.                 } else {
  177.                     bool found(false);
  178.                     for (size_t i(0); (i < currDir.getChildren().size()) & (!found); i++) {
  179.                         if ((!currDir.getChildren()[i]->isFile()) && (currDir.getChildren()[i]->getName() == currentName)) {
  180.                             fs.setWorkingDirectory((Directory*)(currDir.getChildren()[i]));
  181.                             myArg = myArg.substr(1);
  182.                             currentName = "";
  183.                             found = true;
  184.                         }
  185.                     }
  186.                     if (!found) {
  187.                         return false;
  188.                     }
  189.                 }
  190.             }
  191.  
  192.             if (myArg[0] == '.') {
  193.                 if ((myArg.size()>2)&&(myArg[1] == '.') & (myArg[2] == '/')) {
  194.                     if (&fs.getWorkingDirectory() == &fs.getRootDirectory()) {
  195.                         return false;
  196.                     } else {
  197.                         fs.setWorkingDirectory(fs.getWorkingDirectory().getParent());
  198.                         myArg = myArg.substr(3);
  199.                     }
  200.                 }
  201.                 else{
  202.                     if ((myArg.size()>=2)&&(myArg[1] == '.')) {
  203.                         if (&fs.getWorkingDirectory() == &fs.getRootDirectory()) {
  204.                             return false;
  205.                         } else {
  206.                             fs.setWorkingDirectory(fs.getWorkingDirectory().getParent());
  207.                             myArg = myArg.substr(2);
  208.                         }
  209.                     }
  210.                 }
  211.             } else {
  212.                 bool toContinue(true);
  213.                 while ((isLegalChar(myArg[0])) & (toContinue)) {
  214.                     if ((myArg[0] == '.') | (myArg[0] == '/'))
  215.                         toContinue = false;
  216.                     else {
  217.                         currentName = currentName + myArg[0];
  218.                         myArg = myArg.substr(1);
  219.                     }
  220.                 }
  221.             }
  222.         }
  223.     }//while
  224.  
  225.     //the myArg string is empty and there was no '/' at the end
  226.     if ((myArg.size() == 0) & (currentName.size() != 0)) {
  227.         Directory &currDir = fs.getWorkingDirectory();
  228.         if (currDir.getChildren().size() == 0) {
  229.             return false;
  230.         } else {
  231.             bool found(false);
  232.             for (size_t i(0); (i < currDir.getChildren().size()) & (!found); i++) {
  233.                 if ((!currDir.getChildren()[i]->isFile()) && (currDir.getChildren()[i]->getName() == currentName)) {
  234.                     fs.setWorkingDirectory((Directory*)(currDir.getChildren()[i]));
  235.                     found = true;
  236.                 }
  237.             }
  238.             if (!found) {
  239.                 return false;
  240.             }
  241.         }
  242.     }
  243.     return true;
  244. }
  245.  
  246.  
  247. LsCommand::LsCommand(string args):BaseCommand(args) {}
  248.  
  249. void LsCommand::execute(FileSystem &fs) {
  250.     if((verbose==2) | (verbose==3)){
  251.         cout<<this->toString()+' '+this->getArgs()<<endl;
  252.     }
  253.  
  254.     string myArg=getArgs();
  255.  
  256.  
  257.     if (myArg.size()==0){
  258.         Directory& currDir = fs.getWorkingDirectory();
  259.         string toPrint;
  260.         currDir.sortByName();
  261.         for(size_t i(0);i<currDir.getChildren().size();i++){
  262.             toPrint="";
  263.  
  264.             if(currDir.getChildren()[i]->isFile())
  265.                 toPrint +="FILE\t";
  266.             else toPrint +="DIR\t";
  267.  
  268.             toPrint += currDir.getChildren()[i]->getName()+"\t";
  269.             /*toPrint += currDir.getChildren()[i]->getSize();*/
  270.  
  271.             cout<<toPrint;
  272.             cout<<(currDir.getChildren()[i]->getSize())<<endl;
  273.         }
  274.     }
  275.  
  276.     else if((myArg[0]!='[') & (myArg[0]!='-')) {//<path>
  277.         Directory &currentDirToReSet = fs.getWorkingDirectory();
  278.         CdCommand cd(myArg);
  279.         bool didCd=cd.executeBool(fs);
  280.         if(didCd) {
  281.             Directory &currDir = fs.getWorkingDirectory();
  282.             string toPrint;
  283.             currDir.sortByName();
  284.             for (size_t i(0); i < currDir.getChildren().size(); i++) {
  285.                 toPrint = "";
  286.  
  287.                 if (currDir.getChildren()[i]->isFile())
  288.                     toPrint += "FILE\t";
  289.                 else toPrint += "DIR\t";
  290.  
  291.                 toPrint += currDir.getChildren()[i]->getName() + "\t";
  292.                 /*toPrint += currDir.getChildren()[i]->getSize();*/
  293.  
  294.                 cout << toPrint;
  295.                 cout<<(currDir.getChildren()[i]->getSize())<<endl;
  296.             }
  297.             fs.setWorkingDirectory(&currentDirToReSet);
  298.         }
  299.         else{
  300.             cout << "The system cannot find the path specified" << endl;
  301.             return;
  302.         }
  303.     }
  304.  
  305.     else if(((myArg.size()>=4) && (myArg.substr(0,4)=="[-s]")) | ((myArg.size()>=2) && (myArg.substr(0,2)=="-s"))){
  306.         if(myArg.substr(0,4)=="[-s]")
  307.             myArg=myArg.substr(4);
  308.         else myArg=myArg.substr(2);
  309.         if(myArg.size()==0){
  310.             Directory& currDir = fs.getWorkingDirectory();
  311.             string toPrint;
  312.             currDir.sortBySize();
  313.             for(size_t i(0);i<currDir.getChildren().size();i++){
  314.                 toPrint="";
  315.  
  316.                 if(currDir.getChildren()[i]->isFile())
  317.                     toPrint +="FILE\t";
  318.                 else toPrint +="DIR\t";
  319.  
  320.                 toPrint += currDir.getChildren()[i]->getName()+"\t";
  321.                 /*toPrint += currDir.getChildren()[i]->getSize();*/
  322.  
  323.                 cout<<toPrint;
  324.                 cout<<(currDir.getChildren()[i]->getSize())<<endl;
  325.             }
  326.         }
  327.  
  328.         else {//[-s]<path>
  329.             Directory &currentDirToReSet = fs.getWorkingDirectory();
  330.             CdCommand cd(myArg);
  331.             bool didCd = cd.executeBool(fs);
  332.             if (didCd) {
  333.                 Directory &currDir = fs.getWorkingDirectory();
  334.                 string toPrint;
  335.                 currDir.sortBySize();
  336.                 for (size_t i(0); i < currDir.getChildren().size(); i++) {
  337.                     toPrint = "";
  338.  
  339.                     if (currDir.getChildren()[i]->isFile())
  340.                         toPrint += "FILE\t";
  341.                     else toPrint += "DIR\t";
  342.  
  343.                     toPrint += currDir.getChildren()[i]->getName() + "\t";
  344.                     /*toPrint += currDir.getChildren()[i]->getSize();*/
  345.  
  346.                     cout << toPrint;
  347.                     cout<<(currDir.getChildren()[i]->getSize())<<endl;
  348.                 }
  349.                 fs.setWorkingDirectory(&currentDirToReSet);
  350.             }
  351.             else{
  352.                 cout << "The system cannot find the path specified" << endl;
  353.                 return;
  354.             }
  355.         }
  356.     }
  357. }
  358.  
  359. string LsCommand::toString() {
  360.     return "ls";
  361. }
  362.  
  363. LsCommand::~LsCommand() {}
  364.  
  365. MkdirCommand::MkdirCommand(string args):BaseCommand(args) {}
  366.  
  367. void MkdirCommand::execute(FileSystem &fs) {
  368.     if((verbose==2) | (verbose==3)){
  369.         cout<<this->toString()+' '+this->getArgs()<<endl;
  370.     }
  371.  
  372.     Directory* currentDirToReSet = &fs.getWorkingDirectory();
  373.     Directory* currentDir =  &fs.getWorkingDirectory();
  374.     CdCommand cd(getArgs());
  375.     if(cd.executeBool(fs)){
  376.         cout<<"The directory already exists"<<endl;
  377.         fs.setWorkingDirectory(currentDirToReSet);
  378.         return;
  379.     }
  380.     fs.setWorkingDirectory(currentDir);
  381.  
  382.     string myArg = getArgs();
  383.  
  384.     if(myArg.size()!=0)
  385.         myArg=myArg.substr(myArg.find_first_not_of(' '));
  386.  
  387.     if (myArg[0]=='/'){
  388.         fs.setWorkingDirectory(&fs.getRootDirectory());
  389.         myArg=myArg.substr(1);
  390.         currentDir=&fs.getWorkingDirectory();
  391.     }
  392.  
  393.     string currentName="";
  394.  
  395.     while (myArg.size()!=0){
  396.         if((myArg[0]!='/') & (myArg[0]!='.')) {
  397.             currentName += myArg[0];
  398.             myArg=myArg.substr(1);
  399.         }
  400.         if(myArg[0]=='/'){
  401.             bool found(false);
  402.             for(size_t i(0);(i<currentDir->getChildren().size()) & (!found) ;i++){
  403.                 if(currentDir->getChildren()[i]->getName()==currentName ){
  404.                     if (currentDir->getChildren()[i]->isFile()){
  405.                         cout<<"The directory already exists"<<endl;
  406.                         return;
  407.                     }
  408.                     found=true;
  409.                     currentDir= dynamic_cast<Directory *>(currentDir->getChildren()[i]);
  410.                     fs.setWorkingDirectory(currentDir);
  411.                 }
  412.             }
  413.             if(!found){
  414.                 Directory* toAdd = new Directory(currentName,currentDir);
  415.                 currentDir->addFile(toAdd);
  416.                 fs.setWorkingDirectory(toAdd);
  417.                 currentDir=toAdd;
  418.             }
  419.             myArg=myArg.substr(1);
  420.             currentName="";
  421.         }
  422.         if((myArg.size()>=3) && (myArg.substr(0,3)=="../")){
  423.             if(&fs.getRootDirectory()==currentDir)
  424.                 return;
  425.             myArg=myArg.substr(3);
  426.             currentDir=currentDir->getParent();
  427.             fs.setWorkingDirectory(currentDir);
  428.         }
  429.     }//while
  430.     if(currentName.size()>0){//the myArg string is empty and we hold the name of the final directory
  431.         Directory* toAdd = new Directory(currentName,currentDir);
  432.         currentDir->addFile(toAdd);
  433.         fs.setWorkingDirectory(toAdd);
  434.     }
  435.     fs.setWorkingDirectory(currentDirToReSet);
  436. }
  437.  
  438. string MkdirCommand::toString() {
  439.     return "mkdir";
  440. }
  441.  
  442. MkdirCommand::~MkdirCommand() {}
  443.  
  444. /***********oren's code*************/
  445.  MkfileCommand::MkfileCommand(string args):BaseCommand(args){}
  446.  
  447. void MkfileCommand::execute(FileSystem & fs) {
  448.     if((verbose==2) | (verbose==3)){
  449.         cout<<this->toString()+' '+this->getArgs()<<endl;
  450.     }
  451.  
  452.     string myArgs = getArgs();
  453.     string currentName = "";
  454.     Directory& toReturn=fs.getWorkingDirectory();
  455.  
  456.  
  457.     size_t i(0);
  458.     if (myArgs[0] == '/') {
  459.         fs.setWorkingDirectory( &fs.getRootDirectory());
  460.         i = 1;
  461.     } else i = 0;
  462.     for (size_t n=i; n < myArgs.size(); n = n + 1) {
  463.         if (myArgs[n] != ' ')
  464.         {
  465.             if ((myArgs[n] < 'a') & (myArgs[n] > 'z') & (myArgs[n] < 'A') & (myArgs[n] > 'Z') & (myArgs[n] < '0') &
  466.                 (myArgs[n] > '9') & (myArgs[n] != '/') & (myArgs[n] != '.')) {
  467.                 cout << "The system cannot find the path specified" << endl;
  468.                 return;
  469.             } else {
  470.                 if (myArgs[n] == '.') {
  471.                     if (currentName != "") {
  472.                         cout << "The system cannot find the path specified" << endl;
  473.                         return;
  474.                     }
  475.                     if (myArgs.size() < n + 2) {
  476.                         cout << "The system cannot find the path specified" << endl;
  477.                         return;
  478.                     }
  479.                     if ((myArgs[n + 1] != '.') | (myArgs[n + 2] != '/')) {
  480.                         cout << "The system cannot find the path specified" << endl;
  481.                         return;
  482.                     }
  483.                     if (fs.getWorkingDirectory().getAbsolutePath() == fs.getRootDirectory().getAbsolutePath()) {
  484.                         cout << "The system cannot find the path specified" << endl;
  485.                         return;
  486.                     }
  487.                     n = n + 2;
  488.                     fs.setWorkingDirectory(fs.getWorkingDirectory().getParent());
  489.                 } else {
  490.                     if (myArgs[n] == '/') {
  491.  
  492.                         if ((myArgs.size() > n + 1) && (myArgs[n + 1] == '/')) {
  493.                             cout << "The system cannot find the path specified" << endl;
  494.                             return;
  495.                         }
  496.                         bool found = false;
  497.                         for (size_t j = 0; (j < fs.getWorkingDirectory().getChildren().size()) & (!found); j = j + 1) {
  498.                             if (fs.getWorkingDirectory().getChildren()[j]->getName() ==
  499.                                 currentName) {/////////////////////////// check if file
  500.                                 fs.setWorkingDirectory(
  501.                                         dynamic_cast<Directory *>(fs.getWorkingDirectory().getChildren()[j]));
  502.                                 found = true;
  503.                                 currentName = "";
  504.                             }
  505.  
  506.                         }
  507.                         if (!found) {
  508.                             cout << "The system cannot find the path specified" << endl;
  509.                             return;
  510.                         }
  511.  
  512.                         if ((!found)&(fs.getWorkingDirectory().getChildren().size()==0)) {
  513.                             cout << "The system cannot find the path specified" << endl;
  514.                             return;}
  515.                     } else {
  516.                         currentName = currentName + myArgs[n];
  517.                     }
  518.  
  519.                 }
  520.             }
  521.         } else{
  522.             n=myArgs.size();
  523.         }
  524.     }
  525.     myArgs=myArgs.substr(myArgs.find_first_of(' ')+1);
  526.     int num(0);
  527.     if((myArgs.size()>0)&&(!(myArgs==currentName))&((myArgs[0]>='0')&(myArgs[0]<='9')))
  528.         num = stoi(myArgs);
  529.  
  530.     if (currentName.size()>0)
  531.     {
  532.         fs.getWorkingDirectory().addFile(new File(currentName,num));
  533.     }
  534.     else
  535.     {
  536.         cout << "The system cannot find the path specified" << endl;
  537.     }
  538.     fs.setWorkingDirectory(&toReturn);
  539.     return;
  540. }
  541.  
  542.  
  543. string MkfileCommand:: toString(){
  544.     return "mkfile";
  545. }
  546.  
  547. MkfileCommand::~MkfileCommand() {}
  548.  
  549. CpCommand::CpCommand(string args):BaseCommand(args){}
  550.  
  551. void CpCommand:: execute(FileSystem & fs) {
  552.     if ((verbose == 2) | (verbose == 3)) {
  553.         cout << this->toString() + ' ' + this->getArgs() << endl;
  554.     }
  555.  
  556.     string myArg = getArgs();
  557.     string source = myArg.substr(0,myArg.find_first_of(' '));
  558.     string destination = myArg.substr(myArg.find_first_of(' ')+1);
  559.  
  560.     if (source =="/")
  561.         return;
  562.  
  563.     string currentName="";
  564.     Directory& toReturn = fs.getWorkingDirectory();
  565.     Directory* currDir=&fs.getWorkingDirectory();
  566.     if(source[0]=='/') {
  567.         *currDir = fs.getRootDirectory();
  568.         source=source.substr(1);
  569.     }
  570.  
  571.     while(source.size()!=0){
  572.         if((source[0]!='.')&(source[0]!='/')){
  573.             currentName+=source[0];
  574.             source=source.substr(1);
  575.         }
  576.         if((source.size()==1)&&(source[0]=='/')&(currentName.size()>0)){
  577.             source="";
  578.         }
  579.  
  580.         if((source.size()>1)&&(source[0]=='/')&(currentName.size()>0)){
  581.             bool found(false);
  582.             for(size_t i(0);(i<currDir->getChildren().size())&(!found);i++){
  583.                 if(currDir->getChildren()[i]->getName()==currentName){
  584.                     currDir= dynamic_cast<Directory*>(currDir->getChildren()[i]);
  585.                     currentName="";
  586.                     source=source.substr(1);
  587.                     found=true;
  588.                 }
  589.             }
  590.             if(!found){
  591.                 cout<<"No such file or directory"<<endl;
  592.                 return;
  593.             }
  594.         }
  595.         if((source.size()>2)&& (source[0]=='.')&(source[1]=='.')&(source[2]=='/')){
  596.             if((currDir)==(&fs.getRootDirectory())){
  597.                 return;
  598.             }
  599.             else{
  600.                 currDir=(currDir->getParent());
  601.                 source=source.substr(3);
  602.             }
  603.         }
  604.         if((source.size()>=2)&& (source[0]=='.')&(source[1]=='.')){
  605.             if((currDir)==(&fs.getRootDirectory())){
  606.                 return;
  607.             }
  608.             else{
  609.                 currDir=(currDir->getParent());
  610.                 source=source.substr(2);
  611.             }
  612.         }
  613.  
  614.     }
  615.     bool found(false);
  616.     for(size_t i(0);(i<currDir->getChildren().size())&(!found);i++){
  617.         if(currDir->getChildren()[i]->getName()==currentName){
  618.             found=true;
  619.             if(currDir->getChildren()[i]->isFile()){
  620.                 CdCommand cd(destination);
  621.                 if(cd.executeBool(fs)){
  622.                     bool found=false;
  623.                     for (size_t i(0);i<fs.getWorkingDirectory().getChildren().size();i++)
  624.                     {
  625.                         if(fs.getWorkingDirectory().getChildren()[i]->getName()==currDir->getChildren()[i]->getName())
  626.                             found=true;
  627.                     }
  628.                     if(!found)
  629.                         fs.getWorkingDirectory().addFile(new File(currentName,currDir->getChildren()[i]->getSize()));
  630.                 }
  631.                 else{
  632.                     cout<<"No such file or directory"<<endl;
  633.                     return;
  634.                 }
  635.             }
  636.             else{//its a directory
  637.  
  638.                 CdCommand cd(destination);
  639.                 if(cd.executeBool(fs)){
  640.                     bool found=false;
  641.                     for (size_t i(0);i<fs.getWorkingDirectory().getChildren().size();i++)
  642.                     {
  643.                         if(fs.getWorkingDirectory().getChildren()[i]->getName()==currDir->getChildren()[i]->getName())
  644.                             found=true;
  645.                     }
  646.                     if(!found)
  647.                         fs.getWorkingDirectory().addFile(new Directory(dynamic_cast<Directory&>(*currDir->getChildren()[i])));
  648.                 }
  649.                 else{
  650.                     cout<<"No such file or directory"<<endl;
  651.                     return;
  652.                 }
  653.             }
  654.         }
  655.     }
  656.  
  657.     fs.setWorkingDirectory(&toReturn);
  658.  
  659. }
  660.  
  661.  
  662. string CpCommand:: toString(){
  663.     return "cp";
  664. }
  665.  
  666. CpCommand::~CpCommand() {}
  667. /**********************end of oren's code*********/
  668.  
  669. MvCommand::MvCommand(string args):BaseCommand(args){}
  670.  
  671. void MvCommand::execute(FileSystem & fs) {
  672.     if ((verbose == 2) | (verbose == 3)) {
  673.         cout << this->toString() + ' ' + this->getArgs() << endl;
  674.     }
  675.  
  676.     string myArg = getArgs();
  677.     string source = myArg.substr(0,myArg.find_first_of(' '));
  678.     string destination = myArg.substr(myArg.find_first_of(' ')+1);
  679.  
  680.  
  681.     if (source==destination)
  682.         return;
  683.     if (source.size()<=destination.size()) {
  684.         string c="";
  685.         for (size_t i(0); i < destination.size(); i++) {
  686.             c += destination[i];
  687.             if (source==c)
  688.                 return;
  689.         }
  690.     }
  691.  
  692.  
  693.     if (source =="/")
  694.     {
  695.         cout<<"Can’t move directory"<<endl;
  696.         return;
  697.     }
  698.  
  699.  
  700.     string currentName="";
  701.     Directory& toReturn = fs.getWorkingDirectory();
  702.     Directory* currDir=&fs.getWorkingDirectory();
  703.     if(source[0]=='/') {
  704.         *currDir = fs.getRootDirectory();
  705.         source=source.substr(1);
  706.     }
  707.  
  708.  
  709.     while(source.size()!=0){
  710.         if((source[0]!='.')&(source[0]!='/')){
  711.             currentName+=source[0];
  712.             source=source.substr(1);
  713.         }
  714.         if((source.size()==1)&&(source[0]=='/')&(currentName.size()>0)){
  715.             source="";
  716.         }
  717.  
  718.         if((source.size()>1)&&(source[0]=='/')&(currentName.size()>0)){
  719.             bool found(false);
  720.             for(size_t i(0);(i<currDir->getChildren().size())&(!found);i++){
  721.                 if(currDir->getChildren()[i]->getName()==currentName){
  722.                     currDir= dynamic_cast<Directory*>(currDir->getChildren()[i]);
  723.                     currentName="";
  724.                     source=source.substr(1);
  725.                     found=true;
  726.                 }
  727.             }
  728.             if(!found){
  729.                 cout<<"No such file or directory"<<endl;
  730.                 return;
  731.             }
  732.         }
  733.         if((source.size()>2)&& (source[0]=='.')&(source[1]=='.')&(source[2]=='/')){
  734.             if((currDir)==(&fs.getRootDirectory())){
  735.                 return;
  736.             }
  737.             else{
  738.                 currDir=(currDir->getParent());
  739.                 source=source.substr(3);
  740.             }
  741.         }
  742.         if((source.size()>=2)&& (source[0]=='.')&(source[1]=='.')){
  743.             if((currDir)==(&fs.getRootDirectory())){
  744.                 return;
  745.             }
  746.             else{
  747.                 currDir=(currDir->getParent());
  748.                 source=source.substr(2);
  749.             }
  750.         }
  751.  
  752.     }
  753.  
  754.     bool found(false);
  755.     for(size_t i(0);(i<currDir->getChildren().size())&(!found);i++){
  756.         if(currDir->getChildren()[i]->getName()==currentName){
  757.             found=true;
  758.             if(currDir->getChildren()[i]->isFile()){
  759.                 CdCommand cd(destination);
  760.                 if(cd.executeBool(fs)){
  761.                     fs.getWorkingDirectory().addFile(new File(currentName,currDir->getChildren()[i]->getSize()));
  762.                     currDir->removeFile(currDir->getChildren()[i]->getName());
  763.                 }
  764.                 else{
  765.                     cout<<"No such file or directory"<<endl;
  766.                     return;
  767.                 }
  768.             }
  769.             else{//its a directory
  770.  
  771.                 CdCommand cd(destination);
  772.                 if(cd.executeBool(fs)){
  773.                     (dynamic_cast<Directory*>(currDir->getChildren()[i]))->setParent(&fs.getWorkingDirectory());
  774.                 }
  775.                 else{
  776.                     cout<<"No such file or directory"<<endl;
  777.                     return;
  778.                 }
  779.             }
  780.         }
  781.     }
  782.  
  783.     fs.setWorkingDirectory(&toReturn);
  784.  
  785. }
  786.  
  787. string MvCommand:: toString(){
  788.     return "mv";
  789. }
  790.  
  791. MvCommand::~MvCommand() {}
  792.  
  793. RenameCommand::RenameCommand(string args):BaseCommand(args) {}
  794.  
  795. void RenameCommand::execute(FileSystem &fs) {
  796.     if((verbose==2) | (verbose==3)){
  797.         cout<<this->toString()+' '+this->getArgs()<<endl;
  798.     }
  799.  
  800.     string source = "";
  801.     string newName = "";
  802.     string myArg = getArgs();
  803.     myArg = myArg.substr(myArg.find_first_not_of(' '));
  804.  
  805.     while (myArg[0] != ' ') {
  806.         source += myArg[0];
  807.         myArg = myArg.substr(1);
  808.     }
  809.     if (myArg[0] == ' ')
  810.         myArg = myArg.substr(1);
  811.  
  812.     while (myArg.size() > 0) {
  813.         newName += myArg[0];
  814.         myArg = myArg.substr(1);
  815.     }
  816.  
  817.     string workingDirName = fs.getWorkingDirectory().getName();
  818.     Directory* currDir = &fs.getWorkingDirectory();
  819.     if ((source[0] == '/') & (source.size() > 1)) {
  820.         *currDir = fs.getRootDirectory();
  821.         source = source.substr(1);
  822.     }
  823.     string origin=fs.getWorkingDirectory().getAbsolutePath();
  824.  
  825.     string currentName = "";
  826.     bool found(false);
  827.  
  828.     while (!found) {
  829.         if ((source[0] != '.') & (source[0] != '/')) {
  830.             currentName += source[0];
  831.             source = source.substr(1);
  832.         }
  833.  
  834.         if ((source.size() == 0) & (currentName.size() > 0)) {
  835.             if (currentName == workingDirName) {
  836.                 cout << "Can’t rename the working directory" << endl;
  837.                 return;
  838.             }
  839.             size_t counter = 0;
  840.             size_t pos = -1;
  841.             for (size_t i(0); i < currDir->getChildren().size(); i++) {
  842.                 if (currDir->getChildren()[i]->getName() == currentName) {
  843.                     counter++;
  844.                     pos = i;
  845.                 }
  846.             }
  847.  
  848.             for (size_t i(0); i < currDir->getChildren().size(); i++){
  849.                 if (currDir->getChildren()[i]->getName() == newName)
  850.                     return;
  851.             }
  852.  
  853.             if (counter == 1) {
  854.                 if (currDir->getAbsolutePath()==origin)
  855.                 {cout<<"Can’t rename the working directory"<<endl;
  856.                     return;
  857.                 }
  858.                 currDir->getChildren()[pos]->setName(newName);
  859.                 found = true;
  860.                 return;
  861.  
  862.             } else if (counter > 1)
  863.                 return;
  864.  
  865.             else {
  866.                 cout << "No such file or directory" << endl;
  867.                 return;
  868.             }
  869.         }
  870.  
  871.         if ((source[0] == '/') & (currentName.size() > 0)) {
  872.             bool stop(false);
  873.             for (size_t i(0); (i<currDir->getChildren().size()) & (!stop); i++) {
  874.                 if (currDir->getChildren()[i]->getName() == currentName) {
  875.                     if(currDir->getChildren()[i]->isFile()){
  876.                         cout<<"No such file or directory"<<endl;
  877.                         return;}
  878.                     stop=true;
  879.                     currDir=(dynamic_cast<Directory*>(currDir->getChildren()[i]));
  880.                     source=source.substr(1);
  881.                     currentName="";
  882.                 }
  883.             }
  884.             if(!stop){
  885.                 cout << "No such file or directory" << endl;
  886.                 return;
  887.             }
  888.         }
  889.  
  890.         if((source.size()>=3) & (source[0]=='.') & (source[1]=='.') & (source[2]=='/')){
  891.             if(currDir->getAbsolutePath()==fs.getRootDirectory().getAbsolutePath()){
  892.                 cout<<"No such file or directory"<<endl;
  893.                 return;
  894.             }
  895.             currDir=currDir->getParent();
  896.             source=source.substr(3);
  897.         }
  898.     }
  899. }
  900.  
  901. string RenameCommand::toString() {
  902.     return "rename";
  903. }
  904.  
  905. RenameCommand::~RenameCommand() {}
  906.  
  907. RmCommand::RmCommand(string args):BaseCommand(args) {}
  908.  
  909. void RmCommand::execute(FileSystem &fs) {
  910.     if((verbose==2) | (verbose==3)){
  911.         cout<<this->toString()+' '+this->getArgs()<<endl;
  912.     }
  913.  
  914.     string path=getArgs();
  915.     Directory& workingDir = fs.getWorkingDirectory();
  916.  
  917.     if((path.size()==1)&&(path[0]=='/')){
  918.         cout<<"Can't remove directory"<<endl;
  919.         return;
  920.     }
  921.  
  922.     if((path[0]=='/') & (path.size()>1)){
  923.         fs.setWorkingDirectory(&fs.getRootDirectory());
  924.         path=path.substr(1);
  925.     }
  926.  
  927.     Directory* currDir=&fs.getWorkingDirectory();
  928.  
  929.     bool found(false);
  930.     string currentName="";
  931.     while(!found){
  932.         if((path[0]!='/') & (path[0]!='.')){
  933.             currentName+=path[0];
  934.             path=path.substr(1);
  935.         }
  936.  
  937.         if((path[0]=='/')& (path.size()>1) & (currentName.size()>0)){
  938.             bool stop(false);
  939.             for (size_t i(0);(i< currDir->getChildren().size()) & (!stop); i++) {
  940.                 if (currDir->getChildren()[i]->getName() == currentName) {
  941.                     currDir = ((dynamic_cast<Directory*>(currDir->getChildren()[i])));
  942.                     path=path.substr(1);
  943.                     currentName="";
  944.                     stop=true;
  945.                 }
  946.         }
  947.             if(!stop){
  948.                 cout<<"No such file or directory"<<endl;
  949.                 return;
  950.             }
  951.         }
  952.  
  953.         if((path.size()==0) & (currentName.size()>0)){
  954.             bool stop(false);
  955.             for (size_t i=0;(i<currDir->getChildren().size()) &(!stop);i++) {
  956.                 if((currDir->getChildren()[i])->getName() == currentName){
  957.                     if(currDir->getChildren()[i] != &fs.getWorkingDirectory()) {
  958.                         (currDir->removeFile(currDir->getChildren()[i]));
  959.                         stop = true;
  960.                         found=true;
  961.                     }
  962.                     else{
  963.                         cout<<"Can't remove directory"<<endl;
  964.                         return;
  965.                     }
  966.                 }
  967.             }
  968.             if(!stop){
  969.                 cout<<"No such file or directory"<<endl;
  970.                 return;
  971.             }
  972.             fs.setWorkingDirectory(&workingDir);
  973.         }
  974.  
  975.         if ((path.size()>=3) && (path[0]=='.')&(path[1]=='.')&(path[2]=='/')){
  976.             if(currDir == &fs.getRootDirectory()){
  977.                 return;
  978.             }
  979.             currDir=currDir->getParent();
  980.             path=path.substr(3);
  981.         }
  982.         else if((path.size()>=2)&&(path[0]=='.')&(path[1]=='.')){
  983.             if(currDir == &fs.getRootDirectory()){
  984.                 return;
  985.             }
  986.             currDir=currDir->getParent();
  987.             path=path.substr(2);
  988.         }
  989.     }
  990. }
  991.  
  992. string RmCommand::toString() {
  993.     return "rm";
  994. }
  995.  
  996. RmCommand::~RmCommand() {}
  997.  
  998. HistoryCommand::HistoryCommand(string args, const vector<BaseCommand *> & history):BaseCommand(args),history(history){}
  999.  
  1000. void HistoryCommand::execute(FileSystem &fs) {
  1001.     if((verbose==2) | (verbose==3)){
  1002.         cout<<this->toString()+' '+this->getArgs()<<endl;
  1003.     }
  1004.  
  1005.     string currentCommandToPrint="";
  1006.     int counter(0);
  1007.     for (auto i=history.begin() ; i!= history.end() ; i++){
  1008.         cout<<counter;
  1009.         if(i.operator*()->toString() != "error")
  1010.             currentCommandToPrint+="\t"+((i.operator*())->toString());
  1011.         string args = i.operator*()->getArgs();
  1012.  
  1013.         if(i.operator*()->getArgs().length()>0) {
  1014.             if(i.operator*()->toString() == "error"){}
  1015.                 currentCommandToPrint+="\t";
  1016.             currentCommandToPrint += ' ' + ((i.operator*())->getArgs());
  1017.         }
  1018.         cout<<currentCommandToPrint<<endl;
  1019.  
  1020.         currentCommandToPrint="";
  1021.         counter++;
  1022.     }
  1023.     return;
  1024. }
  1025.  
  1026. string HistoryCommand::toString() {
  1027.     return "history";
  1028. }
  1029.  
  1030. HistoryCommand::~HistoryCommand() {}
  1031.  
  1032. VerboseCommand::VerboseCommand(string args):BaseCommand(args){}
  1033.  
  1034. void VerboseCommand::execute(FileSystem &fs) {
  1035.     string myArg=getArgs();
  1036.     myArg=myArg.substr(myArg.find_first_not_of(' '));
  1037.  
  1038.     if(myArg == "0")
  1039.         verbose=0;
  1040.     else if(myArg== "1")
  1041.         verbose=1;
  1042.     else if(myArg=="2")
  1043.         verbose=2;
  1044.     else if (myArg=="3")
  1045.         verbose=3;
  1046.     else{
  1047.         cout<<"Wrong verbose input"<<endl;
  1048.         return;
  1049.     }
  1050. }
  1051.  
  1052. string VerboseCommand::toString() {
  1053.     return "verbose";
  1054. }
  1055.  
  1056. VerboseCommand::~VerboseCommand() {}
  1057.  
  1058. ErrorCommand::ErrorCommand(string args):BaseCommand(args) {
  1059.  
  1060. }
  1061.  
  1062. void ErrorCommand::execute(FileSystem & fs) {
  1063.     if((verbose==2) | (verbose==3)){
  1064.         cout<<this->getArgs()<<endl;
  1065.     }
  1066.     string toPr=getArgs().substr(0,getArgs().find_first_of(' '));
  1067.     cout<<toPr+": Unknown command"<<endl;
  1068. }
  1069.  
  1070. string ErrorCommand::toString() {
  1071.     return "error";
  1072. }
  1073.  
  1074. ErrorCommand::~ErrorCommand() {}
  1075.  
  1076. ExecCommand::ExecCommand(string args, const vector<BaseCommand *> & history):BaseCommand(args),history(history) {
  1077.  
  1078. }
  1079.  
  1080. void ExecCommand::execute(FileSystem & fs)
  1081. {
  1082.     if ((verbose==2)|(verbose==3)) {
  1083.         cout << this->toString() + ' ' + this->getArgs() << endl;
  1084.     }
  1085.     string myArg=getArgs();
  1086.     int c = stoi(myArg);
  1087.  
  1088.     if ((size_t)c>history.size())
  1089.         cout<<"Command not found"<<endl;
  1090.  
  1091.     else
  1092.     {
  1093.         string st=history[c]->getArgs();
  1094.         string commander=history[c]->toString();
  1095.         if (commander=="pwd")
  1096.         {
  1097.             PwdCommand pwd(st);
  1098.             pwd.execute(fs);
  1099.         }else
  1100.         if (commander=="cd")
  1101.         {
  1102.             CdCommand cd(st);
  1103.             cd.execute(fs);
  1104.         }else
  1105.         if (commander=="ls")
  1106.         {
  1107.             LsCommand ls(st);
  1108.             ls.execute(fs);
  1109.         }else
  1110.         if (commander=="mkdir")
  1111.         {
  1112.             MkdirCommand mkdir(st);
  1113.             mkdir.execute(fs);
  1114.         }else
  1115.         if (commander=="mkfile")
  1116.         {
  1117.             MkfileCommand mkfile(st);
  1118.             mkfile.execute(fs);
  1119.         }else
  1120.         if (commander=="cp")
  1121.         {
  1122.             CpCommand cp(st);
  1123.             cp.execute(fs);
  1124.         }else
  1125.         if (commander=="mv")
  1126.         {
  1127.             MvCommand mv(st);
  1128.             mv.execute(fs);
  1129.         }else
  1130.         if (commander=="rename")
  1131.         {
  1132.             RenameCommand rn(st);
  1133.             rn.execute(fs);
  1134.         }else
  1135.         if (commander=="rm")
  1136.         {
  1137.             RmCommand rm(st);
  1138.             rm.execute(fs);
  1139.         }else
  1140.         if (commander=="verbose")
  1141.         {
  1142.             VerboseCommand ver(st);
  1143.             ver.execute(fs);
  1144.         }else
  1145.         if (commander=="history")
  1146.         {
  1147.             HistoryCommand histoy(st,history);
  1148.             histoy.execute(fs);
  1149.         }else
  1150.         if (commander=="exec")
  1151.         {
  1152.             ExecCommand ex(st,history);
  1153.             ex.execute(fs);
  1154.         }else
  1155.         {
  1156.             ErrorCommand er(commander);
  1157.             er.execute(fs);
  1158.         }
  1159.  
  1160.     }
  1161. }
  1162.  
  1163. string ExecCommand::toString() {
  1164.     return "exec";
  1165. }
  1166.  
  1167. ExecCommand::~ExecCommand() {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement