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