]> 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 371bda8fb83f2b8ceb4793c49f067ed5987f2094..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){
@@ -36,61 +57,18 @@ bool InteractEngine::getInput(){
                string options = "";
                int quitCommandCalled = 0;
                
-                               
                while(quitCommandCalled != 1){
-                       
+
                        mothurOutEndLine();
-                       mothurOut("mothur > ");
-                       
-                       //get input char by char so you can check for use of arrow keys
-                       //if (_kbhit()){
-                       //      _getch(); // edit : if you want to check the arrow-keys you must call getch twice because special-keys have two values
-                       //      return _getch();
-                       //}
-                       //return 0; // if no key is pressed
-                       //setbuf(stdin, NULL); //no buffering
-/*if(ch==0)
-{
-ch=getch();
-if(ch==72) cout<<"up";
-else if(ch==75) cout<<"left";
-else if(ch==77) cout<<"right";
-else if(ch==80) cout<<"down";
-cout<<endl;
-}
-else break;
-}
-delay(2000);
-return 0;
-}*/
-                       
-                       //int letter = 0;
-                       //while ((letter != 10) && (letter != 13)) {
-                       //      letter = getch();
-                               
-                       //      cout << "char code = " << letter << endl;
-                               
-                       //      input += char(letter);
-                       //}
-               //      input = input.substr(0, input.length()-1); //cut off newline char
-               
-       //cout << input << endl;                
-                       
-                       getline(cin, input);
-                       if (cin.eof()) { input = "quit()"; }
                        
-                       //save command
-                       //previousInputs.push_back(input);
-                       
-                       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();
-       //cout << " command = " << commandName << endl;
+       
                        options = parser.getOptionString();
                        
                        if (commandName != "") {
@@ -112,13 +90,33 @@ return 0;
        }
 }
 /***********************************************************************/
-void Engine::terminateCommand(int dummy)  {
+string Engine::getCommand()  {
        try {
-               mothurOut("Stopping command."); mothurOutEndLine();
-               cFactory->getCommand();  //terminates command
+               #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, "InteractEngine", "terminateCommand");
+               errorOut(e, "Engine", "getCommand");
                exit(1);
        }
 }