]> git.donarmstrong.com Git - mothur.git/blob - engine.cpp
a few modifications for 1.9
[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 Engine::Engine(){
19         try {
20                 cFactory = CommandFactory::getInstance();
21                 mout = MothurOut::getInstance();
22         }
23         catch(exception& e) {
24                 mout->errorOut(e, "Engine", "Engine");
25                 exit(1);
26         }
27 }
28
29 /***********************************************************************/
30
31 InteractEngine::InteractEngine(string path){
32
33         globaldata = GlobalData::getInstance();
34         globaldata->argv = path;
35         
36 }
37
38 /***********************************************************************/
39
40 InteractEngine::~InteractEngine(){}
41
42 /***********************************************************************/
43 //This function allows the user to input commands one line at a time until they quit.
44 //If the command is garbage it does nothing.
45 bool InteractEngine::getInput(){
46         try {
47                 string input = "";
48                 string commandName = "";
49                 string options = "";
50                 int quitCommandCalled = 0;
51                 
52                 while(quitCommandCalled != 1){
53
54                         mout->mothurOutEndLine();
55                         
56                         input = getCommand();   
57                         mout->mothurOutEndLine();       
58                         
59                         if (mout->control_pressed) { input = "quit()"; }
60                         
61                         //allow user to omit the () on the quit command
62                         if (input == "quit") { input = "quit()"; }
63                         
64                         CommandOptionParser parser(input);
65                         commandName = parser.getCommandString();
66         
67                         options = parser.getOptionString();
68                         
69                         if (commandName != "") {
70                                 mout->executing = true;
71                                 
72                                 #ifdef USE_MPI
73                                         int pid;
74                                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
75                                         
76                                         if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
77                                 #endif
78                                 //executes valid command
79                                 Command* command = cFactory->getCommand(commandName, options);
80                                 quitCommandCalled = command->execute();
81                                 mout->control_pressed = 0;
82                                 mout->executing = false;
83                                 
84                                 #ifdef USE_MPI
85                                         }
86                                 #endif
87                         }else {
88                                 mout->mothurOut("Your input contains errors. Please try again."); 
89                                 mout->mothurOutEndLine();
90                         }
91                 }       
92                 return 1;
93         }
94         catch(exception& e) {
95                 mout->errorOut(e, "InteractEngine", "getInput");
96                 exit(1);
97         }
98 }
99 /***********************************************************************/
100 string Engine::getCommand()  {
101         try {
102         #ifdef USE_MPI //mpirun doesn't work with readline
103                                 string nextCommand = "";
104                                 
105                                 mout->mothurOut("mothur > ");
106                                 getline(cin, nextCommand);
107                                 mout->mothurOutJustToLog(toString(nextCommand));
108                                 
109                                 return nextCommand;
110         #else
111                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
112                         #ifdef USE_READLINE
113                                 char* nextCommand = NULL;
114                                 nextCommand = readline("mothur > ");
115                                 
116                                 if(nextCommand != NULL) {  add_history(nextCommand);  } 
117                                 else{ //^D causes null string and we want it to quit mothur
118                                         nextCommand = "quit"; 
119                                         mout->mothurOut(nextCommand);
120                                 }       
121                                 
122                                 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
123                                 return nextCommand;
124                         #else
125                                 string nextCommand = "";
126                                 mout->mothurOut("mothur > ");
127                                 getline(cin, nextCommand);
128                                 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
129                                 
130                                 return nextCommand;
131                         #endif
132                 #else
133                                 string nextCommand = "";
134                                 
135                                 mout->mothurOut("mothur > ");
136                                 getline(cin, nextCommand);
137                                 mout->mothurOutJustToLog(toString(nextCommand));
138                                 
139                                 return nextCommand;
140                 #endif
141         #endif
142                                                 
143         }
144         catch(exception& e) {
145                 mout->errorOut(e, "Engine", "getCommand");
146                 exit(1);
147         }
148 }
149 /***********************************************************************/
150 //This function opens the batchfile to be used by BatchEngine::getInput.
151 BatchEngine::BatchEngine(string path, string batchFileName){
152         try {
153                 globaldata = GlobalData::getInstance();
154         
155                 openedBatch = openInputFile(batchFileName, inputBatchFile);
156                 globaldata->argv = path;
157                                 
158         }
159         catch(exception& e) {
160                 mout->errorOut(e, "BatchEngine", "BatchEngine");
161                 exit(1);
162         }
163 }
164
165 /***********************************************************************/
166
167 BatchEngine::~BatchEngine(){    }
168
169 /***********************************************************************/
170 //This Function allows the user to run a batchfile containing several commands on Dotur
171 bool BatchEngine::getInput(){
172         try {
173                 //check if this is a valid batchfile
174                 if (openedBatch == 1) {  
175                         mout->mothurOut("unable to open batchfile");  
176                         mout->mothurOutEndLine();
177                         return 1; 
178                 }
179         
180                 string input = "";
181                 string commandName = "";
182                 string options = "";
183                 
184                 //CommandFactory cFactory;
185                 int quitCommandCalled = 0;
186         
187                 while(quitCommandCalled == 0){
188         
189                         if (inputBatchFile.eof()) { input = "quit()"; }
190                         else { input = getline(inputBatchFile); }
191                         
192                         if (input[0] != '#') {
193                                 
194                                 mout->mothurOutEndLine();
195                                 mout->mothurOut("mothur > " + input);
196                                 mout->mothurOutEndLine();
197                                                         
198                                 if (mout->control_pressed) { input = "quit()"; }
199                                 
200                                 //allow user to omit the () on the quit command
201                                 if (input == "quit") { input = "quit()"; }
202
203                                 CommandOptionParser parser(input);
204                                 commandName = parser.getCommandString();
205                                 options = parser.getOptionString();
206                                                                                 
207                                 if (commandName != "") {
208                                         mout->executing = true;
209                                         #ifdef USE_MPI
210                                                 int pid;
211                                                 MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
212                                         
213                                                 if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
214                                         #endif
215                                         //executes valid command
216                                         Command* command = cFactory->getCommand(commandName, options);
217                                         quitCommandCalled = command->execute();
218                                         mout->control_pressed = 0;
219                                         mout->executing = false;
220                                 
221                                         #ifdef USE_MPI
222                                                 }
223                                         #endif
224                                 }else {         
225                                         mout->mothurOut("Invalid."); 
226                                         mout->mothurOutEndLine();
227                                 }
228                                 
229                         }
230                         gobble(inputBatchFile);
231                 }
232                 
233                 inputBatchFile.close();
234                 return 1;
235         }
236         catch(exception& e) {
237                 mout->errorOut(e, "BatchEngine", "getInput");
238                 exit(1);
239         }
240 }
241
242
243 /***********************************************************************/
244 /***********************************************************************/
245 //This function opens the batchfile to be used by BatchEngine::getInput.
246 ScriptEngine::ScriptEngine(string path, string commandString){
247         try {
248                 globaldata = GlobalData::getInstance();
249                 
250                 //remove quotes
251                 listOfCommands = commandString.substr(1, (commandString.length()-1));
252
253                 globaldata->argv = path;
254                                 
255         }
256         catch(exception& e) {
257                 mout->errorOut(e, "ScriptEngine", "ScriptEngine");
258                 exit(1);
259         }
260 }
261
262 /***********************************************************************/
263
264 ScriptEngine::~ScriptEngine(){  }
265
266 /***********************************************************************/
267 //This Function allows the user to run a batchfile containing several commands on mothur
268 bool ScriptEngine::getInput(){
269         try {
270                         
271                 string input = "";
272                 string commandName = "";
273                 string options = "";
274                 
275                 
276                 //CommandFactory cFactory;
277                 int quitCommandCalled = 0;
278         
279                 while(quitCommandCalled == 0){
280                 
281                         input = getNextCommand(listOfCommands); 
282                         
283                         if (input == "") { input = "quit()"; }
284                         
285                         mout->mothurOutEndLine();
286                         mout->mothurOut("mothur > " + input);
287                         mout->mothurOutEndLine();
288                         
289                         if (mout->control_pressed) { input = "quit()"; }
290                                 
291                         //allow user to omit the () on the quit command
292                         if (input == "quit") { input = "quit()"; }
293
294                         CommandOptionParser parser(input);
295                         commandName = parser.getCommandString();
296                         options = parser.getOptionString();
297                                                                                 
298                         if (commandName != "") {
299                                 mout->executing = true;
300                                 #ifdef USE_MPI
301                                         int pid;
302                                         MPI_Comm_rank(MPI_COMM_WORLD, &pid); 
303                                         
304                                         if ((cFactory->MPIEnabled(commandName)) || (pid == 0)) {
305                                 #endif
306                                 //executes valid command
307                                 Command* command = cFactory->getCommand(commandName, options);
308                                 quitCommandCalled = command->execute();
309                                 mout->control_pressed = 0;
310                                 mout->executing = false;
311                                 
312                                 #ifdef USE_MPI
313                                         }
314                                 #endif
315                         }else {         
316                                 mout->mothurOut("Invalid."); 
317                                 mout->mothurOutEndLine();
318                         }
319                         
320                 }
321                 
322                 return 1;
323         }
324         catch(exception& e) {
325                 mout->errorOut(e, "ScriptEngine", "getInput");
326                 exit(1);
327         }
328 }
329 /***********************************************************************/
330 string ScriptEngine::getNextCommand(string& commandString) {
331         try {
332                 string nextcommand = "";
333                 int count = 0;
334                 
335                 //go through string until you reach ; or end
336                 while (count < commandString.length()) { 
337                         
338                         if (commandString[count] == ';') {  break;   }
339                         else {          nextcommand += commandString[count];    }
340                         
341                         count++;
342                 }
343                 
344                 //if you are not at the end
345                 if (count != commandString.length())  {   commandString = commandString.substr(count+1, commandString.length());  }
346                 else { commandString = ""; }
347                                 
348                 
349                 //get rid of spaces in between commands if any
350                 if (commandString.length() > 0) {
351                         while (commandString[0] == ' ') {  
352                                 commandString = commandString.substr(1,commandString.length());
353                                 if (commandString.length() == 0) {  break;  }
354                         }
355                 }
356                                         
357                 return nextcommand;
358         }
359         catch(exception& e) {
360                 mout->errorOut(e, "ScriptEngine", "getNextCommand");
361                 exit(1);
362         }
363 }
364 /***********************************************************************/