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;
26 // ifstream header("introtext.txt");
27 // while(!header.eof()){
29 // buffer = header.get();
33 /***********************************************************************/
35 InteractEngine::~InteractEngine(){
38 /***********************************************************************/
39 //This function allows the user to input commands one line at a time until they quit.
40 //If the command is garbage it does nothing.
41 bool InteractEngine::getInput(){
44 string commandName = "";
46 int quitCommandCalled = 0;
48 //ErrorCheck* errorCheckor = new ErrorCheck();
50 cout << "mothur v.1.4.0" << endl;
51 cout << "Last updated: 6/21/2009" << endl << endl;
53 cout << "Patrick D. Schloss" << endl << endl;
54 cout << "Department of Microbiology" << endl;
55 cout << "The University of Massachusetts" << endl;
56 cout << "pschloss@micro.umass.edu" << endl;
57 cout << "http://schloss.micro.umass.edu/mothur" << endl << endl << endl;
58 cout << "Distributed under the GNU General Public License" << endl << endl;
59 cout << "Type 'help()' for information on the commands that are available" << endl << endl;
60 cout << "Type 'quit()' to exit program" << endl;
62 while(quitCommandCalled != 1){
64 cout << endl << "mothur > ";
66 if (cin.eof()) { input = "quit()"; }
68 //allow user to omit the () on the quit command
69 if (input == "quit") { input = "quit()"; }
71 //errorFree = errorCheckor->checkInput(input);
72 //if (errorFree == true) {
73 CommandOptionParser parser(input);
74 commandName = parser.getCommandString();
75 options = parser.getOptionString();
77 if (commandName != "") {
79 //executes valid command
80 CommandFactory cFactory;
81 Command* command = cFactory.getCommand(commandName, options);
82 quitCommandCalled = command->execute();
85 cout << "Your input contains errors. Please try again." << endl;
91 cout << "Standard Error: " << e.what() << " has occurred in the InteractEngine class Function getInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
95 cout << "An unknown error has occurred in the InteractEngine class function getInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
101 /***********************************************************************/
102 //This function opens the batchfile to be used by BatchEngine::getInput.
103 BatchEngine::BatchEngine(string path, string batchFileName){
105 globaldata = GlobalData::getInstance();
107 openedBatch = openInputFile(batchFileName, inputBatchFile);
108 globaldata->argv = path;
112 // char buffer = ' ';
113 // ifstream header("introtext.txt");
114 // while(!header.eof()){
116 // buffer = header.get();
119 catch(exception& e) {
120 cout << "Standard Error: " << e.what() << " has occurred in the BatchEngine class Function BatchEngine. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
124 cout << "An unknown error has occurred in the BatchEngine class function BatchEngine. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
129 /***********************************************************************/
131 BatchEngine::~BatchEngine(){
134 /***********************************************************************/
135 //This Function allows the user to run a batchfile containing several commands on Dotur
136 bool BatchEngine::getInput(){
138 //check if this is a valid batchfile
139 if (openedBatch == 1) { cout << "unable to open batchfile" << endl; return 1; }
142 string commandName = "";
146 //CommandFactory cFactory;
147 int quitCommandCalled = 0;
149 while(quitCommandCalled == 0){
151 if (inputBatchFile.eof()) { input = "quit()"; }
152 else { getline(inputBatchFile, input); }
154 //cout << "command number" << count << endl; count++;
156 if (input[0] != '#') {
158 cout << endl << "mothur > " << input << endl;
160 //allow user to omit the () on the quit command
161 if (input == "quit") { input = "quit()"; }
163 CommandOptionParser parser(input);
164 commandName = parser.getCommandString();
165 options = parser.getOptionString();
167 if (commandName != "") {
169 //executes valid command
170 CommandFactory cFactory;
171 Command* command = cFactory.getCommand(commandName, options);
172 quitCommandCalled = command->execute();
173 }else { cout << "Invalid." << endl; }
176 gobble(inputBatchFile);
179 inputBatchFile.close();
182 catch(exception& e) {
183 cout << "Standard Error: " << e.what() << " has occurred in the BatchEngine class Function getInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
187 cout << "An unknown error has occurred in the BatchEngine class function getInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
193 /***********************************************************************/
194 /***********************************************************************/
195 //This function opens the batchfile to be used by BatchEngine::getInput.
196 ScriptEngine::ScriptEngine(string path, string commandString){
198 globaldata = GlobalData::getInstance();
201 listOfCommands = commandString.substr(1, (commandString.length()-1));
203 globaldata->argv = path;
208 catch(exception& e) {
209 cout << "Standard Error: " << e.what() << " has occurred in the ScriptEngine class Function ScriptEngine. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
213 cout << "An unknown error has occurred in the ScriptEngine class function ScriptEngine. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
218 /***********************************************************************/
220 ScriptEngine::~ScriptEngine(){
223 /***********************************************************************/
224 //This Function allows the user to run a batchfile containing several commands on Dotur
225 bool ScriptEngine::getInput(){
229 string commandName = "";
233 //CommandFactory cFactory;
234 int quitCommandCalled = 0;
236 while(quitCommandCalled == 0){
238 input = getNextCommand(listOfCommands);
240 if (input == "") { input = "quit()"; }
241 //cout << "command number" << count << endl; count++;
243 cout << endl << "mothur > " << input << endl;
245 //allow user to omit the () on the quit command
246 if (input == "quit") { input = "quit()"; }
248 CommandOptionParser parser(input);
249 commandName = parser.getCommandString();
250 options = parser.getOptionString();
252 if (commandName != "") {
254 //executes valid command
255 CommandFactory cFactory;
256 Command* command = cFactory.getCommand(commandName, options);
257 quitCommandCalled = command->execute();
258 }else { cout << "Invalid." << endl; }
264 catch(exception& e) {
265 cout << "Standard Error: " << e.what() << " has occurred in the ScriptEngine class Function getInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
269 cout << "An unknown error has occurred in the ScriptEngine class function getInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
273 /***********************************************************************/
274 string ScriptEngine::getNextCommand(string& commandString) {
276 string nextcommand = "";
278 nextcommand = commandString.substr(0,commandString.find_first_of(';'));
281 if ((commandString.find_first_of(';')+1) <= commandString.length()) {
282 commandString = commandString.substr(commandString.find_first_of(';')+1, commandString.length());
283 }else { commandString = ""; } //you have reached the last command.
285 //get rid of spaces in between commands if any
286 if (commandString.length() > 0) {
287 while (commandString[0] == ' ') {
288 commandString = commandString.substr(1,commandString.length());
289 if (commandString.length() == 0) { break; }
295 catch(exception& e) {
296 cout << "Standard Error: " << e.what() << " has occurred in the ScriptEngine class Function getNextCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
300 cout << "An unknown error has occurred in the ScriptEngine class function getNextCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
304 /***********************************************************************/