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