Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "../include/Environment.h"
- #include "../include/GlobalVariables.h"
- //#Assisting functions
- // string Trim function
- string trimF(const string& str){
- size_t start = str.find_first_not_of(' ');
- if(start == string::npos)
- return str;
- size_t end = str.find_last_not_of(' ');
- return str.substr(start, (end-start+1));
- }
- //# Vector content copy
- template <typename T>
- vector<T> vectorCopy(const vector<T> &v){
- vector<T> output;
- for(int i=0; i<v.size(); i++)
- output.push_back(v.at(i));
- return output;
- };
- // Constructor
- Environment::Environment():commandsHistory(),fs(*new FileSystem()) {}
- FileSystem& Environment::getFileSystem(){
- return fs;
- }
- void Environment::start() {
- string input;
- vector <BaseCommand*> history;
- cout << fs.getWorkingDirectory().getAbsolutePath() << ">";
- getline(cin, input);
- while(input != "exit"){
- // #Verbose
- if(verbose==2 || verbose==3){}
- cout << input << endl;
- input = trimF(input);
- string firstWord = input;
- string args = "";
- if(input.find(" ")!=string::npos){
- firstWord = trimF(input.substr(0 , input.find(" ")));
- args = trimF(input.substr(input.find(" ")));
- }
- BaseCommand* cmd;
- if(firstWord == "pwd"){
- cmd = new PwdCommand(args);
- }else if(firstWord == "cd"){
- cmd = new CdCommand(args);
- }else if(firstWord == "ls"){
- cmd = new LsCommand(args);
- }else if(firstWord == "mkdir"){
- cmd = new MkdirCommand(args);
- }else if(firstWord == "mkfile"){
- cmd = new MkfileCommand(args);
- }else if(firstWord == "cp"){
- cmd = new CpCommand(args);
- }else if(firstWord == "mv"){
- cmd = new MvCommand(args);
- }else if(firstWord == "rename"){
- cmd = new RenameCommand(args);
- }else if(firstWord == "rm"){
- cmd = new RmCommand(args);
- }else if(firstWord == "history"){
- cmd = new HistoryCommand(args, history);
- }else if(firstWord == "verbose"){
- cmd = new VerboseCommand(args);
- }else if(firstWord == "exec"){
- cmd = new ExecCommand(args, history);
- }else{
- cmd = new ErrorCommand(args);
- }
- //TODO: make sure error command is added to history
- history.push_back(cmd);
- cmd->execute(fs);
- cout << fs.getWorkingDirectory().getAbsolutePath() << ">";
- getline(cin, input);
- cmd = nullptr; // memory clear(?)
- }
- }
- void Environment::addToHistory(BaseCommand *command){
- commandsHistory.push_back(command);
- }
- const vector<BaseCommand*>& Environment::getHistory() const{
- return commandsHistory;
- }
- //copy constructor
- Environment::Environment(const Environment &f):
- commandsHistory(vectorCopy(f.commandsHistory)), fs(f.fs) {
- }
- //copy assignment
- Environment& Environment::operator=(const Environment &f) {
- if(this!=&f)
- {
- //clear();
- commandsHistory = f.commandsHistory;
- fs = f.fs;
- }
- return *this;
- }
- //move assignment
- Environment& Environment::operator=(Environment &&f) {
- if(this!=&f)
- {
- commandsHistory=f.commandsHistory;
- fs = f.fs;
- f.commandsHistory.clear(); //TODO: how to delete vector from memory?
- delete &f.fs; // TODO: is it okay?
- //f.fs = nullptr;
- }
- return *this;
- }
- //move constructor
- Environment::Environment(Environment &&f):commandsHistory(f.commandsHistory), fs(f.fs) {
- f.commandsHistory.clear(); //TODO: how to delete vector from memory?
- delete &f.fs; // TODO: is it okay?
- }
- //destructor
- Environment::~Environment() {
- delete &fs; // TODO: is it okay?
- }
- vector<string> Environment::strToVector(string str, string letter) { // TODO: consider to do it public for all
- // Inserting string into vector using string delimiter ' '
- vector<string> v;
- if (str.find(letter) == 0) str = str.substr(str.find_first_not_of(letter), string::npos);
- while (str.size() != 0) {
- if (str.find_first_of(letter) != string::npos) v.push_back(str.substr(0, str.find_first_of(letter)));
- else v.push_back(str);
- if(str.find(letter) != string::npos) str = str.substr(str.find_first_of(letter), string::npos);
- else str="";
- if(str.size()>0)
- str = str.substr(str.find_first_not_of(letter), string::npos);
- }
- return v;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement