]> git.donarmstrong.com Git - mothur.git/blob - engine.cpp
added sorted parameter to get.oturep, added error checking to chimera classes in...
[mothur.git] / engine.cpp
1 /*
2  *  engine.cpp
3  *  
4  *
5  *  Created by Pat Schloss on 8/15/08.
6  *  Copyright 2008 Patrick D. Schloss. All rights reserved.
7  *
8  *  There's a TON of duplicated code between InteractEngine and BatchEngine
9  *  I couldn't figure out how to transition between ifstream (batch) and cin (interact)
10  *  Fix later, don't have time now.
11  *
12  */
13
14
15 #include "engine.hpp"
16
17 /***********************************************************************/
18 inline void terminateCommand(int dummy)  {
19         
20                 //mothurOut("Stopping command...."); 
21                 //CommandFactory* cFactory = CommandFactory::getInstance();
22                 //cFactory->getCommand();  //deletes old command and makes new no command.  
23                                                                 //this may cause memory leak if old commands execute function allocated memory 
24                                                                 //that is freed in the execute function and not the deconstructor 
25                 //mothurOut("DONE."); mothurOutEndLine();
26 }
27 /***********************************************************************/
28 Engine::Engine(){
29         try {
30                 cFactory = CommandFactory::getInstance();
31         }
32         catch(exception& e) {
33                 errorOut(e, "Engine", "Engine");
34                 exit(1);
35         }
36 }
37
38 /***********************************************************************/
39
40 InteractEngine::InteractEngine(string path){
41
42         globaldata = GlobalData::getInstance();
43         globaldata->argv = path;
44 }
45
46 /***********************************************************************/
47
48 InteractEngine::~InteractEngine(){}
49
50 /***********************************************************************/
51 //This function allows the user to input commands one line at a time until they quit.
52 //If the command is garbage it does nothing.
53 bool InteractEngine::getInput(){
54         try {
55                 string input = "";
56                 string commandName = "";
57                 string options = "";
58                 int quitCommandCalled = 0;
59                 
60                 while(quitCommandCalled != 1){
61
62                         mothurOutEndLine();
63                         
64                         input = getCommand();                   
65                         
66                         //allow user to omit the () on the quit command
67                         if (input == "quit") { input = "quit()"; }
68                         
69                         CommandOptionParser parser(input);
70                         commandName = parser.getCommandString();
71         
72                         options = parser.getOptionString();
73                         
74                         if (commandName != "") {
75                         
76                                 //executes valid command
77                                 Command* command = cFactory->getCommand(commandName, options);
78                                 quitCommandCalled = command->execute();
79                                 
80                         }else {
81                                 mothurOut("Your input contains errors. Please try again."); 
82                                 mothurOutEndLine();
83                         }
84                 }       
85                 return 1;
86         }
87         catch(exception& e) {
88                 errorOut(e, "InteractEngine", "getInput");
89                 exit(1);
90         }
91 }
92 /***********************************************************************/
93 string Engine::getCommand()  {
94         try {
95                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
96                         #ifdef USE_READLINE
97                                 char* nextCommand = NULL;
98                                 nextCommand = readline("mothur > ");
99                                 if(nextCommand != NULL) {  add_history(nextCommand);  }         
100                                 mothurOutJustToLog("mothur > " + toString(nextCommand));
101                                 return nextCommand;
102                         #else
103                                 string nextCommand = "";
104                                 mothurOut("mothur > ");
105                                 getline(cin, nextCommand);
106                                 return nextCommand;
107                         #endif
108                 #else
109                         string nextCommand = "";
110                         mothurOut("mothur > ");
111                         getline(cin, nextCommand);
112                         return nextCommand;
113                 #endif
114                 
115                 mothurOutEndLine();
116                                                 
117         }
118         catch(exception& e) {
119                 errorOut(e, "Engine", "getCommand");
120                 exit(1);
121         }
122 }
123 /***********************************************************************/
124 //This function opens the batchfile to be used by BatchEngine::getInput.
125 BatchEngine::BatchEngine(string path, string batchFileName){
126         try {
127                 globaldata = GlobalData::getInstance();
128         
129                 openedBatch = openInputFile(batchFileName, inputBatchFile);
130                 globaldata->argv = path;
131                                 
132         }
133         catch(exception& e) {
134                 errorOut(e, "BatchEngine", "BatchEngine");
135                 exit(1);
136         }
137 }
138
139 /***********************************************************************/
140
141 BatchEngine::~BatchEngine(){    }
142
143 /***********************************************************************/
144 //This Function allows the user to run a batchfile containing several commands on Dotur
145 bool BatchEngine::getInput(){
146         try {
147                 //check if this is a valid batchfile
148                 if (openedBatch == 1) {  
149                         mothurOut("unable to open batchfile");  
150                         mothurOutEndLine();
151                         return 1; 
152                 }
153         
154                 string input = "";
155                 string commandName = "";
156                 string options = "";
157                 
158                 //CommandFactory cFactory;
159                 int quitCommandCalled = 0;
160         
161                 while(quitCommandCalled == 0){
162         
163                         if (inputBatchFile.eof()) { input = "quit()"; }
164                         else { input = getline(inputBatchFile); }
165                         
166                         if (input[0] != '#') {
167                                 
168                                 mothurOutEndLine();
169                                 mothurOut("mothur > " + input);
170                                 mothurOutEndLine();
171                                 
172                                 
173                                 //allow user to omit the () on the quit command
174                                 if (input == "quit") { input = "quit()"; }
175
176                                 CommandOptionParser parser(input);
177                                 commandName = parser.getCommandString();
178                                 options = parser.getOptionString();
179                                                                                 
180                                 if (commandName != "") {
181
182                                         //executes valid command
183                                         Command* command = cFactory->getCommand(commandName, options);
184                                         quitCommandCalled = command->execute();
185                                 }else {         
186                                         mothurOut("Invalid."); 
187                                         mothurOutEndLine();
188                                 }
189                                 
190                         }
191                         gobble(inputBatchFile);
192                 }
193                 
194                 inputBatchFile.close();
195                 return 1;
196         }
197         catch(exception& e) {
198                 errorOut(e, "BatchEngine", "getInput");
199                 exit(1);
200         }
201 }
202
203
204 /***********************************************************************/
205 /***********************************************************************/
206 //This function opens the batchfile to be used by BatchEngine::getInput.
207 ScriptEngine::ScriptEngine(string path, string commandString){
208         try {
209                 globaldata = GlobalData::getInstance();
210                 
211                 //remove quotes
212                 listOfCommands = commandString.substr(1, (commandString.length()-1));
213
214                 globaldata->argv = path;
215                 
216         }
217         catch(exception& e) {
218                 errorOut(e, "ScriptEngine", "ScriptEngine");
219                 exit(1);
220         }
221 }
222
223 /***********************************************************************/
224
225 ScriptEngine::~ScriptEngine(){  }
226
227 /***********************************************************************/
228 //This Function allows the user to run a batchfile containing several commands on mothur
229 bool ScriptEngine::getInput(){
230         try {
231                         
232                 string input = "";
233                 string commandName = "";
234                 string options = "";
235                 
236                 
237                 //CommandFactory cFactory;
238                 int quitCommandCalled = 0;
239         
240                 while(quitCommandCalled == 0){
241                 
242                         input = getNextCommand(listOfCommands); 
243                         
244                         if (input == "") { input = "quit()"; }
245                         
246                         
247                         mothurOutEndLine();
248                         mothurOut("mothur > " + input);
249                         mothurOutEndLine();
250
251                                 
252                         //allow user to omit the () on the quit command
253                         if (input == "quit") { input = "quit()"; }
254
255                         CommandOptionParser parser(input);
256                         commandName = parser.getCommandString();
257                         options = parser.getOptionString();
258                                                                                 
259                         if (commandName != "") {
260
261                                 //executes valid command
262                                 Command* command = cFactory->getCommand(commandName, options);
263                                 quitCommandCalled = command->execute();
264                         }else {         
265                                 mothurOut("Invalid."); 
266                                 mothurOutEndLine();
267                         }
268                         
269                 }
270                 
271                 return 1;
272         }
273         catch(exception& e) {
274                 errorOut(e, "ScriptEngine", "getInput");
275                 exit(1);
276         }
277 }
278 /***********************************************************************/
279 string ScriptEngine::getNextCommand(string& commandString) {
280         try {
281                 string nextcommand = "";
282                 int count = 0;
283                 
284                 //go through string until you reach ; or end
285                 while (count < commandString.length()) { 
286                         
287                         if (commandString[count] == ';') {  break;   }
288                         else {          nextcommand += commandString[count];    }
289                         
290                         count++;
291                 }
292                 
293                 //if you are not at the end
294                 if (count != commandString.length())  {   commandString = commandString.substr(count+1, commandString.length());  }
295                 else { commandString = ""; }
296                                 
297                 
298                 //get rid of spaces in between commands if any
299                 if (commandString.length() > 0) {
300                         while (commandString[0] == ' ') {  
301                                 commandString = commandString.substr(1,commandString.length());
302                                 if (commandString.length() == 0) {  break;  }
303                         }
304                 }
305                                         
306                 return nextcommand;
307         }
308         catch(exception& e) {
309                 errorOut(e, "ScriptEngine", "getNextCommand");
310                 exit(1);
311         }
312 }
313 /***********************************************************************/