]> git.donarmstrong.com Git - mothur.git/blob - engine.cpp
created mothurOut class to handle logfiles
[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                         //allow user to omit the () on the quit command
60                         if (input == "quit") { input = "quit()"; }
61                         
62                         CommandOptionParser parser(input);
63                         commandName = parser.getCommandString();
64         
65                         options = parser.getOptionString();
66                         
67                         if (commandName != "") {
68                         
69                                 //executes valid command
70                                 Command* command = cFactory->getCommand(commandName, options);
71                                 quitCommandCalled = command->execute();
72                                 
73                         }else {
74                                 mout->mothurOut("Your input contains errors. Please try again."); 
75                                 mout->mothurOutEndLine();
76                         }
77                 }       
78                 return 1;
79         }
80         catch(exception& e) {
81                 mout->errorOut(e, "InteractEngine", "getInput");
82                 exit(1);
83         }
84 }
85 /***********************************************************************/
86 string Engine::getCommand()  {
87         try {
88                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
89                         #ifdef USE_READLINE
90                                 char* nextCommand = NULL;
91                                 nextCommand = readline("mothur > ");
92                                 
93                                 if(nextCommand != NULL) {  add_history(nextCommand);  } 
94                                 else{ //^D causes null string and we want it to quit mothur
95                                         nextCommand = "quit"; 
96                                         cout << nextCommand << endl;
97                                 }       
98                                 
99                                 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
100                                 return nextCommand;
101                         #else
102                                 string nextCommand = "";
103                                 mout->mothurOut("mothur > ");
104                                 getline(cin, nextCommand);
105                                 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
106                                 return nextCommand;
107                         #endif
108                 #else
109                         string nextCommand = "";
110                         mout->mothurOut("mothur > ");
111                         getline(cin, nextCommand);
112                         mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
113                         return nextCommand;
114                 #endif
115                 
116                 mout->mothurOutEndLine();
117                                                 
118         }
119         catch(exception& e) {
120                 mout->errorOut(e, "Engine", "getCommand");
121                 exit(1);
122         }
123 }
124 /***********************************************************************/
125 //This function opens the batchfile to be used by BatchEngine::getInput.
126 BatchEngine::BatchEngine(string path, string batchFileName){
127         try {
128                 globaldata = GlobalData::getInstance();
129         
130                 openedBatch = openInputFile(batchFileName, inputBatchFile);
131                 globaldata->argv = path;
132                                 
133         }
134         catch(exception& e) {
135                 mout->errorOut(e, "BatchEngine", "BatchEngine");
136                 exit(1);
137         }
138 }
139
140 /***********************************************************************/
141
142 BatchEngine::~BatchEngine(){    }
143
144 /***********************************************************************/
145 //This Function allows the user to run a batchfile containing several commands on Dotur
146 bool BatchEngine::getInput(){
147         try {
148                 //check if this is a valid batchfile
149                 if (openedBatch == 1) {  
150                         mout->mothurOut("unable to open batchfile");  
151                         mout->mothurOutEndLine();
152                         return 1; 
153                 }
154         
155                 string input = "";
156                 string commandName = "";
157                 string options = "";
158                 
159                 //CommandFactory cFactory;
160                 int quitCommandCalled = 0;
161         
162                 while(quitCommandCalled == 0){
163         
164                         if (inputBatchFile.eof()) { input = "quit()"; }
165                         else { input = getline(inputBatchFile); }
166                         
167                         if (input[0] != '#') {
168                                 
169                                 mout->mothurOutEndLine();
170                                 mout->mothurOut("mothur > " + input);
171                                 mout->mothurOutEndLine();
172                                 
173                                 
174                                 //allow user to omit the () on the quit command
175                                 if (input == "quit") { input = "quit()"; }
176
177                                 CommandOptionParser parser(input);
178                                 commandName = parser.getCommandString();
179                                 options = parser.getOptionString();
180                                                                                 
181                                 if (commandName != "") {
182
183                                         //executes valid command
184                                         Command* command = cFactory->getCommand(commandName, options);
185                                         quitCommandCalled = command->execute();
186                                 }else {         
187                                         mout->mothurOut("Invalid."); 
188                                         mout->mothurOutEndLine();
189                                 }
190                                 
191                         }
192                         gobble(inputBatchFile);
193                 }
194                 
195                 inputBatchFile.close();
196                 return 1;
197         }
198         catch(exception& e) {
199                 mout->errorOut(e, "BatchEngine", "getInput");
200                 exit(1);
201         }
202 }
203
204
205 /***********************************************************************/
206 /***********************************************************************/
207 //This function opens the batchfile to be used by BatchEngine::getInput.
208 ScriptEngine::ScriptEngine(string path, string commandString){
209         try {
210                 globaldata = GlobalData::getInstance();
211                 
212                 //remove quotes
213                 listOfCommands = commandString.substr(1, (commandString.length()-1));
214
215                 globaldata->argv = path;
216                                 
217         }
218         catch(exception& e) {
219                 mout->errorOut(e, "ScriptEngine", "ScriptEngine");
220                 exit(1);
221         }
222 }
223
224 /***********************************************************************/
225
226 ScriptEngine::~ScriptEngine(){  }
227
228 /***********************************************************************/
229 //This Function allows the user to run a batchfile containing several commands on mothur
230 bool ScriptEngine::getInput(){
231         try {
232                         
233                 string input = "";
234                 string commandName = "";
235                 string options = "";
236                 
237                 
238                 //CommandFactory cFactory;
239                 int quitCommandCalled = 0;
240         
241                 while(quitCommandCalled == 0){
242                 
243                         input = getNextCommand(listOfCommands); 
244                         
245                         if (input == "") { input = "quit()"; }
246                         
247                         
248                         mout->mothurOutEndLine();
249                         mout->mothurOut("mothur > " + input);
250                         mout->mothurOutEndLine();
251
252                                 
253                         //allow user to omit the () on the quit command
254                         if (input == "quit") { input = "quit()"; }
255
256                         CommandOptionParser parser(input);
257                         commandName = parser.getCommandString();
258                         options = parser.getOptionString();
259                                                                                 
260                         if (commandName != "") {
261
262                                 //executes valid command
263                                 Command* command = cFactory->getCommand(commandName, options);
264                                 quitCommandCalled = command->execute();
265                         }else {         
266                                 mout->mothurOut("Invalid."); 
267                                 mout->mothurOutEndLine();
268                         }
269                         
270                 }
271                 
272                 return 1;
273         }
274         catch(exception& e) {
275                 mout->errorOut(e, "ScriptEngine", "getInput");
276                 exit(1);
277         }
278 }
279 /***********************************************************************/
280 string ScriptEngine::getNextCommand(string& commandString) {
281         try {
282                 string nextcommand = "";
283                 int count = 0;
284                 
285                 //go through string until you reach ; or end
286                 while (count < commandString.length()) { 
287                         
288                         if (commandString[count] == ';') {  break;   }
289                         else {          nextcommand += commandString[count];    }
290                         
291                         count++;
292                 }
293                 
294                 //if you are not at the end
295                 if (count != commandString.length())  {   commandString = commandString.substr(count+1, commandString.length());  }
296                 else { commandString = ""; }
297                                 
298                 
299                 //get rid of spaces in between commands if any
300                 if (commandString.length() > 0) {
301                         while (commandString[0] == ' ') {  
302                                 commandString = commandString.substr(1,commandString.length());
303                                 if (commandString.length() == 0) {  break;  }
304                         }
305                 }
306                                         
307                 return nextcommand;
308         }
309         catch(exception& e) {
310                 mout->errorOut(e, "ScriptEngine", "getNextCommand");
311                 exit(1);
312         }
313 }
314 /***********************************************************************/