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;
23 string logFileName = "mothur.logFile";
24 remove(logFileName.c_str());
29 /***********************************************************************/
31 InteractEngine::~InteractEngine(){
34 /***********************************************************************/
35 //This function allows the user to input commands one line at a time until they quit.
36 //If the command is garbage it does nothing.
37 bool InteractEngine::getInput(){
40 string commandName = "";
42 int quitCommandCalled = 0;
44 mothurOut("mothur v.1.4.1");
46 mothurOut("Last updated: 6/23/2009");
51 mothurOut("Patrick D. Schloss");
54 mothurOut("Department of Microbiology");
56 mothurOut("pschloss@micro.umass.edu");
58 mothurOut("http://schloss.micro.umass.edu/mothur");
62 mothurOut("Distributed under the GNU General Public License");
65 mothurOut("Type 'help()' for information on the commands that are available");
68 mothurOut("Type 'quit()' to exit program");
71 while(quitCommandCalled != 1){
74 mothurOut("mothur > ");
76 if (cin.eof()) { input = "quit()"; }
78 mothurOutJustToLog(input);
81 //allow user to omit the () on the quit command
82 if (input == "quit") { input = "quit()"; }
84 CommandOptionParser parser(input);
85 commandName = parser.getCommandString();
86 options = parser.getOptionString();
88 if (commandName != "") {
90 //executes valid command
91 CommandFactory cFactory;
92 Command* command = cFactory.getCommand(commandName, options);
93 quitCommandCalled = command->execute();
96 mothurOut("Your input contains errors. Please try again.");
102 catch(exception& e) {
103 errorOut(e, "InteractEngine", "getInput");
108 /***********************************************************************/
109 //This function opens the batchfile to be used by BatchEngine::getInput.
110 BatchEngine::BatchEngine(string path, string batchFileName){
112 globaldata = GlobalData::getInstance();
114 openedBatch = openInputFile(batchFileName, inputBatchFile);
115 globaldata->argv = path;
116 string logFileName = "mothur.logFile";
117 remove(logFileName.c_str());
121 // char buffer = ' ';
122 // ifstream header("introtext.txt");
123 // while(!header.eof()){
125 // buffer = header.get();
128 catch(exception& e) {
129 errorOut(e, "BatchEngine", "BatchEngine");
134 /***********************************************************************/
136 BatchEngine::~BatchEngine(){
139 /***********************************************************************/
140 //This Function allows the user to run a batchfile containing several commands on Dotur
141 bool BatchEngine::getInput(){
143 //check if this is a valid batchfile
144 if (openedBatch == 1) {
145 mothurOut("unable to open batchfile");
151 string commandName = "";
154 //CommandFactory cFactory;
155 int quitCommandCalled = 0;
157 while(quitCommandCalled == 0){
159 if (inputBatchFile.eof()) { input = "quit()"; }
160 else { getline(inputBatchFile, input); }
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 CommandFactory cFactory;
182 Command* command = cFactory.getCommand(commandName, options);
183 quitCommandCalled = command->execute();
185 mothurOut("Invalid.");
190 gobble(inputBatchFile);
193 inputBatchFile.close();
196 catch(exception& e) {
197 errorOut(e, "BatchEngine", "getInput");
203 /***********************************************************************/
204 /***********************************************************************/
205 //This function opens the batchfile to be used by BatchEngine::getInput.
206 ScriptEngine::ScriptEngine(string path, string commandString){
208 globaldata = GlobalData::getInstance();
211 listOfCommands = commandString.substr(1, (commandString.length()-1));
213 globaldata->argv = path;
214 string logFileName = "mothur.logFile";
215 remove(logFileName.c_str());
220 catch(exception& e) {
221 errorOut(e, "ScriptEngine", "ScriptEngine");
226 /***********************************************************************/
228 ScriptEngine::~ScriptEngine(){
231 /***********************************************************************/
232 //This Function allows the user to run a batchfile containing several commands on mothur
233 bool ScriptEngine::getInput(){
237 string commandName = "";
241 //CommandFactory cFactory;
242 int quitCommandCalled = 0;
244 while(quitCommandCalled == 0){
246 input = getNextCommand(listOfCommands);
248 if (input == "") { input = "quit()"; }
252 mothurOut("mothur > " + input);
256 //allow user to omit the () on the quit command
257 if (input == "quit") { input = "quit()"; }
259 CommandOptionParser parser(input);
260 commandName = parser.getCommandString();
261 options = parser.getOptionString();
263 if (commandName != "") {
265 //executes valid command
266 CommandFactory cFactory;
267 Command* command = cFactory.getCommand(commandName, options);
268 quitCommandCalled = command->execute();
270 mothurOut("Invalid.");
278 catch(exception& e) {
279 errorOut(e, "ScriptEngine", "getInput");
283 /***********************************************************************/
284 string ScriptEngine::getNextCommand(string& commandString) {
286 string nextcommand = "";
288 nextcommand = commandString.substr(0,commandString.find_first_of(';'));
291 if ((commandString.find_first_of(';')+1) <= commandString.length()) {
292 commandString = commandString.substr(commandString.find_first_of(';')+1, commandString.length());
293 }else { commandString = ""; } //you have reached the last command.
295 //get rid of spaces in between commands if any
296 if (commandString.length() > 0) {
297 while (commandString[0] == ' ') {
298 commandString = commandString.substr(1,commandString.length());
299 if (commandString.length() == 0) { break; }
305 catch(exception& e) {
306 errorOut(e, "ScriptEngine", "getNextCommand");
310 /***********************************************************************/