5 * Created by Pat Schloss on 8/15/08.
6 * Copyright 2008 Patrick D. Schloss. All rights reserved.
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.
17 /***********************************************************************/
20 cFactory = CommandFactory::getInstance();
21 mout = MothurOut::getInstance();
24 mout->errorOut(e, "Engine", "Engine");
29 /***********************************************************************/
31 InteractEngine::InteractEngine(string path){
33 globaldata = GlobalData::getInstance();
34 globaldata->argv = path;
38 /***********************************************************************/
40 InteractEngine::~InteractEngine(){}
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(){
48 string commandName = "";
50 int quitCommandCalled = 0;
52 while(quitCommandCalled != 1){
54 mout->mothurOutEndLine();
57 mout->mothurOutEndLine();
59 //allow user to omit the () on the quit command
60 if (input == "quit") { input = "quit()"; }
62 CommandOptionParser parser(input);
63 commandName = parser.getCommandString();
65 options = parser.getOptionString();
67 if (commandName != "") {
69 //executes valid command
70 Command* command = cFactory->getCommand(commandName, options);
71 quitCommandCalled = command->execute();
72 mout->control_pressed = 0;
75 mout->mothurOut("Your input contains errors. Please try again.");
76 mout->mothurOutEndLine();
82 mout->errorOut(e, "InteractEngine", "getInput");
86 /***********************************************************************/
87 string Engine::getCommand() {
89 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
91 char* nextCommand = NULL;
92 nextCommand = readline("mothur > ");
94 if(nextCommand != NULL) { add_history(nextCommand); }
95 else{ //^D causes null string and we want it to quit mothur
97 cout << nextCommand << endl;
100 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
103 string nextCommand = "";
104 mout->mothurOut("mothur > ");
105 getline(cin, nextCommand);
106 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
110 string nextCommand = "";
111 mout->mothurOut("mothur > ");
112 getline(cin, nextCommand);
113 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
117 mout->mothurOutEndLine();
120 catch(exception& e) {
121 mout->errorOut(e, "Engine", "getCommand");
125 /***********************************************************************/
126 //This function opens the batchfile to be used by BatchEngine::getInput.
127 BatchEngine::BatchEngine(string path, string batchFileName){
129 globaldata = GlobalData::getInstance();
131 openedBatch = openInputFile(batchFileName, inputBatchFile);
132 globaldata->argv = path;
135 catch(exception& e) {
136 mout->errorOut(e, "BatchEngine", "BatchEngine");
141 /***********************************************************************/
143 BatchEngine::~BatchEngine(){ }
145 /***********************************************************************/
146 //This Function allows the user to run a batchfile containing several commands on Dotur
147 bool BatchEngine::getInput(){
149 //check if this is a valid batchfile
150 if (openedBatch == 1) {
151 mout->mothurOut("unable to open batchfile");
152 mout->mothurOutEndLine();
157 string commandName = "";
160 //CommandFactory cFactory;
161 int quitCommandCalled = 0;
163 while(quitCommandCalled == 0){
165 if (inputBatchFile.eof()) { input = "quit()"; }
166 else { input = getline(inputBatchFile); }
168 if (input[0] != '#') {
170 mout->mothurOutEndLine();
171 mout->mothurOut("mothur > " + input);
172 mout->mothurOutEndLine();
175 //allow user to omit the () on the quit command
176 if (input == "quit") { input = "quit()"; }
178 CommandOptionParser parser(input);
179 commandName = parser.getCommandString();
180 options = parser.getOptionString();
182 if (commandName != "") {
184 //executes valid command
185 Command* command = cFactory->getCommand(commandName, options);
186 quitCommandCalled = command->execute();
187 mout->control_pressed = 0;
189 mout->mothurOut("Invalid.");
190 mout->mothurOutEndLine();
194 gobble(inputBatchFile);
197 inputBatchFile.close();
200 catch(exception& e) {
201 mout->errorOut(e, "BatchEngine", "getInput");
207 /***********************************************************************/
208 /***********************************************************************/
209 //This function opens the batchfile to be used by BatchEngine::getInput.
210 ScriptEngine::ScriptEngine(string path, string commandString){
212 globaldata = GlobalData::getInstance();
215 listOfCommands = commandString.substr(1, (commandString.length()-1));
217 globaldata->argv = path;
220 catch(exception& e) {
221 mout->errorOut(e, "ScriptEngine", "ScriptEngine");
226 /***********************************************************************/
228 ScriptEngine::~ScriptEngine(){ }
230 /***********************************************************************/
231 //This Function allows the user to run a batchfile containing several commands on mothur
232 bool ScriptEngine::getInput(){
236 string commandName = "";
240 //CommandFactory cFactory;
241 int quitCommandCalled = 0;
243 while(quitCommandCalled == 0){
245 input = getNextCommand(listOfCommands);
247 if (input == "") { input = "quit()"; }
250 mout->mothurOutEndLine();
251 mout->mothurOut("mothur > " + input);
252 mout->mothurOutEndLine();
255 //allow user to omit the () on the quit command
256 if (input == "quit") { input = "quit()"; }
258 CommandOptionParser parser(input);
259 commandName = parser.getCommandString();
260 options = parser.getOptionString();
262 if (commandName != "") {
264 //executes valid command
265 Command* command = cFactory->getCommand(commandName, options);
266 quitCommandCalled = command->execute();
267 mout->control_pressed = 0;
269 mout->mothurOut("Invalid.");
270 mout->mothurOutEndLine();
277 catch(exception& e) {
278 mout->errorOut(e, "ScriptEngine", "getInput");
282 /***********************************************************************/
283 string ScriptEngine::getNextCommand(string& commandString) {
285 string nextcommand = "";
288 //go through string until you reach ; or end
289 while (count < commandString.length()) {
291 if (commandString[count] == ';') { break; }
292 else { nextcommand += commandString[count]; }
297 //if you are not at the end
298 if (count != commandString.length()) { commandString = commandString.substr(count+1, commandString.length()); }
299 else { commandString = ""; }
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; }
312 catch(exception& e) {
313 mout->errorOut(e, "ScriptEngine", "getNextCommand");
317 /***********************************************************************/