]> git.donarmstrong.com Git - mothur.git/blob - mergefilecommand.cpp
added logfile feature
[mothur.git] / mergefilecommand.cpp
1 /*
2  *  mergefilecommand.cpp
3  *  Mothur
4  *
5  *  Created by Pat Schloss on 6/14/09.
6  *  Copyright 2009 Patrick D. Schloss. All rights reserved.
7  *
8  */
9
10 #include "mergefilecommand.h"
11
12 //**********************************************************************************************************************
13
14 MergeFileCommand::MergeFileCommand(string option){
15         try {
16                 abort = false;
17                 
18                 if(option == "help") {
19                         help();
20                         abort = true; 
21                 }
22                 else {
23                         //valid paramters for this command
24                         string Array[] =  {"input", "output"};
25                         vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
26                         
27                         OptionParser parser(option);
28                         map<string,string> parameters = parser.getParameters();
29                         
30                         ValidParameters validParameter;
31                         
32                         //check to make sure all parameters are valid for command
33                         for (map<string,string>::iterator it = parameters.begin(); it != parameters.end(); it++) { 
34                                 if (validParameter.isValidParameter(it->first, myArray, it->second) != true) {  abort = true;  }
35                         }
36                         
37                         string fileList = validParameter.validFile(parameters, "input", false);                 
38                         if(fileList == "not found") { mothurOut("you must enter two or more file names"); mothurOutEndLine();  abort=true;  }
39                         else{   splitAtDash(fileList, fileNames);       }
40                         
41                         numInputFiles = fileNames.size();
42                         ifstream testFile;
43                         if(numInputFiles == 0){
44                                 mothurOut("you must enter two or more file names and you entered " + toString(fileNames.size()) +  " file names"); mothurOutEndLine();
45                                 abort=true;  
46                         }
47                         else{
48                                 for(int i=0;i<numInputFiles;i++){
49                                         if(openInputFile(fileNames[i], testFile)){      abort = true;   }
50                                         testFile.close();
51                                 }
52                         }   
53                         
54                         outputFileName = validParameter.validFile(parameters, "output", false);                 
55                         if (outputFileName == "not found") { mothurOut("you must enter an output file name"); mothurOutEndLine();  abort=true;  }
56                 }
57                         
58         }
59         catch(exception& e) {
60                 errorOut(e, "MergeFileCommand", "MergeFileCommand");
61                 exit(1);
62         }
63 }
64
65 //**********************************************************************************************************************
66
67 MergeFileCommand::~MergeFileCommand()   {       /*      do nothing      */      }
68
69 //**********************************************************************************************************************
70
71 int MergeFileCommand::execute(){
72         try {
73                 if (abort == true) {    return 0;       }
74                 
75                 ofstream outputFile;
76                 openOutputFile(outputFileName, outputFile);
77                 
78                 ifstream inputFile;
79                 char c;
80                 for(int i=0;i<numInputFiles;i++){
81                         openInputFile(fileNames[i], inputFile);
82                         
83                         while(!inputFile.eof()){        c = inputFile.get(); outputFile << c;   }
84                         
85                         inputFile.close();
86                 }
87                 
88                 outputFile.close();
89                 return 0;
90         }
91         catch(exception& e) {
92                 errorOut(e, "MergeFileCommand", "execute");
93                 exit(1);
94         }
95 }
96
97 //**********************************************************************************************************************
98
99 void MergeFileCommand::help(){
100         try {
101                 mothurOut("The merge.file command..."); mothurOutEndLine();
102         }
103         catch(exception& e) {
104                 errorOut(e, "MergeFileCommand", "help");
105                 exit(1);
106         }
107 }
108
109 //**********************************************************************************************************************