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.
18 /***********************************************************************/
20 InteractEngine::InteractEngine(string path){
22 globaldata = GlobalData::getInstance();
23 globaldata->argv = path;
26 /***********************************************************************/
28 InteractEngine::~InteractEngine(){}
30 /***********************************************************************/
31 //This function allows the user to input commands one line at a time until they quit.
32 //If the command is garbage it does nothing.
33 bool InteractEngine::getInput(){
36 string commandName = "";
38 int quitCommandCalled = 0;
40 while(quitCommandCalled != 1){
46 mothurOutJustToLog("mothur > " + input);
49 //allow user to omit the () on the quit command
50 if (input == "quit") { input = "quit()"; }
52 CommandOptionParser parser(input);
53 commandName = parser.getCommandString();
55 options = parser.getOptionString();
57 if (commandName != "") {
59 //executes valid command
60 Command* command = cFactory->getCommand(commandName, options);
61 quitCommandCalled = command->execute();
64 mothurOut("Your input contains errors. Please try again.");
71 errorOut(e, "InteractEngine", "getInput");
75 /***********************************************************************/
76 string Engine::getCommand() {
78 char* nextCommand = NULL;
80 nextCommand = readline("mothur > ");
81 if(nextCommand != NULL) { add_history(nextCommand); }
86 errorOut(e, "Engine", "getCommand");
90 /***********************************************************************/
91 void Engine::terminateCommand(int dummy) {
93 mothurOut("Stopping command."); mothurOutEndLine();
94 cFactory->getCommand(); //terminates command
97 errorOut(e, "Engine", "terminateCommand");
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;
111 catch(exception& e) {
112 errorOut(e, "BatchEngine", "BatchEngine");
117 /***********************************************************************/
119 BatchEngine::~BatchEngine(){ }
121 /***********************************************************************/
122 //This Function allows the user to run a batchfile containing several commands on Dotur
123 bool BatchEngine::getInput(){
125 //check if this is a valid batchfile
126 if (openedBatch == 1) {
127 mothurOut("unable to open batchfile");
133 string commandName = "";
136 //CommandFactory cFactory;
137 int quitCommandCalled = 0;
139 while(quitCommandCalled == 0){
141 if (inputBatchFile.eof()) { input = "quit()"; }
142 else { input = getline(inputBatchFile); }
144 if (input[0] != '#') {
147 mothurOut("mothur > " + input);
151 //allow user to omit the () on the quit command
152 if (input == "quit") { input = "quit()"; }
154 CommandOptionParser parser(input);
155 commandName = parser.getCommandString();
156 options = parser.getOptionString();
158 if (commandName != "") {
160 //executes valid command
161 Command* command = cFactory->getCommand(commandName, options);
162 quitCommandCalled = command->execute();
164 mothurOut("Invalid.");
169 gobble(inputBatchFile);
172 inputBatchFile.close();
175 catch(exception& e) {
176 errorOut(e, "BatchEngine", "getInput");
182 /***********************************************************************/
183 /***********************************************************************/
184 //This function opens the batchfile to be used by BatchEngine::getInput.
185 ScriptEngine::ScriptEngine(string path, string commandString){
187 globaldata = GlobalData::getInstance();
190 listOfCommands = commandString.substr(1, (commandString.length()-1));
192 globaldata->argv = path;
195 catch(exception& e) {
196 errorOut(e, "ScriptEngine", "ScriptEngine");
201 /***********************************************************************/
203 ScriptEngine::~ScriptEngine(){ }
205 /***********************************************************************/
206 //This Function allows the user to run a batchfile containing several commands on mothur
207 bool ScriptEngine::getInput(){
211 string commandName = "";
215 //CommandFactory cFactory;
216 int quitCommandCalled = 0;
218 while(quitCommandCalled == 0){
220 input = getNextCommand(listOfCommands);
222 if (input == "") { input = "quit()"; }
226 mothurOut("mothur > " + input);
230 //allow user to omit the () on the quit command
231 if (input == "quit") { input = "quit()"; }
233 CommandOptionParser parser(input);
234 commandName = parser.getCommandString();
235 options = parser.getOptionString();
237 if (commandName != "") {
239 //executes valid command
240 Command* command = cFactory->getCommand(commandName, options);
241 quitCommandCalled = command->execute();
243 mothurOut("Invalid.");
251 catch(exception& e) {
252 errorOut(e, "ScriptEngine", "getInput");
256 /***********************************************************************/
257 string ScriptEngine::getNextCommand(string& commandString) {
259 string nextcommand = "";
262 //go through string until you reach ; or end
263 while (count < commandString.length()) {
265 if (commandString[count] == ';') { break; }
266 else { nextcommand += commandString[count]; }
271 //if you are not at the end
272 if (count != commandString.length()) { commandString = commandString.substr(count+1, commandString.length()); }
273 else { commandString = ""; }
276 //get rid of spaces in between commands if any
277 if (commandString.length() > 0) {
278 while (commandString[0] == ' ') {
279 commandString = commandString.substr(1,commandString.length());
280 if (commandString.length() == 0) { break; }
286 catch(exception& e) {
287 errorOut(e, "ScriptEngine", "getNextCommand");
291 /***********************************************************************/