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 if (mout->control_pressed) { input = "quit()"; }
61 //allow user to omit the () on the quit command
62 if (input == "quit") { input = "quit()"; }
64 CommandOptionParser parser(input);
65 commandName = parser.getCommandString();
67 options = parser.getOptionString();
69 if (commandName != "") {
70 mout->executing = true;
71 //executes valid command
72 Command* command = cFactory->getCommand(commandName, options);
73 quitCommandCalled = command->execute();
74 mout->control_pressed = 0;
75 mout->executing = false;
77 mout->mothurOut("Your input contains errors. Please try again.");
78 mout->mothurOutEndLine();
84 mout->errorOut(e, "InteractEngine", "getInput");
88 /***********************************************************************/
89 string Engine::getCommand() {
91 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
93 char* nextCommand = NULL;
94 nextCommand = readline("mothur > ");
96 if(nextCommand != NULL) { add_history(nextCommand); }
97 else{ //^D causes null string and we want it to quit mothur
99 cout << nextCommand << endl;
102 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
105 string nextCommand = "";
106 mout->mothurOut("mothur > ");
107 getline(cin, nextCommand);
108 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
112 string nextCommand = "";
113 mout->mothurOut("mothur > ");
114 getline(cin, nextCommand);
115 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
119 mout->mothurOutEndLine();
122 catch(exception& e) {
123 mout->errorOut(e, "Engine", "getCommand");
127 /***********************************************************************/
128 //This function opens the batchfile to be used by BatchEngine::getInput.
129 BatchEngine::BatchEngine(string path, string batchFileName){
131 globaldata = GlobalData::getInstance();
133 openedBatch = openInputFile(batchFileName, inputBatchFile);
134 globaldata->argv = path;
137 catch(exception& e) {
138 mout->errorOut(e, "BatchEngine", "BatchEngine");
143 /***********************************************************************/
145 BatchEngine::~BatchEngine(){ }
147 /***********************************************************************/
148 //This Function allows the user to run a batchfile containing several commands on Dotur
149 bool BatchEngine::getInput(){
151 //check if this is a valid batchfile
152 if (openedBatch == 1) {
153 mout->mothurOut("unable to open batchfile");
154 mout->mothurOutEndLine();
159 string commandName = "";
162 //CommandFactory cFactory;
163 int quitCommandCalled = 0;
165 while(quitCommandCalled == 0){
167 if (inputBatchFile.eof()) { input = "quit()"; }
168 else { input = getline(inputBatchFile); }
170 if (input[0] != '#') {
172 mout->mothurOutEndLine();
173 mout->mothurOut("mothur > " + input);
174 mout->mothurOutEndLine();
176 if (mout->control_pressed) { input = "quit()"; }
178 //allow user to omit the () on the quit command
179 if (input == "quit") { input = "quit()"; }
181 CommandOptionParser parser(input);
182 commandName = parser.getCommandString();
183 options = parser.getOptionString();
185 if (commandName != "") {
186 mout->executing = true;
187 //executes valid command
188 Command* command = cFactory->getCommand(commandName, options);
189 quitCommandCalled = command->execute();
190 mout->control_pressed = 0;
191 mout->executing = false;
193 mout->mothurOut("Invalid.");
194 mout->mothurOutEndLine();
198 gobble(inputBatchFile);
201 inputBatchFile.close();
204 catch(exception& e) {
205 mout->errorOut(e, "BatchEngine", "getInput");
211 /***********************************************************************/
212 /***********************************************************************/
213 //This function opens the batchfile to be used by BatchEngine::getInput.
214 ScriptEngine::ScriptEngine(string path, string commandString){
216 globaldata = GlobalData::getInstance();
219 listOfCommands = commandString.substr(1, (commandString.length()-1));
221 globaldata->argv = path;
224 catch(exception& e) {
225 mout->errorOut(e, "ScriptEngine", "ScriptEngine");
230 /***********************************************************************/
232 ScriptEngine::~ScriptEngine(){ }
234 /***********************************************************************/
235 //This Function allows the user to run a batchfile containing several commands on mothur
236 bool ScriptEngine::getInput(){
240 string commandName = "";
244 //CommandFactory cFactory;
245 int quitCommandCalled = 0;
247 while(quitCommandCalled == 0){
249 input = getNextCommand(listOfCommands);
251 if (input == "") { input = "quit()"; }
254 mout->mothurOutEndLine();
255 mout->mothurOut("mothur > " + input);
256 mout->mothurOutEndLine();
258 if (mout->control_pressed) { input = "quit()"; }
260 //allow user to omit the () on the quit command
261 if (input == "quit") { input = "quit()"; }
263 CommandOptionParser parser(input);
264 commandName = parser.getCommandString();
265 options = parser.getOptionString();
267 if (commandName != "") {
268 mout->executing = true;
269 //executes valid command
270 Command* command = cFactory->getCommand(commandName, options);
271 quitCommandCalled = command->execute();
272 mout->control_pressed = 0;
273 mout->executing = false;
275 mout->mothurOut("Invalid.");
276 mout->mothurOutEndLine();
283 catch(exception& e) {
284 mout->errorOut(e, "ScriptEngine", "getInput");
288 /***********************************************************************/
289 string ScriptEngine::getNextCommand(string& commandString) {
291 string nextcommand = "";
294 //go through string until you reach ; or end
295 while (count < commandString.length()) {
297 if (commandString[count] == ';') { break; }
298 else { nextcommand += commandString[count]; }
303 //if you are not at the end
304 if (count != commandString.length()) { commandString = commandString.substr(count+1, commandString.length()); }
305 else { commandString = ""; }
308 //get rid of spaces in between commands if any
309 if (commandString.length() > 0) {
310 while (commandString[0] == ' ') {
311 commandString = commandString.substr(1,commandString.length());
312 if (commandString.length() == 0) { break; }
318 catch(exception& e) {
319 mout->errorOut(e, "ScriptEngine", "getNextCommand");
323 /***********************************************************************/