Advertisement
Guest User

Untitled

a guest
Nov 25th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.90 KB | None | 0 0
  1. #include "../include/Environment.h"
  2. #include "../include/GlobalVariables.h"
  3. #include <iostream>
  4. #include <algorithm>//
  5. using namespace std;
  6.  
  7. Environment::Environment():commandsHistory(),fs(){
  8.  
  9. }
  10.  
  11. void Environment::start(){
  12.     string s="";
  13.     string commander="";
  14.     cout << "/>";
  15.     getline(cin, s);
  16.     while(s!="exit") {
  17.  
  18.         bool found =false;
  19.         for (size_t i(0);(i<s.size())&(!found);i++)
  20.         {
  21.             if (s[i]==' ')
  22.             {
  23.                 found= true;
  24.                 s.erase(0,i+1);
  25.             } else
  26.                 commander= commander+s[i];
  27.         }
  28.         if(!found)
  29.             s = "";
  30.  
  31.         if (commander=="pwd")
  32.         {
  33.             PwdCommand* pwd = new PwdCommand(s);
  34.             addToHistory(pwd);
  35.             (*pwd).execute(fs);
  36.         }
  37.         else if (commander=="cd")
  38.         {
  39.             CdCommand* cd = new CdCommand(s);
  40.             addToHistory(cd);
  41.             (*cd).execute(fs);
  42.         }
  43.         else if (commander=="ls")
  44.         {
  45.             LsCommand* ls = new LsCommand(s);
  46.             addToHistory(ls);
  47.             (*ls).execute(fs);
  48.         }
  49.         else if (commander=="mkdir")
  50.         {
  51.             MkdirCommand* mkdir =new MkdirCommand(s);
  52.             addToHistory(mkdir);
  53.             (*mkdir).execute(fs);
  54.         }
  55.         else if (commander=="mkfile")
  56.         {
  57.             MkfileCommand* mkfile = new MkfileCommand(s);
  58.             addToHistory(mkfile);
  59.             (*mkfile).execute(fs);
  60.         }
  61.         else if (commander=="cp")
  62.         {
  63.             CpCommand* cp=new CpCommand(s);
  64.             addToHistory(cp);
  65.             (*cp).execute(fs);
  66.         }
  67.         else if (commander=="mv")
  68.         {
  69.             MvCommand* mv=new MvCommand(s);
  70.             addToHistory(mv);
  71.             (*mv).execute(fs);
  72.         }
  73.         else if(commander=="rename"){
  74.             RenameCommand* rename=new RenameCommand(s);
  75.             addToHistory(rename);
  76.             (*rename).execute(fs);
  77.         }
  78.         else if(commander=="rm"){
  79.             RmCommand* rm=new RmCommand(s);
  80.             addToHistory(rm);
  81.             (*rm).execute(fs);
  82.         }
  83.         else if(commander=="history"){
  84.             HistoryCommand* hist = new HistoryCommand(s,commandsHistory);
  85.             (*hist).execute(fs);
  86.             addToHistory(hist);
  87.         }
  88.         else if(commander=="verbose"){
  89.             VerboseCommand* ver=new VerboseCommand(s);
  90.             addToHistory(ver);
  91.             (*ver).execute(fs);
  92.         }
  93.         else if(commander=="exec"){
  94.             ExecCommand* exec=new ExecCommand(s,commandsHistory);
  95.             addToHistory(exec);
  96.             (*exec).execute(fs);
  97.         }
  98.  
  99.         else{
  100.             commander=commander+' '+s;
  101.             ErrorCommand* er=new ErrorCommand(commander);
  102.             addToHistory(er);
  103.             (*er).execute(fs);
  104.         }
  105.  
  106.         s="";
  107.         commander="";
  108.         string tmp = fs.getWorkingDirectory().getAbsolutePath();
  109.         if(tmp.size()>1)
  110.             tmp=tmp.substr(1);
  111.         cout <<tmp+">";
  112.         getline(cin, s);
  113.     }
  114.     return;
  115. }
  116.  
  117. FileSystem& Environment::getFileSystem(){
  118.     return (FileSystem &)fs;
  119. }
  120.  
  121. void Environment::addToHistory(BaseCommand *command){
  122.     commandsHistory.push_back(command);
  123. }
  124.  
  125. const vector<BaseCommand*>& Environment::getHistory() const{
  126.     return  commandsHistory;
  127. }
  128.  
  129. //rule of 5 functions
  130.  
  131. Environment::~Environment(){
  132.     if((verbose==1) | (verbose==3)){
  133.         cout<<"Environment::~Environment()"<<endl;
  134.     }
  135.     clean();
  136. }
  137.  
  138. void Environment::clean() {
  139.     while (commandsHistory.size()!=0){
  140.         commandsHistory.pop_back();
  141.         /*delete(commandsHistory[0]);
  142.         commandsHistory.erase(commandsHistory.begin());*/
  143.     }
  144. //    delete(&commandsHistory);
  145. //    delete(&fs);
  146. }
  147.  
  148. Environment::Environment(const Environment &other):commandsHistory(),fs(other.fs){
  149.     if((verbose==1) | (verbose==3)){
  150.         cout<<"Environment::Environment(const Environment &other)"<<endl;
  151.     }
  152.     copy(other);
  153. }
  154.  
  155. void Environment::copy(const Environment &other) {
  156.     for(size_t i=0;i<other.commandsHistory.size();i++){
  157.         string commander = other.commandsHistory[i]->toString();
  158.         string s= other.commandsHistory[i]->getArgs();
  159.  
  160.         if (commander=="pwd")
  161.         {
  162.             PwdCommand pwd(s);
  163.             commandsHistory.push_back(&pwd);
  164.         }
  165.         else if (commander=="cd")
  166.         {
  167.             CdCommand cd(s);
  168.             commandsHistory.push_back(&cd);
  169.         }
  170.         else if (commander=="ls")
  171.         {
  172.             LsCommand ls(s);
  173.             commandsHistory.push_back(&ls);
  174.         }
  175.         else if (commander=="mkdir")
  176.         {
  177.             MkdirCommand mkdir(s);
  178.             commandsHistory.push_back(&mkdir);
  179.         }
  180.         else if (commander=="cd")
  181.         {
  182.             MkfileCommand mkfile(s);
  183.             commandsHistory.push_back(&mkfile);
  184.         }
  185.         else if (commander=="cp")
  186.         {
  187.             CpCommand cp(s);
  188.             commandsHistory.push_back(&cp);
  189.         }
  190.         else if (commander=="mv")
  191.         {
  192.             MvCommand mv(s);
  193.             commandsHistory.push_back(&mv);
  194.         }
  195.         else if(commander=="rename"){
  196.             RenameCommand rename(s);
  197.             commandsHistory.push_back(&rename);
  198.         }
  199.         else if(commander=="rm"){
  200.             RmCommand rm(s);
  201.             commandsHistory.push_back(&rm);
  202.         }
  203.         else if(commander=="history"){
  204.             HistoryCommand hist(s,commandsHistory);
  205.             commandsHistory.push_back(&hist);
  206.         }
  207.         else if(commander=="verbose"){
  208.             VerboseCommand ver(s);
  209.             commandsHistory.push_back(&ver);
  210.         }
  211.         else if(commander=="exec"){
  212.             ExecCommand exec(s,commandsHistory);
  213.             commandsHistory.push_back(&exec);
  214.         }
  215.  
  216.         else{
  217.             ErrorCommand er(commander);
  218.             commandsHistory.push_back(&er);
  219.         }
  220.     }
  221. }
  222.  
  223. Environment& Environment::operator=(const Environment& other){
  224.     if((verbose==1) | (verbose==3)){
  225.         cout<<"Environment& Environment::operator=(const Environment& other)"<<endl;
  226.     }
  227.     if(this!=&other) {
  228.         clean();
  229.         copy(other);
  230.     }
  231.     return *this;
  232. }
  233.  
  234. Environment::Environment(Environment &&other):commandsHistory(),fs(){
  235.     if((verbose==1) | (verbose==3)){
  236.         cout<<"Environment::Environment(Environment &&other)"<<endl;
  237.     }
  238.     steal(other);
  239. }
  240.  
  241. void Environment::steal(Environment &other) {
  242.     fs.setWorkingDirectory(&other.fs.getWorkingDirectory());
  243.     commandsHistory=other.getHistory();
  244.  
  245.     delete &other.fs;
  246.     delete &commandsHistory;
  247. }
  248.  
  249. Environment& Environment::operator=(Environment &&other){
  250.     if((verbose==1) | (verbose==3)){
  251.         cout<<"Environment& Environment::operator=(Environment &&other)"<<endl;
  252.     }
  253.  
  254.     clean();
  255.     steal(other);
  256.     return *this;
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement