]> git.donarmstrong.com Git - mothur.git/blob - engine.cpp
modified sequence class to read fasta files with comments. this required modification...
[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 InteractEngine::~InteractEngine(){}
28
29 /***********************************************************************/
30 //This function allows the user to input commands one line at a time until they quit.
31 //If the command is garbage it does nothing.
32 bool InteractEngine::getInput(){
33         try {
34                 string input = "";
35                 string commandName = "";
36                 string options = "";
37                 int quitCommandCalled = 0;
38                 
39                                 
40                 while(quitCommandCalled != 1){
41                         
42                         mothurOutEndLine();
43                         mothurOut("mothur > ");
44                         
45                         //get input char by char so you can check for use of arrow keys
46                         //if (_kbhit()){
47                         //      _getch(); // edit : if you want to check the arrow-keys you must call getch twice because special-keys have two values
48                         //      return _getch();
49                         //}
50                         //return 0; // if no key is pressed
51                         //setbuf(stdin, NULL); //no buffering
52 /*if(ch==0)
53 {
54 ch=getch();
55 if(ch==72) cout<<"up";
56 else if(ch==75) cout<<"left";
57 else if(ch==77) cout<<"right";
58 else if(ch==80) cout<<"down";
59 cout<<endl;
60 }
61 else break;
62 }
63 delay(2000);
64 return 0;
65 }*/
66                         
67                         //int letter = 0;
68                         //while ((letter != 10) && (letter != 13)) {
69                         //      letter = getch();
70                                 
71                         //      cout << "char code = " << letter << endl;
72                                 
73                         //      input += char(letter);
74                         //}
75                 //      input = input.substr(0, input.length()-1); //cut off newline char
76                 
77         //cout << input << endl;                
78                         
79                         getline(cin, input);
80                         if (cin.eof()) { input = "quit()"; }
81                         
82                         //save command
83                         //previousInputs.push_back(input);
84                         
85                         mothurOutJustToLog(input);
86                         mothurOutEndLine();
87                         
88                         //allow user to omit the () on the quit command
89                         if (input == "quit") { input = "quit()"; }
90                         
91                         CommandOptionParser parser(input);
92                         commandName = parser.getCommandString();
93         //cout << " command = " << commandName << endl;
94                         options = parser.getOptionString();
95                         
96                         if (commandName != "") {
97                         
98                                 //executes valid command
99                                 Command* command = cFactory->getCommand(commandName, options);
100                                 quitCommandCalled = command->execute();
101                                 
102                         }else {
103                                 mothurOut("Your input contains errors. Please try again."); 
104                                 mothurOutEndLine();
105                         }
106                 }       
107                 return 1;
108         }
109         catch(exception& e) {
110                 errorOut(e, "InteractEngine", "getInput");
111                 exit(1);
112         }
113 }
114 /***********************************************************************/
115 void Engine::terminateCommand(int dummy)  {
116         try {
117                 mothurOut("Stopping command."); mothurOutEndLine();
118                 cFactory->getCommand();  //terminates command
119         }
120         catch(exception& e) {
121                 errorOut(e, "InteractEngine", "terminateCommand");
122                 exit(1);
123         }
124 }
125 /***********************************************************************/
126 //This function opens the batchfile to be used by BatchEngine::getInput.
127 BatchEngine::BatchEngine(string path, string batchFileName){
128         try {
129                 globaldata = GlobalData::getInstance();
130         
131                 openedBatch = openInputFile(batchFileName, inputBatchFile);
132                 globaldata->argv = path;
133                                 
134         }
135         catch(exception& e) {
136                 errorOut(e, "BatchEngine", "BatchEngine");
137                 exit(1);
138         }
139 }
140
141 /***********************************************************************/
142
143 BatchEngine::~BatchEngine(){    }
144
145 /***********************************************************************/
146 //This Function allows the user to run a batchfile containing several commands on Dotur
147 bool BatchEngine::getInput(){
148         try {
149                 //check if this is a valid batchfile
150                 if (openedBatch == 1) {  
151                         mothurOut("unable to open batchfile");  
152                         mothurOutEndLine();
153                         return 1; 
154                 }
155         
156                 string input = "";
157                 string commandName = "";
158                 string options = "";
159                 
160                 //CommandFactory cFactory;
161                 int quitCommandCalled = 0;
162         
163                 while(quitCommandCalled == 0){
164         
165                         if (inputBatchFile.eof()) { input = "quit()"; }
166                         else { input = getline(inputBatchFile); }
167                         
168                         if (input[0] != '#') {
169                                 
170                                 mothurOutEndLine();
171                                 mothurOut("mothur > " + input);
172                                 mothurOutEndLine();
173                                 
174                                 
175                                 //allow user to omit the () on the quit command
176                                 if (input == "quit") { input = "quit()"; }
177
178                                 CommandOptionParser parser(input);
179                                 commandName = parser.getCommandString();
180                                 options = parser.getOptionString();
181                                                                                 
182                                 if (commandName != "") {
183
184                                         //executes valid command
185                                         Command* command = cFactory->getCommand(commandName, options);
186                                         quitCommandCalled = command->execute();
187                                 }else {         
188                                         mothurOut("Invalid."); 
189                                         mothurOutEndLine();
190                                 }
191                                 
192                         }
193                         gobble(inputBatchFile);
194                 }
195                 
196                 inputBatchFile.close();
197                 return 1;
198         }
199         catch(exception& e) {
200                 errorOut(e, "BatchEngine", "getInput");
201                 exit(1);
202         }
203 }
204
205
206 /***********************************************************************/
207 /***********************************************************************/
208 //This function opens the batchfile to be used by BatchEngine::getInput.
209 ScriptEngine::ScriptEngine(string path, string commandString){
210         try {
211                 globaldata = GlobalData::getInstance();
212                 
213                 //remove quotes
214                 listOfCommands = commandString.substr(1, (commandString.length()-1));
215
216                 globaldata->argv = path;
217                 
218         }
219         catch(exception& e) {
220                 errorOut(e, "ScriptEngine", "ScriptEngine");
221                 exit(1);
222         }
223 }
224
225 /***********************************************************************/
226
227 ScriptEngine::~ScriptEngine(){  }
228
229 /***********************************************************************/
230 //This Function allows the user to run a batchfile containing several commands on mothur
231 bool ScriptEngine::getInput(){
232         try {
233                         
234                 string input = "";
235                 string commandName = "";
236                 string options = "";
237                 
238                 
239                 //CommandFactory cFactory;
240                 int quitCommandCalled = 0;
241         
242                 while(quitCommandCalled == 0){
243                 
244                         input = getNextCommand(listOfCommands); 
245                         
246                         if (input == "") { input = "quit()"; }
247                         
248                         
249                         mothurOutEndLine();
250                         mothurOut("mothur > " + input);
251                         mothurOutEndLine();
252
253                                 
254                         //allow user to omit the () on the quit command
255                         if (input == "quit") { input = "quit()"; }
256
257                         CommandOptionParser parser(input);
258                         commandName = parser.getCommandString();
259                         options = parser.getOptionString();
260                                                                                 
261                         if (commandName != "") {
262
263                                 //executes valid command
264                                 Command* command = cFactory->getCommand(commandName, options);
265                                 quitCommandCalled = command->execute();
266                         }else {         
267                                 mothurOut("Invalid."); 
268                                 mothurOutEndLine();
269                         }
270                         
271                 }
272                 
273                 return 1;
274         }
275         catch(exception& e) {
276                 errorOut(e, "ScriptEngine", "getInput");
277                 exit(1);
278         }
279 }
280 /***********************************************************************/
281 string ScriptEngine::getNextCommand(string& commandString) {
282         try {
283                 string nextcommand = "";
284                 int count = 0;
285                 
286                 //go through string until you reach ; or end
287                 while (count < commandString.length()) { 
288                         
289                         if (commandString[count] == ';') {  break;   }
290                         else {          nextcommand += commandString[count];    }
291                         
292                         count++;
293                 }
294                 
295                 //if you are not at the end
296                 if (count != commandString.length())  {   commandString = commandString.substr(count+1, commandString.length());  }
297                 else { commandString = ""; }
298                                 
299                 
300                 //get rid of spaces in between commands if any
301                 if (commandString.length() > 0) {
302                         while (commandString[0] == ' ') {  
303                                 commandString = commandString.substr(1,commandString.length());
304                                 if (commandString.length() == 0) {  break;  }
305                         }
306                 }
307                                         
308                 return nextcommand;
309         }
310         catch(exception& e) {
311                 errorOut(e, "ScriptEngine", "getNextCommand");
312                 exit(1);
313         }
314 }
315 /***********************************************************************/