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();
74 mout->mothurOut("Your input contains errors. Please try again.");
75 mout->mothurOutEndLine();
81 mout->errorOut(e, "InteractEngine", "getInput");
85 /***********************************************************************/
86 string Engine::getCommand() {
88 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
90 char* nextCommand = NULL;
91 nextCommand = readline("mothur > ");
93 if(nextCommand != NULL) { add_history(nextCommand); }
94 else{ //^D causes null string and we want it to quit mothur
96 cout << nextCommand << endl;
99 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
102 string nextCommand = "";
103 mout->mothurOut("mothur > ");
104 getline(cin, nextCommand);
105 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
109 string nextCommand = "";
110 mout->mothurOut("mothur > ");
111 getline(cin, nextCommand);
112 mout->mothurOutJustToLog("mothur > " + toString(nextCommand));
116 mout->mothurOutEndLine();
119 catch(exception& e) {
120 mout->errorOut(e, "Engine", "getCommand");
124 /***********************************************************************/
125 //This function opens the batchfile to be used by BatchEngine::getInput.
126 BatchEngine::BatchEngine(string path, string batchFileName){
128 globaldata = GlobalData::getInstance();
130 openedBatch = openInputFile(batchFileName, inputBatchFile);
131 globaldata->argv = path;
134 catch(exception& e) {
135 mout->errorOut(e, "BatchEngine", "BatchEngine");
140 /***********************************************************************/
142 BatchEngine::~BatchEngine(){ }
144 /***********************************************************************/
145 //This Function allows the user to run a batchfile containing several commands on Dotur
146 bool BatchEngine::getInput(){
148 //check if this is a valid batchfile
149 if (openedBatch == 1) {
150 mout->mothurOut("unable to open batchfile");
151 mout->mothurOutEndLine();
156 string commandName = "";
159 //CommandFactory cFactory;
160 int quitCommandCalled = 0;
162 while(quitCommandCalled == 0){
164 if (inputBatchFile.eof()) { input = "quit()"; }
165 else { input = getline(inputBatchFile); }
167 if (input[0] != '#') {
169 mout->mothurOutEndLine();
170 mout->mothurOut("mothur > " + input);
171 mout->mothurOutEndLine();
174 //allow user to omit the () on the quit command
175 if (input == "quit") { input = "quit()"; }
177 CommandOptionParser parser(input);
178 commandName = parser.getCommandString();
179 options = parser.getOptionString();
181 if (commandName != "") {
183 //executes valid command
184 Command* command = cFactory->getCommand(commandName, options);
185 quitCommandCalled = command->execute();
187 mout->mothurOut("Invalid.");
188 mout->mothurOutEndLine();
192 gobble(inputBatchFile);
195 inputBatchFile.close();
198 catch(exception& e) {
199 mout->errorOut(e, "BatchEngine", "getInput");
205 /***********************************************************************/
206 /***********************************************************************/
207 //This function opens the batchfile to be used by BatchEngine::getInput.
208 ScriptEngine::ScriptEngine(string path, string commandString){
210 globaldata = GlobalData::getInstance();
213 listOfCommands = commandString.substr(1, (commandString.length()-1));
215 globaldata->argv = path;
218 catch(exception& e) {
219 mout->errorOut(e, "ScriptEngine", "ScriptEngine");
224 /***********************************************************************/
226 ScriptEngine::~ScriptEngine(){ }
228 /***********************************************************************/
229 //This Function allows the user to run a batchfile containing several commands on mothur
230 bool ScriptEngine::getInput(){
234 string commandName = "";
238 //CommandFactory cFactory;
239 int quitCommandCalled = 0;
241 while(quitCommandCalled == 0){
243 input = getNextCommand(listOfCommands);
245 if (input == "") { input = "quit()"; }
248 mout->mothurOutEndLine();
249 mout->mothurOut("mothur > " + input);
250 mout->mothurOutEndLine();
253 //allow user to omit the () on the quit command
254 if (input == "quit") { input = "quit()"; }
256 CommandOptionParser parser(input);
257 commandName = parser.getCommandString();
258 options = parser.getOptionString();
260 if (commandName != "") {
262 //executes valid command
263 Command* command = cFactory->getCommand(commandName, options);
264 quitCommandCalled = command->execute();
266 mout->mothurOut("Invalid.");
267 mout->mothurOutEndLine();
274 catch(exception& e) {
275 mout->errorOut(e, "ScriptEngine", "getInput");
279 /***********************************************************************/
280 string ScriptEngine::getNextCommand(string& commandString) {
282 string nextcommand = "";
285 //go through string until you reach ; or end
286 while (count < commandString.length()) {
288 if (commandString[count] == ';') { break; }
289 else { nextcommand += commandString[count]; }
294 //if you are not at the end
295 if (count != commandString.length()) { commandString = commandString.substr(count+1, commandString.length()); }
296 else { commandString = ""; }
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; }
309 catch(exception& e) {
310 mout->errorOut(e, "ScriptEngine", "getNextCommand");
314 /***********************************************************************/