]> git.donarmstrong.com Git - mothur.git/blob - engine.cpp
bugs fixes while testing for 1.5 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
14
15 #include "engine.hpp"
16
17 /***********************************************************************/
18
19 InteractEngine::InteractEngine(string path){
20
21         globaldata = GlobalData::getInstance();
22         globaldata->argv = path;
23         
24         
25 }
26
27 /***********************************************************************/
28
29 InteractEngine::~InteractEngine(){
30         }
31
32 /***********************************************************************/
33 //This function allows the user to input commands one line at a time until they quit.
34 //If the command is garbage it does nothing.
35 bool InteractEngine::getInput(){
36         try {
37                 string input = "";
38                 string commandName = "";
39                 string options = "";
40                 int quitCommandCalled = 0;
41                 
42                                 
43                 while(quitCommandCalled != 1){
44                         
45                         mothurOutEndLine();
46                         mothurOut("mothur > ");
47                         getline(cin, input);
48                         if (cin.eof()) { input = "quit()"; }
49                         
50                         mothurOutJustToLog(input);
51                         mothurOutEndLine();
52                         
53                         //allow user to omit the () on the quit command
54                         if (input == "quit") { input = "quit()"; }
55                         
56                         CommandOptionParser parser(input);
57                         commandName = parser.getCommandString();
58                         options = parser.getOptionString();
59                         
60                         if (commandName != "") {
61                         
62                                 //executes valid command
63                                 CommandFactory cFactory;
64                                 Command* command = cFactory.getCommand(commandName, options);
65                                 quitCommandCalled = command->execute();
66                                 
67                         }else {
68                                 mothurOut("Your input contains errors. Please try again."); 
69                                 mothurOutEndLine();
70                         }
71                 }       
72                 return 1;
73         }
74         catch(exception& e) {
75                 errorOut(e, "InteractEngine", "getInput");
76                 exit(1);
77         }
78 }
79
80 /***********************************************************************/
81 //This function opens the batchfile to be used by BatchEngine::getInput.
82 BatchEngine::BatchEngine(string path, string batchFileName){
83         try {
84                 globaldata = GlobalData::getInstance();
85         
86                 openedBatch = openInputFile(batchFileName, inputBatchFile);
87                 globaldata->argv = path;
88                                 
89         }
90         catch(exception& e) {
91                 errorOut(e, "BatchEngine", "BatchEngine");
92                 exit(1);
93         }
94 }
95
96 /***********************************************************************/
97
98 BatchEngine::~BatchEngine(){
99         }
100
101 /***********************************************************************/
102 //This Function allows the user to run a batchfile containing several commands on Dotur
103 bool BatchEngine::getInput(){
104         try {
105                 //check if this is a valid batchfile
106                 if (openedBatch == 1) {  
107                         mothurOut("unable to open batchfile");  
108                         mothurOutEndLine();
109                         return 1; 
110                 }
111         
112                 string input = "";
113                 string commandName = "";
114                 string options = "";
115                 
116                 //CommandFactory cFactory;
117                 int quitCommandCalled = 0;
118         
119                 while(quitCommandCalled == 0){
120         
121                         if (inputBatchFile.eof()) { input = "quit()"; }
122                         else { getline(inputBatchFile, input); }
123         //cout << "input = ";           
124         //for (int i = 0; i < input.size();  i++) {  cout << input[i];  if ((input[i] == '\n') || (input[i] == '\r')) {  char c = input[i]; cout << "line feed = " << c << endl; } }
125         //cout << endl;         
126                         
127                         if (input[0] != '#') {
128                                 
129                                 mothurOutEndLine();
130                                 mothurOut("mothur > " + input);
131                                 mothurOutEndLine();
132                                 
133                                 
134                                 //allow user to omit the () on the quit command
135                                 if (input == "quit") { input = "quit()"; }
136
137                                 CommandOptionParser parser(input);
138                                 commandName = parser.getCommandString();
139                                 options = parser.getOptionString();
140                                                                                 
141                                 if (commandName != "") {
142
143                                         //executes valid command
144                                         CommandFactory cFactory;
145                                         Command* command = cFactory.getCommand(commandName, options);
146                                         quitCommandCalled = command->execute();
147                                 }else {         
148                                         mothurOut("Invalid."); 
149                                         mothurOutEndLine();
150                                 }
151                                 
152                         }
153                         gobble(inputBatchFile);
154                 }
155                 
156                 inputBatchFile.close();
157                 return 1;
158         }
159         catch(exception& e) {
160                 errorOut(e, "BatchEngine", "getInput");
161                 exit(1);
162         }
163 }
164
165
166 /***********************************************************************/
167 /***********************************************************************/
168 //This function opens the batchfile to be used by BatchEngine::getInput.
169 ScriptEngine::ScriptEngine(string path, string commandString){
170         try {
171                 globaldata = GlobalData::getInstance();
172                 
173                 //remove quotes
174                 listOfCommands = commandString.substr(1, (commandString.length()-1));
175
176                 globaldata->argv = path;
177                 
178                 
179         
180         }
181         catch(exception& e) {
182                 errorOut(e, "ScriptEngine", "ScriptEngine");
183                 exit(1);
184         }
185 }
186
187 /***********************************************************************/
188
189 ScriptEngine::~ScriptEngine(){
190         }
191
192 /***********************************************************************/
193 //This Function allows the user to run a batchfile containing several commands on mothur
194 bool ScriptEngine::getInput(){
195         try {
196                         
197                 string input = "";
198                 string commandName = "";
199                 string options = "";
200                 
201                 
202                 //CommandFactory cFactory;
203                 int quitCommandCalled = 0;
204         
205                 while(quitCommandCalled == 0){
206                 
207                         input = getNextCommand(listOfCommands); 
208                         
209                         if (input == "") { input = "quit()"; }
210                         
211                         
212                         mothurOutEndLine();
213                         mothurOut("mothur > " + input);
214                         mothurOutEndLine();
215
216                                 
217                         //allow user to omit the () on the quit command
218                         if (input == "quit") { input = "quit()"; }
219
220                         CommandOptionParser parser(input);
221                         commandName = parser.getCommandString();
222                         options = parser.getOptionString();
223                                                                                 
224                         if (commandName != "") {
225
226                                 //executes valid command
227                                 CommandFactory cFactory;
228                                 Command* command = cFactory.getCommand(commandName, options);
229                                 quitCommandCalled = command->execute();
230                         }else {         
231                                 mothurOut("Invalid."); 
232                                 mothurOutEndLine();
233                         }
234                         
235                 }
236                 
237                 return 1;
238         }
239         catch(exception& e) {
240                 errorOut(e, "ScriptEngine", "getInput");
241                 exit(1);
242         }
243 }
244 /***********************************************************************/
245 string ScriptEngine::getNextCommand(string& commandString) {
246         try {
247                 string nextcommand = "";
248                 int count = 0;
249                 
250                 //go through string until you reach ; or end
251                 while (count < commandString.length()) { 
252                         
253                         if (commandString[count] == ';') {  break;   }
254                         else {          nextcommand += commandString[count];    }
255                         
256                         count++;
257                 }
258                 
259                 //if you are not at the end
260                 if (count != commandString.length())  {   commandString = commandString.substr(count+1, commandString.length());  }
261                 else { commandString = ""; }
262                                 
263                 
264                 //get rid of spaces in between commands if any
265                 if (commandString.length() > 0) {
266                         while (commandString[0] == ' ') {  
267                                 commandString = commandString.substr(1,commandString.length());
268                                 if (commandString.length() == 0) {  break;  }
269                         }
270                 }
271                                         
272                 return nextcommand;
273         }
274         catch(exception& e) {
275                 errorOut(e, "ScriptEngine", "getNextCommand");
276                 exit(1);
277         }
278 }
279 /***********************************************************************/