From 55dad85695dba70b4447d1430733ecd85754783c Mon Sep 17 00:00:00 2001 From: westcott Date: Wed, 8 Jul 2009 16:23:14 +0000 Subject: [PATCH] added system command --- Mothur.xcodeproj/project.pbxproj | 6 +++ commandfactory.cpp | 3 ++ systemcommand.cpp | 81 ++++++++++++++++++++++++++++++++ systemcommand.h | 32 +++++++++++++ 4 files changed, 122 insertions(+) create mode 100644 systemcommand.cpp create mode 100644 systemcommand.h diff --git a/Mothur.xcodeproj/project.pbxproj b/Mothur.xcodeproj/project.pbxproj index dfe49ee..136b75f 100644 --- a/Mothur.xcodeproj/project.pbxproj +++ b/Mothur.xcodeproj/project.pbxproj @@ -79,6 +79,7 @@ 37B73C761004BEFD008C4B41 /* listseqscommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37B73C751004BEFD008C4B41 /* listseqscommand.cpp */; }; 37B73CA61004D89A008C4B41 /* getseqscommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37B73CA51004D89A008C4B41 /* getseqscommand.cpp */; }; 37B73CC01004EB38008C4B41 /* removeseqscommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37B73CBF1004EB38008C4B41 /* removeseqscommand.cpp */; }; + 37B73CCF1004F5E0008C4B41 /* systemcommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37B73CCE1004F5E0008C4B41 /* systemcommand.cpp */; }; 37C1D9730F86506E0059E3F0 /* binsequencecommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37C1D9720F86506E0059E3F0 /* binsequencecommand.cpp */; }; 37C753CE0FB3415200DBD02E /* distancecommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37C753CD0FB3415200DBD02E /* distancecommand.cpp */; }; 37D928550F21331F001D4494 /* ace.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 37D927B80F21331F001D4494 /* ace.cpp */; }; @@ -327,6 +328,8 @@ 37B73CA51004D89A008C4B41 /* getseqscommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = getseqscommand.cpp; sourceTree = ""; }; 37B73CBE1004EB38008C4B41 /* removeseqscommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = removeseqscommand.h; sourceTree = ""; }; 37B73CBF1004EB38008C4B41 /* removeseqscommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = removeseqscommand.cpp; sourceTree = ""; }; + 37B73CCD1004F5E0008C4B41 /* systemcommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = systemcommand.h; sourceTree = ""; }; + 37B73CCE1004F5E0008C4B41 /* systemcommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = systemcommand.cpp; sourceTree = ""; }; 37C1D9710F86506E0059E3F0 /* binsequencecommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = binsequencecommand.h; sourceTree = SOURCE_ROOT; }; 37C1D9720F86506E0059E3F0 /* binsequencecommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = binsequencecommand.cpp; sourceTree = SOURCE_ROOT; }; 37C753CC0FB3415200DBD02E /* distancecommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = distancecommand.h; sourceTree = SOURCE_ROOT; }; @@ -835,6 +838,8 @@ 37D928460F21331F001D4494 /* summarycommand.cpp */, 37D9284B0F21331F001D4494 /* summarysharedcommand.h */, 37D9284A0F21331F001D4494 /* summarysharedcommand.cpp */, + 37B73CCD1004F5E0008C4B41 /* systemcommand.h */, + 37B73CCE1004F5E0008C4B41 /* systemcommand.cpp */, 211C38370F961E1F00FEE541 /* treegroupscommand.h */, 211C38360F961E1F00FEE541 /* treegroupscommand.cpp */, 7E09C5340FDA7F65002ECAE5 /* trimseqscommand.h */, @@ -1129,6 +1134,7 @@ 37B73C761004BEFD008C4B41 /* listseqscommand.cpp in Sources */, 37B73CA61004D89A008C4B41 /* getseqscommand.cpp in Sources */, 37B73CC01004EB38008C4B41 /* removeseqscommand.cpp in Sources */, + 37B73CCF1004F5E0008C4B41 /* systemcommand.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/commandfactory.cpp b/commandfactory.cpp index c91dbf9..fce55bb 100644 --- a/commandfactory.cpp +++ b/commandfactory.cpp @@ -53,6 +53,7 @@ #include "listseqscommand.h" #include "getseqscommand.h" #include "removeseqscommand.h" +#include "systemcommand.h" /***********************************************************/ @@ -104,6 +105,7 @@ CommandFactory::CommandFactory(){ commands["list.seqs"] = "list.seqs"; commands["get.seqs"] = "get.seqs"; commands["remove.seqs"] = "get.seqs"; + commands["system"] = "system"; commands["quit"] = "quit"; } @@ -165,6 +167,7 @@ Command* CommandFactory::getCommand(string commandName, string optionString){ else if(commandName == "get.seqs") { command = new GetSeqsCommand(optionString); } else if(commandName == "remove.seqs") { command = new RemoveSeqsCommand(optionString); } else if(commandName == "merge.files") { command = new MergeFileCommand(optionString); } + else if(commandName == "system") { command = new SystemCommand(optionString); } else { command = new NoCommand(optionString); } return command; diff --git a/systemcommand.cpp b/systemcommand.cpp new file mode 100644 index 0000000..5bc9b46 --- /dev/null +++ b/systemcommand.cpp @@ -0,0 +1,81 @@ +/* + * systemcommand.cpp + * Mothur + * + * Created by Sarah Westcott on 7/8/09. + * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved. + * + */ + +#include "systemcommand.h" + +//********************************************************************************************************************** + +SystemCommand::SystemCommand(string option){ + try { + abort = false; + + //allow user to run help + if(option == "help") { help(); abort = true; } + + else { + //valid paramters for this command + string Array[] = {"command"}; + vector myArray (Array, Array+(sizeof(Array)/sizeof(string))); + + OptionParser parser(option); + map parameters = parser.getParameters(); + + ValidParameters validParameter; + + //check to make sure all parameters are valid for command + for (map::iterator it = parameters.begin(); it != parameters.end(); it++) { + if (validParameter.isValidParameter(it->first, myArray, it->second) != true) { abort = true; } + } + + //check for required parameters + command = validParameter.validFile(parameters, "command", false); + if (command == "not found") { mothurOut("command is a required parameter."); mothurOutEndLine(); abort = true; } + } + + } + catch(exception& e) { + errorOut(e, "SystemCommand", "SystemCommand"); + exit(1); + } +} +//********************************************************************************************************************** + +void SystemCommand::help(){ + try { + mothurOut("The system command allows you to execute a system command from within mothur.\n"); + mothurOut("The system command parameter is command and it is required.\n"); + mothurOut("The system command should be in the following format: system(command=yourCommand).\n"); + mothurOut("Example system(command=clear).\n"); + mothurOut("Note: No spaces between parameter labels (i.e. command), '=' and parameters (i.e.yourCommand).\n\n"); + } + catch(exception& e) { + errorOut(e, "SystemCommand", "help"); + exit(1); + } +} + +//********************************************************************************************************************** + +int SystemCommand::execute(){ + try { + + if (abort == true) { return 0; } + + system(command.c_str()); + + return 0; + } + + catch(exception& e) { + errorOut(e, "SystemCommand", "execute"); + exit(1); + } +} + +//********************************************************************************************************************** diff --git a/systemcommand.h b/systemcommand.h new file mode 100644 index 0000000..410a12e --- /dev/null +++ b/systemcommand.h @@ -0,0 +1,32 @@ +#ifndef SYSTEMCOMMAND_H +#define SYSTEMCOMMAND_H + +/* + * systemcommand.h + * Mothur + * + * Created by Sarah Westcott on 7/8/09. + * Copyright 2009 Schloss Lab UMASS Amherst. All rights reserved. + * + */ + +#include "command.hpp" + + +class SystemCommand : public Command { + + public: + + SystemCommand(string); + ~SystemCommand(){}; + int execute(); + void help(); + + private: + string command; + bool abort; + +}; + +#endif + -- 2.39.2