]> git.donarmstrong.com Git - mothur.git/blobdiff - engine.cpp
fixed some bugs and added mgcluster command
[mothur.git] / engine.cpp
index 55617bc3f80a97e1832ff61c3e09678de8eca1ca..593c2c3128d08cfe8abddfa81a1e384a1d17aa62 100644 (file)
 
 #include "engine.hpp"
 
+/***********************************************************************/
+Engine::Engine(){
+       try {
+               cFactory = CommandFactory::getInstance();
+       }
+       catch(exception& e) {
+               errorOut(e, "Engine", "Engine");
+               exit(1);
+       }
+}
+
 /***********************************************************************/
 
 InteractEngine::InteractEngine(string path){
 
        globaldata = GlobalData::getInstance();
        globaldata->argv = path;
-       
-       system("clear");
 }
 
 /***********************************************************************/
 
-InteractEngine::~InteractEngine(){
-       }
+InteractEngine::~InteractEngine(){}
 
 /***********************************************************************/
 //This function allows the user to input commands one line at a time until they quit.
@@ -39,29 +47,24 @@ bool InteractEngine::getInput(){
                string options = "";
                int quitCommandCalled = 0;
                
-                               
                while(quitCommandCalled != 1){
-                       
+
                        mothurOutEndLine();
-                       mothurOut("mothur > ");
-                       getline(cin, input);
-                       if (cin.eof()) { input = "quit()"; }
                        
-                       mothurOutJustToLog(input);
-                       mothurOutEndLine();
+                       input = getCommand();                   
                        
                        //allow user to omit the () on the quit command
                        if (input == "quit") { input = "quit()"; }
                        
                        CommandOptionParser parser(input);
                        commandName = parser.getCommandString();
+       
                        options = parser.getOptionString();
                        
                        if (commandName != "") {
                        
                                //executes valid command
-                               CommandFactory cFactory;
-                               Command* command = cFactory.getCommand(commandName, options);
+                               Command* command = cFactory->getCommand(commandName, options);
                                quitCommandCalled = command->execute();
                                
                        }else {
@@ -76,7 +79,37 @@ bool InteractEngine::getInput(){
                exit(1);
        }
 }
-
+/***********************************************************************/
+string Engine::getCommand()  {
+       try {
+               #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
+                       #ifdef USE_READLINE
+                               char* nextCommand = NULL;
+                               nextCommand = readline("mothur > ");
+                               if(nextCommand != NULL) {  add_history(nextCommand);  }         
+                               mothurOutJustToLog("mothur > " + toString(nextCommand));
+                               return nextCommand;
+                       #else
+                               string nextCommand = "";
+                               mothurOut("mothur > ");
+                               getline(cin, nextCommand);
+                               return nextCommand;
+                       #endif
+               #else
+                       string nextCommand = "";
+                       mothurOut("mothur > ");
+                       getline(cin, nextCommand);
+                       return nextCommand;
+               #endif
+               
+               mothurOutEndLine();
+                                               
+       }
+       catch(exception& e) {
+               errorOut(e, "Engine", "getCommand");
+               exit(1);
+       }
+}
 /***********************************************************************/
 //This function opens the batchfile to be used by BatchEngine::getInput.
 BatchEngine::BatchEngine(string path, string batchFileName){
@@ -86,14 +119,6 @@ BatchEngine::BatchEngine(string path, string batchFileName){
                openedBatch = openInputFile(batchFileName, inputBatchFile);
                globaldata->argv = path;
                                
-               system("clear");
-       
-       //      char buffer = ' ';
-       //      ifstream header("introtext.txt");
-       //      while(!header.eof()){
-       //              cout << buffer;
-       //              buffer = header.get();
-       //      }
        }
        catch(exception& e) {
                errorOut(e, "BatchEngine", "BatchEngine");
@@ -103,8 +128,7 @@ BatchEngine::BatchEngine(string path, string batchFileName){
 
 /***********************************************************************/
 
-BatchEngine::~BatchEngine(){
-       }
+BatchEngine::~BatchEngine(){   }
 
 /***********************************************************************/
 //This Function allows the user to run a batchfile containing several commands on Dotur
@@ -127,9 +151,7 @@ bool BatchEngine::getInput(){
                while(quitCommandCalled == 0){
        
                        if (inputBatchFile.eof()) { input = "quit()"; }
-                       else { getline(inputBatchFile, input); }
-                       
-                       
+                       else { input = getline(inputBatchFile); }
                        
                        if (input[0] != '#') {
                                
@@ -148,8 +170,7 @@ bool BatchEngine::getInput(){
                                if (commandName != "") {
 
                                        //executes valid command
-                                       CommandFactory cFactory;
-                                       Command* command = cFactory.getCommand(commandName, options);
+                                       Command* command = cFactory->getCommand(commandName, options);
                                        quitCommandCalled = command->execute();
                                }else {         
                                        mothurOut("Invalid."); 
@@ -182,8 +203,6 @@ ScriptEngine::ScriptEngine(string path, string commandString){
 
                globaldata->argv = path;
                
-               system("clear");
-       
        }
        catch(exception& e) {
                errorOut(e, "ScriptEngine", "ScriptEngine");
@@ -193,8 +212,7 @@ ScriptEngine::ScriptEngine(string path, string commandString){
 
 /***********************************************************************/
 
-ScriptEngine::~ScriptEngine(){
-       }
+ScriptEngine::~ScriptEngine(){         }
 
 /***********************************************************************/
 //This Function allows the user to run a batchfile containing several commands on mothur
@@ -231,8 +249,7 @@ bool ScriptEngine::getInput(){
                        if (commandName != "") {
 
                                //executes valid command
-                               CommandFactory cFactory;
-                               Command* command = cFactory.getCommand(commandName, options);
+                               Command* command = cFactory->getCommand(commandName, options);
                                quitCommandCalled = command->execute();
                        }else {         
                                mothurOut("Invalid."); 
@@ -252,13 +269,21 @@ bool ScriptEngine::getInput(){
 string ScriptEngine::getNextCommand(string& commandString) {
        try {
                string nextcommand = "";
+               int count = 0;
                
-               nextcommand = commandString.substr(0,commandString.find_first_of(';'));
-
+               //go through string until you reach ; or end
+               while (count < commandString.length()) { 
+                       
+                       if (commandString[count] == ';') {  break;   }
+                       else {          nextcommand += commandString[count];    }
+                       
+                       count++;
+               }
+               
+               //if you are not at the end
+               if (count != commandString.length())  {   commandString = commandString.substr(count+1, commandString.length());  }
+               else { commandString = ""; }
                                
-               if ((commandString.find_first_of(';')+1) <= commandString.length()) {
-                       commandString = commandString.substr(commandString.find_first_of(';')+1, commandString.length());
-               }else { commandString = ""; } //you have reached the last command.
                
                //get rid of spaces in between commands if any
                if (commandString.length() > 0) {