Advertisement
Guest User

Environment

a guest
Nov 23rd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.52 KB | None | 0 0
  1. #include <iostream>
  2. #include "../include/Environment.h"
  3. #include "../include/GlobalVariables.h"
  4.  
  5. //#Assisting functions
  6. // string Trim function
  7. string trimF(const string& str){
  8.     size_t start = str.find_first_not_of(' ');
  9.     if(start == string::npos)
  10.         return str;
  11.     size_t end = str.find_last_not_of(' ');
  12.     return str.substr(start, (end-start+1));
  13. }
  14.  
  15. //# Vector content copy
  16. template <typename T>
  17. vector<T> vectorCopy(const vector<T> &v){
  18.     vector<T> output;
  19.     for(int i=0; i<v.size(); i++)
  20.         output.push_back(v.at(i));
  21.     return output;
  22. };
  23.  
  24. // Constructor
  25. Environment::Environment():commandsHistory(),fs(*new FileSystem()) {}
  26.  
  27. FileSystem& Environment::getFileSystem(){
  28.     return fs;
  29. }
  30.  
  31. void Environment::start() {
  32.     string input;
  33.     vector <BaseCommand*> history;
  34.  
  35.     cout << fs.getWorkingDirectory().getAbsolutePath() << ">";
  36.     getline(cin, input);
  37.     while(input != "exit"){
  38.         // #Verbose
  39.         if(verbose==2 || verbose==3){}
  40.             cout << input << endl;
  41.  
  42.         input = trimF(input);
  43.         string firstWord = input;
  44.         string args = "";
  45.         if(input.find(" ")!=string::npos){
  46.             firstWord = trimF(input.substr(0 , input.find(" ")));
  47.             args = trimF(input.substr(input.find(" ")));
  48.         }
  49.         BaseCommand* cmd;
  50.         if(firstWord == "pwd"){
  51.             cmd = new PwdCommand(args);
  52.         }else if(firstWord == "cd"){
  53.             cmd = new CdCommand(args);
  54.         }else if(firstWord == "ls"){
  55.             cmd = new LsCommand(args);
  56.         }else if(firstWord == "mkdir"){
  57.             cmd = new MkdirCommand(args);
  58.         }else if(firstWord == "mkfile"){
  59.             cmd = new MkfileCommand(args);
  60.         }else if(firstWord == "cp"){
  61.             cmd = new CpCommand(args);
  62.         }else if(firstWord == "mv"){
  63.             cmd = new MvCommand(args);
  64.         }else if(firstWord == "rename"){
  65.             cmd = new RenameCommand(args);
  66.         }else if(firstWord == "rm"){
  67.             cmd = new RmCommand(args);
  68.         }else if(firstWord == "history"){
  69.             cmd = new HistoryCommand(args, history);
  70.         }else if(firstWord == "verbose"){
  71.             cmd = new VerboseCommand(args);
  72.         }else if(firstWord == "exec"){
  73.             cmd = new ExecCommand(args, history);
  74.         }else{
  75.             cmd = new ErrorCommand(args);
  76.         }
  77.  
  78.         //TODO: make sure error command is added to history
  79.         history.push_back(cmd);
  80.         cmd->execute(fs);
  81.         cout << fs.getWorkingDirectory().getAbsolutePath() << ">";
  82.         getline(cin, input);
  83.  
  84.         cmd = nullptr; // memory clear(?)
  85.     }
  86.  
  87. }
  88.  
  89. void Environment::addToHistory(BaseCommand *command){
  90.     commandsHistory.push_back(command);
  91. }
  92.  
  93. const vector<BaseCommand*>& Environment::getHistory() const{
  94.     return commandsHistory;
  95. }
  96.  
  97. //copy constructor
  98. Environment::Environment(const Environment &f):
  99.         commandsHistory(vectorCopy(f.commandsHistory)), fs(f.fs) {
  100. }
  101. //copy assignment
  102. Environment& Environment::operator=(const Environment &f) {
  103.     if(this!=&f)
  104.     {
  105.         //clear();
  106.         commandsHistory = f.commandsHistory;
  107.         fs = f.fs;
  108.     }
  109.     return *this;
  110. }
  111. //move assignment
  112. Environment& Environment::operator=(Environment &&f) {
  113.     if(this!=&f)
  114.     {
  115.         commandsHistory=f.commandsHistory;
  116.         fs = f.fs;
  117.         f.commandsHistory.clear();  //TODO: how to delete vector from memory?
  118.         delete &f.fs; // TODO: is it okay?
  119.         //f.fs = nullptr;
  120.     }
  121.     return *this;
  122. }
  123. //move constructor
  124. Environment::Environment(Environment &&f):commandsHistory(f.commandsHistory), fs(f.fs) {
  125.     f.commandsHistory.clear(); //TODO: how to delete vector from memory?
  126.     delete &f.fs; // TODO: is it okay?
  127. }
  128. //destructor
  129. Environment::~Environment() {
  130.     delete &fs; // TODO: is it okay?
  131. }
  132.  
  133.  
  134. vector<string> Environment::strToVector(string str, string letter) { // TODO: consider to do it public for all
  135.  
  136.     // Inserting string into vector using string delimiter ' '
  137.     vector<string> v;
  138.     if (str.find(letter) == 0) str = str.substr(str.find_first_not_of(letter), string::npos);
  139.     while (str.size() != 0) {
  140.         if (str.find_first_of(letter) != string::npos) v.push_back(str.substr(0, str.find_first_of(letter)));
  141.         else v.push_back(str);
  142.         if(str.find(letter) != string::npos) str = str.substr(str.find_first_of(letter), string::npos);
  143.         else str="";
  144.         if(str.size()>0)
  145.             str = str.substr(str.find_first_not_of(letter), string::npos);
  146.     }
  147.     return v;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement