]> git.donarmstrong.com Git - mothur.git/blob - mergefilecommand.cpp
merged pat's trim seqs edits with sarah's major overhaul of global data; also added...
[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") { cout << "you must enter two or more file names" << endl;  abort=true;  }
39                         else{   splitAtDash(fileList, fileNames);       }
40                         
41                         numInputFiles = fileNames.size();
42                         ifstream testFile;
43                         if(numInputFiles == 0){
44                                 cout << "you must enter two or more file names and you entered " << fileNames.size() <<  " file names" << endl;
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") { cout << "you must enter an output file name" << endl;  abort=true;  }
56                 }
57                         
58         }
59         catch(exception& e) {
60                 cout << "Standard Error: " << e.what() << " has occurred in the MergeFileCommand class Function MergeFileCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
61                 exit(1);
62         }
63         catch(...) {
64                 cout << "An unknown error has occurred in the MergeFileCommand class function MergeFileCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
65                 exit(1);
66         }       
67 }
68
69 //**********************************************************************************************************************
70
71 MergeFileCommand::~MergeFileCommand()   {       /*      do nothing      */      }
72
73 //**********************************************************************************************************************
74
75 int MergeFileCommand::execute(){
76         try {
77                 if (abort == true) {    return 0;       }
78                 
79                 ofstream outputFile;
80                 openOutputFile(outputFileName, outputFile);
81                 
82                 ifstream inputFile;
83                 char c;
84                 for(int i=0;i<numInputFiles;i++){
85                         openInputFile(fileNames[i], inputFile);
86                         
87                         while(!inputFile.eof()){        c = inputFile.get(); outputFile << c;   }
88                         
89                         inputFile.close();
90                 }
91                 return 0;
92         }
93         catch(exception& e) {
94                 cout << "Standard Error: " << e.what() << " has occurred in the BinSeqCommand class Function BinSeqCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
95                 exit(1);
96         }
97         catch(...) {
98                 cout << "An unknown error has occurred in the BinSeqCommand class function BinSeqCommand. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
99                 exit(1);
100         }       
101         
102 }
103
104 //**********************************************************************************************************************
105
106 void MergeFileCommand::help(){
107         try {
108                 cout << "The merge.file command..." << endl;
109         }
110         catch(exception& e) {
111                 cout << "Standard Error: " << e.what() << " has occurred in the MergeFileCommand class Function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
112                 exit(1);
113         }
114         catch(...) {
115                 cout << "An unknown error has occurred in the MergeFileCommand class function help. Please contact Pat Schloss at pschloss@microbio.umass.edu." << "\n";
116                 exit(1);
117         }       
118         
119 }
120
121 //**********************************************************************************************************************