]> git.donarmstrong.com Git - mothur.git/blob - engine.cpp
addition of summary.seq command [pds]
[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 using namespace std;
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                 int quitCommandCalled = 0;
46                 bool errorFree;
47                 ErrorCheck* errorCheckor = new ErrorCheck();
48                 
49                 cout << "mothur v.1.3.0" << endl;
50                 cout << "Last updated: 4/25/2009" << endl << endl;
51                 cout << "by" << endl;
52                 cout << "Patrick D. Schloss" << endl << endl;
53                 cout << "Department of Microbiology" << endl;
54                 cout << "The University of Massachusetts" << endl;
55                 cout << "pschloss@micro.umass.edu" << endl;
56                 cout << "http://schloss.micro.umass.edu/mothur" << endl << endl << endl;
57                 cout << "Distributed under the GNU General Public License" << endl << endl;
58                 cout << "Type 'help()' for information on the commands that are available" << endl << endl;
59                 cout << "Type 'quit()' to exit program" << endl;
60
61                 while(quitCommandCalled != 1){
62
63                         cout << endl << "mothur > ";
64                         getline(cin, input);
65                         if (cin.eof()) { input = "quit()"; }
66                         
67                         //allow user to omit the () on the quit command
68                         if (input == "quit") { input = "quit()"; }
69                         
70                         errorFree = errorCheckor->checkInput(input);
71                         if (errorFree == true) {
72                                 CommandOptionParser parser(input);
73                                 commandName = parser.getCommandString();
74                         
75                                 //executes valid command
76                                 CommandFactory cFactory;
77                                 Command* command = cFactory.getCommand(commandName);
78                                 quitCommandCalled = command->execute();
79                 
80                         }else {
81                                         cout << "Your input contains errors. Please try again." << endl;
82                         }
83                 }       
84                 return 1;
85         }
86         catch(exception& e) {
87                 cout << "Standard Error: " << e.what() << " has occurred in the InteractEngine class Function getInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
88                 exit(1);
89         }
90         catch(...) {
91                 cout << "An unknown error has occurred in the InteractEngine class function getInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
92                 exit(1);
93         }
94
95 }
96
97 /***********************************************************************/
98 //This function opens the batchfile to be used by BatchEngine::getInput.
99 BatchEngine::BatchEngine(string path, string batchFileName){
100         try {
101                 globaldata = GlobalData::getInstance();
102                 openedBatch = openInputFile(batchFileName, inputBatchFile);
103                 globaldata->argv = path;
104
105                 system("clear");
106         
107         //      char buffer = ' ';
108         //      ifstream header("introtext.txt");
109         //      while(!header.eof()){
110         //              cout << buffer;
111         //              buffer = header.get();
112         //      }
113         }
114         catch(exception& e) {
115                 cout << "Standard Error: " << e.what() << " has occurred in the BatchEngine class Function BatchEngine. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
116                 exit(1);
117         }
118         catch(...) {
119                 cout << "An unknown error has occurred in the BatchEngine class function BatchEngine. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
120                 exit(1);
121         }
122 }
123
124 /***********************************************************************/
125
126 BatchEngine::~BatchEngine(){
127         }
128
129 /***********************************************************************/
130 //This Function allows the user to run a batchfile containing several commands on Dotur
131 bool BatchEngine::getInput(){
132         try {
133                 //check if this is a valid batchfile
134                 if (openedBatch == 1) {  cout << "unable to open batchfile" << endl;  return 1; }
135         
136                 string input = "";
137                 string commandName = "";
138                 bool errorFree;
139                 ErrorCheck* errorCheckor = new ErrorCheck();
140
141                 CommandFactory cFactory;
142                 int quitCommandCalled = 0;
143         
144                 while(quitCommandCalled == 0){
145                 
146                         getline(inputBatchFile, input);
147                         if (input[0] != '#') {
148                                 if (inputBatchFile.eof()) { input = "quit()"; }
149                         
150                                 cout << endl << "mothur > " << input << endl;
151                                 
152                                 //allow user to omit the () on the quit command
153                                 if (input == "quit") { input = "quit()"; }
154
155                                 errorFree = errorCheckor->checkInput(input);
156                                 if (errorFree == true) {
157                                         CommandOptionParser parser(input);
158                                         commandName = parser.getCommandString();
159                                         ifstream filehandle;
160                 
161                                         if (openedBatch == 0) { //able to open batchfile
162                                                 //executes valid command
163                                                 CommandFactory cFactory;
164                                                 Command* command = cFactory.getCommand(commandName);
165                                                 quitCommandCalled = command->execute();
166                                         }
167                                         else {
168                                                 cout << "Invalid." << endl;
169                                         }
170                                 }
171                                 else {
172                                         cout << "Unable to open batchfile." << endl;
173                                 }
174                         }else { if (inputBatchFile.eof()) { input = "quit()"; } }
175                 }
176                 return 1;
177         }
178         catch(exception& e) {
179                 cout << "Standard Error: " << e.what() << " has occurred in the BatchEngine class Function getInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
180                 exit(1);
181         }
182         catch(...) {
183                 cout << "An unknown error has occurred in the BatchEngine class function getInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
184                 exit(1);
185         }
186 }
187
188
189 /***********************************************************************/
190