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 { getline(inputBatchFile, input); }
123 //cout << "input = ";
124 //for (int i = 0; i < input.size(); i++) { cout << input[i]; if ((input[i] == '\n') || (input[i] == '\r')) { char c = input[i]; cout << "line feed = " << c << endl; } }
127 if (input[0] != '#') {
130 mothurOut("mothur > " + input);
134 //allow user to omit the () on the quit command
135 if (input == "quit") { input = "quit()"; }
137 CommandOptionParser parser(input);
138 commandName = parser.getCommandString();
139 options = parser.getOptionString();
141 if (commandName != "") {
143 //executes valid command
144 CommandFactory cFactory;
145 Command* command = cFactory.getCommand(commandName, options);
146 quitCommandCalled = command->execute();
148 mothurOut("Invalid.");
153 gobble(inputBatchFile);
156 inputBatchFile.close();
159 catch(exception& e) {
160 errorOut(e, "BatchEngine", "getInput");
166 /***********************************************************************/
167 /***********************************************************************/
168 //This function opens the batchfile to be used by BatchEngine::getInput.
169 ScriptEngine::ScriptEngine(string path, string commandString){
171 globaldata = GlobalData::getInstance();
174 listOfCommands = commandString.substr(1, (commandString.length()-1));
176 globaldata->argv = path;
181 catch(exception& e) {
182 errorOut(e, "ScriptEngine", "ScriptEngine");
187 /***********************************************************************/
189 ScriptEngine::~ScriptEngine(){
192 /***********************************************************************/
193 //This Function allows the user to run a batchfile containing several commands on mothur
194 bool ScriptEngine::getInput(){
198 string commandName = "";
202 //CommandFactory cFactory;
203 int quitCommandCalled = 0;
205 while(quitCommandCalled == 0){
207 input = getNextCommand(listOfCommands);
209 if (input == "") { input = "quit()"; }
213 mothurOut("mothur > " + input);
217 //allow user to omit the () on the quit command
218 if (input == "quit") { input = "quit()"; }
220 CommandOptionParser parser(input);
221 commandName = parser.getCommandString();
222 options = parser.getOptionString();
224 if (commandName != "") {
226 //executes valid command
227 CommandFactory cFactory;
228 Command* command = cFactory.getCommand(commandName, options);
229 quitCommandCalled = command->execute();
231 mothurOut("Invalid.");
239 catch(exception& e) {
240 errorOut(e, "ScriptEngine", "getInput");
244 /***********************************************************************/
245 string ScriptEngine::getNextCommand(string& commandString) {
247 string nextcommand = "";
250 //go through string until you reach ; or end
251 while (count < commandString.length()) {
253 if (commandString[count] == ';') { break; }
254 else { nextcommand += commandString[count]; }
259 //if you are not at the end
260 if (count != commandString.length()) { commandString = commandString.substr(count+1, commandString.length()); }
261 else { commandString = ""; }
264 //get rid of spaces in between commands if any
265 if (commandString.length() > 0) {
266 while (commandString[0] == ' ') {
267 commandString = commandString.substr(1,commandString.length());
268 if (commandString.length() == 0) { break; }
274 catch(exception& e) {
275 errorOut(e, "ScriptEngine", "getNextCommand");
279 /***********************************************************************/