]> git.donarmstrong.com Git - mothur.git/blob - engine.cpp
added logfile feature
[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 InteractEngine::InteractEngine(string path){
20
21         globaldata = GlobalData::getInstance();
22         globaldata->argv = path;
23         string logFileName = "mothur.logFile";
24         remove(logFileName.c_str());
25         
26         system("clear");
27 }
28
29 /***********************************************************************/
30
31 InteractEngine::~InteractEngine(){
32         }
33
34 /***********************************************************************/
35 //This function allows the user to input commands one line at a time until they quit.
36 //If the command is garbage it does nothing.
37 bool InteractEngine::getInput(){
38         try {
39                 string input = "";
40                 string commandName = "";
41                 string options = "";
42                 int quitCommandCalled = 0;
43                 
44                 mothurOut("mothur v.1.4.1");
45                 mothurOutEndLine();             
46                 mothurOut("Last updated: 6/23/2009");
47                 mothurOutEndLine();     
48                 mothurOutEndLine();             
49                 mothurOut("by");
50                 mothurOutEndLine();             
51                 mothurOut("Patrick D. Schloss");
52                 mothurOutEndLine();
53                 mothurOutEndLine();                     
54                 mothurOut("Department of Microbiology");
55                 mothurOutEndLine();             
56                 mothurOut("pschloss@micro.umass.edu");
57                 mothurOutEndLine();             
58                 mothurOut("http://schloss.micro.umass.edu/mothur");
59                 mothurOutEndLine();     
60                 mothurOutEndLine();     
61                 mothurOutEndLine();             
62                 mothurOut("Distributed under the GNU General Public License");
63                 mothurOutEndLine();
64                 mothurOutEndLine();                     
65                 mothurOut("Type 'help()' for information on the commands that are available");
66                 mothurOutEndLine();
67                 mothurOutEndLine();                     
68                 mothurOut("Type 'quit()' to exit program");
69                 mothurOutEndLine();     
70                 
71                 while(quitCommandCalled != 1){
72                         
73                         mothurOutEndLine();
74                         mothurOut("mothur > ");
75                         getline(cin, input);
76                         if (cin.eof()) { input = "quit()"; }
77                         
78                         mothurOutJustToLog(input);
79                         mothurOutEndLine();
80                         
81                         //allow user to omit the () on the quit command
82                         if (input == "quit") { input = "quit()"; }
83                         
84                         CommandOptionParser parser(input);
85                         commandName = parser.getCommandString();
86                         options = parser.getOptionString();
87                         
88                         if (commandName != "") {
89                         
90                                 //executes valid command
91                                 CommandFactory cFactory;
92                                 Command* command = cFactory.getCommand(commandName, options);
93                                 quitCommandCalled = command->execute();
94                                 
95                         }else {
96                                 mothurOut("Your input contains errors. Please try again."); 
97                                 mothurOutEndLine();
98                         }
99                 }       
100                 return 1;
101         }
102         catch(exception& e) {
103                 errorOut(e, "InteractEngine", "getInput");
104                 exit(1);
105         }
106 }
107
108 /***********************************************************************/
109 //This function opens the batchfile to be used by BatchEngine::getInput.
110 BatchEngine::BatchEngine(string path, string batchFileName){
111         try {
112                 globaldata = GlobalData::getInstance();
113         
114                 openedBatch = openInputFile(batchFileName, inputBatchFile);
115                 globaldata->argv = path;
116                 string logFileName = "mothur.logFile";
117                 remove(logFileName.c_str());
118                 
119                 system("clear");
120         
121         //      char buffer = ' ';
122         //      ifstream header("introtext.txt");
123         //      while(!header.eof()){
124         //              cout << buffer;
125         //              buffer = header.get();
126         //      }
127         }
128         catch(exception& e) {
129                 errorOut(e, "BatchEngine", "BatchEngine");
130                 exit(1);
131         }
132 }
133
134 /***********************************************************************/
135
136 BatchEngine::~BatchEngine(){
137         }
138
139 /***********************************************************************/
140 //This Function allows the user to run a batchfile containing several commands on Dotur
141 bool BatchEngine::getInput(){
142         try {
143                 //check if this is a valid batchfile
144                 if (openedBatch == 1) {  
145                         mothurOut("unable to open batchfile");  
146                         mothurOutEndLine();
147                         return 1; 
148                 }
149         
150                 string input = "";
151                 string commandName = "";
152                 string options = "";
153                 
154                 //CommandFactory cFactory;
155                 int quitCommandCalled = 0;
156         
157                 while(quitCommandCalled == 0){
158         
159                         if (inputBatchFile.eof()) { input = "quit()"; }
160                         else { getline(inputBatchFile, input); }
161                         
162                         
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                                         CommandFactory cFactory;
182                                         Command* command = cFactory.getCommand(commandName, options);
183                                         quitCommandCalled = command->execute();
184                                 }else {         
185                                         mothurOut("Invalid."); 
186                                         mothurOutEndLine();
187                                 }
188                                 
189                         }
190                         gobble(inputBatchFile);
191                 }
192                 
193                 inputBatchFile.close();
194                 return 1;
195         }
196         catch(exception& e) {
197                 errorOut(e, "BatchEngine", "getInput");
198                 exit(1);
199         }
200 }
201
202
203 /***********************************************************************/
204 /***********************************************************************/
205 //This function opens the batchfile to be used by BatchEngine::getInput.
206 ScriptEngine::ScriptEngine(string path, string commandString){
207         try {
208                 globaldata = GlobalData::getInstance();
209                 
210                 //remove quotes
211                 listOfCommands = commandString.substr(1, (commandString.length()-1));
212
213                 globaldata->argv = path;
214                 string logFileName = "mothur.logFile";
215                 remove(logFileName.c_str());
216
217                 system("clear");
218         
219         }
220         catch(exception& e) {
221                 errorOut(e, "ScriptEngine", "ScriptEngine");
222                 exit(1);
223         }
224 }
225
226 /***********************************************************************/
227
228 ScriptEngine::~ScriptEngine(){
229         }
230
231 /***********************************************************************/
232 //This Function allows the user to run a batchfile containing several commands on mothur
233 bool ScriptEngine::getInput(){
234         try {
235                         
236                 string input = "";
237                 string commandName = "";
238                 string options = "";
239                 
240                 
241                 //CommandFactory cFactory;
242                 int quitCommandCalled = 0;
243         
244                 while(quitCommandCalled == 0){
245                 
246                         input = getNextCommand(listOfCommands); 
247                         
248                         if (input == "") { input = "quit()"; }
249                         
250                         
251                         mothurOutEndLine();
252                         mothurOut("mothur > " + input);
253                         mothurOutEndLine();
254
255                                 
256                         //allow user to omit the () on the quit command
257                         if (input == "quit") { input = "quit()"; }
258
259                         CommandOptionParser parser(input);
260                         commandName = parser.getCommandString();
261                         options = parser.getOptionString();
262                                                                                 
263                         if (commandName != "") {
264
265                                 //executes valid command
266                                 CommandFactory cFactory;
267                                 Command* command = cFactory.getCommand(commandName, options);
268                                 quitCommandCalled = command->execute();
269                         }else {         
270                                 mothurOut("Invalid."); 
271                                 mothurOutEndLine();
272                         }
273                         
274                 }
275                 
276                 return 1;
277         }
278         catch(exception& e) {
279                 errorOut(e, "ScriptEngine", "getInput");
280                 exit(1);
281         }
282 }
283 /***********************************************************************/
284 string ScriptEngine::getNextCommand(string& commandString) {
285         try {
286                 string nextcommand = "";
287                 
288                 nextcommand = commandString.substr(0,commandString.find_first_of(';'));
289
290                                 
291                 if ((commandString.find_first_of(';')+1) <= commandString.length()) {
292                         commandString = commandString.substr(commandString.find_first_of(';')+1, commandString.length());
293                 }else { commandString = ""; } //you have reached the last command.
294                 
295                 //get rid of spaces in between commands if any
296                 if (commandString.length() > 0) {
297                         while (commandString[0] == ' ') {  
298                                 commandString = commandString.substr(1,commandString.length());
299                                 if (commandString.length() == 0) {  break;  }
300                         }
301                 }
302                                         
303                 return nextcommand;
304         }
305         catch(exception& e) {
306                 errorOut(e, "ScriptEngine", "getNextCommand");
307                 exit(1);
308         }
309 }
310 /***********************************************************************/