5 * Created by Pat Schloss on 6/14/09.
6 * Copyright 2009 Patrick D. Schloss. All rights reserved.
10 #include "mergefilecommand.h"
12 //**********************************************************************************************************************
14 MergeFileCommand::MergeFileCommand(string option) {
18 if(option == "help") {
23 //valid paramters for this command
24 string Array[] = {"input", "output","outputdir","inputdir"};
25 vector<string> myArray (Array, Array+(sizeof(Array)/sizeof(string)));
27 OptionParser parser(option);
28 map<string,string> parameters = parser.getParameters();
30 ValidParameters validParameter;
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; }
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 = ""; }
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{ m->splitAtDash(fileList, fileNames); }
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 = ""; }
49 numInputFiles = fileNames.size();
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();
56 for(int i=0;i<numInputFiles;i++){
58 string path = m->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]; }
63 if(m->openInputFile(fileNames[i], testFile)){ abort = true; }
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 + m->getSimpleName(outputFileName); }
75 m->errorOut(e, "MergeFileCommand", "MergeFileCommand");
80 //**********************************************************************************************************************
82 MergeFileCommand::~MergeFileCommand() { /* do nothing */ }
84 //**********************************************************************************************************************
86 int MergeFileCommand::execute(){
88 if (abort == true) { return 0; }
91 m->openOutputFile(outputFileName, outputFile);
94 for(int i=0;i<numInputFiles;i++){
95 ifstream inputFile; //declaration must be inside for loop of windows throws an error
97 m->openInputFile(fileNames[i], inputFile);
99 while(!inputFile.eof()){
100 if (m->control_pressed) { inputFile.close(); outputFile.close(); remove(outputFileName.c_str()); return 0; }
104 if (int(c) != -1) { outputFile << c; }
112 if (m->control_pressed) { remove(outputFileName.c_str()); return 0; }
114 m->mothurOutEndLine();
115 m->mothurOut("Output File Name: "); m->mothurOutEndLine();
116 m->mothurOut(outputFileName); m->mothurOutEndLine();
117 m->mothurOutEndLine();
121 catch(exception& e) {
122 m->errorOut(e, "MergeFileCommand", "execute");
127 //**********************************************************************************************************************
129 void MergeFileCommand::help(){
131 m->mothurOut("The merge.file command..."); m->mothurOutEndLine();
133 catch(exception& e) {
134 m->errorOut(e, "MergeFileCommand", "help");
139 //**********************************************************************************************************************