From ad3f59ce304c37faff88fbc5ea8c1df047326df4 Mon Sep 17 00:00:00 2001 From: derek Date: Sat, 11 Jun 2011 16:55:08 -0400 Subject: [PATCH] Cleaned up help formatting & added some string utilities --- src/utils/CMakeLists.txt | 2 +- src/utils/bamtools_options.cpp | 33 +++++++++++------ src/utils/bamtools_options.h | 14 ++++---- src/utils/bamtools_utilities.cpp | 62 +++++++++++++++++++++++++++++++- src/utils/bamtools_utilities.h | 19 +++++++++- 5 files changed, 110 insertions(+), 20 deletions(-) diff --git a/src/utils/CMakeLists.txt b/src/utils/CMakeLists.txt index d1e26ff..5bbe624 100644 --- a/src/utils/CMakeLists.txt +++ b/src/utils/CMakeLists.txt @@ -24,6 +24,6 @@ target_link_libraries ( BamTools-utils BamTools ) # set BamTools library properties set_target_properties( BamTools-utils PROPERTIES - SOVERSION 1.0.0 + SOVERSION 1.0.2 OUTPUT_NAME bamtools-utils ) diff --git a/src/utils/bamtools_options.cpp b/src/utils/bamtools_options.cpp index c46d004..157357f 100644 --- a/src/utils/bamtools_options.cpp +++ b/src/utils/bamtools_options.cpp @@ -3,7 +3,7 @@ // Marth Lab, Department of Biology, Boston College // All rights reserved. // --------------------------------------------------------------------------- -// Last modified: 19 November 2010 +// Last modified: 11 June 2011 // --------------------------------------------------------------------------- // Parses command line arguments and creates a help menu // --------------------------------------------------------------------------- @@ -32,12 +32,15 @@ string Options::m_description; // the main description string Options::m_exampleArguments; // the example arguments vector Options::m_optionGroups; // stores the option groups map Options::m_optionsMap; // stores the options in a map -string Options::m_stdin = "stdin"; // string representation of stdin -string Options::m_stdout = "stdout"; // string representation of stdout +const string Options::m_stdin = "stdin"; // string representation of stdin +const string Options::m_stdout = "stdout"; // string representation of stdout // adds a simple option to the parser -void Options::AddOption(const string& argument, const string& optionDescription, bool& foundArgument, OptionGroup* group) { - +void Options::AddOption(const string& argument, + const string& optionDescription, + bool& foundArgument, + OptionGroup* group) +{ Option o; o.Argument = argument; o.Description = optionDescription; @@ -110,7 +113,8 @@ void Options::DisplayHelp(void) { const std::string stringValue = optionIter->DefaultValue; sb << stringValue; } else { - printf("ERROR: Found an unsupported data type for argument %s when casting the default value.\n", optionIter->Argument.c_str()); + printf("ERROR: Found an unsupported data type for argument %s when casting the default value.\n", + optionIter->Argument.c_str()); exit(1); } @@ -231,15 +235,18 @@ void Options::Parse(int argc, char* argv[], int offset) { vector* pVectorValue = (vector*)ovMapIter->second.pValue; pVectorValue->push_back(val); } else { - printf("ERROR: Found an unsupported data type for argument %s when parsing the arguments.\n", argument.c_str()); + printf("ERROR: Found an unsupported data type for argument %s when parsing the arguments.\n", + argument.c_str()); exit(1); } } else { - errorBuilder << ERROR_SPACER << "The argument (" << argument << ") expects a value, but none was found." << endl; + errorBuilder << ERROR_SPACER << "The argument (" << argument + << ") expects a value, but none was found." << endl; foundError = true; } } else { - errorBuilder << ERROR_SPACER << "The argument (" << argument << ") expects a value, but none was found." << endl; + errorBuilder << ERROR_SPACER << "The argument (" << argument + << ") expects a value, but none was found." << endl; foundError = true; } } @@ -249,7 +256,8 @@ void Options::Parse(int argc, char* argv[], int offset) { // check if we missed any required parameters for (ovMapIter = m_optionsMap.begin(); ovMapIter != m_optionsMap.end(); ++ovMapIter) { if (ovMapIter->second.IsRequired && !*ovMapIter->second.pFoundArgument) { - errorBuilder << ERROR_SPACER << ovMapIter->second.ValueTypeDescription << " was not specified. Please use the " << ovMapIter->first << " parameter." << endl; + errorBuilder << ERROR_SPACER << ovMapIter->second.ValueTypeDescription + << " was not specified. Please use the " << ovMapIter->first << " parameter." << endl; foundError = true; } } @@ -264,7 +272,10 @@ void Options::Parse(int argc, char* argv[], int offset) { } // sets the program info -void Options::SetProgramInfo(const string& programName, const string& description, const string& arguments) { +void Options::SetProgramInfo(const string& programName, + const string& description, + const string& arguments) +{ m_programName = programName; m_description = description; m_exampleArguments = arguments; diff --git a/src/utils/bamtools_options.h b/src/utils/bamtools_options.h index 7b9073d..98ddc3d 100644 --- a/src/utils/bamtools_options.h +++ b/src/utils/bamtools_options.h @@ -3,7 +3,7 @@ // Marth Lab, Department of Biology, Boston College // All rights reserved. // --------------------------------------------------------------------------- -// Last modified: 19 November 2010 +// Last modified: 11 June 2011 // --------------------------------------------------------------------------- // Parses command line arguments and creates a help menu // --------------------------------------------------------------------------- @@ -34,8 +34,8 @@ namespace BamTools { #define ARGUMENT_LENGTH 35 -#define DESC_LENGTH_FIRST_ROW 50 -#define DESC_LENGTH 39 +#define DESC_LENGTH_FIRST_ROW 30 +#define DESC_LENGTH 42 #define MAX_LINE_LENGTH 78 #ifdef WIN32 @@ -130,7 +130,9 @@ class UTILS_EXPORT Options { // parses the command line static void Parse(int argc, char* argv[], int offset = 0); // sets the program info - static void SetProgramInfo(const std::string& programName, const std::string& description, const std::string& arguments); + static void SetProgramInfo(const std::string& programName, + const std::string& description, + const std::string& arguments); // returns string representation of stdin static const std::string& StandardIn(void); // returns string representation of stdout @@ -149,9 +151,9 @@ class UTILS_EXPORT Options { // stores the options in a map static std::map m_optionsMap; // string representation of stdin - static std::string m_stdin; + static const std::string m_stdin; // string representation of stdout - static std::string m_stdout; + static const std::string m_stdout; }; // adds a value option to the parser diff --git a/src/utils/bamtools_utilities.cpp b/src/utils/bamtools_utilities.cpp index bb65c7b..3b6d46a 100644 --- a/src/utils/bamtools_utilities.cpp +++ b/src/utils/bamtools_utilities.cpp @@ -3,7 +3,7 @@ // Marth Lab, Department of Biology, Boston College // All rights reserved. // --------------------------------------------------------------------------- -// Last modified: 26 January 2011 +// Last modified: 9 June 2011 // --------------------------------------------------------------------------- // Provides general utilities used by BamTools sub-tools. // *************************************************************************** @@ -15,8 +15,10 @@ using namespace BamTools; #include #include +#include #include #include +#include using namespace std; namespace BamTools { @@ -31,6 +33,26 @@ const char REVCOMP_LOOKUP[] = {'T', 0, 'G', 'H', } // namespace BamTools +// returns true if 'source' contains 'pattern' +bool Utilities::Contains(const string& source, const string& pattern) { + return ( source.find(pattern) != string::npos ); +} + +// returns true if 'source' contains 'c' +bool Utilities::Contains(const std::string &source, const char c) { + return ( source.find(c) != string::npos ); +} + +// returns true if 'source' ends with 'pattern' +bool Utilities::EndsWith(const string& source, const string& pattern) { + return ( source.find(pattern) == (source.length() - pattern.length()) ); +} + +// returns true if 'source' ends with 'c' +bool Utilities::EndsWith(const std::string& source, const char c) { + return ( source.find(c) == (source.length() - 1) ); +} + // check if a file exists bool Utilities::FileExists(const string& filename) { ifstream f(filename.c_str(), ifstream::in); @@ -270,3 +292,41 @@ void Utilities::ReverseComplement(string& sequence) { // reverse it Reverse(sequence); } + +vector Utilities::Split(const string& source, const char delim) { + + stringstream ss(source); + string field; + vector fields; + + while ( getline(ss, field, delim) ) + fields.push_back(field); + return fields; +} + +vector Utilities::Split(const string& source, const string& delims) { + + vector fields; + + char* tok; + char cchars [source.size()+1]; + char* cstr = &cchars[0]; + strcpy(cstr, source.c_str()); + tok = strtok(cstr, delims.c_str()); + while (tok != NULL) { + fields.push_back(tok); + tok = strtok(NULL, delims.c_str()); + } + + return fields; +} + +// returns true if 'source' starts with 'pattern' +bool Utilities::StartsWith(const string& source, const string& pattern) { + return ( source.find(pattern) == 0 ); +} + +// returns true if 'source' starts with 'c' +bool Utilities::StartsWith(const std::string &source, const char c) { + return ( source.find(c) == 0 ); +} diff --git a/src/utils/bamtools_utilities.h b/src/utils/bamtools_utilities.h index 3701690..3320e33 100644 --- a/src/utils/bamtools_utilities.h +++ b/src/utils/bamtools_utilities.h @@ -3,7 +3,7 @@ // Marth Lab, Department of Biology, Boston College // All rights reserved. // --------------------------------------------------------------------------- -// Last modified: 26 January 2011 +// Last modified: 9 June 2011 // --------------------------------------------------------------------------- // Provides general utilities used by BamTools sub-tools. // *************************************************************************** @@ -16,6 +16,7 @@ #include #include #include +#include #define BAMTOOLS_ASSERT_UNREACHABLE assert( false ) #define BAMTOOLS_ASSERT_MESSAGE( condition, message ) if (!( condition )) throw std::runtime_error( message ); @@ -28,6 +29,14 @@ class BamMultiReader; class UTILS_EXPORT Utilities { public: + // returns true if 'source' contains 'pattern' or 'c' + static bool Contains(const std::string& source, const std::string& pattern); + static bool Contains(const std::string& source, const char c); + + // returns true if 'source' ends with 'pattern' or 'c' + static bool EndsWith(const std::string& source, const std::string& pattern); + static bool EndsWith(const std::string& source, const char c); + // check if a file exists static bool FileExists(const std::string& fname); @@ -44,6 +53,14 @@ class UTILS_EXPORT Utilities { // sequence utilities static void Reverse(std::string& sequence); static void ReverseComplement(std::string& sequence); + + // split string on delimiter character (or string of allowed delimiters) + static std::vector Split(const std::string& source, const char delim); + static std::vector Split(const std::string& source, const std::string& delims); + + // returns true if 'source' starts with 'pattern' or 'c' + static bool StartsWith(const std::string& source, const std::string& pattern); + static bool StartsWith(const std::string &source, const char c); }; } // namespace BamTools -- 2.39.5