]> git.donarmstrong.com Git - mothur.git/blob - engine.cpp
added mothur.h and fixed includes in many files
[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
16 #include "utilities.hpp"
17 #include "globaldata.hpp"
18 #include "commandoptionparser.hpp"
19 #include "command.hpp"
20 #include "commandfactory.hpp"
21 #include "errorchecking.h"
22 #include "engine.hpp"
23
24 /***********************************************************************/
25
26 InteractEngine::InteractEngine(){
27
28         globaldata = GlobalData::getInstance();
29
30         system("clear");
31 //      char buffer = ' ';
32 //      ifstream header("introtext.txt");
33 //      while(!header.eof()){
34 //              cout << buffer;
35 //              buffer = header.get();
36 //      }
37 }
38
39 /***********************************************************************/
40
41 InteractEngine::~InteractEngine(){
42         }
43
44 /***********************************************************************/
45 //This function allows the user to input commands one line at a time until they quit.
46 //If the command is garbage it does nothing.
47 bool InteractEngine::getInput(){
48         try {
49                 string input = "";
50                 string commandName = "";
51                 int quitCommandCalled = 0;
52                 bool errorFree;
53                 ErrorCheck* errorCheckor = new ErrorCheck();
54                 
55                 cout << "mothur v1.0" << endl;
56                 cout << "Last updated: 1/29/2009" << endl << endl;
57                 cout << "by" << endl;
58                 cout << "Patrick D. Schloss" << endl << endl;
59                 cout << "Department of Microbiology" << endl;
60                 cout << "The University of Massachusetts" << endl;
61                 cout << "pschloss@micro.umass.edu" << endl;
62                 cout << "http://schloss.micro.umass.edu/mothur" << endl << endl << endl;
63                 cout << "Distributed under the GNU General Public License" << endl << endl;
64                 cout << "Type 'help()' for information on the commands that are available" << endl << endl;
65                 cout << "Type 'quit()' to exit program" << endl;
66
67                 while(quitCommandCalled != 1){
68
69                         cout << endl << "mothur > ";
70                         getline(cin, input);
71                         if (cin.eof()) { input = "quit()"; }
72                         
73                         errorFree = errorCheckor->checkInput(input);
74                         if (errorFree == true) {
75                                 CommandOptionParser parser(input);
76                                 commandName = parser.getCommandString();
77                         
78                                 //executes valid command
79                                 CommandFactory cFactory;
80                                 Command* command = cFactory.getCommand(commandName);
81                                 quitCommandCalled = command->execute();
82                 
83                         }else {
84                                         cout << "Your input contains errors. Please try again." << endl;
85                         }
86                 }       
87                 return 1;
88         }
89         catch(exception& e) {
90                 cout << "Standard Error: " << e.what() << " has occurred in the InteractEngine class Function getInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
91                 exit(1);
92         }
93         catch(...) {
94                 cout << "An unknown error has occurred in the InteractEngine class function getInput. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
95                 exit(1);
96         }
97
98 }
99
100 /***********************************************************************/
101 //This function opens the batchfile to be used by BatchEngine::getInput.
102 BatchEngine::BatchEngine(string batchFileName){
103         try {
104                 globaldata = GlobalData::getInstance();
105                 openedBatch = openInputFile(batchFileName, inputBatchFile);
106
107                 system("clear");
108         
109         //      char buffer = ' ';
110         //      ifstream header("introtext.txt");
111         //      while(!header.eof()){
112         //              cout << buffer;
113         //              buffer = header.get();
114         //      }
115         }
116         catch(exception& e) {
117                 cout << "Standard Error: " << e.what() << " has occurred in the BatchEngine class Function BatchEngine. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
118                 exit(1);
119         }
120         catch(...) {
121                 cout << "An unknown error has occurred in the BatchEngine class function BatchEngine. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
122                 exit(1);
123         }
124 }
125
126 /***********************************************************************/
127
128 BatchEngine::~BatchEngine(){
129         }
130
131 /***********************************************************************/
132 //This Function allows the user to run a batchfile containing several commands on Dotur
133 bool BatchEngine::getInput(){
134         try {
135                 string input = "";
136                 string commandName = "";
137                 bool errorFree;
138                 ErrorCheck* errorCheckor = new ErrorCheck();
139
140                 CommandFactory cFactory;
141                 int quitCommandCalled = 0;
142         
143                 while(quitCommandCalled == 0){
144                 
145                         getline(inputBatchFile, input);
146                         if (inputBatchFile.eof()) { input = "quit()"; }
147                         
148                         cout << endl << "mothur > " << input << endl;
149                         errorFree = errorCheckor->checkInput(input);
150                         if (errorFree == true) {
151                                 CommandOptionParser parser(input);
152                                 commandName = parser.getCommandString();
153                                 ifstream filehandle;
154                 
155                                 if (openedBatch == 0) { //able to open batchfile
156                                         //executes valid command
157                                         CommandFactory cFactory;
158                                         Command* command = cFactory.getCommand(commandName);
159                                         quitCommandCalled = command->execute();
160                                 }
161                                 else {
162                                         cout << "Invalid." << endl;
163                                 }
164                         }
165                         else {
166                                 cout << "Unable to open batchfile." << endl;
167                         }
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