]> git.donarmstrong.com Git - mothur.git/blob - engine.cpp
fixed multiple bugs for 1.4 release
[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 v1.2.0" << endl;
50                 cout << "Last updated: 4/14/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                         errorFree = errorCheckor->checkInput(input);
68                         if (errorFree == true) {
69                                 CommandOptionParser parser(input);
70                                 commandName = parser.getCommandString();
71                         
72                                 //executes valid command
73                                 CommandFactory cFactory;
74                                 Command* command = cFactory.getCommand(commandName);
75                                 quitCommandCalled = command->execute();
76                 
77                         }else {
78                                         cout << "Your input contains errors. Please try again." << endl;
79                         }
80                 }       
81                 return 1;
82         }
83         catch(exception& e) {
84                 cout << "Standard Error: " << e.what() << " has occurred in the InteractEngine class Function getInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
85                 exit(1);
86         }
87         catch(...) {
88                 cout << "An unknown error has occurred in the InteractEngine class function getInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
89                 exit(1);
90         }
91
92 }
93
94 /***********************************************************************/
95 //This function opens the batchfile to be used by BatchEngine::getInput.
96 BatchEngine::BatchEngine(string path, string batchFileName){
97         try {
98                 globaldata = GlobalData::getInstance();
99                 openedBatch = openInputFile(batchFileName, inputBatchFile);
100                 globaldata->argv = path;
101
102                 system("clear");
103         
104         //      char buffer = ' ';
105         //      ifstream header("introtext.txt");
106         //      while(!header.eof()){
107         //              cout << buffer;
108         //              buffer = header.get();
109         //      }
110         }
111         catch(exception& e) {
112                 cout << "Standard Error: " << e.what() << " has occurred in the BatchEngine class Function BatchEngine. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
113                 exit(1);
114         }
115         catch(...) {
116                 cout << "An unknown error has occurred in the BatchEngine class function BatchEngine. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
117                 exit(1);
118         }
119 }
120
121 /***********************************************************************/
122
123 BatchEngine::~BatchEngine(){
124         }
125
126 /***********************************************************************/
127 //This Function allows the user to run a batchfile containing several commands on Dotur
128 bool BatchEngine::getInput(){
129         try {
130                 //check if this is a valid batchfile
131                 if (openedBatch == 1) {  cout << "unable to open batchfile" << endl;  return 1; }
132         
133                 string input = "";
134                 string commandName = "";
135                 bool errorFree;
136                 ErrorCheck* errorCheckor = new ErrorCheck();
137
138                 CommandFactory cFactory;
139                 int quitCommandCalled = 0;
140         
141                 while(quitCommandCalled == 0){
142                 
143                         getline(inputBatchFile, input);
144                         if (input[0] != '#') {
145                                 if (inputBatchFile.eof()) { input = "quit()"; }
146                         
147                                 cout << endl << "mothur > " << input << endl;
148                                 errorFree = errorCheckor->checkInput(input);
149                                 if (errorFree == true) {
150                                         CommandOptionParser parser(input);
151                                         commandName = parser.getCommandString();
152                                         ifstream filehandle;
153                 
154                                         if (openedBatch == 0) { //able to open batchfile
155                                                 //executes valid command
156                                                 CommandFactory cFactory;
157                                                 Command* command = cFactory.getCommand(commandName);
158                                                 quitCommandCalled = command->execute();
159                                         }
160                                         else {
161                                                 cout << "Invalid." << endl;
162                                         }
163                                 }
164                                 else {
165                                         cout << "Unable to open batchfile." << endl;
166                                 }
167                         }else { if (inputBatchFile.eof()) { input = "quit()"; } }
168                 }
169                 return 1;
170         }
171         catch(exception& e) {
172                 cout << "Standard Error: " << e.what() << " has occurred in the BatchEngine class Function getInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
173                 exit(1);
174         }
175         catch(...) {
176                 cout << "An unknown error has occurred in the BatchEngine class function getInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
177                 exit(1);
178         }
179 }
180
181
182 /***********************************************************************/
183