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