]> git.donarmstrong.com Git - mothur.git/blob - engine.cpp
modified read.otu and shared command
[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         system("clear");
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                 system("clear");
90         
91         //      char buffer = ' ';
92         //      ifstream header("introtext.txt");
93         //      while(!header.eof()){
94         //              cout << buffer;
95         //              buffer = header.get();
96         //      }
97         }
98         catch(exception& e) {
99                 errorOut(e, "BatchEngine", "BatchEngine");
100                 exit(1);
101         }
102 }
103
104 /***********************************************************************/
105
106 BatchEngine::~BatchEngine(){
107         }
108
109 /***********************************************************************/
110 //This Function allows the user to run a batchfile containing several commands on Dotur
111 bool BatchEngine::getInput(){
112         try {
113                 //check if this is a valid batchfile
114                 if (openedBatch == 1) {  
115                         mothurOut("unable to open batchfile");  
116                         mothurOutEndLine();
117                         return 1; 
118                 }
119         
120                 string input = "";
121                 string commandName = "";
122                 string options = "";
123                 
124                 //CommandFactory cFactory;
125                 int quitCommandCalled = 0;
126         
127                 while(quitCommandCalled == 0){
128         
129                         if (inputBatchFile.eof()) { input = "quit()"; }
130                         else { getline(inputBatchFile, input); }
131                         
132                         
133                         
134                         if (input[0] != '#') {
135                                 
136                                 mothurOutEndLine();
137                                 mothurOut("mothur > " + input);
138                                 mothurOutEndLine();
139                                 
140                                 
141                                 //allow user to omit the () on the quit command
142                                 if (input == "quit") { input = "quit()"; }
143
144                                 CommandOptionParser parser(input);
145                                 commandName = parser.getCommandString();
146                                 options = parser.getOptionString();
147                                                                                 
148                                 if (commandName != "") {
149
150                                         //executes valid command
151                                         CommandFactory cFactory;
152                                         Command* command = cFactory.getCommand(commandName, options);
153                                         quitCommandCalled = command->execute();
154                                 }else {         
155                                         mothurOut("Invalid."); 
156                                         mothurOutEndLine();
157                                 }
158                                 
159                         }
160                         gobble(inputBatchFile);
161                 }
162                 
163                 inputBatchFile.close();
164                 return 1;
165         }
166         catch(exception& e) {
167                 errorOut(e, "BatchEngine", "getInput");
168                 exit(1);
169         }
170 }
171
172
173 /***********************************************************************/
174 /***********************************************************************/
175 //This function opens the batchfile to be used by BatchEngine::getInput.
176 ScriptEngine::ScriptEngine(string path, string commandString){
177         try {
178                 globaldata = GlobalData::getInstance();
179                 
180                 //remove quotes
181                 listOfCommands = commandString.substr(1, (commandString.length()-1));
182
183                 globaldata->argv = path;
184                 
185                 system("clear");
186         
187         }
188         catch(exception& e) {
189                 errorOut(e, "ScriptEngine", "ScriptEngine");
190                 exit(1);
191         }
192 }
193
194 /***********************************************************************/
195
196 ScriptEngine::~ScriptEngine(){
197         }
198
199 /***********************************************************************/
200 //This Function allows the user to run a batchfile containing several commands on mothur
201 bool ScriptEngine::getInput(){
202         try {
203                         
204                 string input = "";
205                 string commandName = "";
206                 string options = "";
207                 
208                 
209                 //CommandFactory cFactory;
210                 int quitCommandCalled = 0;
211         
212                 while(quitCommandCalled == 0){
213                 
214                         input = getNextCommand(listOfCommands); 
215                         
216                         if (input == "") { input = "quit()"; }
217                         
218                         
219                         mothurOutEndLine();
220                         mothurOut("mothur > " + input);
221                         mothurOutEndLine();
222
223                                 
224                         //allow user to omit the () on the quit command
225                         if (input == "quit") { input = "quit()"; }
226
227                         CommandOptionParser parser(input);
228                         commandName = parser.getCommandString();
229                         options = parser.getOptionString();
230                                                                                 
231                         if (commandName != "") {
232
233                                 //executes valid command
234                                 CommandFactory cFactory;
235                                 Command* command = cFactory.getCommand(commandName, options);
236                                 quitCommandCalled = command->execute();
237                         }else {         
238                                 mothurOut("Invalid."); 
239                                 mothurOutEndLine();
240                         }
241                         
242                 }
243                 
244                 return 1;
245         }
246         catch(exception& e) {
247                 errorOut(e, "ScriptEngine", "getInput");
248                 exit(1);
249         }
250 }
251 /***********************************************************************/
252 string ScriptEngine::getNextCommand(string& commandString) {
253         try {
254                 string nextcommand = "";
255                 
256                 nextcommand = commandString.substr(0,commandString.find_first_of(';'));
257
258                                 
259                 if ((commandString.find_first_of(';')+1) <= commandString.length()) {
260                         commandString = commandString.substr(commandString.find_first_of(';')+1, commandString.length());
261                 }else { commandString = ""; } //you have reached the last command.
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 /***********************************************************************/