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 /***********************************************************************/
20 cFactory = CommandFactory::getInstance();
23 errorOut(e, "Engine", "Engine");
28 /***********************************************************************/
30 InteractEngine::InteractEngine(string path){
32 globaldata = GlobalData::getInstance();
33 globaldata->argv = path;
36 /***********************************************************************/
38 InteractEngine::~InteractEngine(){}
40 /***********************************************************************/
41 //This function allows the user to input commands one line at a time until they quit.
42 //If the command is garbage it does nothing.
43 bool InteractEngine::getInput(){
46 string commandName = "";
48 int quitCommandCalled = 0;
50 while(quitCommandCalled != 1){
56 //allow user to omit the () on the quit command
57 if (input == "quit") { input = "quit()"; }
59 CommandOptionParser parser(input);
60 commandName = parser.getCommandString();
62 options = parser.getOptionString();
64 if (commandName != "") {
66 //executes valid command
67 Command* command = cFactory->getCommand(commandName, options);
68 quitCommandCalled = command->execute();
71 mothurOut("Your input contains errors. Please try again.");
78 errorOut(e, "InteractEngine", "getInput");
82 /***********************************************************************/
83 string Engine::getCommand() {
85 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
87 char* nextCommand = NULL;
88 nextCommand = readline("mothur > ");
89 if(nextCommand != NULL) { add_history(nextCommand); }
90 mothurOutJustToLog("mothur > " + toString(nextCommand));
93 string nextCommand = "";
94 mothurOut("mothur > ");
95 getline(cin, nextCommand);
99 string nextCommand = "";
100 mothurOut("mothur > ");
101 getline(cin, nextCommand);
108 catch(exception& e) {
109 errorOut(e, "Engine", "getCommand");
113 /***********************************************************************/
114 //This function opens the batchfile to be used by BatchEngine::getInput.
115 BatchEngine::BatchEngine(string path, string batchFileName){
117 globaldata = GlobalData::getInstance();
119 openedBatch = openInputFile(batchFileName, inputBatchFile);
120 globaldata->argv = path;
123 catch(exception& e) {
124 errorOut(e, "BatchEngine", "BatchEngine");
129 /***********************************************************************/
131 BatchEngine::~BatchEngine(){ }
133 /***********************************************************************/
134 //This Function allows the user to run a batchfile containing several commands on Dotur
135 bool BatchEngine::getInput(){
137 //check if this is a valid batchfile
138 if (openedBatch == 1) {
139 mothurOut("unable to open batchfile");
145 string commandName = "";
148 //CommandFactory cFactory;
149 int quitCommandCalled = 0;
151 while(quitCommandCalled == 0){
153 if (inputBatchFile.eof()) { input = "quit()"; }
154 else { input = getline(inputBatchFile); }
156 if (input[0] != '#') {
159 mothurOut("mothur > " + input);
163 //allow user to omit the () on the quit command
164 if (input == "quit") { input = "quit()"; }
166 CommandOptionParser parser(input);
167 commandName = parser.getCommandString();
168 options = parser.getOptionString();
170 if (commandName != "") {
172 //executes valid command
173 Command* command = cFactory->getCommand(commandName, options);
174 quitCommandCalled = command->execute();
176 mothurOut("Invalid.");
181 gobble(inputBatchFile);
184 inputBatchFile.close();
187 catch(exception& e) {
188 errorOut(e, "BatchEngine", "getInput");
194 /***********************************************************************/
195 /***********************************************************************/
196 //This function opens the batchfile to be used by BatchEngine::getInput.
197 ScriptEngine::ScriptEngine(string path, string commandString){
199 globaldata = GlobalData::getInstance();
202 listOfCommands = commandString.substr(1, (commandString.length()-1));
204 globaldata->argv = path;
207 catch(exception& e) {
208 errorOut(e, "ScriptEngine", "ScriptEngine");
213 /***********************************************************************/
215 ScriptEngine::~ScriptEngine(){ }
217 /***********************************************************************/
218 //This Function allows the user to run a batchfile containing several commands on mothur
219 bool ScriptEngine::getInput(){
223 string commandName = "";
227 //CommandFactory cFactory;
228 int quitCommandCalled = 0;
230 while(quitCommandCalled == 0){
232 input = getNextCommand(listOfCommands);
234 if (input == "") { input = "quit()"; }
238 mothurOut("mothur > " + input);
242 //allow user to omit the () on the quit command
243 if (input == "quit") { input = "quit()"; }
245 CommandOptionParser parser(input);
246 commandName = parser.getCommandString();
247 options = parser.getOptionString();
249 if (commandName != "") {
251 //executes valid command
252 Command* command = cFactory->getCommand(commandName, options);
253 quitCommandCalled = command->execute();
255 mothurOut("Invalid.");
263 catch(exception& e) {
264 errorOut(e, "ScriptEngine", "getInput");
268 /***********************************************************************/
269 string ScriptEngine::getNextCommand(string& commandString) {
271 string nextcommand = "";
274 //go through string until you reach ; or end
275 while (count < commandString.length()) {
277 if (commandString[count] == ';') { break; }
278 else { nextcommand += commandString[count]; }
283 //if you are not at the end
284 if (count != commandString.length()) { commandString = commandString.substr(count+1, commandString.length()); }
285 else { commandString = ""; }
288 //get rid of spaces in between commands if any
289 if (commandString.length() > 0) {
290 while (commandString[0] == ' ') {
291 commandString = commandString.substr(1,commandString.length());
292 if (commandString.length() == 0) { break; }
298 catch(exception& e) {
299 errorOut(e, "ScriptEngine", "getNextCommand");
303 /***********************************************************************/