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