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;
92 // ifstream header("introtext.txt");
93 // while(!header.eof()){
95 // buffer = header.get();
99 errorOut(e, "BatchEngine", "BatchEngine");
104 /***********************************************************************/
106 BatchEngine::~BatchEngine(){
109 /***********************************************************************/
110 //This Function allows the user to run a batchfile containing several commands on Dotur
111 bool BatchEngine::getInput(){
113 //check if this is a valid batchfile
114 if (openedBatch == 1) {
115 mothurOut("unable to open batchfile");
121 string commandName = "";
124 //CommandFactory cFactory;
125 int quitCommandCalled = 0;
127 while(quitCommandCalled == 0){
129 if (inputBatchFile.eof()) { input = "quit()"; }
130 else { getline(inputBatchFile, input); }
134 if (input[0] != '#') {
137 mothurOut("mothur > " + input);
141 //allow user to omit the () on the quit command
142 if (input == "quit") { input = "quit()"; }
144 CommandOptionParser parser(input);
145 commandName = parser.getCommandString();
146 options = parser.getOptionString();
148 if (commandName != "") {
150 //executes valid command
151 CommandFactory cFactory;
152 Command* command = cFactory.getCommand(commandName, options);
153 quitCommandCalled = command->execute();
155 mothurOut("Invalid.");
160 gobble(inputBatchFile);
163 inputBatchFile.close();
166 catch(exception& e) {
167 errorOut(e, "BatchEngine", "getInput");
173 /***********************************************************************/
174 /***********************************************************************/
175 //This function opens the batchfile to be used by BatchEngine::getInput.
176 ScriptEngine::ScriptEngine(string path, string commandString){
178 globaldata = GlobalData::getInstance();
181 listOfCommands = commandString.substr(1, (commandString.length()-1));
183 globaldata->argv = path;
188 catch(exception& e) {
189 errorOut(e, "ScriptEngine", "ScriptEngine");
194 /***********************************************************************/
196 ScriptEngine::~ScriptEngine(){
199 /***********************************************************************/
200 //This Function allows the user to run a batchfile containing several commands on mothur
201 bool ScriptEngine::getInput(){
205 string commandName = "";
209 //CommandFactory cFactory;
210 int quitCommandCalled = 0;
212 while(quitCommandCalled == 0){
214 input = getNextCommand(listOfCommands);
216 if (input == "") { input = "quit()"; }
220 mothurOut("mothur > " + input);
224 //allow user to omit the () on the quit command
225 if (input == "quit") { input = "quit()"; }
227 CommandOptionParser parser(input);
228 commandName = parser.getCommandString();
229 options = parser.getOptionString();
231 if (commandName != "") {
233 //executes valid command
234 CommandFactory cFactory;
235 Command* command = cFactory.getCommand(commandName, options);
236 quitCommandCalled = command->execute();
238 mothurOut("Invalid.");
246 catch(exception& e) {
247 errorOut(e, "ScriptEngine", "getInput");
251 /***********************************************************************/
252 string ScriptEngine::getNextCommand(string& commandString) {
254 string nextcommand = "";
256 nextcommand = commandString.substr(0,commandString.find_first_of(';'));
259 if ((commandString.find_first_of(';')+1) <= commandString.length()) {
260 commandString = commandString.substr(commandString.find_first_of(';')+1, commandString.length());
261 }else { commandString = ""; } //you have reached the last command.
263 //get rid of spaces in between commands if any
264 if (commandString.length() > 0) {
265 while (commandString[0] == ' ') {
266 commandString = commandString.substr(1,commandString.length());
267 if (commandString.length() == 0) { break; }
273 catch(exception& e) {
274 errorOut(e, "ScriptEngine", "getNextCommand");
278 /***********************************************************************/