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); }
124 if (input[0] != '#') {
127 mothurOut("mothur > " + input);
131 //allow user to omit the () on the quit command
132 if (input == "quit") { input = "quit()"; }
134 CommandOptionParser parser(input);
135 commandName = parser.getCommandString();
136 options = parser.getOptionString();
138 if (commandName != "") {
140 //executes valid command
141 CommandFactory cFactory;
142 Command* command = cFactory.getCommand(commandName, options);
143 quitCommandCalled = command->execute();
145 mothurOut("Invalid.");
150 gobble(inputBatchFile);
153 inputBatchFile.close();
156 catch(exception& e) {
157 errorOut(e, "BatchEngine", "getInput");
163 /***********************************************************************/
164 /***********************************************************************/
165 //This function opens the batchfile to be used by BatchEngine::getInput.
166 ScriptEngine::ScriptEngine(string path, string commandString){
168 globaldata = GlobalData::getInstance();
171 listOfCommands = commandString.substr(1, (commandString.length()-1));
173 globaldata->argv = path;
178 catch(exception& e) {
179 errorOut(e, "ScriptEngine", "ScriptEngine");
184 /***********************************************************************/
186 ScriptEngine::~ScriptEngine(){
189 /***********************************************************************/
190 //This Function allows the user to run a batchfile containing several commands on mothur
191 bool ScriptEngine::getInput(){
195 string commandName = "";
199 //CommandFactory cFactory;
200 int quitCommandCalled = 0;
202 while(quitCommandCalled == 0){
204 input = getNextCommand(listOfCommands);
206 if (input == "") { input = "quit()"; }
210 mothurOut("mothur > " + input);
214 //allow user to omit the () on the quit command
215 if (input == "quit") { input = "quit()"; }
217 CommandOptionParser parser(input);
218 commandName = parser.getCommandString();
219 options = parser.getOptionString();
221 if (commandName != "") {
223 //executes valid command
224 CommandFactory cFactory;
225 Command* command = cFactory.getCommand(commandName, options);
226 quitCommandCalled = command->execute();
228 mothurOut("Invalid.");
236 catch(exception& e) {
237 errorOut(e, "ScriptEngine", "getInput");
241 /***********************************************************************/
242 string ScriptEngine::getNextCommand(string& commandString) {
244 string nextcommand = "";
247 //go through string until you reach ; or end
248 while (count < commandString.length()) {
250 if (commandString[count] == ';') { break; }
251 else { nextcommand += commandString[count]; }
256 //if you are not at the end
257 if (count != commandString.length()) { commandString = commandString.substr(count+1, commandString.length()); }
258 else { commandString = ""; }
261 //get rid of spaces in between commands if any
262 if (commandString.length() > 0) {
263 while (commandString[0] == ' ') {
264 commandString = commandString.substr(1,commandString.length());
265 if (commandString.length() == 0) { break; }
271 catch(exception& e) {
272 errorOut(e, "ScriptEngine", "getNextCommand");
276 /***********************************************************************/