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