]> git.donarmstrong.com Git - mothur.git/blob - mergefilecommand.cpp
fcf63895b960fa8293df03dcff7c1e5bce28c914
[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                 char c;
79                 for(int i=0;i<numInputFiles;i++){
80                         ifstream inputFile; //declaration must be inside for loop of windows throws an error
81                         
82                         openInputFile(fileNames[i], inputFile);
83                         
84                         while(!inputFile.eof()){        c = inputFile.get(); outputFile << c;   }
85                         
86                         inputFile.close();
87                 }
88                 
89                 outputFile.close();
90                 return 0;
91         }
92         catch(exception& e) {
93                 errorOut(e, "MergeFileCommand", "execute");
94                 exit(1);
95         }
96 }
97
98 //**********************************************************************************************************************
99
100 void MergeFileCommand::help(){
101         try {
102                 mothurOut("The merge.file command..."); mothurOutEndLine();
103         }
104         catch(exception& e) {
105                 errorOut(e, "MergeFileCommand", "help");
106                 exit(1);
107         }
108 }
109
110 //**********************************************************************************************************************