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