]> git.donarmstrong.com Git - mothur.git/blob - engine.cpp
fixed bugs in venn and aligner
[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 { input = getline(inputBatchFile); }
123                         
124                         
125                         
126                         if (input[0] != '#') {
127                                 
128                                 mothurOutEndLine();
129                                 mothurOut("mothur > " + input);
130                                 mothurOutEndLine();
131                                 
132                                 
133                                 //allow user to omit the () on the quit command
134                                 if (input == "quit") { input = "quit()"; }
135
136                                 CommandOptionParser parser(input);
137                                 commandName = parser.getCommandString();
138                                 options = parser.getOptionString();
139                                                                                 
140                                 if (commandName != "") {
141
142                                         //executes valid command
143                                         CommandFactory cFactory;
144                                         Command* command = cFactory.getCommand(commandName, options);
145                                         quitCommandCalled = command->execute();
146                                 }else {         
147                                         mothurOut("Invalid."); 
148                                         mothurOutEndLine();
149                                 }
150                                 
151                         }
152                         gobble(inputBatchFile);
153                 }
154                 
155                 inputBatchFile.close();
156                 return 1;
157         }
158         catch(exception& e) {
159                 errorOut(e, "BatchEngine", "getInput");
160                 exit(1);
161         }
162 }
163
164
165 /***********************************************************************/
166 /***********************************************************************/
167 //This function opens the batchfile to be used by BatchEngine::getInput.
168 ScriptEngine::ScriptEngine(string path, string commandString){
169         try {
170                 globaldata = GlobalData::getInstance();
171                 
172                 //remove quotes
173                 listOfCommands = commandString.substr(1, (commandString.length()-1));
174
175                 globaldata->argv = path;
176                 
177                 
178         
179         }
180         catch(exception& e) {
181                 errorOut(e, "ScriptEngine", "ScriptEngine");
182                 exit(1);
183         }
184 }
185
186 /***********************************************************************/
187
188 ScriptEngine::~ScriptEngine(){
189         }
190
191 /***********************************************************************/
192 //This Function allows the user to run a batchfile containing several commands on mothur
193 bool ScriptEngine::getInput(){
194         try {
195                         
196                 string input = "";
197                 string commandName = "";
198                 string options = "";
199                 
200                 
201                 //CommandFactory cFactory;
202                 int quitCommandCalled = 0;
203         
204                 while(quitCommandCalled == 0){
205                 
206                         input = getNextCommand(listOfCommands); 
207                         
208                         if (input == "") { input = "quit()"; }
209                         
210                         
211                         mothurOutEndLine();
212                         mothurOut("mothur > " + input);
213                         mothurOutEndLine();
214
215                                 
216                         //allow user to omit the () on the quit command
217                         if (input == "quit") { input = "quit()"; }
218
219                         CommandOptionParser parser(input);
220                         commandName = parser.getCommandString();
221                         options = parser.getOptionString();
222                                                                                 
223                         if (commandName != "") {
224
225                                 //executes valid command
226                                 CommandFactory cFactory;
227                                 Command* command = cFactory.getCommand(commandName, options);
228                                 quitCommandCalled = command->execute();
229                         }else {         
230                                 mothurOut("Invalid."); 
231                                 mothurOutEndLine();
232                         }
233                         
234                 }
235                 
236                 return 1;
237         }
238         catch(exception& e) {
239                 errorOut(e, "ScriptEngine", "getInput");
240                 exit(1);
241         }
242 }
243 /***********************************************************************/
244 string ScriptEngine::getNextCommand(string& commandString) {
245         try {
246                 string nextcommand = "";
247                 int count = 0;
248                 
249                 //go through string until you reach ; or end
250                 while (count < commandString.length()) { 
251                         
252                         if (commandString[count] == ';') {  break;   }
253                         else {          nextcommand += commandString[count];    }
254                         
255                         count++;
256                 }
257                 
258                 //if you are not at the end
259                 if (count != commandString.length())  {   commandString = commandString.substr(count+1, commandString.length());  }
260                 else { commandString = ""; }
261                                 
262                 
263                 //get rid of spaces in between commands if any
264                 if (commandString.length() > 0) {
265                         while (commandString[0] == ' ') {  
266                                 commandString = commandString.substr(1,commandString.length());
267                                 if (commandString.length() == 0) {  break;  }
268                         }
269                 }
270                                         
271                 return nextcommand;
272         }
273         catch(exception& e) {
274                 errorOut(e, "ScriptEngine", "getNextCommand");
275                 exit(1);
276         }
277 }
278 /***********************************************************************/