]> git.donarmstrong.com Git - mothur.git/blob - mergefilecommand.cpp
added checks for ^C to quit command instead of program
[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") { m->mothurOut("you must enter two or more file names"); m->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                                 m->mothurOut("you must enter two or more file names and you entered " + toString(fileNames.size()) +  " file names"); m->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") { m->mothurOut("you must enter an output file name"); m->mothurOutEndLine();  abort=true;  }
70                         else if (outputDir != "") { outputFileName = outputDir + getSimpleName(outputFileName); }
71                 }
72                         
73         }
74         catch(exception& e) {
75                 m->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()){        
100                                 if (m->control_pressed) { inputFile.close(); outputFile.close(); remove(outputFileName.c_str()); return 0;  }
101                         
102                                 c = inputFile.get(); 
103                                 //-1 is eof char
104                                 if (int(c) != -1) { outputFile << c; }   
105                         }
106                         
107                         inputFile.close();
108                 }
109                 
110                 outputFile.close();
111                 
112                 if (m->control_pressed) { remove(outputFileName.c_str()); return 0;  }
113                 
114                 m->mothurOutEndLine();
115                 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
116                 m->mothurOut(outputFileName); m->mothurOutEndLine();    
117                 m->mothurOutEndLine();
118
119                 return 0;
120         }
121         catch(exception& e) {
122                 m->errorOut(e, "MergeFileCommand", "execute");
123                 exit(1);
124         }
125 }
126
127 //**********************************************************************************************************************
128
129 void MergeFileCommand::help(){
130         try {
131                 m->mothurOut("The merge.file command..."); m->mothurOutEndLine();
132         }
133         catch(exception& e) {
134                 m->errorOut(e, "MergeFileCommand", "help");
135                 exit(1);
136         }
137 }
138
139 //**********************************************************************************************************************