]> git.donarmstrong.com Git - mothur.git/blobdiff - engine.cpp
added sorted parameter to get.oturep, added error checking to chimera classes in...
[mothur.git] / engine.cpp
index f0fd05ab2f46d5f6ebbd680de70639d8599da5d5..c6ade387da83e0ebbdcf67addd28f99e7267a500 100644 (file)
 
 #include "engine.hpp"
 
+/***********************************************************************/
+inline void terminateCommand(int dummy)  {
+       
+               //mothurOut("Stopping command...."); 
+               //CommandFactory* cFactory = CommandFactory::getInstance();
+               //cFactory->getCommand();  //deletes old command and makes new no command.  
+                                                               //this may cause memory leak if old commands execute function allocated memory 
+                                                               //that is freed in the execute function and not the deconstructor 
+               //mothurOut("DONE."); mothurOutEndLine();
+}
+/***********************************************************************/
+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;
-       string logFileName = "mothur.logFile";
-       remove(logFileName.c_str());
-       
-       system("clear");
 }
 
 /***********************************************************************/
 
-InteractEngine::~InteractEngine(){
-       }
+InteractEngine::~InteractEngine(){}
 
 /***********************************************************************/
 //This function allows the user to input commands one line at a time until they quit.
@@ -41,55 +57,24 @@ bool InteractEngine::getInput(){
                string options = "";
                int quitCommandCalled = 0;
                
-               mothurOut("mothur v.1.4.1");
-               mothurOutEndLine();             
-               mothurOut("Last updated: 6/23/2009");
-               mothurOutEndLine();     
-               mothurOutEndLine();             
-               mothurOut("by");
-               mothurOutEndLine();             
-               mothurOut("Patrick D. Schloss");
-               mothurOutEndLine();
-               mothurOutEndLine();                     
-               mothurOut("Department of Microbiology");
-               mothurOutEndLine();             
-               mothurOut("pschloss@micro.umass.edu");
-               mothurOutEndLine();             
-               mothurOut("http://schloss.micro.umass.edu/mothur");
-               mothurOutEndLine();     
-               mothurOutEndLine();     
-               mothurOutEndLine();             
-               mothurOut("Distributed under the GNU General Public License");
-               mothurOutEndLine();
-               mothurOutEndLine();                     
-               mothurOut("Type 'help()' for information on the commands that are available");
-               mothurOutEndLine();
-               mothurOutEndLine();                     
-               mothurOut("Type 'quit()' to exit program");
-               mothurOutEndLine();     
-               
                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 {
@@ -104,7 +89,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){
@@ -113,17 +128,7 @@ BatchEngine::BatchEngine(string path, string batchFileName){
        
                openedBatch = openInputFile(batchFileName, inputBatchFile);
                globaldata->argv = path;
-               string logFileName = "mothur.logFile";
-               remove(logFileName.c_str());
-               
-               system("clear");
-       
-       //      char buffer = ' ';
-       //      ifstream header("introtext.txt");
-       //      while(!header.eof()){
-       //              cout << buffer;
-       //              buffer = header.get();
-       //      }
+                               
        }
        catch(exception& e) {
                errorOut(e, "BatchEngine", "BatchEngine");
@@ -133,8 +138,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
@@ -157,9 +161,7 @@ bool BatchEngine::getInput(){
                while(quitCommandCalled == 0){
        
                        if (inputBatchFile.eof()) { input = "quit()"; }
-                       else { getline(inputBatchFile, input); }
-                       
-                       
+                       else { input = getline(inputBatchFile); }
                        
                        if (input[0] != '#') {
                                
@@ -178,8 +180,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."); 
@@ -211,11 +212,7 @@ ScriptEngine::ScriptEngine(string path, string commandString){
                listOfCommands = commandString.substr(1, (commandString.length()-1));
 
                globaldata->argv = path;
-               string logFileName = "mothur.logFile";
-               remove(logFileName.c_str());
-
-               system("clear");
-       
+               
        }
        catch(exception& e) {
                errorOut(e, "ScriptEngine", "ScriptEngine");
@@ -225,8 +222,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
@@ -263,8 +259,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."); 
@@ -284,13 +279,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) {