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