]> git.donarmstrong.com Git - mothur.git/blob - engine.cpp
working on chimeras
[mothur.git] / engine.cpp
1 /*
2  *  engine.cpp
3  *  
4  *
5  *  Created by Pat Schloss on 8/15/08.
6  *  Copyright 2008 Patrick D. Schloss. All rights reserved.
7  *
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.
11  *
12  */
13
14
15 #include "engine.hpp"
16
17 /***********************************************************************/
18 Engine::Engine(){
19         try {
20                 cFactory = CommandFactory::getInstance();
21         }
22         catch(exception& e) {
23                 errorOut(e, "Engine", "Engine");
24                 exit(1);
25         }
26 }
27
28 /***********************************************************************/
29
30 InteractEngine::InteractEngine(string path){
31
32         globaldata = GlobalData::getInstance();
33         globaldata->argv = path;
34 }
35
36 /***********************************************************************/
37
38 InteractEngine::~InteractEngine(){}
39
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(){
44         try {
45                 string input = "";
46                 string commandName = "";
47                 string options = "";
48                 int quitCommandCalled = 0;
49                 
50                 while(quitCommandCalled != 1){
51
52                         mothurOutEndLine();
53                         
54                         input = getCommand();                   
55                         
56                         //allow user to omit the () on the quit command
57                         if (input == "quit") { input = "quit()"; }
58                         
59                         CommandOptionParser parser(input);
60                         commandName = parser.getCommandString();
61         
62                         options = parser.getOptionString();
63                         
64                         if (commandName != "") {
65                         
66                                 //executes valid command
67                                 Command* command = cFactory->getCommand(commandName, options);
68                                 quitCommandCalled = command->execute();
69                                 
70                         }else {
71                                 mothurOut("Your input contains errors. Please try again."); 
72                                 mothurOutEndLine();
73                         }
74                 }       
75                 return 1;
76         }
77         catch(exception& e) {
78                 errorOut(e, "InteractEngine", "getInput");
79                 exit(1);
80         }
81 }
82 /***********************************************************************/
83 string Engine::getCommand()  {
84         try {
85                 #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux)
86                         #ifdef USE_READLINE
87                                 char* nextCommand = NULL;
88                                 nextCommand = readline("mothur > ");
89                                 
90                                 if(nextCommand != NULL) {  add_history(nextCommand);  } 
91                                 else{ //^D causes null string and we want it to quit mothur
92                                         nextCommand = "quit"; 
93                                         cout << nextCommand << endl;
94                                 }       
95                                 
96                                 mothurOutJustToLog("mothur > " + toString(nextCommand));
97                                 return nextCommand;
98                         #else
99                                 string nextCommand = "";
100                                 mothurOut("mothur > ");
101                                 getline(cin, nextCommand);
102                                 mothurOutJustToLog("mothur > " + toString(nextCommand));
103                                 return nextCommand;
104                         #endif
105                 #else
106                         string nextCommand = "";
107                         mothurOut("mothur > ");
108                         getline(cin, nextCommand);
109                         mothurOutJustToLog("mothur > " + toString(nextCommand));
110                         return nextCommand;
111                 #endif
112                 
113                 mothurOutEndLine();
114                                                 
115         }
116         catch(exception& e) {
117                 errorOut(e, "Engine", "getCommand");
118                 exit(1);
119         }
120 }
121 /***********************************************************************/
122 //This function opens the batchfile to be used by BatchEngine::getInput.
123 BatchEngine::BatchEngine(string path, string batchFileName){
124         try {
125                 globaldata = GlobalData::getInstance();
126         
127                 openedBatch = openInputFile(batchFileName, inputBatchFile);
128                 globaldata->argv = path;
129                                 
130         }
131         catch(exception& e) {
132                 errorOut(e, "BatchEngine", "BatchEngine");
133                 exit(1);
134         }
135 }
136
137 /***********************************************************************/
138
139 BatchEngine::~BatchEngine(){    }
140
141 /***********************************************************************/
142 //This Function allows the user to run a batchfile containing several commands on Dotur
143 bool BatchEngine::getInput(){
144         try {
145                 //check if this is a valid batchfile
146                 if (openedBatch == 1) {  
147                         mothurOut("unable to open batchfile");  
148                         mothurOutEndLine();
149                         return 1; 
150                 }
151         
152                 string input = "";
153                 string commandName = "";
154                 string options = "";
155                 
156                 //CommandFactory cFactory;
157                 int quitCommandCalled = 0;
158         
159                 while(quitCommandCalled == 0){
160         
161                         if (inputBatchFile.eof()) { input = "quit()"; }
162                         else { input = getline(inputBatchFile); }
163                         
164                         if (input[0] != '#') {
165                                 
166                                 mothurOutEndLine();
167                                 mothurOut("mothur > " + input);
168                                 mothurOutEndLine();
169                                 
170                                 
171                                 //allow user to omit the () on the quit command
172                                 if (input == "quit") { input = "quit()"; }
173
174                                 CommandOptionParser parser(input);
175                                 commandName = parser.getCommandString();
176                                 options = parser.getOptionString();
177                                                                                 
178                                 if (commandName != "") {
179
180                                         //executes valid command
181                                         Command* command = cFactory->getCommand(commandName, options);
182                                         quitCommandCalled = command->execute();
183                                 }else {         
184                                         mothurOut("Invalid."); 
185                                         mothurOutEndLine();
186                                 }
187                                 
188                         }
189                         gobble(inputBatchFile);
190                 }
191                 
192                 inputBatchFile.close();
193                 return 1;
194         }
195         catch(exception& e) {
196                 errorOut(e, "BatchEngine", "getInput");
197                 exit(1);
198         }
199 }
200
201
202 /***********************************************************************/
203 /***********************************************************************/
204 //This function opens the batchfile to be used by BatchEngine::getInput.
205 ScriptEngine::ScriptEngine(string path, string commandString){
206         try {
207                 globaldata = GlobalData::getInstance();
208                 
209                 //remove quotes
210                 listOfCommands = commandString.substr(1, (commandString.length()-1));
211
212                 globaldata->argv = path;
213                 
214         }
215         catch(exception& e) {
216                 errorOut(e, "ScriptEngine", "ScriptEngine");
217                 exit(1);
218         }
219 }
220
221 /***********************************************************************/
222
223 ScriptEngine::~ScriptEngine(){  }
224
225 /***********************************************************************/
226 //This Function allows the user to run a batchfile containing several commands on mothur
227 bool ScriptEngine::getInput(){
228         try {
229                         
230                 string input = "";
231                 string commandName = "";
232                 string options = "";
233                 
234                 
235                 //CommandFactory cFactory;
236                 int quitCommandCalled = 0;
237         
238                 while(quitCommandCalled == 0){
239                 
240                         input = getNextCommand(listOfCommands); 
241                         
242                         if (input == "") { input = "quit()"; }
243                         
244                         
245                         mothurOutEndLine();
246                         mothurOut("mothur > " + input);
247                         mothurOutEndLine();
248
249                                 
250                         //allow user to omit the () on the quit command
251                         if (input == "quit") { input = "quit()"; }
252
253                         CommandOptionParser parser(input);
254                         commandName = parser.getCommandString();
255                         options = parser.getOptionString();
256                                                                                 
257                         if (commandName != "") {
258
259                                 //executes valid command
260                                 Command* command = cFactory->getCommand(commandName, options);
261                                 quitCommandCalled = command->execute();
262                         }else {         
263                                 mothurOut("Invalid."); 
264                                 mothurOutEndLine();
265                         }
266                         
267                 }
268                 
269                 return 1;
270         }
271         catch(exception& e) {
272                 errorOut(e, "ScriptEngine", "getInput");
273                 exit(1);
274         }
275 }
276 /***********************************************************************/
277 string ScriptEngine::getNextCommand(string& commandString) {
278         try {
279                 string nextcommand = "";
280                 int count = 0;
281                 
282                 //go through string until you reach ; or end
283                 while (count < commandString.length()) { 
284                         
285                         if (commandString[count] == ';') {  break;   }
286                         else {          nextcommand += commandString[count];    }
287                         
288                         count++;
289                 }
290                 
291                 //if you are not at the end
292                 if (count != commandString.length())  {   commandString = commandString.substr(count+1, commandString.length());  }
293                 else { commandString = ""; }
294                                 
295                 
296                 //get rid of spaces in between commands if any
297                 if (commandString.length() > 0) {
298                         while (commandString[0] == ' ') {  
299                                 commandString = commandString.substr(1,commandString.length());
300                                 if (commandString.length() == 0) {  break;  }
301                         }
302                 }
303                                         
304                 return nextcommand;
305         }
306         catch(exception& e) {
307                 errorOut(e, "ScriptEngine", "getNextCommand");
308                 exit(1);
309         }
310 }
311 /***********************************************************************/