]> git.donarmstrong.com Git - mothur.git/blob - mergefilecommand.cpp
added set.dir command and modified commands to redirect input and output, removed...
[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","outputdir","inputdir"};
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                         //if the user changes the input directory command factory will send this info to us in the output parameter 
38                         string inputDir = validParameter.validFile(parameters, "inputdir", false);              
39                         if (inputDir == "not found"){   inputDir = "";          }
40                         
41                         string fileList = validParameter.validFile(parameters, "input", false);                 
42                         if(fileList == "not found") { mothurOut("you must enter two or more file names"); mothurOutEndLine();  abort=true;  }
43                         else{   splitAtDash(fileList, fileNames);       }
44                         
45                         //if the user changes the output directory command factory will send this info to us in the output parameter 
46                         string outputDir = validParameter.validFile(parameters, "outputdir", false);            if (outputDir == "not found")   {       outputDir = "";         }
47                         
48                         
49                         numInputFiles = fileNames.size();
50                         ifstream testFile;
51                         if(numInputFiles == 0){
52                                 mothurOut("you must enter two or more file names and you entered " + toString(fileNames.size()) +  " file names"); mothurOutEndLine();
53                                 abort=true;  
54                         }
55                         else{
56                                 for(int i=0;i<numInputFiles;i++){
57                                         if (inputDir != "") {
58                                                 string path = hasPath(fileNames[i]);
59                                                 //if the user has not given a path then, add inputdir. else leave path alone.
60                                                 if (path == "") {       fileNames[i] = inputDir + fileNames[i];         }
61                                         }
62                                         
63                                         if(openInputFile(fileNames[i], testFile)){      abort = true;   }
64                                         testFile.close();
65                                 }
66                         }   
67                         
68                         outputFileName = validParameter.validFile(parameters, "output", false);                 
69                         if (outputFileName == "not found") { mothurOut("you must enter an output file name"); mothurOutEndLine();  abort=true;  }
70                         else if (outputDir != "") { outputFileName = outputDir + getSimpleName(outputFileName); }
71                 }
72                         
73         }
74         catch(exception& e) {
75                 errorOut(e, "MergeFileCommand", "MergeFileCommand");
76                 exit(1);
77         }
78 }
79
80 //**********************************************************************************************************************
81
82 MergeFileCommand::~MergeFileCommand()   {       /*      do nothing      */      }
83
84 //**********************************************************************************************************************
85
86 int MergeFileCommand::execute(){
87         try {
88                 if (abort == true) {    return 0;       }
89                 
90                 ofstream outputFile;
91                 openOutputFile(outputFileName, outputFile);
92                 
93                 char c;
94                 for(int i=0;i<numInputFiles;i++){
95                         ifstream inputFile; //declaration must be inside for loop of windows throws an error
96                         
97                         openInputFile(fileNames[i], inputFile);
98                         
99                         while(!inputFile.eof()){        c = inputFile.get(); outputFile << c;   }
100                         
101                         inputFile.close();
102                 }
103                 
104                 outputFile.close();
105                 return 0;
106         }
107         catch(exception& e) {
108                 errorOut(e, "MergeFileCommand", "execute");
109                 exit(1);
110         }
111 }
112
113 //**********************************************************************************************************************
114
115 void MergeFileCommand::help(){
116         try {
117                 mothurOut("The merge.file command..."); mothurOutEndLine();
118         }
119         catch(exception& e) {
120                 errorOut(e, "MergeFileCommand", "help");
121                 exit(1);
122         }
123 }
124
125 //**********************************************************************************************************************