]> git.donarmstrong.com Git - mothur.git/blob - engine.cpp
mothur v.1.4.1
[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.4.1" << endl;
51                 cout << "Last updated: 6/23/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                 
179                 inputBatchFile.close();
180                 return 1;
181         }
182         catch(exception& e) {
183                 cout << "Standard Error: " << e.what() << " has occurred in the BatchEngine class Function getInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
184                 exit(1);
185         }
186         catch(...) {
187                 cout << "An unknown error has occurred in the BatchEngine class function getInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
188                 exit(1);
189         }
190 }
191
192
193 /***********************************************************************/
194 /***********************************************************************/
195 //This function opens the batchfile to be used by BatchEngine::getInput.
196 ScriptEngine::ScriptEngine(string path, string commandString){
197         try {
198                 globaldata = GlobalData::getInstance();
199                 
200                 //remove quotes
201                 listOfCommands = commandString.substr(1, (commandString.length()-1));
202
203                 globaldata->argv = path;
204
205                 system("clear");
206         
207         }
208         catch(exception& e) {
209                 cout << "Standard Error: " << e.what() << " has occurred in the ScriptEngine class Function ScriptEngine. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
210                 exit(1);
211         }
212         catch(...) {
213                 cout << "An unknown error has occurred in the ScriptEngine class function ScriptEngine. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
214                 exit(1);
215         }
216 }
217
218 /***********************************************************************/
219
220 ScriptEngine::~ScriptEngine(){
221         }
222
223 /***********************************************************************/
224 //This Function allows the user to run a batchfile containing several commands on Dotur
225 bool ScriptEngine::getInput(){
226         try {
227                         
228                 string input = "";
229                 string commandName = "";
230                 string options = "";
231                 //int count = 1;
232                 
233                 //CommandFactory cFactory;
234                 int quitCommandCalled = 0;
235         
236                 while(quitCommandCalled == 0){
237                 
238                         input = getNextCommand(listOfCommands); 
239                         
240                         if (input == "") { input = "quit()"; }
241                         //cout << "command number" << count << endl; count++;
242                         
243                         cout << endl << "mothur > " << input << endl;
244                                 
245                         //allow user to omit the () on the quit command
246                         if (input == "quit") { input = "quit()"; }
247
248                         CommandOptionParser parser(input);
249                         commandName = parser.getCommandString();
250                         options = parser.getOptionString();
251                                                                                 
252                         if (commandName != "") {
253
254                                 //executes valid command
255                                 CommandFactory cFactory;
256                                 Command* command = cFactory.getCommand(commandName, options);
257                                 quitCommandCalled = command->execute();
258                         }else {         cout << "Invalid." << endl;             }
259                         
260                 }
261                 
262                 return 1;
263         }
264         catch(exception& e) {
265                 cout << "Standard Error: " << e.what() << " has occurred in the ScriptEngine class Function getInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
266                 exit(1);
267         }
268         catch(...) {
269                 cout << "An unknown error has occurred in the ScriptEngine class function getInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
270                 exit(1);
271         }
272 }
273 /***********************************************************************/
274 string ScriptEngine::getNextCommand(string& commandString) {
275         try {
276                 string nextcommand = "";
277                 
278                 nextcommand = commandString.substr(0,commandString.find_first_of(';'));
279
280                                 
281                 if ((commandString.find_first_of(';')+1) <= commandString.length()) {
282                         commandString = commandString.substr(commandString.find_first_of(';')+1, commandString.length());
283                 }else { commandString = ""; } //you have reached the last command.
284                 
285                 //get rid of spaces in between commands if any
286                 if (commandString.length() > 0) {
287                         while (commandString[0] == ' ') {  
288                                 commandString = commandString.substr(1,commandString.length());
289                                 if (commandString.length() == 0) {  break;  }
290                         }
291                 }
292                                         
293                 return nextcommand;
294         }
295         catch(exception& e) {
296                 cout << "Standard Error: " << e.what() << " has occurred in the ScriptEngine class Function getNextCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
297                 exit(1);
298         }
299         catch(...) {
300                 cout << "An unknown error has occurred in the ScriptEngine class function getNextCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
301                 exit(1);
302         }
303 }
304 /***********************************************************************/