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;
25 /***********************************************************************/
27 InteractEngine::~InteractEngine(){}
29 /***********************************************************************/
30 //This function allows the user to input commands one line at a time until they quit.
31 //If the command is garbage it does nothing.
32 bool InteractEngine::getInput(){
35 string commandName = "";
37 int quitCommandCalled = 0;
40 while(quitCommandCalled != 1){
43 mothurOut("mothur > ");
45 //get input char by char so you can check for use of arrow keys
47 // _getch(); // edit : if you want to check the arrow-keys you must call getch twice because special-keys have two values
50 //return 0; // if no key is pressed
51 //setbuf(stdin, NULL); //no buffering
55 if(ch==72) cout<<"up";
56 else if(ch==75) cout<<"left";
57 else if(ch==77) cout<<"right";
58 else if(ch==80) cout<<"down";
68 //while ((letter != 10) && (letter != 13)) {
71 // cout << "char code = " << letter << endl;
73 // input += char(letter);
75 // input = input.substr(0, input.length()-1); //cut off newline char
77 //cout << input << endl;
80 if (cin.eof()) { input = "quit()"; }
83 //previousInputs.push_back(input);
85 mothurOutJustToLog(input);
88 //allow user to omit the () on the quit command
89 if (input == "quit") { input = "quit()"; }
91 CommandOptionParser parser(input);
92 commandName = parser.getCommandString();
93 //cout << " command = " << commandName << endl;
94 options = parser.getOptionString();
96 if (commandName != "") {
98 //executes valid command
99 Command* command = cFactory->getCommand(commandName, options);
100 quitCommandCalled = command->execute();
103 mothurOut("Your input contains errors. Please try again.");
109 catch(exception& e) {
110 errorOut(e, "InteractEngine", "getInput");
114 /***********************************************************************/
115 void Engine::terminateCommand(int dummy) {
117 mothurOut("Stopping command."); mothurOutEndLine();
118 cFactory->getCommand(); //terminates command
120 catch(exception& e) {
121 errorOut(e, "InteractEngine", "terminateCommand");
125 /***********************************************************************/
126 //This function opens the batchfile to be used by BatchEngine::getInput.
127 BatchEngine::BatchEngine(string path, string batchFileName){
129 globaldata = GlobalData::getInstance();
131 openedBatch = openInputFile(batchFileName, inputBatchFile);
132 globaldata->argv = path;
135 catch(exception& e) {
136 errorOut(e, "BatchEngine", "BatchEngine");
141 /***********************************************************************/
143 BatchEngine::~BatchEngine(){ }
145 /***********************************************************************/
146 //This Function allows the user to run a batchfile containing several commands on Dotur
147 bool BatchEngine::getInput(){
149 //check if this is a valid batchfile
150 if (openedBatch == 1) {
151 mothurOut("unable to open batchfile");
157 string commandName = "";
160 //CommandFactory cFactory;
161 int quitCommandCalled = 0;
163 while(quitCommandCalled == 0){
165 if (inputBatchFile.eof()) { input = "quit()"; }
166 else { input = getline(inputBatchFile); }
168 if (input[0] != '#') {
171 mothurOut("mothur > " + input);
175 //allow user to omit the () on the quit command
176 if (input == "quit") { input = "quit()"; }
178 CommandOptionParser parser(input);
179 commandName = parser.getCommandString();
180 options = parser.getOptionString();
182 if (commandName != "") {
184 //executes valid command
185 Command* command = cFactory->getCommand(commandName, options);
186 quitCommandCalled = command->execute();
188 mothurOut("Invalid.");
193 gobble(inputBatchFile);
196 inputBatchFile.close();
199 catch(exception& e) {
200 errorOut(e, "BatchEngine", "getInput");
206 /***********************************************************************/
207 /***********************************************************************/
208 //This function opens the batchfile to be used by BatchEngine::getInput.
209 ScriptEngine::ScriptEngine(string path, string commandString){
211 globaldata = GlobalData::getInstance();
214 listOfCommands = commandString.substr(1, (commandString.length()-1));
216 globaldata->argv = path;
219 catch(exception& e) {
220 errorOut(e, "ScriptEngine", "ScriptEngine");
225 /***********************************************************************/
227 ScriptEngine::~ScriptEngine(){ }
229 /***********************************************************************/
230 //This Function allows the user to run a batchfile containing several commands on mothur
231 bool ScriptEngine::getInput(){
235 string commandName = "";
239 //CommandFactory cFactory;
240 int quitCommandCalled = 0;
242 while(quitCommandCalled == 0){
244 input = getNextCommand(listOfCommands);
246 if (input == "") { input = "quit()"; }
250 mothurOut("mothur > " + input);
254 //allow user to omit the () on the quit command
255 if (input == "quit") { input = "quit()"; }
257 CommandOptionParser parser(input);
258 commandName = parser.getCommandString();
259 options = parser.getOptionString();
261 if (commandName != "") {
263 //executes valid command
264 Command* command = cFactory->getCommand(commandName, options);
265 quitCommandCalled = command->execute();
267 mothurOut("Invalid.");
275 catch(exception& e) {
276 errorOut(e, "ScriptEngine", "getInput");
280 /***********************************************************************/
281 string ScriptEngine::getNextCommand(string& commandString) {
283 string nextcommand = "";
286 //go through string until you reach ; or end
287 while (count < commandString.length()) {
289 if (commandString[count] == ';') { break; }
290 else { nextcommand += commandString[count]; }
295 //if you are not at the end
296 if (count != commandString.length()) { commandString = commandString.substr(count+1, commandString.length()); }
297 else { commandString = ""; }
300 //get rid of spaces in between commands if any
301 if (commandString.length() > 0) {
302 while (commandString[0] == ' ') {
303 commandString = commandString.substr(1,commandString.length());
304 if (commandString.length() == 0) { break; }
310 catch(exception& e) {
311 errorOut(e, "ScriptEngine", "getNextCommand");
315 /***********************************************************************/