]> git.donarmstrong.com Git - mothur.git/blob - engine.cpp
broke up globaldata and moved error checking and help into commands
[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.3.0" << endl;
51                 cout << "Last updated: 5/29/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                 openedBatch = openInputFile(batchFileName, inputBatchFile);
107                 globaldata->argv = path;
108
109                 system("clear");
110         
111         //      char buffer = ' ';
112         //      ifstream header("introtext.txt");
113         //      while(!header.eof()){
114         //              cout << buffer;
115         //              buffer = header.get();
116         //      }
117         }
118         catch(exception& e) {
119                 cout << "Standard Error: " << e.what() << " has occurred in the BatchEngine class Function BatchEngine. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
120                 exit(1);
121         }
122         catch(...) {
123                 cout << "An unknown error has occurred in the BatchEngine class function BatchEngine. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
124                 exit(1);
125         }
126 }
127
128 /***********************************************************************/
129
130 BatchEngine::~BatchEngine(){
131         }
132
133 /***********************************************************************/
134 //This Function allows the user to run a batchfile containing several commands on Dotur
135 bool BatchEngine::getInput(){
136         try {
137                 //check if this is a valid batchfile
138                 if (openedBatch == 1) {  cout << "unable to open batchfile" << endl;  return 1; }
139         
140                 string input = "";
141                 string commandName = "";
142                 string options = "";
143                 
144                 //CommandFactory cFactory;
145                 int quitCommandCalled = 0;
146         
147                 while(quitCommandCalled == 0){
148                 
149                         if (inputBatchFile.eof()) { input = "quit()"; }
150                         else { getline(inputBatchFile, input); }
151                         
152                         if (input[0] != '#') {
153                         
154                                 cout << endl << "mothur > " << input << endl;
155                                 
156                                 //allow user to omit the () on the quit command
157                                 if (input == "quit") { input = "quit()"; }
158
159                                 CommandOptionParser parser(input);
160                                 commandName = parser.getCommandString();
161                                 options = parser.getOptionString();
162                                                                                 
163                                 if (commandName != "") {
164
165                                         //executes valid command
166                                         CommandFactory cFactory;
167                                         Command* command = cFactory.getCommand(commandName, options);
168                                         quitCommandCalled = command->execute();
169                                 }else {         cout << "Invalid." << endl;             }
170                                 
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