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();
23 errorOut(e, "Engine", "Engine");
28 /***********************************************************************/
30 InteractEngine::InteractEngine(string path){
32 globaldata = GlobalData::getInstance();
33 globaldata->argv = path;
36 /***********************************************************************/
38 InteractEngine::~InteractEngine(){}
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(){
46 string commandName = "";
48 int quitCommandCalled = 0;
50 while(quitCommandCalled != 1){
56 //allow user to omit the () on the quit command
57 if (input == "quit") { input = "quit()"; }
59 CommandOptionParser parser(input);
60 commandName = parser.getCommandString();
62 options = parser.getOptionString();
64 if (commandName != "") {
66 //executes valid command
67 Command* command = cFactory->getCommand(commandName, options);
68 quitCommandCalled = command->execute();
71 mothurOut("Your input contains errors. Please try again.");
78 errorOut(e, "InteractEngine", "getInput");
82 /***********************************************************************/
83 string Engine::getCommand() {
85 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
87 char* nextCommand = NULL;
88 nextCommand = readline("mothur > ");
90 if(nextCommand != NULL) { add_history(nextCommand); }
91 else{ //^D causes null string and we want it to quit mothur
93 cout << nextCommand << endl;
96 mothurOutJustToLog("mothur > " + toString(nextCommand));
99 string nextCommand = "";
100 mothurOut("mothur > ");
101 getline(cin, nextCommand);
102 mothurOutJustToLog("mothur > " + toString(nextCommand));
106 string nextCommand = "";
107 mothurOut("mothur > ");
108 getline(cin, nextCommand);
109 mothurOutJustToLog("mothur > " + toString(nextCommand));
116 catch(exception& e) {
117 errorOut(e, "Engine", "getCommand");
121 /***********************************************************************/
122 //This function opens the batchfile to be used by BatchEngine::getInput.
123 BatchEngine::BatchEngine(string path, string batchFileName){
125 globaldata = GlobalData::getInstance();
127 openedBatch = openInputFile(batchFileName, inputBatchFile);
128 globaldata->argv = path;
131 catch(exception& e) {
132 errorOut(e, "BatchEngine", "BatchEngine");
137 /***********************************************************************/
139 BatchEngine::~BatchEngine(){ }
141 /***********************************************************************/
142 //This Function allows the user to run a batchfile containing several commands on Dotur
143 bool BatchEngine::getInput(){
145 //check if this is a valid batchfile
146 if (openedBatch == 1) {
147 mothurOut("unable to open batchfile");
153 string commandName = "";
156 //CommandFactory cFactory;
157 int quitCommandCalled = 0;
159 while(quitCommandCalled == 0){
161 if (inputBatchFile.eof()) { input = "quit()"; }
162 else { input = getline(inputBatchFile); }
164 if (input[0] != '#') {
167 mothurOut("mothur > " + input);
171 //allow user to omit the () on the quit command
172 if (input == "quit") { input = "quit()"; }
174 CommandOptionParser parser(input);
175 commandName = parser.getCommandString();
176 options = parser.getOptionString();
178 if (commandName != "") {
180 //executes valid command
181 Command* command = cFactory->getCommand(commandName, options);
182 quitCommandCalled = command->execute();
184 mothurOut("Invalid.");
189 gobble(inputBatchFile);
192 inputBatchFile.close();
195 catch(exception& e) {
196 errorOut(e, "BatchEngine", "getInput");
202 /***********************************************************************/
203 /***********************************************************************/
204 //This function opens the batchfile to be used by BatchEngine::getInput.
205 ScriptEngine::ScriptEngine(string path, string commandString){
207 globaldata = GlobalData::getInstance();
210 listOfCommands = commandString.substr(1, (commandString.length()-1));
212 globaldata->argv = path;
215 catch(exception& e) {
216 errorOut(e, "ScriptEngine", "ScriptEngine");
221 /***********************************************************************/
223 ScriptEngine::~ScriptEngine(){ }
225 /***********************************************************************/
226 //This Function allows the user to run a batchfile containing several commands on mothur
227 bool ScriptEngine::getInput(){
231 string commandName = "";
235 //CommandFactory cFactory;
236 int quitCommandCalled = 0;
238 while(quitCommandCalled == 0){
240 input = getNextCommand(listOfCommands);
242 if (input == "") { input = "quit()"; }
246 mothurOut("mothur > " + input);
250 //allow user to omit the () on the quit command
251 if (input == "quit") { input = "quit()"; }
253 CommandOptionParser parser(input);
254 commandName = parser.getCommandString();
255 options = parser.getOptionString();
257 if (commandName != "") {
259 //executes valid command
260 Command* command = cFactory->getCommand(commandName, options);
261 quitCommandCalled = command->execute();
263 mothurOut("Invalid.");
271 catch(exception& e) {
272 errorOut(e, "ScriptEngine", "getInput");
276 /***********************************************************************/
277 string ScriptEngine::getNextCommand(string& commandString) {
279 string nextcommand = "";
282 //go through string until you reach ; or end
283 while (count < commandString.length()) {
285 if (commandString[count] == ';') { break; }
286 else { nextcommand += commandString[count]; }
291 //if you are not at the end
292 if (count != commandString.length()) { commandString = commandString.substr(count+1, commandString.length()); }
293 else { commandString = ""; }
296 //get rid of spaces in between commands if any
297 if (commandString.length() > 0) {
298 while (commandString[0] == ' ') {
299 commandString = commandString.substr(1,commandString.length());
300 if (commandString.length() == 0) { break; }
306 catch(exception& e) {
307 errorOut(e, "ScriptEngine", "getNextCommand");
311 /***********************************************************************/