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 /***********************************************************************/
19 InteractEngine::InteractEngine(string path){
21 globaldata = GlobalData::getInstance();
22 globaldata->argv = path;
27 /***********************************************************************/
29 InteractEngine::~InteractEngine(){
32 /***********************************************************************/
33 //This function allows the user to input commands one line at a time until they quit.
34 //If the command is garbage it does nothing.
35 bool InteractEngine::getInput(){
38 string commandName = "";
40 int quitCommandCalled = 0;
43 while(quitCommandCalled != 1){
46 mothurOut("mothur > ");
48 if (cin.eof()) { input = "quit()"; }
50 mothurOutJustToLog(input);
53 //allow user to omit the () on the quit command
54 if (input == "quit") { input = "quit()"; }
56 CommandOptionParser parser(input);
57 commandName = parser.getCommandString();
58 options = parser.getOptionString();
60 if (commandName != "") {
62 //executes valid command
63 CommandFactory cFactory;
64 Command* command = cFactory.getCommand(commandName, options);
65 quitCommandCalled = command->execute();
68 mothurOut("Your input contains errors. Please try again.");
75 errorOut(e, "InteractEngine", "getInput");
80 /***********************************************************************/
81 //This function opens the batchfile to be used by BatchEngine::getInput.
82 BatchEngine::BatchEngine(string path, string batchFileName){
84 globaldata = GlobalData::getInstance();
86 openedBatch = openInputFile(batchFileName, inputBatchFile);
87 globaldata->argv = path;
91 errorOut(e, "BatchEngine", "BatchEngine");
96 /***********************************************************************/
98 BatchEngine::~BatchEngine(){
101 /***********************************************************************/
102 //This Function allows the user to run a batchfile containing several commands on Dotur
103 bool BatchEngine::getInput(){
105 //check if this is a valid batchfile
106 if (openedBatch == 1) {
107 mothurOut("unable to open batchfile");
113 string commandName = "";
116 //CommandFactory cFactory;
117 int quitCommandCalled = 0;
119 while(quitCommandCalled == 0){
121 if (inputBatchFile.eof()) { input = "quit()"; }
122 else { input = getline(inputBatchFile); }
126 if (input[0] != '#') {
129 mothurOut("mothur > " + input);
133 //allow user to omit the () on the quit command
134 if (input == "quit") { input = "quit()"; }
136 CommandOptionParser parser(input);
137 commandName = parser.getCommandString();
138 options = parser.getOptionString();
140 if (commandName != "") {
142 //executes valid command
143 CommandFactory cFactory;
144 Command* command = cFactory.getCommand(commandName, options);
145 quitCommandCalled = command->execute();
147 mothurOut("Invalid.");
152 gobble(inputBatchFile);
155 inputBatchFile.close();
158 catch(exception& e) {
159 errorOut(e, "BatchEngine", "getInput");
165 /***********************************************************************/
166 /***********************************************************************/
167 //This function opens the batchfile to be used by BatchEngine::getInput.
168 ScriptEngine::ScriptEngine(string path, string commandString){
170 globaldata = GlobalData::getInstance();
173 listOfCommands = commandString.substr(1, (commandString.length()-1));
175 globaldata->argv = path;
180 catch(exception& e) {
181 errorOut(e, "ScriptEngine", "ScriptEngine");
186 /***********************************************************************/
188 ScriptEngine::~ScriptEngine(){
191 /***********************************************************************/
192 //This Function allows the user to run a batchfile containing several commands on mothur
193 bool ScriptEngine::getInput(){
197 string commandName = "";
201 //CommandFactory cFactory;
202 int quitCommandCalled = 0;
204 while(quitCommandCalled == 0){
206 input = getNextCommand(listOfCommands);
208 if (input == "") { input = "quit()"; }
212 mothurOut("mothur > " + input);
216 //allow user to omit the () on the quit command
217 if (input == "quit") { input = "quit()"; }
219 CommandOptionParser parser(input);
220 commandName = parser.getCommandString();
221 options = parser.getOptionString();
223 if (commandName != "") {
225 //executes valid command
226 CommandFactory cFactory;
227 Command* command = cFactory.getCommand(commandName, options);
228 quitCommandCalled = command->execute();
230 mothurOut("Invalid.");
238 catch(exception& e) {
239 errorOut(e, "ScriptEngine", "getInput");
243 /***********************************************************************/
244 string ScriptEngine::getNextCommand(string& commandString) {
246 string nextcommand = "";
248 nextcommand = commandString.substr(0,commandString.find_first_of(';'));
251 if ((commandString.find_first_of(';')+1) <= commandString.length()) {
252 commandString = commandString.substr(commandString.find_first_of(';')+1, commandString.length());
253 }else { commandString = ""; } //you have reached the last command.
255 //get rid of spaces in between commands if any
256 if (commandString.length() > 0) {
257 while (commandString[0] == ' ') {
258 commandString = commandString.substr(1,commandString.length());
259 if (commandString.length() == 0) { break; }
265 catch(exception& e) {
266 errorOut(e, "ScriptEngine", "getNextCommand");
270 /***********************************************************************/